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

Commit eb3a692

Browse files
committed
move stuff around, show in enumerator
1 parent 80e648d commit eb3a692

File tree

5 files changed

+56
-40
lines changed

5 files changed

+56
-40
lines changed

app/controllers/discourse_ai/admin/ai_spam_controller.rb

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,46 +44,7 @@ def spam_config
4444
settings: AiModerationSetting.spam,
4545
}
4646

47-
spam_status = [Reviewable.statuses[:approved], Reviewable.statuses[:deleted]]
48-
ham_status = [Reviewable.statuses[:rejected], Reviewable.statuses[:ignored]]
49-
50-
# todo maybe move this to a report class
51-
sql = <<~SQL
52-
WITH spam_stats AS (
53-
SELECT
54-
asl.reviewable_id,
55-
asl.post_id,
56-
asl.is_spam,
57-
r.status as reviewable_status,
58-
r.target_type,
59-
r.potential_spam
60-
FROM ai_spam_logs asl
61-
LEFT JOIN reviewables r ON r.id = asl.reviewable_id
62-
WHERE asl.created_at > :date
63-
),
64-
post_reviewables AS (
65-
SELECT
66-
target_id post_id,
67-
COUNT(DISTINCT target_id) as false_negative_count
68-
FROM reviewables
69-
WHERE target_type = 'Post'
70-
AND status IN (:spam)
71-
AND potential_spam
72-
AND target_id IN (SELECT post_id FROM spam_stats)
73-
GROUP BY target_id
74-
)
75-
SELECT
76-
COUNT(*) AS scanned_count,
77-
SUM(CASE WHEN is_spam THEN 1 ELSE 0 END) AS spam_detected,
78-
COUNT(CASE WHEN reviewable_status IN (:ham) THEN 1 END) AS false_positives,
79-
COALESCE(SUM(pr.false_negative_count), 0) AS false_negatives
80-
FROM spam_stats
81-
LEFT JOIN post_reviewables pr USING (post_id)
82-
SQL
83-
84-
stats = DB.query(sql, spam: spam_status, ham: ham_status, date: 1.week.ago).first
85-
86-
spam_config[:stats] = stats
47+
spam_config[:stats] = DiscourseAi::AiModeration::SpamReport.generate(min_date: 1.week.ago)
8748
spam_config
8849
end
8950
end

config/locales/client.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ en:
307307
ai_persona: "Persona (%{persona})"
308308
ai_summarization: "Summarize"
309309
ai_embeddings_semantic_search: "AI search"
310+
ai_spam: "Spam"
310311
in_use_warning:
311312
one: "This model is currently used by %{settings}. If misconfigured, the feature won't work as expected."
312313
other: "This model is currently used by the following: %{settings}. If misconfigured, features won't work as expected. "

lib/ai_moderation/spam_report.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseAi
4+
module AiModeration
5+
class SpamReport
6+
def self.generate(min_date: 1.week.ago)
7+
spam_status = [Reviewable.statuses[:approved], Reviewable.statuses[:deleted]]
8+
ham_status = [Reviewable.statuses[:rejected], Reviewable.statuses[:ignored]]
9+
10+
sql = <<~SQL
11+
WITH spam_stats AS (
12+
SELECT
13+
asl.reviewable_id,
14+
asl.post_id,
15+
asl.is_spam,
16+
r.status as reviewable_status,
17+
r.target_type,
18+
r.potential_spam
19+
FROM ai_spam_logs asl
20+
LEFT JOIN reviewables r ON r.id = asl.reviewable_id
21+
WHERE asl.created_at > :min_date
22+
),
23+
post_reviewables AS (
24+
SELECT
25+
target_id post_id,
26+
COUNT(DISTINCT target_id) as false_negative_count
27+
FROM reviewables
28+
WHERE target_type = 'Post'
29+
AND status IN (:spam)
30+
AND potential_spam
31+
AND target_id IN (SELECT post_id FROM spam_stats)
32+
GROUP BY target_id
33+
)
34+
SELECT
35+
COUNT(*) AS scanned_count,
36+
SUM(CASE WHEN is_spam THEN 1 ELSE 0 END) AS spam_detected,
37+
COUNT(CASE WHEN reviewable_status IN (:ham) THEN 1 END) AS false_positives,
38+
COALESCE(SUM(pr.false_negative_count), 0) AS false_negatives
39+
FROM spam_stats
40+
LEFT JOIN post_reviewables pr USING (post_id)
41+
SQL
42+
43+
DB.query(sql, spam: spam_status, ham: ham_status, min_date: min_date).first
44+
end
45+
end
46+
end
47+
end

lib/ai_moderation/spam_scanner.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def self.flagging_user
5252
end
5353

5454
def self.after_cooked_post(post)
55+
return if !enabled?
56+
return if !should_scan_post(post)
5557
return if !post.custom_fields[SHOULD_SCAN_POST_CUSTOM_FIELD]
5658
return if post.updated_at < MAX_AGE_TO_SCAN.ago
5759

lib/configuration/llm_enumerator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def self.global_usage
3838
rval[model_id] << { type: :ai_embeddings_semantic_search }
3939
end
4040

41+
if SiteSetting.ai_spam_detection_enabled
42+
model_id = AiModerationSetting.spam[:llm_model_id]
43+
rval[model_id] << { type: :ai_spam }
44+
end
45+
4146
rval
4247
end
4348

0 commit comments

Comments
 (0)