Revert "DEV: Topic tracking state improvements (#12958)" (#13209)

Revert “DEV: Topic tracking state improvements (#12958)” (#13209)

This reverts commit 002c6763440c65bd6587dbe5b8e820b3713f406c.

Perf regression, we will redo it.

diff --git a/app/assets/javascripts/discourse/app/components/discovery-topics-list.js b/app/assets/javascripts/discourse/app/components/discovery-topics-list.js
index 9b414b9..8665981 100644
--- a/app/assets/javascripts/discourse/app/components/discovery-topics-list.js
+++ b/app/assets/javascripts/discourse/app/components/discovery-topics-list.js
@@ -21,19 +21,8 @@ const DiscoveryTopicsListComponent = Component.extend(UrlRefresh, LoadMore, {
     }
   },
 
-  @on("didInsertElement")
-  _monitorTrackingState() {
-    this.stateChangeCallbackId = this.topicTrackingState.onStateChange(
-      this._updateTrackingTopics.bind(this)
-    );
-  },
-
-  @on("willDestroyElement")
-  _removeTrackingStateChangeMonitor() {
-    this.topicTrackingState.offStateChange(this.stateChangeCallbackId);
-  },
-
-  _updateTrackingTopics() {
+  @observes("topicTrackingState.states")
+  _updateTopics() {
     this.topicTrackingState.updateTopics(this.model.topics);
   },
 
diff --git a/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js b/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js
index edcae9b..f2a1177 100644
--- a/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js
+++ b/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js
@@ -51,7 +51,9 @@ export default Mixin.create({
 
       promise.then((result) => {
         if (result && result.topic_ids) {
-          this.topicTrackingState.removeTopics(result.topic_ids);
+          const tracker = this.topicTrackingState;
+          result.topic_ids.forEach((t) => tracker.removeTopic(t));
+          tracker.incrementMessageCount();
         }
 
         this.send("closeModal");
diff --git a/app/assets/javascripts/discourse/app/models/topic-tracking-state.js b/app/assets/javascripts/discourse/app/models/topic-tracking-state.js
index 8b424ec..b00e842 100644
--- a/app/assets/javascripts/discourse/app/models/topic-tracking-state.js
+++ b/app/assets/javascripts/discourse/app/models/topic-tracking-state.js
@@ -1,11 +1,11 @@
 import EmberObject, { get } from "@ember/object";
 import discourseComputed, { on } from "discourse-common/utils/decorators";
 import Category from "discourse/models/category";
-import { deepEqual, deepMerge } from "discourse-common/lib/object";
 import DiscourseURL from "discourse/lib/url";
 import { NotificationLevels } from "discourse/lib/notification-levels";
 import PreloadStore from "discourse/lib/preload-store";
 import User from "discourse/models/user";
+import { deepEqual } from "discourse-common/lib/object";
 import { isEmpty } from "@ember/utils";
 
 function isNew(topic) {
@@ -13,7 +13,6 @@ function isNew(topic) {
     topic.last_read_post_number === null &&
     ((topic.notification_level !== 0 && !topic.notification_level) ||
       topic.notification_level >= NotificationLevels.TRACKING) &&
-    topic.created_in_new_period &&
     isUnseen(topic)
   );
 }
@@ -22,8 +21,7 @@ function isUnread(topic) {
   return (
     topic.last_read_post_number !== null &&
     topic.last_read_post_number < topic.highest_post_number &&
-    topic.notification_level >= NotificationLevels.TRACKING &&
-    topic.unread_not_too_old
+    topic.notification_level >= NotificationLevels.TRACKING
   );
 }
 
@@ -50,47 +48,105 @@ const TopicTrackingState = EmberObject.extend({
   _setup() {
     this.unreadSequence = [];
     this.newSequence = [];
-    this.states = new Map();
-    this.messageIncrementCallbacks = {};
-    this.stateChangeCallbacks = {};
-    this._trackedTopicLimit = 4000;
+    this.states = {};
   },
 
-  /**
-   * Subscribe to MessageBus channels which are used for publishing changes
-   * to the tracking state. Each message received will modify state for
-   * a particular topic.
-   *
-   * See app/models/topic_tracking_state.rb for the data payloads published
-   * to each of the channels.
-   *
-   * @method establishChannels
-   */
   establishChannels() {
-    this.messageBus.subscribe("/new", this._processChannelPayload.bind(this));
-    this.messageBus.subscribe(
-      "/latest",
-      this._processChannelPayload.bind(this)
-    );
+    const tracker = this;
+
+    const process = (data) => {
+      if (["muted", "unmuted"].includes(data.message_type)) {
+        tracker.trackMutedOrUnmutedTopic(data);
+        return;
+      }
+
+      tracker.pruneOldMutedAndUnmutedTopics();
+
+      if (tracker.isMutedTopic(data.topic_id)) {
+        return;
+      }
+
+      if (
+        this.siteSettings.mute_all_categories_by_default &&
+        !tracker.isUnmutedTopic(data.topic_id)
+      ) {
+        return;
+      }
+
+      if (data.message_type === "delete") {
+        tracker.removeTopic(data.topic_id);
+        tracker.incrementMessageCount();
+      }
+
+      if (["new_topic", "latest"].includes(data.message_type)) {
+        const muted_category_ids = User.currentProp("muted_category_ids");
+        if (
+          muted_category_ids &&
+          muted_category_ids.includes(data.payload.category_id)
+        ) {
+          return;
+        }
+      }
+
+      if (["new_topic", "latest"].includes(data.message_type)) {
+        const mutedTagIds = User.currentProp("muted_tag_ids");
+        if (
+          hasMutedTags(
+            data.payload.topic_tag_ids,
+            mutedTagIds,
+            this.siteSettings
+          )
+        ) {
+          return;
+        }
+      }
+
+      if (data.message_type === "latest") {
+        tracker.notify(data);
+      }
+
+      if (data.message_type === "dismiss_new") {
+        tracker.dismissNewTopic(data);
+      }
+
+      if (["new_topic", "unread", "read"].includes(data.message_type)) {
+        tracker.notify(data);
+        const old = tracker.states["t" + data.topic_id];
+        if (!deepEqual(old, data.payload)) {
+          tracker.states["t" + data.topic_id] = data.payload;
+          tracker.notifyPropertyChange("states");
+          tracker.incrementMessageCount();
+        }
+      }
+    };
+
+    this.messageBus.subscribe("/new", process);
+    this.messageBus.subscribe("/latest", process);
     if (this.currentUser) {
       this.messageBus.subscribe(
         "/unread/" + this.currentUser.get("id"),
-        this._processChannelPayload.bind(this)
+        process
       );
     }
 
     this.messageBus.subscribe("/delete", (msg) => {
-      this.modifyStateProp(msg, "deleted", true);
-      this.incrementMessageCount();
+      const old = tracker.states["t" + msg.topic_id];
+      if (old) {
+        old.deleted = true;
+      }
+      tracker.incrementMessageCount();
     });
 
     this.messageBus.subscribe("/recover", (msg) => {
-      this.modifyStateProp(msg, "deleted", false);
-      this.incrementMessageCount();
+      const old = tracker.states["t" + msg.topic_id];
+      if (old) {
+        delete old.deleted;
+      }
+      tracker.incrementMessageCount();
     });
 
     this.messageBus.subscribe("/destroy", (msg) => {
-      this.incrementMessageCount();
+      tracker.incrementMessageCount();
       const currentRoute = DiscourseURL.router.currentRoute.parent;
       if (
         currentRoute.name === "topic" &&
@@ -125,6 +181,17 @@ const TopicTrackingState = EmberObject.extend({
     this.currentUser && this.currentUser.set(key, topics);
   },
 
+  dismissNewTopic(data) {
+    data.payload.topic_ids.forEach((k) => {
+      const topic = this.states[`t${k}`];
+      this.states[`t${k}`] = Object.assign({}, topic, {
+        is_seen: true,
+      });
+    });
+    this.notifyPropertyChange("states");
+    this.incrementMessageCount();
+  },
+
   pruneOldMutedAndUnmutedTopics() {
     const now = Date.now();
     let mutedTopics = this.mutedTopics().filter(
@@ -146,50 +213,22 @@ const TopicTrackingState = EmberObject.extend({
     return !!this.unmutedTopics().findBy("topicId", topicId);
   },
 
-  /**
-   * Updates the topic's last_read_post_number to the highestSeen post
-   * number, as long as the topic is being tracked.
-   *

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

GitHub sha: b81b24de

This commit appears in #13209 which was approved by jjaffeux, nbianca, and tgxworld. It was merged by SamSaffron.