Skip to content

Commit

Permalink
FIX: allow scanning of trashed posts and deleted users for test (#1024)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SamSaffron authored Dec 11, 2024
1 parent 47f5da7 commit 47c1ea3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
41 changes: 23 additions & 18 deletions lib/ai_moderation/spam_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def self.test_post(post, custom_instructions: nil, llm_id: nil)
llm_model = llm_id ? LlmModel.find(llm_id) : settings.llm_model
llm = llm_model.to_llm
custom_instructions = custom_instructions || settings.custom_instructions.presence
context = build_context(post)
context = build_context(post, post.topic || Topic.with_deleted.find_by(id: post.topic_id))
prompt = completion_prompt(post, context: context, custom_instructions: custom_instructions)

result =
Expand Down Expand Up @@ -247,34 +247,39 @@ def self.check_if_spam(result)
(result.present? && result.strip.downcase.start_with?("spam"))
end

def self.build_context(post)
def self.build_context(post, topic = nil)
topic ||= post.topic
context = []

# Clear distinction between reply and new topic
if post.is_first_post?
context << "NEW TOPIC POST ANALYSIS"
context << "- Topic title: #{post.topic.title}"
context << "- Category: #{post.topic.category&.name}"
context << "- Topic title: #{topic.title}"
context << "- Category: #{topic.category&.name}"
else
context << "REPLY POST ANALYSIS"
context << "- In topic: #{post.topic.title}"
context << "- Category: #{post.topic.category&.name}"
context << "- Topic started by: #{post.topic.user.username}"

# Include parent post context for replies
if post.reply_to_post.present?
parent = post.reply_to_post
context << "\nReplying to #{parent.user.username}'s post:"
context << "#{parent.raw[0..500]}..." if parent.raw.length > 500
context << parent.raw if parent.raw.length <= 500
context << "- In topic: #{topic.title}"
context << "- Category: #{topic.category&.name}"
context << "- Topic started by: #{topic.user&.username}"

if post.reply_to_post_number.present?
parent =
Post.with_deleted.find_by(topic_id: topic.id, post_number: post.reply_to_post_number)
if parent
context << "\nReplying to #{parent.user&.username}'s post:"
context << "#{parent.raw[0..500]}..." if parent.raw.length > 500
context << parent.raw if parent.raw.length <= 500
end
end
end

context << "\nPost Author Information:"
context << "- Username: #{post.user.username}"
context << "- Account age: #{(Time.current - post.user.created_at).to_i / 86_400} days"
context << "- Total posts: #{post.user.post_count}"
context << "- Trust level: #{post.user.trust_level}"
if post.user # during test we may not have a user
context << "- Username: #{post.user.username}"
context << "- Account age: #{(Time.current - post.user.created_at).to_i / 86_400} days"
context << "- Total posts: #{post.user.post_count}"
context << "- Trust level: #{post.user.trust_level}"
end

context << "\nPost Content (first #{MAX_RAW_SCAN_LENGTH} chars):\n"
context << post.raw[0..MAX_RAW_SCAN_LENGTH]
Expand Down
6 changes: 5 additions & 1 deletion spec/requests/admin/ai_spam_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@

before { sign_in(admin) }

it "can scan using post url" do
it "can scan using post url (even when trashed and user deleted)" do
User.where(id: spam_post2.user_id).delete_all
spam_post2.topic.trash!
spam_post2.trash!

llm2 = Fabricate(:llm_model, name: "DiffLLM")

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

0 comments on commit 47c1ea3

Please sign in to comment.