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

Commit f9f89ad

Browse files
authored
FIX: keep track of silence reason when spam detection flags user (#1046)
Previously reason was blank for silencing user
1 parent b480f13 commit f9f89ad

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

config/locales/server.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ en:
253253
conversation_deleted: "Conversation share deleted successfully"
254254
spam_detection:
255255
flag_reason: "Flagged as spam by <a href='%{url}'>Discourse AI</a>"
256+
silence_reason: "User silenced automatically by <a href='%{url}'>Discourse AI</a>"
256257
ai_bot:
257258
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]"
258259
default_pm_prefix: "[Untitled AI bot PM]"

lib/ai_moderation/spam_scanner.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ def self.handle_spam(post, log)
337337
url = "#{Discourse.base_url}/admin/plugins/discourse-ai/ai-spam"
338338
reason = I18n.t("discourse_ai.spam_detection.flag_reason", url: url)
339339

340+
flagging_user = self.flagging_user
341+
340342
result =
341343
PostActionCreator.new(
342344
flagging_user,
@@ -347,9 +349,20 @@ def self.handle_spam(post, log)
347349
).perform
348350

349351
log.update!(reviewable: result.reviewable)
350-
SpamRule::AutoSilence.new(post.user, post).silence_user
351-
# this is required cause tl1 is not auto hidden
352-
# we want to also handle tl1
352+
353+
reason = I18n.t("discourse_ai.spam_detection.silence_reason", url: url)
354+
silencer =
355+
UserSilencer.new(
356+
post.user,
357+
flagging_user,
358+
message: :too_many_spam_flags,
359+
post_id: post.id,
360+
reason: reason,
361+
keep_posts: true,
362+
)
363+
silencer.silence
364+
365+
# silencer will not hide tl1 posts, so we do this here
353366
hide_posts_and_topics(post.user)
354367
end
355368

spec/lib/modules/ai_moderation/spam_scanner_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@
215215
expect(post.user.reload.silenced_till).to be_present
216216
expect(post.topic.reload.visible).to eq(false)
217217

218+
history = UserHistory.where(action: UserHistory.actions[:silence_user]).order(:id).last
219+
220+
url = "#{Discourse.base_url}/admin/plugins/discourse-ai/ai-spam"
221+
222+
expect(history.target_user_id).to eq(post.user_id)
223+
expect(history.details).to include(
224+
I18n.t("discourse_ai.spam_detection.silence_reason", url: url),
225+
)
226+
218227
expect(log.reviewable).to be_present
219228
expect(log.reviewable.created_by_id).to eq(described_class.flagging_user.id)
220229
end

0 commit comments

Comments
 (0)