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

Commit c3cd5d6

Browse files
committed
DEV: Add spam
1 parent 0fc854d commit c3cd5d6

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

app/controllers/discourse_ai/admin/ai_spam_controller.rb

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ def show
1010
end
1111

1212
def update
13+
# Get the initial settings for logging changes
14+
initial_settings = AiModerationSetting.spam
15+
initial_custom_instructions = initial_settings&.data&.dig("custom_instructions")
16+
initial_llm_model_id = initial_settings&.llm_model_id
17+
1318
updated_params = {}
1419
if allowed_params.key?(:llm_model_id)
1520
llm_model_id = updated_params[:llm_model_id] = allowed_params[:llm_model_id]
@@ -36,6 +41,9 @@ def update
3641
else
3742
AiModerationSetting.create!(updated_params.merge(setting_type: :spam))
3843
end
44+
45+
# Log any changes to custom_instructions or llm_model_id
46+
log_ai_spam_update(initial_llm_model_id, initial_custom_instructions, allowed_params)
3947
end
4048

4149
is_enabled = ActiveModel::Type::Boolean.new.cast(allowed_params[:is_enabled])
@@ -112,6 +120,34 @@ def fix_errors
112120
end
113121

114122
private
123+
124+
def log_ai_spam_update(initial_llm_model_id, initial_custom_instructions, params)
125+
# Track changes for logging
126+
changes_to_log = {}
127+
128+
# Only track llm_model_id changes when it's in the params AND has actually changed
129+
if params.key?(:llm_model_id) && initial_llm_model_id.to_s != params[:llm_model_id].to_s
130+
# Get model names for better logging
131+
old_model_name = LlmModel.find_by(id: initial_llm_model_id)&.display_name || initial_llm_model_id
132+
new_model_name = LlmModel.find_by(id: params[:llm_model_id])&.display_name || params[:llm_model_id]
133+
134+
changes_to_log[:llm_model_id] = "#{old_model_name}#{new_model_name}"
135+
end
136+
137+
# Only track custom_instructions changes when it's in the params AND has actually changed
138+
if params.key?(:custom_instructions) && initial_custom_instructions != params[:custom_instructions]
139+
changes_to_log[:custom_instructions] = params[:custom_instructions]
140+
end
141+
142+
# Log the changes if any were made to llm_model_id or custom_instructions
143+
if changes_to_log.present?
144+
# Log the changes using StaffActionLogger (without a subject as requested)
145+
StaffActionLogger.new(current_user).log_custom(
146+
"update_ai_spam_settings",
147+
changes_to_log
148+
)
149+
end
150+
end
115151

116152
def allowed_params
117153
params.permit(:is_enabled, :llm_model_id, :custom_instructions)
@@ -134,4 +170,4 @@ def spam_config
134170
end
135171
end
136172
end
137-
end
173+
end

config/locales/client.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ en:
3939
create_ai_embedding: "Create AI embedding"
4040
update_ai_embedding: "Update AI embedding"
4141
delete_ai_embedding: "Delete AI embedding"
42+
update_ai_spam_settings: "Update AI spam settings"
4243

4344
js:
4445
discourse_automation:

spec/requests/admin/ai_spam_controller_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,78 @@
119119
"custom instructions new",
120120
)
121121
end
122+
123+
it "logs staff action when custom_instructions change" do
124+
put "/admin/plugins/discourse-ai/ai-spam.json",
125+
params: {
126+
is_enabled: true,
127+
llm_model_id: llm_model.id,
128+
custom_instructions: "updated instructions"
129+
}
130+
131+
expect(response.status).to eq(200)
132+
133+
# Verify the log was created with the right subject
134+
history = UserHistory.where(action: UserHistory.actions[:custom_staff], custom_type: "update_ai_spam_settings").last
135+
expect(history).to be_present
136+
expect(history.subject).to eq("AI Spam Detection")
137+
expect(history.details).to include("custom_instructions_changed")
138+
end
139+
140+
it "logs staff action when llm_model_id changes" do
141+
# Create another model to change to
142+
new_llm_model = Fabricate(:llm_model, name: "New Test Model", display_name: "New Test Model")
143+
144+
put "/admin/plugins/discourse-ai/ai-spam.json",
145+
params: {
146+
llm_model_id: new_llm_model.id
147+
}
148+
149+
expect(response.status).to eq(200)
150+
151+
# Verify the log was created with the right subject
152+
history = UserHistory.where(action: UserHistory.actions[:custom_staff], custom_type: "update_ai_spam_settings").last
153+
expect(history).to be_present
154+
expect(history.subject).to eq("AI Spam Detection")
155+
expect(history.details).to include("llm_model_id")
156+
end
157+
158+
it "does not log staff action when only is_enabled changes" do
159+
# Check initial count of logs
160+
initial_count = UserHistory.where(action: UserHistory.actions[:custom_staff], custom_type: "update_ai_spam_settings").count
161+
162+
# Update only the is_enabled setting
163+
put "/admin/plugins/discourse-ai/ai-spam.json",
164+
params: {
165+
is_enabled: false
166+
}
167+
168+
expect(response.status).to eq(200)
169+
170+
# Verify no new log was created
171+
current_count = UserHistory.where(action: UserHistory.actions[:custom_staff], custom_type: "update_ai_spam_settings").count
172+
expect(current_count).to eq(initial_count)
173+
end
174+
175+
it "logs both custom_instructions and llm_model_id changes in one entry" do
176+
# Create another model to change to
177+
new_llm_model = Fabricate(:llm_model, name: "Another Test Model", display_name: "Another Test Model")
178+
179+
put "/admin/plugins/discourse-ai/ai-spam.json",
180+
params: {
181+
llm_model_id: new_llm_model.id,
182+
custom_instructions: "new instructions for both changes"
183+
}
184+
185+
expect(response.status).to eq(200)
186+
187+
# Verify the log was created with all changes
188+
history = UserHistory.where(action: UserHistory.actions[:custom_staff], custom_type: "update_ai_spam_settings").last
189+
expect(history).to be_present
190+
expect(history.subject).to eq("AI Spam Detection")
191+
expect(history.details).to include("llm_model_id")
192+
expect(history.details).to include("custom_instructions_changed")
193+
end
122194
end
123195
end
124196
end

0 commit comments

Comments
 (0)