2015-09-03 16:55:55 -04:00
|
|
|
import TopicTrackingState from "discourse/models/topic-tracking-state";
|
2015-09-20 20:36:20 -04:00
|
|
|
import createStore from "helpers/create-store";
|
2019-11-08 13:30:41 -05:00
|
|
|
import Category from "discourse/models/category";
|
2020-01-17 05:02:57 -05:00
|
|
|
import { NotificationLevels } from "discourse/lib/notification-levels";
|
2020-04-23 00:57:35 -04:00
|
|
|
import User from "discourse/models/user";
|
2014-02-10 23:28:05 -05:00
|
|
|
|
2020-04-23 00:57:35 -04: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-10 23:28:05 -05:00
|
|
|
|
2020-06-01 03:05:04 -04: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");
|
|
|
|
});
|
|
|
|
|
2020-08-03 20:39:43 -04:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("sync", function (assert) {
|
2015-09-03 16:55:55 -04:00
|
|
|
const state = TopicTrackingState.create();
|
2014-02-10 23:28:05 -05:00
|
|
|
state.states["t111"] = { last_read_post_number: null };
|
|
|
|
|
|
|
|
state.updateSeen(111, 7);
|
2015-09-03 16:55:55 -04:00
|
|
|
const list = {
|
|
|
|
topics: [
|
2018-06-15 11:03:24 -04:00
|
|
|
{
|
2014-02-10 23:28:05 -05:00
|
|
|
highest_post_number: null,
|
|
|
|
id: 111,
|
|
|
|
unread: 10,
|
|
|
|
new_posts: 10,
|
2018-06-15 11:03:24 -04:00
|
|
|
},
|
|
|
|
],
|
2015-09-03 16:55:55 -04:00
|
|
|
};
|
2014-02-10 23:28:05 -05:00
|
|
|
|
|
|
|
state.sync(list, "new");
|
2015-09-03 16:55:55 -04:00
|
|
|
assert.equal(
|
|
|
|
list.topics.length,
|
|
|
|
0,
|
|
|
|
"expect new topic to be removed as it was seen"
|
|
|
|
);
|
2014-02-10 23:28:05 -05:00
|
|
|
});
|
2015-09-20 20:36:20 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("subscribe to category", function (assert) {
|
2015-09-20 20:36:20 -04: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-08 13:30:41 -05:00
|
|
|
sandbox.stub(Category, "list").returns(categoryList);
|
2015-09-20 20:36:20 -04:00
|
|
|
|
|
|
|
const state = TopicTrackingState.create();
|
|
|
|
|
2019-12-16 16:33:38 -05:00
|
|
|
state.trackIncoming("c/darth/1/l/latest");
|
2015-09-20 20:36:20 -04: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-16 16:33:38 -05:00
|
|
|
state.trackIncoming("c/darth/luke/2/l/latest");
|
2015-09-20 20:36:20 -04: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-14 13:57:58 -04:00
|
|
|
});
|
2020-01-17 05:02:57 -05: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 02:47:45 -04:00
|
|
|
const foo = store.createRecord("category", {
|
|
|
|
id: 1,
|
|
|
|
slug: "foo",
|
|
|
|
});
|
2020-01-17 05:02:57 -05: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,
|
|
|
|
});
|
2020-07-15 22:24:51 -04:00
|
|
|
const qux = store.createRecord("category", {
|
|
|
|
id: 4,
|
|
|
|
slug: "qux",
|
|
|
|
});
|
|
|
|
sandbox.stub(Category, "list").returns([foo, bar, baz, qux]);
|
2020-01-17 05:02:57 -05:00
|
|
|
|
2020-07-15 22:24:51 -04:00
|
|
|
let currentUser = User.create({
|
|
|
|
username: "chuck",
|
|
|
|
muted_category_ids: [4],
|
|
|
|
});
|
|
|
|
|
|
|
|
const state = TopicTrackingState.create({ currentUser });
|
2020-01-17 05:02:57 -05:00
|
|
|
|
|
|
|
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 02:47:45 -04:00
|
|
|
assert.equal(state.countNew(1, "missing-tag"), 0);
|
2020-01-17 05:02:57 -05: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 02:47:45 -04:00
|
|
|
category_id: 3,
|
|
|
|
tags: ["amazing"],
|
2020-01-17 05:02:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(state.countNew(1), 2);
|
|
|
|
assert.equal(state.countNew(2), 2);
|
|
|
|
assert.equal(state.countNew(3), 1);
|
2020-06-11 02:47:45 -04:00
|
|
|
assert.equal(state.countNew(3, "amazing"), 1);
|
|
|
|
assert.equal(state.countNew(3, "missing"), 0);
|
2020-01-17 05:02:57 -05: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-07-15 22:24:51 -04:00
|
|
|
|
|
|
|
state.states["t115"] = {
|
|
|
|
last_read_post_number: null,
|
|
|
|
id: 115,
|
|
|
|
category_id: 4,
|
|
|
|
};
|
|
|
|
assert.equal(state.countNew(4), 0);
|
2020-01-17 05:02:57 -05:00
|
|
|
});
|
2020-04-23 00:57:35 -04: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);
|
|
|
|
});
|