A platform for community discussion. Free, open, simple.
FIX: exclude filtered bot PMs from unread PM count (#44133)
Krisf673d157f393
diff --git a/app/models/private_message_topic_tracking_state.rb b/app/models/private_message_topic_tracking_state.rbindex 45be8238ff7..a53aa28f36e 100644--- a/app/models/private_message_topic_tracking_state.rb+++ b/app/models/private_message_topic_tracking_state.rb@@ -71,6 +71,8 @@ class PrivateMessageTopicTrackingStateTopicTrackingState.new_filter_sqlend+ filter_sql = topics_filter_sql++<<~SQLSELECTDISTINCT topics.id AS topic_id,@@ -93,13 +95,30 @@ class PrivateMessageTopicTrackingState((#{unread}) OR (#{new})) ANDtopics.deleted_at IS NULL#{user.staff? ? "" : "AND topics.visible"}+ #{filter_sql ? "AND #{filter_sql}" : ""}SQLend+ # Lets plugins exclude topics from PM tracking entirely, e.g. topics the+ # personal inbox lists no longer show, so they don't leave stray counts.+ def self.topics_filter_sql+ conditions =+ DiscoursePluginRegistry.apply_modifier(:private_message_topic_tracking_state_filters, [])+ conditions.map { |condition| "(#{condition})" }.join(" AND ").presence+ end++ def self.tracked_topic?(topic)+ sql = topics_filter_sql+ sql.nil? || Topic.where(id: topic.id).where(sql).exists?+ end++ private_class_method :topics_filter_sql, :tracked_topic?+def self.publish_unread(post)topic = post.topicreturn unless topic.private_message?return if post.small_action?+ return unless tracked_topic?(topic)scope = TopicUser.tracking(post.topic_id).includes(user: %i[user_stat user_option])@@ -147,6 +166,7 @@ class PrivateMessageTopicTrackingStatedef self.publish_new(topic)return unless topic.private_message?+ return unless tracked_topic?(topic)message = {message_type: NEW_MESSAGE_TYPE,diff --git a/plugins/discourse-ai/lib/ai_bot/entry_point.rb b/plugins/discourse-ai/lib/ai_bot/entry_point.rbindex 43ba2fc44fe..59cf0e342fb 100644--- a/plugins/discourse-ai/lib/ai_bot/entry_point.rb+++ b/plugins/discourse-ai/lib/ai_bot/entry_point.rb@@ -126,20 +126,30 @@ module DiscourseAiend,) { true }+ not_bot_pm_sql = <<~SQL+ NOT EXISTS (+ SELECT 1 FROM topic_custom_fields tcf_pm_inbox+ WHERE tcf_pm_inbox.topic_id = topics.id+ AND tcf_pm_inbox.name = '#{TOPIC_AI_BOT_PM_FIELD}'+ AND tcf_pm_inbox.value = 't'+ )+ SQL+# 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+ list.where(not_bot_pm_sql)+ end++ # Keep bot PMs out of the inbox unread/new counts too, since the+ # lists above no longer show them.+ plugin.register_modifier(:private_message_topic_tracking_state_filters) do |filters|+ next filters unless SiteSetting.ai_bot_enabled++ filters + [not_bot_pm_sql]endplugin.register_modifier(:guardian_can_send_private_message_to_target) do |allowed, params|diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/entry_point_spec.rbindex 9ed29e03d20..ad993714ff8 100644--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/entry_point_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/entry_point_spec.rb@@ -91,6 +91,54 @@ describe DiscourseAi::AiBot::EntryPoint doendend+ describe "PM topic tracking state" do+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }++ let!(:bot_pm) { PostCreator.create!(admin, post_args).topic }+ let!(:human_pm) do+ PostCreator.create!(+ user,+ title: "A question for a human instead",+ raw: "Hello there, do you have a minute?",+ archetype: Archetype.private_message,+ target_usernames: admin.username,+ ).topic+ end+ let!(:bot_reply) do+ PostCreator.create!(gpt_bot, topic_id: bot_pm.id, raw: "Here is my considered answer.")+ end++ before do+ PostCreator.create!(user, topic_id: human_pm.id, raw: "Following up on this one.")+ TopicUser.update_last_read(admin, bot_pm.id, 1, 1, 0)+ TopicUser.update_last_read(admin, human_pm.id, 1, 1, 0)+ end++ it "leaves bot PMs out of the unread report" do+ expect(PrivateMessageTopicTrackingState.report(admin).map(&:topic_id)).to contain_exactly(+ human_pm.id,+ )+ end++ it "does not publish unread updates for bot PMs" do+ messages =+ MessageBus.track_publish(PrivateMessageTopicTrackingState.user_channel(admin.id)) do+ PrivateMessageTopicTrackingState.publish_unread(bot_reply)+ end++ expect(messages).to be_empty+ end++ it "includes bot PMs when the AI bot is disabled" do+ SiteSetting.ai_bot_enabled = false++ expect(PrivateMessageTopicTrackingState.report(admin).map(&:topic_id)).to contain_exactly(+ bot_pm.id,+ human_pm.id,+ )+ end+ end+it "adds information about forcing default llm to current_user_serializer" doGroup.refresh_automatic_groups!diff --git a/spec/models/private_message_topic_tracking_state_spec.rb b/spec/models/private_message_topic_tracking_state_spec.rbindex 0189eddabd1..04e5b4a9558 100644--- a/spec/models/private_message_topic_tracking_state_spec.rb+++ b/spec/models/private_message_topic_tracking_state_spec.rb@@ -226,4 +226,58 @@ RSpec.describe PrivateMessageTopicTrackingState doexpect(data["payload"]["notification_level"]).to eq(nil)endend++ describe "private_message_topic_tracking_state_filters modifier" do+ let(:plugin) { Plugin::Instance.new }+ let(:modifier_block) { ->(filters) { filters + [<<~SQL] } }+ NOT EXISTS (+ SELECT 1 FROM topic_custom_fields tcf+ WHERE tcf.topic_id = topics.id+ AND tcf.name = 'untracked'+ AND tcf.value = 't'+ )+ SQL++ before do+ private_message.custom_fields["untracked"] = "t"+ private_message.save_custom_fields++ DiscoursePluginRegistry.register_modifier(+ plugin,+ :private_message_topic_tracking_state_filters,+ &modifier_block+ )+ end++ after do+ DiscoursePluginRegistry.unregister_modifier(+ plugin,+ :private_message_topic_tracking_state_filters,+ &modifier_block+ )+ end++ it "excludes filtered topics from .report" do+ expect(described_class.report(user_2).map(&:topic_id)).to contain_exactly(group_message.id)+ end++ it "does not publish new messages for filtered topics" do+ messages = MessageBus.track_publish { described_class.publish_new(private_message) }++ expect(messages).to be_empty+ end++ it "does not publish unread messages for filtered topics" do+ messages =+ MessageBus.track_publish { described_class.publish_unread(private_message.first_post) }++ expect(messages).to be_empty+ end++ it "still publishes messages for other topics" do+ messages = MessageBus.track_publish { described_class.publish_new(group_message) }++ expect(messages.map(&:channel)).to contain_exactly(described_class.group_channel(group.id))+ end+ endend