Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 47c1ea3

Browse files
authored
FIX: allow scanning of trashed posts and deleted users for test (#1024)
When a post is trashed we should still be allowed to scan it post.topic will be nil for a trashed topic even if post is trashed
1 parent 47f5da7 commit 47c1ea3

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

lib/ai_moderation/spam_scanner.rb

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def self.test_post(post, custom_instructions: nil, llm_id: nil)
124124
llm_model = llm_id ? LlmModel.find(llm_id) : settings.llm_model
125125
llm = llm_model.to_llm
126126
custom_instructions = custom_instructions || settings.custom_instructions.presence
127-
context = build_context(post)
127+
context = build_context(post, post.topic || Topic.with_deleted.find_by(id: post.topic_id))
128128
prompt = completion_prompt(post, context: context, custom_instructions: custom_instructions)
129129

130130
result =
@@ -247,34 +247,39 @@ def self.check_if_spam(result)
247247
(result.present? && result.strip.downcase.start_with?("spam"))
248248
end
249249

250-
def self.build_context(post)
250+
def self.build_context(post, topic = nil)
251+
topic ||= post.topic
251252
context = []
252253

253254
# Clear distinction between reply and new topic
254255
if post.is_first_post?
255256
context << "NEW TOPIC POST ANALYSIS"
256-
context << "- Topic title: #{post.topic.title}"
257-
context << "- Category: #{post.topic.category&.name}"
257+
context << "- Topic title: #{topic.title}"
258+
context << "- Category: #{topic.category&.name}"
258259
else
259260
context << "REPLY POST ANALYSIS"
260-
context << "- In topic: #{post.topic.title}"
261-
context << "- Category: #{post.topic.category&.name}"
262-
context << "- Topic started by: #{post.topic.user.username}"
263-
264-
# Include parent post context for replies
265-
if post.reply_to_post.present?
266-
parent = post.reply_to_post
267-
context << "\nReplying to #{parent.user.username}'s post:"
268-
context << "#{parent.raw[0..500]}..." if parent.raw.length > 500
269-
context << parent.raw if parent.raw.length <= 500
261+
context << "- In topic: #{topic.title}"
262+
context << "- Category: #{topic.category&.name}"
263+
context << "- Topic started by: #{topic.user&.username}"
264+
265+
if post.reply_to_post_number.present?
266+
parent =
267+
Post.with_deleted.find_by(topic_id: topic.id, post_number: post.reply_to_post_number)
268+
if parent
269+
context << "\nReplying to #{parent.user&.username}'s post:"
270+
context << "#{parent.raw[0..500]}..." if parent.raw.length > 500
271+
context << parent.raw if parent.raw.length <= 500
272+
end
270273
end
271274
end
272275

273276
context << "\nPost Author Information:"
274-
context << "- Username: #{post.user.username}"
275-
context << "- Account age: #{(Time.current - post.user.created_at).to_i / 86_400} days"
276-
context << "- Total posts: #{post.user.post_count}"
277-
context << "- Trust level: #{post.user.trust_level}"
277+
if post.user # during test we may not have a user
278+
context << "- Username: #{post.user.username}"
279+
context << "- Account age: #{(Time.current - post.user.created_at).to_i / 86_400} days"
280+
context << "- Total posts: #{post.user.post_count}"
281+
context << "- Trust level: #{post.user.trust_level}"
282+
end
278283

279284
context << "\nPost Content (first #{MAX_RAW_SCAN_LENGTH} chars):\n"
280285
context << post.raw[0..MAX_RAW_SCAN_LENGTH]

spec/requests/admin/ai_spam_controller_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@
120120

121121
before { sign_in(admin) }
122122

123-
it "can scan using post url" do
123+
it "can scan using post url (even when trashed and user deleted)" do
124+
User.where(id: spam_post2.user_id).delete_all
125+
spam_post2.topic.trash!
126+
spam_post2.trash!
127+
124128
llm2 = Fabricate(:llm_model, name: "DiffLLM")
125129

126130
DiscourseAi::Completions::Llm.with_prepared_responses(["spam", "just because"]) do

0 commit comments

Comments
 (0)