Sam 47f5da7e42
FEATURE: Add AI-powered spam detection for new user posts (#1004)
This introduces a comprehensive spam detection system that uses LLM models
to automatically identify and flag potential spam posts. The system is
designed to be both powerful and configurable while preventing false positives.

Key Features:
* Automatically scans first 3 posts from new users (TL0/TL1)
* Creates dedicated AI flagging user to distinguish from system flags
* Tracks false positives/negatives for quality monitoring
* Supports custom instructions to fine-tune detection
* Includes test interface for trying detection on any post

Technical Implementation:
* New database tables:
  - ai_spam_logs: Stores scan history and results
  - ai_moderation_settings: Stores LLM config and custom instructions
* Rate limiting and safeguards:
  - Minimum 10-minute delay between rescans
  - Only scans significant edits (>10 char difference)
  - Maximum 3 scans per post
  - 24-hour maximum age for scannable posts
* Admin UI features:
  - Real-time testing capabilities
  - 7-day statistics dashboard
  - Configurable LLM model selection
  - Custom instruction support

Security and Performance:
* Respects trust levels - only scans TL0/TL1 users
* Skips private messages entirely
* Stops scanning users after 3 successful public posts
* Includes comprehensive test coverage
* Maintains audit log of all scan attempts


---------

Co-authored-by: Keegan George <kgeorge13@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-12-12 09:17:25 +11:00

49 lines
1.5 KiB
Ruby

# frozen_string_literal: true
RSpec.describe "AI Spam Configuration", type: :system, js: true do
fab!(:admin)
let(:llm_model) { Fabricate(:llm_model) }
before do
SiteSetting.discourse_ai_enabled = true
sign_in(admin)
end
it "can properly configure spam settings" do
visit "/admin/plugins/discourse-ai/ai-spam"
expect(page).to have_css(".ai-spam__llm-placeholder")
toggle = PageObjects::Components::DToggleSwitch.new(".ai-spam__toggle")
toggle.toggle
dialog = PageObjects::Components::Dialog.new
expect(dialog).to have_content(I18n.t("discourse_ai.llm.configuration.must_select_model"))
dialog.click_ok
expect(toggle.unchecked?).to eq(true)
llm_model
visit "/admin/plugins/discourse-ai/ai-spam"
toggle = PageObjects::Components::DToggleSwitch.new(".ai-spam__toggle")
toggle.toggle
try_until_success { expect(AiModerationSetting.spam&.llm_model_id).to eq(llm_model.id) }
find(".ai-spam__instructions-input").fill_in(with: "Test spam detection instructions")
find(".ai-spam__instructions-save").click
toasts = PageObjects::Components::Toasts.new
expect(toasts).to have_content(I18n.t("js.discourse_ai.spam.settings_saved"))
expect(AiModerationSetting.spam.custom_instructions).to eq("Test spam detection instructions")
visit "/admin/plugins/discourse-ai/ai-llms"
expect(find(".ai-llm-list-editor__usages")).to have_content(
I18n.t("js.discourse_ai.llms.usage.ai_spam"),
)
end
end