DEV: new api to iterate through tracked topics

`topicTrackingState.forEachTracked(topic,isNew,isUnread)` can be used to
iterate through tracking state efficiently.

This is handy for extension looking at subsets of tags and categories.
This commit is contained in:
Sam Saffron 2020-08-04 10:39:43 +10:00
parent 5e46ec8b76
commit eb18f91cf7
No known key found for this signature in database
GPG Key ID: B9606168D2FFD9F5
2 changed files with 98 additions and 19 deletions

View File

@ -441,6 +441,18 @@ const TopicTrackingState = EmberObject.extend({
return this.countCategoryByState("unread", categoryId, tagId); return this.countCategoryByState("unread", categoryId, tagId);
}, },
forEachTracked(fn) {
Object.values(this.states).forEach(topic => {
if (topic.archetype !== "private_message" && !topic.deleted) {
let newTopic = isNew(topic);
let unreadTopic = isUnread(topic);
if (newTopic || unreadTopic) {
fn(topic, newTopic, unreadTopic);
}
}
});
},
countTags(tags) { countTags(tags) {
let counts = {}; let counts = {};
@ -448,26 +460,18 @@ const TopicTrackingState = EmberObject.extend({
counts[tag] = { unreadCount: 0, newCount: 0 }; counts[tag] = { unreadCount: 0, newCount: 0 };
}); });
Object.values(this.states).forEach(topic => { this.forEachTracked((topic, newTopic, unreadTopic) => {
if ( if (topic.tags) {
topic.archetype !== "private_message" && tags.forEach(tag => {
!topic.deleted && if (topic.tags.indexOf(tag) > -1) {
topic.tags if (unreadTopic) {
) { counts[tag].unreadCount++;
let newTopic = isNew(topic);
let unreadTopic = isUnread(topic);
if (newTopic || unreadTopic) {
tags.forEach(tag => {
if (topic.tags.indexOf(tag) > -1) {
if (unreadTopic) {
counts[tag].unreadCount++;
}
if (newTopic) {
counts[tag].newCount++;
}
} }
}); if (newTopic) {
} counts[tag].newCount++;
}
}
});
} }
}); });

View File

@ -64,6 +64,81 @@ QUnit.test("tag counts", function(assert) {
assert.equal(states["unread"].newCount, 0, "unread counts"); assert.equal(states["unread"].newCount, 0, "unread counts");
}); });
QUnit.test("forEachTracked", function(assert) {
const state = TopicTrackingState.create();
state.loadStates([
{
topic_id: 1,
last_read_post_number: null,
tags: ["foo", "new"]
},
{
topic_id: 2,
last_read_post_number: null,
tags: ["new"]
},
{
topic_id: 3,
last_read_post_number: null,
tags: ["random"]
},
{
topic_id: 4,
last_read_post_number: 1,
highest_post_number: 7,
category_id: 7,
tags: ["unread"],
notification_level: NotificationLevels.TRACKING
},
{
topic_id: 5,
last_read_post_number: 1,
highest_post_number: 7,
tags: ["bar", "unread"],
category_id: 7,
notification_level: NotificationLevels.TRACKING
},
{
topic_id: 6,
last_read_post_number: 1,
highest_post_number: 7,
tags: null,
notification_level: NotificationLevels.TRACKING
}
]);
let randomUnread = 0,
randomNew = 0,
sevenUnread = 0,
sevenNew = 0;
state.forEachTracked((topic, isNew, isUnread) => {
if (topic.category_id === 7) {
if (isNew) {
sevenNew += 1;
}
if (isUnread) {
sevenUnread += 1;
}
}
if (topic.tags && topic.tags.indexOf("random") > -1) {
if (isNew) {
randomNew += 1;
}
if (isUnread) {
randomUnread += 1;
}
}
});
assert.equal(randomNew, 1, "random new");
assert.equal(randomUnread, 0, "random unread");
assert.equal(sevenNew, 0, "seven unread");
assert.equal(sevenUnread, 2, "seven unread");
});
QUnit.test("sync", function(assert) { QUnit.test("sync", function(assert) {
const state = TopicTrackingState.create(); const state = TopicTrackingState.create();
state.states["t111"] = { last_read_post_number: null }; state.states["t111"] = { last_read_post_number: null };