Revert "DEV: Minor topic-tracking-state refactor (#17707)" (#17724)

This reverts commit 8d613e0b85.
This commit is contained in:
Jarek Radosz 2022-07-29 01:36:14 +02:00 committed by GitHub
parent 497d9849d3
commit 6849775a2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 31 deletions

View File

@ -1,5 +1,5 @@
import EmberObject, { get } from "@ember/object"; 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 Category from "discourse/models/category";
import { deepEqual, deepMerge } from "discourse-common/lib/object"; import { deepEqual, deepMerge } from "discourse-common/lib/object";
import DiscourseURL from "discourse/lib/url"; import DiscourseURL from "discourse/lib/url";
@ -15,7 +15,7 @@ function isNew(topic) {
((topic.notification_level !== 0 && !topic.notification_level) || ((topic.notification_level !== 0 && !topic.notification_level) ||
topic.notification_level >= NotificationLevels.TRACKING) && topic.notification_level >= NotificationLevels.TRACKING) &&
topic.created_in_new_period && 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) { function hasMutedTags(topicTags, mutedTags, siteSettings) {
if (!mutedTags || !topicTags) { if (!mutedTags || !topicTags) {
return false; return false;
@ -41,12 +45,9 @@ function hasMutedTags(topicTags, mutedTags, siteSettings) {
const TopicTrackingState = EmberObject.extend({ const TopicTrackingState = EmberObject.extend({
messageCount: 0, messageCount: 0,
incomingCount: 0,
newIncoming: null,
init() {
this._super(...arguments);
@on("init")
_setup() {
this.states = new Map(); this.states = new Map();
this.stateChangeCallbacks = {}; this.stateChangeCallbacks = {};
this._trackedTopicLimit = 4000; this._trackedTopicLimit = 4000;
@ -175,7 +176,7 @@ const TopicTrackingState = EmberObject.extend({
* incomingCount > 0). * incomingCount > 0).
* *
* This will do nothing unless resetTracking or trackIncoming has been * 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. * is called by various topic routes, as is resetTracking.
* *
* @method notifyIncoming * @method notifyIncoming
@ -253,7 +254,7 @@ const TopicTrackingState = EmberObject.extend({
} }
// hasIncoming relies on this count // 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 * @method resetTracking
*/ */
resetTracking() { resetTracking() {
this.newIncoming = new Set(); this.newIncoming = [];
this.set("incomingCount", 0); this.set("incomingCount", 0);
}, },
@ -279,10 +280,10 @@ const TopicTrackingState = EmberObject.extend({
* @param {String} filter - Valid values are all, categories, and any topic list * @param {String} filter - Valid values are all, categories, and any topic list
* filters e.g. latest, unread, new. As well as this * filters e.g. latest, unread, new. As well as this
* specific category and tag URLs like tag/test/l/latest, * 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) { trackIncoming(filter) {
this.newIncoming = new Set(); this.newIncoming = [];
let category, tag; let category, tag;
@ -314,11 +315,11 @@ const TopicTrackingState = EmberObject.extend({
* Used to determine whether toshow 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" * e.g. "see 1 new or updated topic"
* *
* @method hasIncoming * @method incomingCount
*/ */
@discourseComputed("incomingCount") @discourseComputed("incomingCount")
hasIncoming(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, last_read_post_number: state.last_read_post_number,
unread_posts: unread, unread_posts: unread,
is_seen: state.is_seen, 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 result = [categoryId];
const categories = Category.list(); const categories = Category.list();
for (const currentCategoryId of result) { for (let i = 0; i < result.length; ++i) {
for (const category of categories) { for (let j = 0; j < categories.length; ++j) {
if (currentCategoryId === category.parent_category_id) { if (result[i] === categories[j].parent_category_id) {
result.push(category.id); result[result.length] = categories[j].id;
} }
} }
} }
@ -492,7 +493,7 @@ const TopicTrackingState = EmberObject.extend({
); );
let filterFn = type === "new" ? isNew : isUnread; let filterFn = type === "new" ? isNew : isUnread;
return [...this.states.values()].filter((topic) => { return Array.from(this.states.values()).filter((topic) => {
if (!filterFn(topic)) { if (!filterFn(topic)) {
return false; return false;
} }
@ -937,11 +938,13 @@ const TopicTrackingState = EmberObject.extend({
}, },
_addIncoming(topicId) { _addIncoming(topicId) {
this.newIncoming.add(topicId); if (!this.newIncoming.includes(topicId)) {
this.newIncoming.push(topicId);
}
}, },
_trackedTopics(opts = {}) { _trackedTopics(opts = {}) {
return [...this.states.values()] return Array.from(this.states.values())
.map((topic) => { .map((topic) => {
let newTopic = isNew(topic); let newTopic = isNew(topic);
let unreadTopic = isUnread(topic); let unreadTopic = isUnread(topic);

View File

@ -566,7 +566,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) {
await publishToMessageBus(`/unread`, unreadTopicPayload); await publishToMessageBus(`/unread`, unreadTopicPayload);
assert.deepEqual( assert.deepEqual(
[...trackingState.newIncoming], trackingState.newIncoming,
[111], [111],
"unread topic is incoming" "unread topic is incoming"
); );
@ -588,12 +588,12 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) {
assert.strictEqual(trackingState.filterTag, "test"); assert.strictEqual(trackingState.filterTag, "test");
assert.strictEqual(trackingState.filter, "latest"); 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.filterCategory.id, 6);
assert.strictEqual(trackingState.filterTag, undefined); assert.strictEqual(trackingState.filterTag, undefined);
assert.strictEqual(trackingState.filter, "latest"); 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.filterCategory.id, 6);
assert.strictEqual(trackingState.filterTag, "test"); assert.strictEqual(trackingState.filterTag, "test");
assert.strictEqual(trackingState.filter, "latest"); assert.strictEqual(trackingState.filter, "latest");
@ -632,7 +632,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) {
unreadCategoriesLatestTopicsPayload unreadCategoriesLatestTopicsPayload
); );
assert.deepEqual( assert.deepEqual(
[...trackingState.newIncoming], trackingState.newIncoming,
[111], [111],
"unread topic is incoming" "unread topic is incoming"
); );
@ -814,7 +814,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) {
await publishToMessageBus("/new", newTopicPayload); await publishToMessageBus("/new", newTopicPayload);
assert.deepEqual( assert.deepEqual(
[...trackingState.newIncoming], trackingState.newIncoming,
[222], [222],
"new topic is incoming" "new topic is incoming"
); );
@ -996,9 +996,9 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) {
sinon.stub(Category, "list").returns([foo, bar, baz]); sinon.stub(Category, "list").returns([foo, bar, baz]);
const trackingState = TopicTrackingState.create(); const trackingState = TopicTrackingState.create();
assert.deepEqual([...trackingState.getSubCategoryIds(1)], [1, 2, 3]); assert.deepEqual(Array.from(trackingState.getSubCategoryIds(1)), [1, 2, 3]);
assert.deepEqual([...trackingState.getSubCategoryIds(2)], [2, 3]); assert.deepEqual(Array.from(trackingState.getSubCategoryIds(2)), [2, 3]);
assert.deepEqual([...trackingState.getSubCategoryIds(3)], [3]); assert.deepEqual(Array.from(trackingState.getSubCategoryIds(3)), [3]);
}); });
test("countNew", function (assert) { test("countNew", function (assert) {