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 0e47f9bc7b1..03059fb3926 100644 --- a/app/assets/javascripts/discourse/app/models/topic-tracking-state.js +++ b/app/assets/javascripts/discourse/app/models/topic-tracking-state.js @@ -1,5 +1,5 @@ import EmberObject, { get } from "@ember/object"; -import discourseComputed, { bind } from "discourse-common/utils/decorators"; +import discourseComputed, { bind, 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"; @@ -15,7 +15,7 @@ function isNew(topic) { ((topic.notification_level !== 0 && !topic.notification_level) || topic.notification_level >= NotificationLevels.TRACKING) && topic.created_in_new_period && - !topic.is_seen + isUnseen(topic) ); } @@ -27,6 +27,10 @@ function isUnread(topic) { ); } +function isUnseen(topic) { + return !topic.is_seen; +} + function hasMutedTags(topicTags, mutedTags, siteSettings) { if (!mutedTags || !topicTags) { return false; @@ -41,12 +45,9 @@ function hasMutedTags(topicTags, mutedTags, siteSettings) { const TopicTrackingState = EmberObject.extend({ messageCount: 0, - incomingCount: 0, - newIncoming: null, - - init() { - this._super(...arguments); + @on("init") + _setup() { this.states = new Map(); this.stateChangeCallbacks = {}; this._trackedTopicLimit = 4000; @@ -175,7 +176,7 @@ const TopicTrackingState = EmberObject.extend({ * incomingCount > 0). * * This will do nothing unless resetTracking or trackIncoming has been - * called; newIncoming will be null instead of a Set. trackIncoming + * called; newIncoming will be null instead of an array. trackIncoming * is called by various topic routes, as is resetTracking. * * @method notifyIncoming @@ -253,7 +254,7 @@ const TopicTrackingState = EmberObject.extend({ } // hasIncoming relies on this count - this.set("incomingCount", this.newIncoming.size); + this.set("incomingCount", this.newIncoming.length); }, /** @@ -264,7 +265,7 @@ const TopicTrackingState = EmberObject.extend({ * @method resetTracking */ resetTracking() { - this.newIncoming = new Set(); + this.newIncoming = []; this.set("incomingCount", 0); }, @@ -279,10 +280,10 @@ const TopicTrackingState = EmberObject.extend({ * @param {String} filter - Valid values are all, categories, and any topic list * filters e.g. latest, unread, new. As well as this * specific category and tag URLs like tag/test/l/latest, - * c/cat/sub-cat/6/l/latest or tags/c/cat/sub-cat/6/test/l/latest. + * c/cat/subcat/6/l/latest or tags/c/cat/subcat/6/test/l/latest. */ trackIncoming(filter) { - this.newIncoming = new Set(); + this.newIncoming = []; let category, tag; @@ -311,14 +312,14 @@ const TopicTrackingState = EmberObject.extend({ }, /** - * Used to determine whether to show the message at the top of the topic list + * Used to determine whether toshow the message at the top of the topic list * e.g. "see 1 new or updated topic" * - * @method hasIncoming + * @method incomingCount */ @discourseComputed("incomingCount") hasIncoming(incomingCount) { - return incomingCount > 0; + return incomingCount && incomingCount > 0; }, /** @@ -394,7 +395,7 @@ const TopicTrackingState = EmberObject.extend({ last_read_post_number: state.last_read_post_number, unread_posts: unread, is_seen: state.is_seen, - unseen: !state.last_read_post_number && !state.is_seen, + unseen: !state.last_read_post_number && isUnseen(state), }); } }); @@ -465,10 +466,10 @@ const TopicTrackingState = EmberObject.extend({ const result = [categoryId]; const categories = Category.list(); - for (const currentCategoryId of result) { - for (const category of categories) { - if (currentCategoryId === category.parent_category_id) { - result.push(category.id); + for (let i = 0; i < result.length; ++i) { + for (let j = 0; j < categories.length; ++j) { + if (result[i] === categories[j].parent_category_id) { + result[result.length] = categories[j].id; } } } @@ -492,7 +493,7 @@ const TopicTrackingState = EmberObject.extend({ ); let filterFn = type === "new" ? isNew : isUnread; - return [...this.states.values()].filter((topic) => { + return Array.from(this.states.values()).filter((topic) => { if (!filterFn(topic)) { return false; } @@ -937,11 +938,13 @@ const TopicTrackingState = EmberObject.extend({ }, _addIncoming(topicId) { - this.newIncoming.add(topicId); + if (!this.newIncoming.includes(topicId)) { + this.newIncoming.push(topicId); + } }, _trackedTopics(opts = {}) { - return [...this.states.values()] + return Array.from(this.states.values()) .map((topic) => { let newTopic = isNew(topic); let unreadTopic = isUnread(topic); diff --git a/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js b/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js index 8bd3cb3cecf..4ae475f5d87 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js @@ -566,7 +566,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { await publishToMessageBus(`/unread`, unreadTopicPayload); assert.deepEqual( - [...trackingState.newIncoming], + trackingState.newIncoming, [111], "unread topic is incoming" ); @@ -588,12 +588,12 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { assert.strictEqual(trackingState.filterTag, "test"); assert.strictEqual(trackingState.filter, "latest"); - trackingState.trackIncoming("c/cat/sub-cat/6/l/latest"); + trackingState.trackIncoming("c/cat/subcat/6/l/latest"); assert.strictEqual(trackingState.filterCategory.id, 6); assert.strictEqual(trackingState.filterTag, undefined); assert.strictEqual(trackingState.filter, "latest"); - trackingState.trackIncoming("tags/c/cat/sub-cat/6/test/l/latest"); + trackingState.trackIncoming("tags/c/cat/subcat/6/test/l/latest"); assert.strictEqual(trackingState.filterCategory.id, 6); assert.strictEqual(trackingState.filterTag, "test"); assert.strictEqual(trackingState.filter, "latest"); @@ -632,7 +632,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { unreadCategoriesLatestTopicsPayload ); assert.deepEqual( - [...trackingState.newIncoming], + trackingState.newIncoming, [111], "unread topic is incoming" ); @@ -814,7 +814,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { await publishToMessageBus("/new", newTopicPayload); assert.deepEqual( - [...trackingState.newIncoming], + trackingState.newIncoming, [222], "new topic is incoming" ); @@ -996,9 +996,9 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { sinon.stub(Category, "list").returns([foo, bar, baz]); const trackingState = TopicTrackingState.create(); - assert.deepEqual([...trackingState.getSubCategoryIds(1)], [1, 2, 3]); - assert.deepEqual([...trackingState.getSubCategoryIds(2)], [2, 3]); - assert.deepEqual([...trackingState.getSubCategoryIds(3)], [3]); + assert.deepEqual(Array.from(trackingState.getSubCategoryIds(1)), [1, 2, 3]); + assert.deepEqual(Array.from(trackingState.getSubCategoryIds(2)), [2, 3]); + assert.deepEqual(Array.from(trackingState.getSubCategoryIds(3)), [3]); }); test("countNew", function (assert) {