FIX: Do not publish post for PM topic tracking if not new for user. (#14469)

FIX: Do not publish post for PM topic tracking if not new for user. (#14469)

diff --git a/app/assets/javascripts/discourse/app/models/private-message-topic-tracking-state.js b/app/assets/javascripts/discourse/app/models/private-message-topic-tracking-state.js
index 1cb6810..b5aee27 100644
--- a/app/assets/javascripts/discourse/app/models/private-message-topic-tracking-state.js
+++ b/app/assets/javascripts/discourse/app/models/private-message-topic-tracking-state.js
@@ -169,7 +169,7 @@ const PrivateMessageTopicTrackingState = EmberObject.extend({
           [INBOX_FILTER, ARCHIVE_FILTER].includes(this.filter) &&
           (!message.payload.acting_user_id ||
             message.payload.acting_user_id !== this.currentUser.id) &&
-          (this.inbox === "all" || this._displayMessageForGroupInbox(message))
+          this._displayMessageForGroupInbox(message)
         ) {
           this._notifyIncoming(message.topic_id);
         }
@@ -187,7 +187,6 @@ const PrivateMessageTopicTrackingState = EmberObject.extend({
 
   _shouldDisplayMessageForInbox(message) {
     return (
-      this.inbox === "all" ||
       this._displayMessageForGroupInbox(message) ||
       (this.inbox === "user" &&
         (message.payload.group_ids.length === 0 ||
diff --git a/app/models/private_message_topic_tracking_state.rb b/app/models/private_message_topic_tracking_state.rb
index 74ad8d5..76bdbb8 100644
--- a/app/models/private_message_topic_tracking_state.rb
+++ b/app/models/private_message_topic_tracking_state.rb
@@ -98,13 +98,14 @@ class PrivateMessageTopicTrackingState
   end
 
   def self.publish_unread(post)
-    return unless post.topic.private_message?
+    topic = post.topic
+    return unless topic.private_message?
 
     scope = TopicUser
       .tracking(post.topic_id)
-      .includes(user: :user_stat)
+      .includes(user: [:user_stat, :user_option])
 
-    allowed_group_ids = post.topic.allowed_groups.pluck(:id)
+    allowed_group_ids = topic.allowed_groups.pluck(:id)
 
     group_ids =
       if post.post_type == Post.types[:whisper]
@@ -123,6 +124,12 @@ class PrivateMessageTopicTrackingState
       .select([:user_id, :last_read_post_number, :notification_level])
       .each do |tu|
 
+      if tu.last_read_post_number.nil? &&
+          topic.created_at < tu.user.user_option.treat_as_new_topic_start_date
+
+        next
+      end
+
       message = {
         topic_id: post.topic_id,
         message_type: UNREAD_MESSAGE_TYPE,
diff --git a/spec/models/private_message_topic_tracking_state_spec.rb b/spec/models/private_message_topic_tracking_state_spec.rb
index f0fdb38..3d9b1bc 100644
--- a/spec/models/private_message_topic_tracking_state_spec.rb
+++ b/spec/models/private_message_topic_tracking_state_spec.rb
@@ -166,6 +166,17 @@ describe PrivateMessageTopicTrackingState do
         .to eq(NotificationLevels.all[:watching])
       expect(data['payload']['group_ids']).to eq([])
     end
+
+    it 'does not publish message_bus message if post in topic is not new for user' do
+      group_message.update!(created_at: 3.days.ago)
+      user_2.user_option.update!(new_topic_duration_minutes: 2.days.minutes)
+
+      messages = MessageBus.track_publish do
+        described_class.publish_unread(group_message.first_post)
+      end
+
+      expect(messages).to eq([])
+    end
   end
 
   describe '.publish_group_archived' do

GitHub sha: a1745e05ae32018e2095c661eb24d92dc067b246

This commit appears in #14469 which was approved by SamSaffron. It was merged by tgxworld.