Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: keep track of silence reason when spam detection flags user #1046

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ en:
conversation_deleted: "Conversation share deleted successfully"
spam_detection:
flag_reason: "Flagged as spam by <a href='%{url}'>Discourse AI</a>"
silence_reason: "User silenced automatically by <a href='%{url}'>Discourse AI</a>"
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]"
Expand Down
19 changes: 16 additions & 3 deletions lib/ai_moderation/spam_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ def self.handle_spam(post, log)
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,
Expand All @@ -347,9 +349,20 @@ def self.handle_spam(post, log)
).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

Expand Down
9 changes: 9 additions & 0 deletions spec/lib/modules/ai_moderation/spam_scanner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@
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
Expand Down
Loading