FEATURE: Publish read topic tracking events for private messages. (#14274)

FEATURE: Publish read topic tracking events for private messages. (#14274)

Follow-up to fc1fd1b41689694b3916dc4e10605eb9b8bb89b7

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 de46556..83ca813 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
@@ -2,6 +2,7 @@ import EmberObject from "@ember/object";
 import { ajax } from "discourse/lib/ajax";
 import { on } from "discourse-common/utils/decorators";
 import { popupAjaxError } from "discourse/lib/ajax-error";
+import { deepEqual, deepMerge } from "discourse-common/lib/object";
 import {
   ARCHIVE_FILTER,
   INBOX_FILTER,
@@ -139,6 +140,15 @@ const PrivateMessageTopicTrackingState = EmberObject.extend({
         }
 
         break;
+      case "read":
+        this._modifyState(message.topic_id, message.payload);
+
+        if (
+          this.filter === UNREAD_FILTER &&
+          this._shouldDisplayMessageForInbox(message)
+        ) {
+          this._notifyIncoming(message.topic_id);
+        }
       case "unread":
         this._modifyState(message.topic_id, message.payload);
 
@@ -206,7 +216,14 @@ const PrivateMessageTopicTrackingState = EmberObject.extend({
   },
 
   _modifyState(topicId, data, opts = {}) {
-    this.states.set(topicId, data);
+    const oldState = this.states.get(topicId);
+    let newState = data;
+
+    if (oldState && !deepEqual(oldState, newState)) {
+      newState = deepMerge(oldState, newState);
+    }
+
+    this.states.set(topicId, newState);
 
     if (!opts.skipIncrement) {
       this.incrementProperty("statesModificationCounter");
diff --git a/app/assets/javascripts/discourse/tests/acceptance/user-private-messages-test.js b/app/assets/javascripts/discourse/tests/acceptance/user-private-messages-test.js
index d785f1b..be77c56 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/user-private-messages-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/user-private-messages-test.js
@@ -170,11 +170,26 @@ acceptance(
       });
     });
 
+    const publishReadToMessageBus = function (opts = {}) {
+      publishToMessageBus(
+        `/private-message-topic-tracking-state/user/${opts.userId || 5}`,
+        {
+          topic_id: opts.topicId,
+          message_type: "read",
+          payload: {
+            last_read_post_number: 2,
+            highest_post_number: 2,
+            notification_level: 2,
+          },
+        }
+      );
+    };
+
     const publishUnreadToMessageBus = function (opts = {}) {
       publishToMessageBus(
         `/private-message-topic-tracking-state/user/${opts.userId || 5}`,
         {
-          topic_id: Math.random(),
+          topic_id: opts.topicId,
           message_type: "unread",
           payload: {
             last_read_post_number: 1,
@@ -190,7 +205,7 @@ acceptance(
       publishToMessageBus(
         `/private-message-topic-tracking-state/user/${opts.userId || 5}`,
         {
-          topic_id: Math.random(),
+          topic_id: opts.topicId,
           message_type: "new_topic",
           payload: {
             last_read_post_number: null,
@@ -201,55 +216,55 @@ acceptance(
       );
     };
 
-    const publishArchiveToMessageBus = function (userId) {
+    const publishArchiveToMessageBus = function (opts) {
       publishToMessageBus(
-        `/private-message-topic-tracking-state/user/${userId || 5}`,
+        `/private-message-topic-tracking-state/user/${opts.userId || 5}`,
         {
-          topic_id: Math.random(),
+          topic_id: opts.topicId,
           message_type: "archive",
         }
       );
     };
 
-    const publishGroupArchiveToMessageBus = function (groupIds) {
+    const publishGroupArchiveToMessageBus = function (opts) {
       publishToMessageBus(
-        `/private-message-topic-tracking-state/group/${groupIds[0]}`,
+        `/private-message-topic-tracking-state/group/${opts.groupIds[0]}`,
         {
-          topic_id: Math.random(),
+          topic_id: opts.topicId,
           message_type: "group_archive",
           payload: {
-            group_ids: groupIds,
+            group_ids: opts.groupIds,
           },
         }
       );
     };
 
-    const publishGroupUnreadToMessageBus = function (groupIds) {
+    const publishGroupUnreadToMessageBus = function (opts) {
       publishToMessageBus(
-        `/private-message-topic-tracking-state/group/${groupIds[0]}`,
+        `/private-message-topic-tracking-state/group/${opts.groupIds[0]}`,
         {
-          topic_id: Math.random(),
+          topic_id: opts.topicId,
           message_type: "unread",
           payload: {
             last_read_post_number: 1,
             highest_post_number: 2,
             notification_level: 2,
-            group_ids: groupIds || [],
+            group_ids: opts.groupIds || [],
           },
         }
       );
     };
 
-    const publishGroupNewToMessageBus = function (groupIds) {
+    const publishGroupNewToMessageBus = function (opts) {
       publishToMessageBus(
-        `/private-message-topic-tracking-state/group/${groupIds[0]}`,
+        `/private-message-topic-tracking-state/group/${opts.groupIds[0]}`,
         {
-          topic_id: Math.random(),
+          topic_id: opts.topicId,
           message_type: "new_topic",
           payload: {
             last_read_post_number: null,
             highest_post_number: 1,
-            group_ids: groupIds || [],
+            group_ids: opts.groupIds || [],
           },
         }
       );
@@ -264,7 +279,7 @@ acceptance(
       ]) {
         await visit(url);
 
-        publishArchiveToMessageBus();
+        publishArchiveToMessageBus({ topicId: 1 });
 
         await visit(url); // wait for re-render
 
@@ -280,7 +295,7 @@ acceptance(
       ]) {
         await visit(url);
 
-        publishArchiveToMessageBus();
+        publishArchiveToMessageBus({ topicId: 1 });
 
         await visit(url); // wait for re-render
 
@@ -291,6 +306,16 @@ acceptance(
       }
     });
 
+    test("incoming read message on unread filter", async function (assert) {
+      await visit("/u/charlie/messages/unread");
+
+      publishReadToMessageBus({ topicId: 1 });
+
+      await visit("/u/charlie/messages/unread"); // wait for re-render
+
+      assert.ok(exists(".show-mores"), `displays the topic incoming info`);
+    });
+
     test("incoming group archive message on all and archive filter", async function (assert) {
       for (const url of [
         "/u/charlie/messages",
@@ -300,7 +325,7 @@ acceptance(
       ]) {
         await visit(url);
 
-        publishGroupArchiveToMessageBus([14]);
+        publishGroupArchiveToMessageBus({ groupIds: [14], topicId: 1 });
 
         await visit(url); // wait for re-render
 
@@ -316,7 +341,7 @@ acceptance(
       ]) {
         await visit(url);
 
-        publishGroupArchiveToMessageBus([14]);
+        publishGroupArchiveToMessageBus({ groupIds: [14], topicId: 1 });
 
         await visit(url); // wait for re-render
 
@@ -330,8 +355,8 @@ acceptance(
     test("incoming unread and new messages on all filter", async function (assert) {
       await visit("/u/charlie/messages");
 
-      publishUnreadToMessageBus();
-      publishNewToMessageBus();
+      publishUnreadToMessageBus({ topicId: 1 });
+      publishNewToMessageBus({ topicId: 2 });
 
       await visit("/u/charlie/messages"); // wait for re-render
 
@@ -351,7 +376,7 @@ acceptance(
     test("incoming new messages while viewing new", async function (assert) {
       await visit("/u/charlie/messages/new");
 
-      publishNewToMessageBus();
+      publishNewToMessageBus({ topicId: 1 });
 
       await visit("/u/charlie/messages/new"); // wait for re-render
 
@@ -383,8 +408,8 @@ acceptance(
     test("incoming unread messages while viewing group unread", async function (assert) {
       await visit("/u/charlie/messages/group/awesome_group/unread");
 

[... diff too long, it was truncated ...]

GitHub sha: 412587f70a9559cfeb7fe3dd54ce0f34ac974620

This commit appears in #14274 which was approved by martin. It was merged by tgxworld.