2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2022-08-14 15:33:13 +02:00
|
|
|
const user_topics = zrequire("user_topics");
|
2021-06-27 21:38:26 +02:00
|
|
|
const muted_users = zrequire("muted_users");
|
2021-03-10 15:09:20 +01:00
|
|
|
const people = zrequire("people");
|
2020-07-15 01:29:15 +02:00
|
|
|
const pmc = zrequire("pm_conversations");
|
2017-06-01 07:46:23 +02:00
|
|
|
|
2021-04-29 13:47:56 +02:00
|
|
|
const params = {
|
|
|
|
recent_private_conversations: [
|
|
|
|
{user_ids: [1], max_message_id: 100},
|
|
|
|
{user_ids: [3], max_message_id: 99},
|
|
|
|
{user_ids: [1, 2], max_message_id: 98},
|
|
|
|
{user_ids: [1, 2, 3], max_message_id: 97},
|
|
|
|
{user_ids: [15], max_message_id: 96}, // self
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-04-29 13:12:28 +02:00
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-04-29 13:12:28 +02:00
|
|
|
pmc.clear_for_testing();
|
2022-08-29 14:16:25 +02:00
|
|
|
user_topics.set_user_topics([]);
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.set_muted_users([]);
|
2021-04-29 13:12:28 +02:00
|
|
|
people.initialize_current_user(15);
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-04-29 13:12:28 +02:00
|
|
|
});
|
2021-03-10 15:09:20 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 13:12:28 +02:00
|
|
|
test("partners", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user1_id = 1;
|
|
|
|
const user2_id = 2;
|
|
|
|
const user3_id = 3;
|
2017-06-01 07:46:23 +02:00
|
|
|
|
|
|
|
pmc.set_partner(user1_id);
|
|
|
|
pmc.set_partner(user3_id);
|
|
|
|
|
|
|
|
assert.equal(pmc.is_partner(user1_id), true);
|
|
|
|
assert.equal(pmc.is_partner(user2_id), false);
|
|
|
|
assert.equal(pmc.is_partner(user3_id), true);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-02-09 18:10:39 +01:00
|
|
|
|
2021-04-29 13:12:28 +02:00
|
|
|
test("insert_recent_private_message", () => {
|
|
|
|
pmc.recent.initialize(params);
|
2019-11-21 21:01:15 +01:00
|
|
|
|
2021-04-29 13:47:56 +02:00
|
|
|
// Base data
|
2019-11-21 21:01:15 +01:00
|
|
|
assert.deepEqual(pmc.recent.get(), [
|
2021-04-29 13:47:56 +02:00
|
|
|
{user_ids_string: "1", max_message_id: 100},
|
|
|
|
{user_ids_string: "3", max_message_id: 99},
|
|
|
|
{user_ids_string: "1,2", max_message_id: 98},
|
|
|
|
{user_ids_string: "1,2,3", max_message_id: 97},
|
|
|
|
{user_ids_string: "15", max_message_id: 96},
|
2019-11-21 21:01:15 +01:00
|
|
|
]);
|
|
|
|
|
2021-04-29 13:47:56 +02:00
|
|
|
// Insert new messages (which should rearrange these entries).
|
|
|
|
pmc.recent.insert([1], 1000);
|
|
|
|
pmc.recent.insert([1, 2, 3], 999);
|
2023-06-16 17:37:19 +02:00
|
|
|
// direct message to oneself
|
|
|
|
pmc.recent.insert([], 101);
|
2018-02-09 18:10:39 +01:00
|
|
|
|
2021-04-29 13:47:56 +02:00
|
|
|
// Try to backdate user1's latest message.
|
2020-01-01 15:42:46 +01:00
|
|
|
pmc.recent.insert([1], 555);
|
2018-02-09 18:10:39 +01:00
|
|
|
|
|
|
|
assert.deepEqual(pmc.recent.get(), [
|
2021-04-29 13:47:56 +02:00
|
|
|
{user_ids_string: "1", max_message_id: 1000},
|
|
|
|
{user_ids_string: "1,2,3", max_message_id: 999},
|
|
|
|
{user_ids_string: "15", max_message_id: 101},
|
|
|
|
{user_ids_string: "3", max_message_id: 99},
|
|
|
|
{user_ids_string: "1,2", max_message_id: 98},
|
2018-02-09 18:10:39 +01:00
|
|
|
]);
|
2021-04-29 13:47:56 +02:00
|
|
|
assert.deepEqual(pmc.recent.get_strings(), ["1", "1,2,3", "15", "3", "1,2"]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2021-04-29 14:11:55 +02:00
|
|
|
|
|
|
|
test("muted_users", () => {
|
|
|
|
pmc.recent.initialize(params);
|
|
|
|
|
|
|
|
// Base data
|
|
|
|
assert.deepEqual(pmc.recent.get(), [
|
|
|
|
{user_ids_string: "1", max_message_id: 100},
|
|
|
|
{user_ids_string: "3", max_message_id: 99},
|
|
|
|
{user_ids_string: "1,2", max_message_id: 98},
|
|
|
|
{user_ids_string: "1,2,3", max_message_id: 97},
|
|
|
|
{user_ids_string: "15", max_message_id: 96},
|
|
|
|
]);
|
|
|
|
assert.deepEqual(pmc.recent.get_strings(), ["1", "3", "1,2", "1,2,3", "15"]);
|
|
|
|
|
|
|
|
// Mute some users.
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.add_muted_user(1);
|
|
|
|
muted_users.add_muted_user(2);
|
2021-04-29 14:11:55 +02:00
|
|
|
|
|
|
|
// We should now get back only those messages which are either-
|
2023-06-16 17:37:19 +02:00
|
|
|
// 1:1 direct messages in which the other user hasn't been muted.
|
2021-04-29 14:11:55 +02:00
|
|
|
// Huddles where there's at least one non-muted participant.
|
|
|
|
assert.deepEqual(pmc.recent.get(), [
|
|
|
|
{user_ids_string: "3", max_message_id: 99},
|
|
|
|
{user_ids_string: "1,2,3", max_message_id: 97},
|
|
|
|
{user_ids_string: "15", max_message_id: 96},
|
|
|
|
]);
|
|
|
|
assert.deepEqual(pmc.recent.get_strings(), ["3", "1,2,3", "15"]);
|
|
|
|
});
|