Skip to content

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

FEATURE: Bot only PMs filter (#39891)

Gabriel Grubba7c271a81abcd
diff --git a/frontend/discourse/app/templates/user-private-messages/user.gjs b/frontend/discourse/app/templates/user-private-messages/user.gjs
index 35750bd34bf..5e8e605ae09 100644
--- a/frontend/discourse/app/templates/user-private-messages/user.gjs
+++ b/frontend/discourse/app/templates/user-private-messages/user.gjs
@@ -1,5 +1,7 @@
import { trustHTML } from "@ember/template";
+import PluginOutlet from "discourse/components/plugin-outlet";
import MessagesSecondaryNav from "discourse/components/user-nav/messages-secondary-nav";
+import lazyHash from "discourse/helpers/lazy-hash";
import DNavigationItem from "discourse/ui-kit/d-navigation-item";
import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
@@ -59,6 +61,13 @@ export default <template>
<span>{{i18n "user.messages.archive"}}</span>
</DNavigationItem>
+ <PluginOutlet
+ @name="user-messages-nav-bottom"
+ @outletArgs={{lazyHash
+ }}
+ />
</MessagesSecondaryNav>
{{outlet}}
diff --git a/lib/topic_query/private_message_lists.rb b/lib/topic_query/private_message_lists.rb
index 57d94a25fb8..fe657a4a965 100644
--- a/lib/topic_query/private_message_lists.rb
+++ b/lib/topic_query/private_message_lists.rb
@@ -6,6 +6,7 @@ class TopicQuery
list = user_personal_private_messages(user)
list = not_archived(list, user)
list = have_posts_from_others(list, user)
+ list = apply_personal_inbox_modifier(list, user)
create_list(:private_messages, {}, list, &blk)
end
@@ -51,12 +52,14 @@ class TopicQuery
list = filter_private_message_new(user, type)
list = TopicQuery.remove_muted_tags(list, user, skip_categories: true)
list = remove_dismissed(list, user)
+ list = apply_personal_inbox_modifier(list, user) if type == :user
create_list(:private_messages, {}, list)
end
def list_private_messages_unread(user, type = :user)
list = filter_private_messages_unread(user, type)
+ list = apply_personal_inbox_modifier(list, user) if type == :user
create_list(:private_messages, {}, list)
end
@@ -162,6 +165,10 @@ class TopicQuery
private
+ def apply_personal_inbox_modifier(list, user)
+ DiscoursePluginRegistry.apply_modifier(:private_messages_personal_inbox_query, list, user)
+ end
+
def append_read_state(list, group)
group_id = group.id
return list if group_id.nil?
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-chats-tab.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-chats-tab.gjs
new file mode 100644
index 00000000000..461463d1cb6
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-chats-tab.gjs
@@ -0,0 +1,28 @@
+import Component from "@glimmer/component";
+import { service } from "@ember/service";
+import DNavigationItem from "discourse/ui-kit/d-navigation-item";
+import dIcon from "discourse/ui-kit/helpers/d-icon";
+import { i18n } from "discourse-i18n";
+
+export default class AiBotChatsTab extends Component {
+ @service siteSettings;
+
+ get showTab() {
+ return (
+ this.args.outletArgs?.viewingSelf && this.siteSettings.ai_bot_enabled
+ );
+ }
+
+ <template>
+ {{#if this.showTab}}
+ <DNavigationItem
+ @route="discourse-ai-bot-conversations"
+ @ariaCurrentContext="subNav"
+ class="user-nav__messages-ai-bot-chats"
+ >
+ {{dIcon "robot"}}
+ <span>{{i18n "discourse_ai.bot_chats.tab_label"}}</span>
+ </DNavigationItem>
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-pm.gjs b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-pm.gjs
new file mode 100644
index 00000000000..bb2ef704b12
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-pm.gjs
@@ -0,0 +1,11 @@
+import { apiInitializer } from "discourse/lib/api";
+import AiBotChatsTab from "../components/ai-bot-chats-tab";
+
+export default apiInitializer((api) => {
+ const siteSettings = api.container.lookup("service:site-settings");
+ if (!siteSettings.ai_bot_enabled) {
+ return;
+ }
+
+ api.renderInOutlet("user-messages-nav-bottom", AiBotChatsTab);
+});
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index e448ad57e12..c1bb364ee6c 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -226,6 +226,9 @@ en:
discourse_ai:
title: "AI"
+ bot_chats:
+ tab_label: "Bot chats"
+
default_llm:
title: "Default LLM model"
description: "The default LLM model to use for all AI features. This will be used if no LLM is specified in the feature configuration or agent. If no default LLM is specified, the last created LLM will be used."
diff --git a/plugins/discourse-ai/lib/ai_bot/entry_point.rb b/plugins/discourse-ai/lib/ai_bot/entry_point.rb
index ea25bb41908..c9017233f28 100644
--- a/plugins/discourse-ai/lib/ai_bot/entry_point.rb
+++ b/plugins/discourse-ai/lib/ai_bot/entry_point.rb
@@ -71,6 +71,22 @@ module DiscourseAi
plugin.register_topic_custom_field_type(TOPIC_AI_BOT_PM_FIELD, :string)
+ # Hide bot PMs from the personal inbox queries (Latest, New, Unread)
+ # so human conversations are not buried under bot replies. Sent and
+ # Archive are intentionally untouched.
+ plugin.register_modifier(:private_messages_personal_inbox_query) do |list, _user|
+ next list unless SiteSetting.ai_bot_enabled
+
+ list.where(<<~SQL, field: TOPIC_AI_BOT_PM_FIELD)
+ NOT EXISTS (
+ SELECT 1 FROM topic_custom_fields tcf_pm_inbox
+ WHERE tcf_pm_inbox.topic_id = topics.id
+ AND tcf_pm_inbox.name = :field
+ AND tcf_pm_inbox.value = 't'
+ )
+ SQL
+ end
+
plugin.on(:topic_created) do |topic|
next if !topic.private_message?
creator = topic.user
diff --git a/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-chats-tab-test.js b/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-chats-tab-test.js
new file mode 100644
index 00000000000..b742de93736
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-chats-tab-test.js
@@ -0,0 +1,82 @@
+import { click, currentURL, visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+
+acceptance("AI Bot - Bot chats tab", function (needs) {
+ needs.user({
+ ai_enabled_agents: [],
+ ai_enabled_chat_bots: [],
+ });
+
+ needs.settings({
+ discourse_ai_enabled: true,
+ ai_bot_enabled: true,
+ });
+
+ needs.pretender((server, helper) => {
+ const emptyList = () => helper.response({ topic_list: { topics: [] } });
+
+ server.get("/topics/private-messages/:username.json", emptyList);
+ server.get("/discourse-ai/ai-bot/conversations.json", () => {
+ return helper.response({
+ conversations: [],
+ meta: {
+ has_more: false,
+ },
+ });
+ });
+ });
+
+ test("links to ai-bot conversations and navigates there on click", async function (assert) {
+ await visit("/u/eviltrout/messages");
+
+ assert
+ .dom(".user-nav__messages-ai-bot-chats")
+ .exists("bot chats tab is rendered");
+ assert
+ .dom(".user-nav__messages-ai-bot-chats a")
+ .hasAttribute(
+ "href",
+ "/discourse-ai/ai-bot/conversations",
+ "tab link resolves to the ai-bot conversations route"
+ );
+
+ await click(".user-nav__messages-ai-bot-chats a");
+
+ assert.strictEqual(
+ currentURL(),
+ "/discourse-ai/ai-bot/conversations",
+ "transitions to the ai-bot conversations route"
+ );
+ });
+
+ test("tab is hidden when viewing another user's messages", async function (assert) {
+ await visit("/u/charlie/messages");
+
+ assert
+ .dom(".user-nav__messages-ai-bot-chats")
+ .doesNotExist("bot chats tab is not rendered for other users");
+ });
+});
+
+acceptance("AI Bot - Bot chats tab - disabled", function (needs) {
+ needs.user();
+
+ needs.settings({
+ discourse_ai_enabled: true,
+ ai_bot_enabled: false,
+ });
+
+ needs.pretender((server, helper) => {
+ const emptyList = () => helper.response({ topic_list: { topics: [] } });
+ server.get("/topics/private-messages/:username.json", emptyList);
+ });
+
+ test("tab is hidden when ai_bot is disabled", async function (assert) {
+ await visit("/u/eviltrout/messages");
+
+ assert
+ .dom(".user-nav__messages-ai-bot-chats")
+ .doesNotExist("bot chats tab is not rendered when ai_bot is disabled");
+ });
+});
diff --git a/spec/lib/topic_query/private_message_lists_spec.rb b/spec/lib/topic_query/private_message_lists_spec.rb
index a68d19eb1bd..3a4cf76d801 100644
--- a/spec/lib/topic_query/private_message_lists_spec.rb
+++ b/spec/lib/topic_query/private_message_lists_spec.rb
@@ -57,6 +57,66 @@ RSpec.describe TopicQuery::PrivateMessageLists do
end
end
+ describe "private_messages_personal_inbox_query modifier" do
+ fab!(:hidden_pm) do
+ create_post(
+ user: user,
+ target_usernames: [user_2.username],
+ archetype: Archetype.private_message,
+ ).topic
+ end
+
+ let(:plugin) { Plugin::Instance.new }
+ let(:modifier_block) { ->(list, _user) { list.where(<<~SQL) } }
+ NOT EXISTS (
+ SELECT 1 FROM topic_custom_fields tcf
+ WHERE tcf.topic_id = topics.id
+ AND tcf.name = 'hidden_from_inbox'
+ AND tcf.value = 't'
+ )
+ SQL
+
+ before do
+ hidden_pm.custom_fields["hidden_from_inbox"] = "t"
+ hidden_pm.save_custom_fields
+
+ DiscoursePluginRegistry.register_modifier(
+ plugin,
+ :private_messages_personal_inbox_query,
+ &modifier_block
+ )
+ end
+
+ after do
+ DiscoursePluginRegistry.unregister_modifier(
+ plugin,
+ :private_messages_personal_inbox_query,
+ &modifier_block
+ )
+ end
+
+ it "filters topics out of #list_private_messages" do
+ topics = TopicQuery.new(user_2).list_private_messages(user_2).topics
+
+ expect(topics).to include(private_message)
+ expect(topics).not_to include(hidden_pm)
+ end
+
+ it "leaves #list_private_messages_archive untouched" do
+ [private_message, hidden_pm].each { |pm| UserArchivedMessage.archive!(user_2.id, pm) }
+
+ topics = TopicQuery.new(user_2).list_private_messages_archive(user_2).topics
+
+ expect(topics).to include(private_message, hidden_pm)
+ end
+
+ it "leaves #list_private_messages_sent untouched" do
+ topics = TopicQuery.new(user).list_private_messages_sent(user).topics
+
+ expect(topics).to include(private_message, hidden_pm)
+ end
+ end
+
describe "#list_private_messages_group" do
it "should return the right list for a group user" do
group.add(user_2)