From f9f89adac5f5a3748197569eebbfe534ae4d001d Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 27 Dec 2024 17:47:16 +1100 Subject: [PATCH] FIX: keep track of silence reason when spam detection flags user (#1046) Previously reason was blank for silencing user --- config/locales/server.en.yml | 1 + lib/ai_moderation/spam_scanner.rb | 19 ++++++++++++++++--- .../ai_moderation/spam_scanner_spec.rb | 9 +++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index df5ef2da..dd5c43ca 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -253,6 +253,7 @@ en: conversation_deleted: "Conversation share deleted successfully" spam_detection: flag_reason: "Flagged as spam by Discourse AI" + silence_reason: "User silenced automatically by Discourse AI" ai_bot: reply_error: "Sorry, it looks like our system encountered an unexpected issue while trying to reply.\n\n[details='Error details']\n%{details}\n[/details]" default_pm_prefix: "[Untitled AI bot PM]" diff --git a/lib/ai_moderation/spam_scanner.rb b/lib/ai_moderation/spam_scanner.rb index 3143357e..a8e83377 100644 --- a/lib/ai_moderation/spam_scanner.rb +++ b/lib/ai_moderation/spam_scanner.rb @@ -337,6 +337,8 @@ module DiscourseAi url = "#{Discourse.base_url}/admin/plugins/discourse-ai/ai-spam" reason = I18n.t("discourse_ai.spam_detection.flag_reason", url: url) + flagging_user = self.flagging_user + result = PostActionCreator.new( flagging_user, @@ -347,9 +349,20 @@ module DiscourseAi ).perform log.update!(reviewable: result.reviewable) - SpamRule::AutoSilence.new(post.user, post).silence_user - # this is required cause tl1 is not auto hidden - # we want to also handle tl1 + + reason = I18n.t("discourse_ai.spam_detection.silence_reason", url: url) + silencer = + UserSilencer.new( + post.user, + flagging_user, + message: :too_many_spam_flags, + post_id: post.id, + reason: reason, + keep_posts: true, + ) + silencer.silence + + # silencer will not hide tl1 posts, so we do this here hide_posts_and_topics(post.user) end diff --git a/spec/lib/modules/ai_moderation/spam_scanner_spec.rb b/spec/lib/modules/ai_moderation/spam_scanner_spec.rb index ab18b409..29ab9cd5 100644 --- a/spec/lib/modules/ai_moderation/spam_scanner_spec.rb +++ b/spec/lib/modules/ai_moderation/spam_scanner_spec.rb @@ -215,6 +215,15 @@ RSpec.describe DiscourseAi::AiModeration::SpamScanner do expect(post.user.reload.silenced_till).to be_present expect(post.topic.reload.visible).to eq(false) + history = UserHistory.where(action: UserHistory.actions[:silence_user]).order(:id).last + + url = "#{Discourse.base_url}/admin/plugins/discourse-ai/ai-spam" + + expect(history.target_user_id).to eq(post.user_id) + expect(history.details).to include( + I18n.t("discourse_ai.spam_detection.silence_reason", url: url), + ) + expect(log.reviewable).to be_present expect(log.reviewable.created_by_id).to eq(described_class.flagging_user.id) end