Skip to content

A platform for community discussion. Free, open, simple.

DEV: Skip flagging a post via AI spam if it is already in queue (#40037)

Penar Musaraj624ded13aedf
diff --git a/plugins/discourse-ai/lib/ai_moderation/spam_scanner.rb b/plugins/discourse-ai/lib/ai_moderation/spam_scanner.rb
index 6489f32367f..7b11b60fc65 100644
--- a/plugins/discourse-ai/lib/ai_moderation/spam_scanner.rb
+++ b/plugins/discourse-ai/lib/ai_moderation/spam_scanner.rb
@@ -411,6 +411,11 @@ module DiscourseAi
end
def self.handle_spam(post, log, triggering_user_id: nil)
+ if post_has_existing_flag?(post)
+ log.update!(error: "skipped because post already has a flag")
+ return
+ end
+
url = "#{Discourse.base_url}/admin/plugins/discourse-ai/ai-spam"
reason = I18n.t("discourse_ai.spam_detection.flag_reason", url: url)
@@ -465,6 +470,10 @@ module DiscourseAi
Topic.where(id: post.topic_id).update_all(visible: false) if post.post_number == 1
end
+
+ def self.post_has_existing_flag?(post)
+ PostAction.active.flags.where(post: post).exists?
+ end
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_moderation/spam_scanner_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_moderation/spam_scanner_spec.rb
index 857417880d5..6006cfd30e5 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_moderation/spam_scanner_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_moderation/spam_scanner_spec.rb
@@ -442,7 +442,7 @@ RSpec.describe DiscourseAi::AiModeration::SpamScanner do
).to be_blank
end
- it "does not silence the user or hide the post when a flag cannot be created" do
+ it "skips posts already flagged by the scanner" do
post = post_with_uploaded_image
Fabricate(
:post_action,
@@ -462,6 +462,51 @@ RSpec.describe DiscourseAi::AiModeration::SpamScanner do
log = AiSpamLog.find_by(post: post)
+ expect(log.reviewable).to be_nil
+ expect(log.error).to eq("skipped because post already has a flag")
+ expect(post.user.reload).not_to be_silenced
+ expect(post.topic.reload).to be_visible
+ end
+
+ it "skips posts that already have an active flag" do
+ flagger = Fabricate(:user)
+ Fabricate(
+ :post_action,
+ post: post,
+ user: flagger,
+ post_action_type_id: PostActionType.types[:spam],
+ )
+
+ described_class.new_post(post)
+
+ expect do
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ [{ spam: true, reason: "spam detected" }],
+ ) { post.rebake! }
+ end.not_to change { PostAction.active.flags.where(post: post).count }
+
+ log = AiSpamLog.find_by(post: post)
+
+ expect(log.reviewable).to be_nil
+ expect(log.error).to eq("skipped because post already has a flag")
+ expect(post.user.reload).not_to be_silenced
+ expect(post.topic.reload).to be_visible
+ end
+
+ it "does not silence or hide when flag creation fails" do
+ flagging_user = Fabricate(:user, trust_level: TrustLevel[4])
+ SiteSetting.ai_spam_detection_user_id = flagging_user.id
+
+ described_class.new_post(post)
+
+ expect do
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ [{ spam: true, reason: "spam detected" }],
+ ) { post.rebake! }
+ end.not_to change { PostAction.active.flags.where(post: post).count }
+
+ log = AiSpamLog.find_by(post: post)
+
expect(log.reviewable).to be_nil
expect(log.error).to match(/unable to flag post as spam/)
expect(post.user.reload).not_to be_silenced