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

Commit b24669c

Browse files
authored
DEV: Add structure for errors in spam (#1054)
This update adds some structure for handling errors in the spam config while also handling a specific error related to the spam scanning user not being an admin account.
1 parent 24b69bf commit b24669c

File tree

9 files changed

+164
-2
lines changed

9 files changed

+164
-2
lines changed

app/controllers/discourse_ai/admin/ai_spam_controller.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ def test
8686
render json: result
8787
end
8888

89+
def fix_errors
90+
case params[:error]
91+
when "spam_scanner_not_admin"
92+
begin
93+
DiscourseAi::AiModeration::SpamScanner.fix_spam_scanner_not_admin
94+
render json: success_json
95+
rescue ActiveRecord::RecordInvalid
96+
render_json_error(
97+
I18n.t("discourse_ai.spam_detection.bot_user_update_failed"),
98+
status: :unprocessable_entity,
99+
)
100+
rescue StandardError
101+
render_json_error(
102+
I18n.t("discourse_ai.spam_detection.unexpected"),
103+
status: :internal_server_error,
104+
)
105+
end
106+
else
107+
render_json_error(
108+
I18n.t("discourse_ai.spam_detection.invalid_error_type"),
109+
status: :bad_request,
110+
)
111+
end
112+
end
113+
89114
private
90115

91116
def allowed_params

app/serializers/ai_spam_serializer.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class AiSpamSerializer < ApplicationSerializer
77
:available_llms,
88
:stats,
99
:flagging_username,
10-
:spam_score_type
10+
:spam_score_type,
11+
:spam_scanning_user
1112

1213
def is_enabled
1314
object[:enabled]
@@ -47,4 +48,10 @@ def stats
4748
def settings
4849
object[:settings]
4950
end
51+
52+
def spam_scanning_user
53+
user = DiscourseAi::AiModeration::SpamScanner.flagging_user
54+
55+
user.serializable_hash(only: %i[id username name admin]) if user.present?
56+
end
5057
end

assets/javascripts/discourse/components/ai-spam.gjs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import DTooltip from "discourse/components/d-tooltip";
1313
import withEventValue from "discourse/helpers/with-event-value";
1414
import { ajax } from "discourse/lib/ajax";
1515
import { popupAjaxError } from "discourse/lib/ajax-error";
16+
import dIcon from "discourse-common/helpers/d-icon";
1617
import i18n from "discourse-common/helpers/i18n";
1718
import getURL from "discourse-common/lib/get-url";
1819
import AdminConfigAreaCard from "admin/components/admin-config-area-card";
@@ -35,10 +36,51 @@ export default class AiSpam extends Component {
3536
@tracked isEnabled = false;
3637
@tracked selectedLLM = null;
3738
@tracked customInstructions = "";
39+
@tracked errors = [];
3840

3941
constructor() {
4042
super(...arguments);
4143
this.initializeFromModel();
44+
45+
if (this.args.model?.spam_scanning_user?.admin === false) {
46+
this.errors.push({
47+
message: i18n("discourse_ai.spam.errors.scan_not_admin.message"),
48+
button: {
49+
label: i18n("discourse_ai.spam.errors.scan_not_admin.action"),
50+
action: this.fixScanUserNotAdmin,
51+
},
52+
});
53+
}
54+
}
55+
56+
@action
57+
async fixScanUserNotAdmin() {
58+
const spamScanningUser = this.args.model.spam_scanning_user;
59+
if (!spamScanningUser || spamScanningUser.admin) {
60+
return;
61+
}
62+
try {
63+
const response = await ajax(
64+
`/admin/plugins/discourse-ai/ai-spam/fix-errors`,
65+
{
66+
type: "POST",
67+
data: {
68+
error: "spam_scanner_not_admin",
69+
},
70+
}
71+
);
72+
73+
if (response.success) {
74+
this.toasts.success({
75+
data: { message: i18n("discourse_ai.spam.errors.resolved") },
76+
duration: 2000,
77+
});
78+
}
79+
} catch (error) {
80+
popupAjaxError(error);
81+
} finally {
82+
window.location.reload();
83+
}
4284
}
4385

4486
@action
@@ -165,11 +207,22 @@ export default class AiSpam extends Component {
165207
<template>
166208
<div class="ai-spam">
167209
<section class="ai-spam__settings">
210+
<div class="ai-spam__errors">
211+
{{#each this.errors as |e|}}
212+
<div class="alert alert-error">
213+
{{dIcon "triangle-exclamation"}}
214+
<p>{{e.message}}</p>
215+
<DButton
216+
@action={{e.button.action}}
217+
@translatedLabel={{e.button.label}}
218+
/>
219+
</div>
220+
{{/each}}
221+
</div>
168222
<DPageSubheader
169223
@titleLabel={{i18n "discourse_ai.spam.title"}}
170224
@descriptionLabel={{i18n "discourse_ai.spam.spam_description"}}
171225
/>
172-
173226
<div class="control-group ai-spam__enabled">
174227
<DToggleSwitch
175228
class="ai-spam__toggle"

assets/stylesheets/modules/llms/common/spam.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@
4545
&__stats {
4646
margin-top: 2em;
4747
}
48+
49+
&__errors {
50+
.alert {
51+
display: flex;
52+
align-items: center;
53+
gap: 0.5rem;
54+
55+
.btn {
56+
margin-left: auto;
57+
}
58+
}
59+
}
4860
}
4961

5062
.spam-test-modal {

config/locales/client.en.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ en:
160160
stat_tooltips:
161161
incorrectly_flagged: "Items that the AI bot flagged as spam where moderators disagreed"
162162
missed_spam: "Items flagged by the community as spam that were not detected by the AI bot, which moderators agreed with"
163+
errors:
164+
scan_not_admin:
165+
message: "Warning: spam scanning will not work correctly because the spam scan account is not an admin"
166+
action: "Fix"
167+
resolved: "The error has been resolved!"
163168

164169
usage:
165170
short_title: "Usage"

config/locales/server.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ en:
255255
spam_detection:
256256
flag_reason: "Flagged as spam by <a href='%{url}'>Discourse AI</a>"
257257
silence_reason: "User silenced automatically by <a href='%{url}'>Discourse AI</a>"
258+
invalid_error_type: "Invalid error type provided"
259+
unexpected: "An unexpected error occured"
260+
bot_user_update_failed: "Failed to update the spam scanning bot user"
261+
258262
ai_bot:
259263
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]"
260264
default_pm_prefix: "[Untitled AI bot PM]"

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
get "/ai-spam", to: "discourse_ai/admin/ai_spam#show"
8484
put "/ai-spam", to: "discourse_ai/admin/ai_spam#update"
8585
post "/ai-spam/test", to: "discourse_ai/admin/ai_spam#test"
86+
post "/ai-spam/fix-errors", to: "discourse_ai/admin/ai_spam#fix_errors"
8687

8788
resources :ai_llms,
8889
only: %i[index new create edit update destroy],

lib/ai_moderation/spam_scanner.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ def self.perform_scan(post)
241241
end
242242
end
243243

244+
def self.fix_spam_scanner_not_admin
245+
user = DiscourseAi::AiModeration::SpamScanner.flagging_user
246+
247+
if user.present?
248+
user.update!(admin: true)
249+
else
250+
raise Discourse::NotFound
251+
end
252+
end
253+
244254
private
245255

246256
def self.check_if_spam(result)

spec/requests/admin/ai_spam_controller_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,49 @@
306306
end
307307
end
308308
end
309+
310+
describe "#fix_errors" do
311+
fab!(:setting) do
312+
AiModerationSetting.create(
313+
{
314+
setting_type: :spam,
315+
llm_model_id: llm_model.id,
316+
data: {
317+
custom_instructions: "custom instructions",
318+
},
319+
},
320+
)
321+
fab!(:llm_model)
322+
323+
before do
324+
sign_in(admin)
325+
DiscourseAi::AiModeration::SpamScanner.flagging_user.update!(admin: false)
326+
end
327+
328+
it "resolves spam scanner not admin error" do
329+
post "/admin/plugins/discourse-ai/ai-spam/fix-errors",
330+
params: {
331+
error: "spam_scanner_not_admin",
332+
}
333+
334+
expect(response.status).to eq(200)
335+
expect(DiscourseAi::AiModeration::SpamScanner.flagging_user.reload.admin).to eq(true)
336+
end
337+
338+
it "returns an error when it can't update the user" do
339+
DiscourseAi::AiModeration::SpamScanner.flagging_user.destroy
340+
341+
post "/admin/plugins/discourse-ai/ai-spam/fix-errors",
342+
params: {
343+
error: "spam_scanner_not_admin",
344+
}
345+
346+
expect(response.status).to eq(422)
347+
expect(response.parsed_body["errors"]).to be_present
348+
expect(response.parsed_body["errors"].first).to eq(
349+
I18n.t("discourse_ai.spam_detection.bot_user_update_failed"),
350+
)
351+
end
352+
end
353+
end
309354
end

0 commit comments

Comments
 (0)