mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Include sub-sub-categories in new/unread counts (#8710)
The count used to be less than the real one because the topics in sub-sub-categories and deeper were not taken into account.
This commit is contained in:
parent
2583aedd42
commit
7d8c33a094
@ -76,15 +76,6 @@ const TopicTrackingState = EmberObject.extend({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fill parent_category_id we need it for counting new/unread
|
|
||||||
if (data.payload && data.payload.category_id) {
|
|
||||||
var category = Category.findById(data.payload.category_id);
|
|
||||||
|
|
||||||
if (category && category.parent_category_id) {
|
|
||||||
data.payload.parent_category_id = category.parent_category_id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.message_type === "latest") {
|
if (data.message_type === "latest") {
|
||||||
tracker.notify(data);
|
tracker.notify(data);
|
||||||
}
|
}
|
||||||
@ -364,30 +355,43 @@ const TopicTrackingState = EmberObject.extend({
|
|||||||
this.incrementProperty("messageCount");
|
this.incrementProperty("messageCount");
|
||||||
},
|
},
|
||||||
|
|
||||||
countNew(category_id) {
|
getSubCategoryIds(categoryId) {
|
||||||
|
const result = [categoryId];
|
||||||
|
const categories = Category.list();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Set(result);
|
||||||
|
},
|
||||||
|
|
||||||
|
countNew(categoryId) {
|
||||||
|
const subcategoryIds = this.getSubCategoryIds(categoryId);
|
||||||
return _.chain(this.states)
|
return _.chain(this.states)
|
||||||
.filter(isNew)
|
.filter(isNew)
|
||||||
.filter(
|
.filter(
|
||||||
topic =>
|
topic =>
|
||||||
topic.archetype !== "private_message" &&
|
topic.archetype !== "private_message" &&
|
||||||
!topic.deleted &&
|
!topic.deleted &&
|
||||||
(topic.category_id === category_id ||
|
(!categoryId || subcategoryIds.has(topic.category_id))
|
||||||
topic.parent_category_id === category_id ||
|
|
||||||
!category_id)
|
|
||||||
)
|
)
|
||||||
.value().length;
|
.value().length;
|
||||||
},
|
},
|
||||||
|
|
||||||
countUnread(category_id) {
|
countUnread(categoryId) {
|
||||||
|
const subcategoryIds = this.getSubCategoryIds(categoryId);
|
||||||
return _.chain(this.states)
|
return _.chain(this.states)
|
||||||
.filter(isUnread)
|
.filter(isUnread)
|
||||||
.filter(
|
.filter(
|
||||||
topic =>
|
topic =>
|
||||||
topic.archetype !== "private_message" &&
|
topic.archetype !== "private_message" &&
|
||||||
!topic.deleted &&
|
!topic.deleted &&
|
||||||
(topic.category_id === category_id ||
|
(!categoryId || subcategoryIds.has(topic.category_id))
|
||||||
topic.parent_category_id === category_id ||
|
|
||||||
!category_id)
|
|
||||||
)
|
)
|
||||||
.value().length;
|
.value().length;
|
||||||
},
|
},
|
||||||
@ -434,10 +438,6 @@ const TopicTrackingState = EmberObject.extend({
|
|||||||
// I am taking some shortcuts here to avoid 500 gets for a large list
|
// I am taking some shortcuts here to avoid 500 gets for a large list
|
||||||
if (data) {
|
if (data) {
|
||||||
data.forEach(topic => {
|
data.forEach(topic => {
|
||||||
let category = Category.findById(topic.category_id);
|
|
||||||
if (category && category.parent_category_id) {
|
|
||||||
topic.parent_category_id = category.parent_category_id;
|
|
||||||
}
|
|
||||||
states["t" + topic.topic_id] = topic;
|
states["t" + topic.topic_id] = topic;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import TopicTrackingState from "discourse/models/topic-tracking-state";
|
import TopicTrackingState from "discourse/models/topic-tracking-state";
|
||||||
import createStore from "helpers/create-store";
|
import createStore from "helpers/create-store";
|
||||||
import Category from "discourse/models/category";
|
import Category from "discourse/models/category";
|
||||||
|
import { NotificationLevels } from "discourse/lib/notification-levels";
|
||||||
|
|
||||||
QUnit.module("model:topic-tracking-state");
|
QUnit.module("model:topic-tracking-state");
|
||||||
|
|
||||||
@ -91,3 +92,79 @@ QUnit.test("subscribe to category", function(assert) {
|
|||||||
"expect to properly track incoming for subcategory"
|
"expect to properly track incoming for subcategory"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test("getSubCategoryIds", assert => {
|
||||||
|
const store = createStore();
|
||||||
|
const foo = store.createRecord("category", { id: 1, slug: "foo" });
|
||||||
|
const bar = store.createRecord("category", {
|
||||||
|
id: 2,
|
||||||
|
slug: "bar",
|
||||||
|
parent_category_id: foo.id
|
||||||
|
});
|
||||||
|
const baz = store.createRecord("category", {
|
||||||
|
id: 3,
|
||||||
|
slug: "baz",
|
||||||
|
parent_category_id: bar.id
|
||||||
|
});
|
||||||
|
sandbox.stub(Category, "list").returns([foo, bar, baz]);
|
||||||
|
|
||||||
|
const state = TopicTrackingState.create();
|
||||||
|
assert.deepEqual(Array.from(state.getSubCategoryIds(1)), [1, 2, 3]);
|
||||||
|
assert.deepEqual(Array.from(state.getSubCategoryIds(2)), [2, 3]);
|
||||||
|
assert.deepEqual(Array.from(state.getSubCategoryIds(3)), [3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test("countNew", assert => {
|
||||||
|
const store = createStore();
|
||||||
|
const foo = store.createRecord("category", { id: 1, slug: "foo" });
|
||||||
|
const bar = store.createRecord("category", {
|
||||||
|
id: 2,
|
||||||
|
slug: "bar",
|
||||||
|
parent_category_id: foo.id
|
||||||
|
});
|
||||||
|
const baz = store.createRecord("category", {
|
||||||
|
id: 3,
|
||||||
|
slug: "baz",
|
||||||
|
parent_category_id: bar.id
|
||||||
|
});
|
||||||
|
sandbox.stub(Category, "list").returns([foo, bar, baz]);
|
||||||
|
|
||||||
|
const state = TopicTrackingState.create();
|
||||||
|
|
||||||
|
assert.equal(state.countNew(1), 0);
|
||||||
|
assert.equal(state.countNew(2), 0);
|
||||||
|
assert.equal(state.countNew(3), 0);
|
||||||
|
|
||||||
|
state.states["t112"] = {
|
||||||
|
last_read_post_number: null,
|
||||||
|
id: 112,
|
||||||
|
notification_level: NotificationLevels.TRACKING,
|
||||||
|
category_id: 2
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.equal(state.countNew(1), 1);
|
||||||
|
assert.equal(state.countNew(2), 1);
|
||||||
|
assert.equal(state.countNew(3), 0);
|
||||||
|
|
||||||
|
state.states["t113"] = {
|
||||||
|
last_read_post_number: null,
|
||||||
|
id: 113,
|
||||||
|
notification_level: NotificationLevels.TRACKING,
|
||||||
|
category_id: 3
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.equal(state.countNew(1), 2);
|
||||||
|
assert.equal(state.countNew(2), 2);
|
||||||
|
assert.equal(state.countNew(3), 1);
|
||||||
|
|
||||||
|
state.states["t111"] = {
|
||||||
|
last_read_post_number: null,
|
||||||
|
id: 111,
|
||||||
|
notification_level: NotificationLevels.TRACKING,
|
||||||
|
category_id: 1
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.equal(state.countNew(1), 3);
|
||||||
|
assert.equal(state.countNew(2), 2);
|
||||||
|
assert.equal(state.countNew(3), 1);
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user