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

Commit 32dc45b

Browse files
authored
FIX: never block spam scanning user (#1437)
Previously staff and bots would get scanned if TL was low Additionally if somehow spam scanner user was blocked (deactivated, silenced, banned) it would stop the feature from working This adds an override that ensures unconditionally the user is setup correctly prior to scanning
1 parent bc8e57d commit 32dc45b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/ai_moderation/spam_scanner.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,20 @@ def self.flagging_user
4747
user = nil
4848
if SiteSetting.ai_spam_detection_user_id.present?
4949
user = User.find_by(id: SiteSetting.ai_spam_detection_user_id)
50+
ensure_safe_flagging_user!(user)
5051
end
5152
user || Discourse.system_user
5253
end
5354

55+
def self.ensure_safe_flagging_user!(user)
56+
# only do repair on bot users, if somehow it is set to a human skip repairs
57+
return if !user.bot?
58+
user.update!(silenced_till: nil) if user.silenced?
59+
user.update!(trust_level: TrustLevel[4]) if user.trust_level != TrustLevel[4]
60+
user.update!(suspended_till: nil, suspended_at: nil) if user.suspended?
61+
user.update!(active: true) if !user.active?
62+
end
63+
5464
def self.after_cooked_post(post)
5565
return if !enabled?
5666
return if !should_scan_post?(post)
@@ -94,6 +104,9 @@ def self.should_scan_post?(post)
94104
return false if !post.present?
95105
return false if post.user.trust_level > TrustLevel[1]
96106
return false if post.topic.private_message?
107+
return false if post.user.bot?
108+
return false if post.user.staff?
109+
97110
if Post
98111
.where(user_id: post.user_id)
99112
.joins(:topic)

spec/lib/modules/ai_moderation/spam_scanner_spec.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
expect(described_class.should_scan_post?(post)).to eq(false)
5050
end
5151

52+
it "returns false for bots" do
53+
post.user.id = -100
54+
expect(described_class.should_scan_post?(post)).to eq(false)
55+
end
56+
57+
it "returns false for staff" do
58+
post.user.moderator = true
59+
expect(described_class.should_scan_post?(post)).to eq(false)
60+
end
61+
5262
it "returns false for users with many public posts" do
5363
Fabricate(:post, user: user, topic: topic)
5464
Fabricate(:post, user: user, topic: topic)
@@ -207,6 +217,26 @@
207217
end
208218
end
209219

220+
it "unsilences flagging user if erronuously silenced" do
221+
described_class.flagging_user.update!(silenced_till: 1.day.from_now)
222+
expect(described_class.flagging_user.silenced?).to eq(false)
223+
end
224+
225+
it "ensures flagging user is tl4" do
226+
described_class.flagging_user.update!(trust_level: 0)
227+
expect(described_class.flagging_user.trust_level).to eq(4)
228+
end
229+
230+
it "unsuspends user if it was erronuously suspended" do
231+
described_class.flagging_user.update!(suspended_till: 1.day.from_now, suspended_at: 1.day.ago)
232+
expect(described_class.flagging_user.suspended?).to eq(false)
233+
end
234+
235+
it "makes sure account is active" do
236+
described_class.flagging_user.update!(active: false)
237+
expect(described_class.flagging_user.active).to eq(true)
238+
end
239+
210240
describe "integration test" do
211241
fab!(:llm_model)
212242
let(:api_audit_log) { Fabricate(:api_audit_log) }
@@ -243,8 +273,13 @@
243273
it "correctly handles spam scanning" do
244274
expect(described_class.flagging_user.id).not_to eq(Discourse.system_user.id)
245275

246-
# flag post for scanning
247276
post = post_with_uploaded_image
277+
# this is surprising, core fabricator is not linking
278+
# we need it linked so we scan uploads
279+
post.link_post_uploads
280+
281+
expect(described_class.should_scan_post?(post)).to eq(true)
282+
expect(post.upload_ids).to be_present
248283

249284
described_class.new_post(post)
250285

0 commit comments

Comments
 (0)