2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
const {zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-16 23:38:59 +01:00
|
|
|
const blueslip = require("../zjsunit/zblueslip");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2021-06-28 20:39:04 +02:00
|
|
|
const muted_topics = zrequire("muted_topics");
|
2021-06-27 21:38:26 +02:00
|
|
|
const muted_users = zrequire("muted_users");
|
2021-02-10 04:53:22 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
2013-08-20 22:05:56 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const design = {
|
2018-12-13 22:26:10 +01:00
|
|
|
stream_id: 100,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "design",
|
2018-12-13 22:26:10 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const devel = {
|
2018-12-13 22:26:10 +01:00
|
|
|
stream_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "devel",
|
2018-12-13 22:26:10 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const office = {
|
2018-12-13 22:26:10 +01:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "office",
|
2018-12-13 22:26:10 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const social = {
|
2018-12-13 22:26:10 +01:00
|
|
|
stream_id: 103,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "social",
|
2018-12-13 22:26:10 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const unknown = {
|
2018-12-13 22:26:10 +01:00
|
|
|
stream_id: 999,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "whatever",
|
2018-12-13 22:26:10 +01:00
|
|
|
};
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(design);
|
|
|
|
stream_data.add_sub(devel);
|
|
|
|
stream_data.add_sub(office);
|
|
|
|
stream_data.add_sub(social);
|
2018-12-13 22:26:10 +01:00
|
|
|
|
2021-03-11 16:50:30 +01:00
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-06-28 20:39:04 +02:00
|
|
|
muted_topics.set_muted_topics([]);
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.set_muted_users([]);
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-03-11 16:50:30 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test("edge_cases", () => {
|
|
|
|
// private messages
|
2021-06-28 20:39:04 +02:00
|
|
|
assert.ok(!muted_topics.is_topic_muted(undefined, undefined));
|
2021-03-27 12:42:16 +01:00
|
|
|
|
|
|
|
// invalid user
|
2021-06-27 21:38:26 +02:00
|
|
|
assert.ok(!muted_users.is_user_muted(undefined));
|
2021-03-11 16:50:30 +01:00
|
|
|
});
|
|
|
|
|
2021-03-27 12:42:16 +01:00
|
|
|
test("add_and_remove_mutes", () => {
|
2021-06-28 20:39:04 +02:00
|
|
|
assert.ok(!muted_topics.is_topic_muted(devel.stream_id, "java"));
|
|
|
|
muted_topics.add_muted_topic(devel.stream_id, "java");
|
|
|
|
assert.ok(muted_topics.is_topic_muted(devel.stream_id, "java"));
|
2013-08-27 19:36:31 +02:00
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
// test idempotency
|
2021-06-28 20:39:04 +02:00
|
|
|
muted_topics.add_muted_topic(devel.stream_id, "java");
|
|
|
|
assert.ok(muted_topics.is_topic_muted(devel.stream_id, "java"));
|
2013-09-09 20:33:25 +02:00
|
|
|
|
2021-06-28 20:39:04 +02:00
|
|
|
muted_topics.remove_muted_topic(devel.stream_id, "java");
|
|
|
|
assert.ok(!muted_topics.is_topic_muted(devel.stream_id, "java"));
|
2013-09-09 20:33:25 +02:00
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
// test idempotency
|
2021-06-28 20:39:04 +02:00
|
|
|
muted_topics.remove_muted_topic(devel.stream_id, "java");
|
|
|
|
assert.ok(!muted_topics.is_topic_muted(devel.stream_id, "java"));
|
2013-09-09 20:33:25 +02:00
|
|
|
|
|
|
|
// test unknown stream is harmless too
|
2021-06-28 20:39:04 +02:00
|
|
|
muted_topics.remove_muted_topic(unknown.stream_id, "java");
|
|
|
|
assert.ok(!muted_topics.is_topic_muted(unknown.stream_id, "java"));
|
2021-03-27 12:42:16 +01:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
assert.ok(!muted_users.is_user_muted(1));
|
|
|
|
muted_users.add_muted_user(1);
|
|
|
|
assert.ok(muted_users.is_user_muted(1));
|
2021-03-27 12:42:16 +01:00
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
// test idempotency
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.add_muted_user(1);
|
|
|
|
assert.ok(muted_users.is_user_muted(1));
|
2021-03-27 12:42:16 +01:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.remove_muted_user(1);
|
|
|
|
assert.ok(!muted_users.is_user_muted(1));
|
2021-03-27 12:42:16 +01:00
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
// test idempotency
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.remove_muted_user(1);
|
|
|
|
assert.ok(!muted_users.is_user_muted(1));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-09-09 20:33:25 +02:00
|
|
|
|
2021-05-05 05:27:06 +02:00
|
|
|
test("get_unmuted_users", () => {
|
|
|
|
const hamlet = {
|
|
|
|
user_id: 1,
|
|
|
|
full_name: "King Hamlet",
|
|
|
|
};
|
|
|
|
const cordelia = {
|
|
|
|
user_id: 2,
|
|
|
|
full_name: "Cordelia, Lear's Daughter",
|
|
|
|
};
|
|
|
|
const othello = {
|
|
|
|
user_id: 3,
|
|
|
|
full_name: "Othello, Moor of Venice",
|
|
|
|
};
|
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.add_muted_user(hamlet.user_id);
|
|
|
|
muted_users.add_muted_user(cordelia.user_id);
|
2021-05-05 05:27:06 +02:00
|
|
|
|
|
|
|
assert.deepEqual(
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.filter_muted_user_ids([hamlet.user_id, cordelia.user_id, othello.user_id]),
|
2021-05-05 05:27:06 +02:00
|
|
|
[othello.user_id],
|
|
|
|
);
|
2021-06-27 21:38:26 +02:00
|
|
|
assert.deepEqual(muted_users.filter_muted_users([hamlet, cordelia, othello]), [othello]);
|
2021-05-05 05:27:06 +02:00
|
|
|
});
|
|
|
|
|
2021-03-27 12:42:16 +01:00
|
|
|
test("get_mutes", () => {
|
2021-06-28 20:39:04 +02:00
|
|
|
assert.deepEqual(muted_topics.get_muted_topics(), []);
|
|
|
|
muted_topics.add_muted_topic(office.stream_id, "gossip", 1577836800);
|
|
|
|
muted_topics.add_muted_topic(devel.stream_id, "java", 1577836700);
|
|
|
|
const all_muted_topics = muted_topics
|
|
|
|
.get_muted_topics()
|
|
|
|
.sort((a, b) => a.date_muted - b.date_muted);
|
2021-03-11 16:50:30 +01:00
|
|
|
|
2021-06-30 19:26:14 +02:00
|
|
|
assert.deepEqual(all_muted_topics, [
|
2020-02-05 13:14:24 +01:00
|
|
|
{
|
2021-03-11 16:50:30 +01:00
|
|
|
date_muted: 1577836700000,
|
|
|
|
date_muted_str: "Dec\u00A031,\u00A02019",
|
2020-02-05 13:14:24 +01:00
|
|
|
stream: devel.name,
|
|
|
|
stream_id: devel.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "java",
|
2020-02-05 13:14:24 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
date_muted: 1577836800000,
|
2021-01-02 06:19:48 +01:00
|
|
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
2020-02-05 13:14:24 +01:00
|
|
|
stream: office.name,
|
|
|
|
stream_id: office.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "gossip",
|
2020-07-15 00:34:28 +02:00
|
|
|
},
|
|
|
|
]);
|
2021-03-27 12:42:16 +01:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
assert.deepEqual(muted_users.get_muted_users(), []);
|
|
|
|
muted_users.add_muted_user(6, 1577836800);
|
|
|
|
muted_users.add_muted_user(4, 1577836800);
|
2021-06-28 20:39:04 +02:00
|
|
|
const all_muted_users = muted_users
|
|
|
|
.get_muted_users()
|
|
|
|
.sort((a, b) => a.date_muted - b.date_muted);
|
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
assert.deepEqual(all_muted_users, [
|
2021-03-27 12:42:16 +01:00
|
|
|
{
|
|
|
|
date_muted: 1577836800000,
|
|
|
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
|
|
|
id: 6,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
date_muted: 1577836800000,
|
|
|
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
|
|
|
id: 4,
|
|
|
|
},
|
|
|
|
]);
|
2021-03-11 16:50:30 +01:00
|
|
|
});
|
2013-09-10 20:34:41 +02:00
|
|
|
|
2021-03-11 16:50:30 +01:00
|
|
|
test("unknown streams", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "Unknown stream in set_muted_topics: BOGUS STREAM");
|
2018-12-13 22:26:10 +01:00
|
|
|
|
2018-12-15 19:53:20 +01:00
|
|
|
page_params.muted_topics = [
|
2020-07-15 01:29:15 +02:00
|
|
|
["social", "breakfast", 1577836800],
|
|
|
|
["design", "typography", 1577836800],
|
|
|
|
["BOGUS STREAM", "whatever", 1577836800],
|
2018-12-15 19:53:20 +01:00
|
|
|
];
|
2021-03-27 12:42:16 +01:00
|
|
|
page_params.muted_users = [
|
|
|
|
{id: 3, timestamp: 1577836800},
|
|
|
|
{id: 2, timestamp: 1577836800},
|
|
|
|
];
|
2021-06-28 20:39:04 +02:00
|
|
|
muted_topics.initialize();
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.initialize();
|
2018-12-13 22:26:10 +01:00
|
|
|
|
2021-06-28 20:39:04 +02:00
|
|
|
assert.deepEqual(muted_topics.get_muted_topics().sort(), [
|
2020-02-05 13:14:24 +01:00
|
|
|
{
|
|
|
|
date_muted: 1577836800000,
|
2021-01-02 06:19:48 +01:00
|
|
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
2020-02-05 13:14:24 +01:00
|
|
|
stream: social.name,
|
|
|
|
stream_id: social.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "breakfast",
|
2020-02-05 13:14:24 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
date_muted: 1577836800000,
|
2021-01-02 06:19:48 +01:00
|
|
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
2020-02-05 13:14:24 +01:00
|
|
|
stream: design.name,
|
|
|
|
stream_id: design.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "typography",
|
2020-07-15 00:34:28 +02:00
|
|
|
},
|
|
|
|
]);
|
2021-03-27 12:42:16 +01:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
assert.deepEqual(muted_users.get_muted_users().sort(), [
|
2021-03-27 12:42:16 +01:00
|
|
|
{
|
|
|
|
date_muted: 1577836800000,
|
|
|
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
|
|
|
id: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
date_muted: 1577836800000,
|
|
|
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
|
|
|
id: 2,
|
|
|
|
},
|
|
|
|
]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-09-10 21:33:59 +02:00
|
|
|
|
2021-03-11 16:50:30 +01:00
|
|
|
test("case_insensitivity", () => {
|
2021-06-28 20:39:04 +02:00
|
|
|
muted_topics.set_muted_topics([]);
|
|
|
|
assert.ok(!muted_topics.is_topic_muted(social.stream_id, "breakfast"));
|
|
|
|
muted_topics.set_muted_topics([["SOCial", "breakfast"]]);
|
|
|
|
assert.ok(muted_topics.is_topic_muted(social.stream_id, "breakfast"));
|
|
|
|
assert.ok(muted_topics.is_topic_muted(social.stream_id, "breakFAST"));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|