2015-09-04 04:55:55 +08:00
|
|
|
import TopicTrackingState from "discourse/models/topic-tracking-state";
|
2015-09-21 08:36:20 +08:00
|
|
|
import createStore from "helpers/create-store";
|
2019-11-09 02:30:41 +08:00
|
|
|
import Category from "discourse/models/category";
|
2020-01-17 18:02:57 +08:00
|
|
|
import { NotificationLevels } from "discourse/lib/notification-levels";
|
2020-04-23 12:57:35 +08:00
|
|
|
import User from "discourse/models/user";
|
2014-02-11 12:28:05 +08:00
|
|
|
|
2020-04-23 12:57:35 +08:00
|
|
|
QUnit.module("model:topic-tracking-state", {
|
|
|
|
beforeEach() {
|
|
|
|
this.clock = sinon.useFakeTimers(new Date(2012, 11, 31, 12, 0).getTime());
|
|
|
|
},
|
|
|
|
|
|
|
|
afterEach() {
|
|
|
|
this.clock.restore();
|
|
|
|
}
|
|
|
|
});
|
2014-02-11 12:28:05 +08:00
|
|
|
|
2020-06-01 15:05:04 +08:00
|
|
|
QUnit.test("tag counts", 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,
|
|
|
|
tags: ["unread"],
|
|
|
|
notification_level: NotificationLevels.TRACKING
|
|
|
|
},
|
|
|
|
{
|
|
|
|
topic_id: 5,
|
|
|
|
last_read_post_number: 1,
|
|
|
|
highest_post_number: 7,
|
|
|
|
tags: ["bar", "unread"],
|
|
|
|
notification_level: NotificationLevels.TRACKING
|
|
|
|
},
|
|
|
|
{
|
|
|
|
topic_id: 6,
|
|
|
|
last_read_post_number: 1,
|
|
|
|
highest_post_number: 7,
|
|
|
|
tags: null,
|
|
|
|
notification_level: NotificationLevels.TRACKING
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
const states = state.countTags(["new", "unread"]);
|
|
|
|
|
|
|
|
assert.equal(states["new"].newCount, 2, "new counts");
|
|
|
|
assert.equal(states["new"].unreadCount, 0, "new counts");
|
|
|
|
assert.equal(states["unread"].unreadCount, 2, "unread counts");
|
|
|
|
assert.equal(states["unread"].newCount, 0, "unread counts");
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("sync", function(assert) {
|
2015-09-04 04:55:55 +08:00
|
|
|
const state = TopicTrackingState.create();
|
2014-02-11 12:28:05 +08:00
|
|
|
state.states["t111"] = { last_read_post_number: null };
|
|
|
|
|
|
|
|
state.updateSeen(111, 7);
|
2015-09-04 04:55:55 +08:00
|
|
|
const list = {
|
|
|
|
topics: [
|
2018-06-15 23:03:24 +08:00
|
|
|
{
|
2014-02-11 12:28:05 +08:00
|
|
|
highest_post_number: null,
|
|
|
|
id: 111,
|
|
|
|
unread: 10,
|
|
|
|
new_posts: 10
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
]
|
2015-09-04 04:55:55 +08:00
|
|
|
};
|
2014-02-11 12:28:05 +08:00
|
|
|
|
|
|
|
state.sync(list, "new");
|
2015-09-04 04:55:55 +08:00
|
|
|
assert.equal(
|
|
|
|
list.topics.length,
|
|
|
|
0,
|
|
|
|
"expect new topic to be removed as it was seen"
|
|
|
|
);
|
2014-02-11 12:28:05 +08:00
|
|
|
});
|
2015-09-21 08:36:20 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("subscribe to category", function(assert) {
|
2015-09-21 08:36:20 +08:00
|
|
|
const store = createStore();
|
|
|
|
const darth = store.createRecord("category", { id: 1, slug: "darth" }),
|
|
|
|
luke = store.createRecord("category", {
|
|
|
|
id: 2,
|
|
|
|
slug: "luke",
|
|
|
|
parentCategory: darth
|
|
|
|
}),
|
|
|
|
categoryList = [darth, luke];
|
|
|
|
|
2019-11-09 02:30:41 +08:00
|
|
|
sandbox.stub(Category, "list").returns(categoryList);
|
2015-09-21 08:36:20 +08:00
|
|
|
|
|
|
|
const state = TopicTrackingState.create();
|
|
|
|
|
2019-12-17 05:33:38 +08:00
|
|
|
state.trackIncoming("c/darth/1/l/latest");
|
2015-09-21 08:36:20 +08:00
|
|
|
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 1,
|
|
|
|
payload: { category_id: 2, topic_id: 1 }
|
|
|
|
});
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 2,
|
|
|
|
payload: { category_id: 3, topic_id: 2 }
|
|
|
|
});
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 3,
|
|
|
|
payload: { category_id: 1, topic_id: 3 }
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
state.get("incomingCount"),
|
|
|
|
2,
|
|
|
|
"expect to properly track incoming for category"
|
|
|
|
);
|
|
|
|
|
|
|
|
state.resetTracking();
|
2019-12-17 05:33:38 +08:00
|
|
|
state.trackIncoming("c/darth/luke/2/l/latest");
|
2015-09-21 08:36:20 +08:00
|
|
|
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 1,
|
|
|
|
payload: { category_id: 2, topic_id: 1 }
|
|
|
|
});
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 2,
|
|
|
|
payload: { category_id: 3, topic_id: 2 }
|
|
|
|
});
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 3,
|
|
|
|
payload: { category_id: 1, topic_id: 3 }
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
state.get("incomingCount"),
|
|
|
|
1,
|
|
|
|
"expect to properly track incoming for subcategory"
|
|
|
|
);
|
2017-06-15 01:57:58 +08:00
|
|
|
});
|
2020-01-17 18:02:57 +08:00
|
|
|
|
|
|
|
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();
|
2020-06-11 14:47:45 +08:00
|
|
|
const foo = store.createRecord("category", {
|
|
|
|
id: 1,
|
|
|
|
slug: "foo"
|
|
|
|
});
|
2020-01-17 18:02:57 +08:00
|
|
|
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);
|
2020-06-11 14:47:45 +08:00
|
|
|
assert.equal(state.countNew(1, "missing-tag"), 0);
|
2020-01-17 18:02:57 +08:00
|
|
|
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,
|
2020-06-11 14:47:45 +08:00
|
|
|
category_id: 3,
|
|
|
|
tags: ["amazing"]
|
2020-01-17 18:02:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(state.countNew(1), 2);
|
|
|
|
assert.equal(state.countNew(2), 2);
|
|
|
|
assert.equal(state.countNew(3), 1);
|
2020-06-11 14:47:45 +08:00
|
|
|
assert.equal(state.countNew(3, "amazing"), 1);
|
|
|
|
assert.equal(state.countNew(3, "missing"), 0);
|
2020-01-17 18:02:57 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2020-04-23 12:57:35 +08:00
|
|
|
|
|
|
|
QUnit.test("mute topic", function(assert) {
|
|
|
|
let currentUser = User.create({
|
|
|
|
username: "chuck",
|
|
|
|
muted_category_ids: []
|
|
|
|
});
|
|
|
|
|
|
|
|
const state = TopicTrackingState.create({ currentUser });
|
|
|
|
|
|
|
|
state.trackMutedTopic(1);
|
|
|
|
assert.equal(currentUser.muted_topics[0].topicId, 1);
|
|
|
|
|
|
|
|
state.pruneOldMutedTopics();
|
|
|
|
assert.equal(state.isMutedTopic(1), true);
|
|
|
|
|
|
|
|
this.clock.tick(60000);
|
|
|
|
state.pruneOldMutedTopics();
|
|
|
|
assert.equal(state.isMutedTopic(1), false);
|
|
|
|
});
|