Skip to content

A platform for community discussion. Free, open, simple.

FIX: Suppress composer mention warning for AI bot users (#39986)

Régis Hanol0bef34a01c1e
diff --git a/app/controllers/composer_controller.rb b/app/controllers/composer_controller.rb
index 0b433032ebe..a30e7c908f7 100644
--- a/app/controllers/composer_controller.rb
+++ b/app/controllers/composer_controller.rb
@@ -96,7 +96,7 @@ class ComposerController < ApplicationController
# Non-staff users can see only basic information why the users cannot see the topic.
reason = nil if !guardian.is_staff? && reason != :private && reason != :category
- reason
+ DiscoursePluginRegistry.apply_modifier(:composer_mention_user_reason, reason, user)
end
def group_reason(group)
diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rb
index 39ccfe094a0..20a520fb5f4 100644
--- a/plugins/discourse-ai/plugin.rb
+++ b/plugins/discourse-ai/plugin.rb
@@ -142,6 +142,11 @@ after_initialize do
Post.prepend DiscourseAi::PostExtensions
end
+ # AI bots reply via `skip_guardian: true`, so the reachability warning is misleading.
+ register_modifier(:composer_mention_user_reason) do |reason, user|
+ DiscourseAi::AiBot::EntryPoint.all_bot_ids.include?(user.id) ? nil : reason
+ end
+
register_modifier(:post_should_secure_uploads?) do |_, _, topic|
if topic.private_message? && SharedAiConversation.exists?(target: topic)
false
diff --git a/plugins/discourse-ai/spec/requests/composer_controller_spec.rb b/plugins/discourse-ai/spec/requests/composer_controller_spec.rb
new file mode 100644
index 00000000000..092ef65da6b
--- /dev/null
+++ b/plugins/discourse-ai/spec/requests/composer_controller_spec.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+RSpec.describe ComposerController do
+ fab!(:admin)
+ fab!(:ai_agent)
+ fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
+ fab!(:restricted_topic) { Fabricate(:topic, category: private_category) }
+
+ before do
+ enable_current_plugin
+ sign_in(admin)
+ end
+
+ describe "#mentions" do
+ it "suppresses the reachability warning when mentioning an AI bot user" do
+ agent_user = ai_agent.create_user!
+
+ get "/composer/mentions.json",
+ params: {
+ names: [agent_user.username.upcase],
+ topic_id: restricted_topic.id,
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["users"]).to contain_exactly(agent_user.username_lower)
+ expect(response.parsed_body["user_reasons"]).to eq({})
+ end
+ end
+end
diff --git a/spec/requests/composer_controller_spec.rb b/spec/requests/composer_controller_spec.rb
index 754d8819aaf..f5340f579f3 100644
--- a/spec/requests/composer_controller_spec.rb
+++ b/spec/requests/composer_controller_spec.rb
@@ -329,6 +329,56 @@ RSpec.describe ComposerController do
end
end
+ context "with the composer_mention_user_reason modifier" do
+ fab!(:modified_user) { Fabricate(:user, username: "ModifiedReason") }
+ fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
+ fab!(:restricted_topic) { Fabricate(:topic, category: private_category) }
+
+ before { sign_in(Fabricate(:admin)) }
+
+ it "lets a plugin clear the reachability reason" do
+ target_id = modified_user.id
+ modifier = Proc.new { |reason, user| user.id == target_id ? nil : reason }
+ plugin_instance = Plugin::Instance.new
+ DiscoursePluginRegistry.register_modifier(
+ plugin_instance,
+ :composer_mention_user_reason,
+ &modifier
+ )
+
+ begin
+ get "/composer/mentions.json",
+ params: {
+ names: [modified_user.username],
+ topic_id: restricted_topic.id,
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["users"]).to contain_exactly(modified_user.username.downcase)
+ expect(response.parsed_body["user_reasons"]).to eq({})
+ ensure
+ DiscoursePluginRegistry.unregister_modifier(
+ plugin_instance,
+ :composer_mention_user_reason,
+ &modifier
+ )
+ end
+ end
+
+ it "still returns the reachability reason without the modifier" do
+ get "/composer/mentions.json",
+ params: {
+ names: [modified_user.username],
+ topic_id: restricted_topic.id,
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["user_reasons"]).to eq(
+ modified_user.username.downcase => "category",
+ )
+ end
+ end
+
context "with an invalid topic" do
it "returns an error" do
get "/composer/mentions.json", params: { names: base_names, topic_id: -1 }