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

Commit 2c99aa0

Browse files
committed
DEV: Add structure for errors in spam
This PR 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 2c99aa0

File tree

7 files changed

+93
-2
lines changed

7 files changed

+93
-2
lines changed

app/controllers/discourse_ai/admin/ai_spam_controller.rb

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

89+
def fix_errors
90+
if params[:error] == "spam_scanner_not_admin"
91+
DiscourseAi::AiModeration::SpamScanner.fix_spam_scanner_not_admin
92+
end
93+
94+
render json: success_json
95+
end
96+
8997
private
9098

9199
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/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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ 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+
user.update!(admin: true) if user.present?
247+
end
248+
244249
private
245250

246251
def self.check_if_spam(result)

0 commit comments

Comments
 (0)