2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2024-11-13 07:05:32 +01:00
|
|
|
const {zrequire} = require("./lib/namespace.cjs");
|
|
|
|
const {run_test} = require("./lib/test.cjs");
|
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");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_current_user} = zrequire("state_data");
|
|
|
|
|
|
|
|
const current_user = {};
|
|
|
|
set_current_user(current_user);
|
2017-06-01 07:46:23 +02:00
|
|
|
|
2024-07-02 12:13:17 +02:00
|
|
|
const alice = {
|
|
|
|
user_id: 1,
|
|
|
|
email: "alice@example.com",
|
|
|
|
full_name: "Alice",
|
|
|
|
};
|
|
|
|
|
|
|
|
const isaac = {
|
|
|
|
user_id: 2,
|
|
|
|
email: "isaac@example.com",
|
|
|
|
full_name: "Isaac",
|
|
|
|
};
|
|
|
|
|
|
|
|
const alex = {
|
|
|
|
user_id: 3,
|
|
|
|
email: "alex@example.com",
|
|
|
|
full_name: "Alex",
|
|
|
|
};
|
|
|
|
|
|
|
|
const me = {
|
|
|
|
user_id: 15,
|
|
|
|
email: "me@example.com",
|
|
|
|
full_name: "Me",
|
|
|
|
};
|
|
|
|
|
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(isaac);
|
|
|
|
people.add_active_user(alex);
|
|
|
|
people.add_active_user(me);
|
|
|
|
|
2021-04-29 13:47:56 +02:00
|
|
|
const params = {
|
|
|
|
recent_private_conversations: [
|
2024-07-02 12:13:17 +02:00
|
|
|
{user_ids: [alice.user_id], max_message_id: 100},
|
|
|
|
{user_ids: [alex.user_id], max_message_id: 99},
|
|
|
|
{user_ids: [alice.user_id, isaac.user_id], max_message_id: 98},
|
|
|
|
{user_ids: [alice.user_id, isaac.user_id, alex.user_id], max_message_id: 97},
|
|
|
|
{user_ids: [me.user_id], max_message_id: 96}, // self
|
2021-04-29 13:47:56 +02:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
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([]);
|
2024-07-02 12:13:17 +02:00
|
|
|
people.initialize_current_user(me.user_id);
|
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.
|
2024-06-11 21:45:05 +02:00
|
|
|
// Direct message groups where there's at least one non-muted participant.
|
2021-04-29 14:11:55 +02:00
|
|
|
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"]);
|
|
|
|
});
|
2024-07-02 12:13:17 +02:00
|
|
|
|
2024-10-09 21:21:41 +02:00
|
|
|
test("has_conversation", ({override}) => {
|
|
|
|
override(current_user, "user_id", me.user_id);
|
2024-07-02 12:13:17 +02:00
|
|
|
pmc.recent.initialize(params);
|
|
|
|
|
|
|
|
// Tests if `has_conversation` returns `true` when there are previous
|
|
|
|
// messages in the conversation.
|
|
|
|
assert.ok(pmc.recent.has_conversation("1"));
|
|
|
|
assert.ok(pmc.recent.has_conversation("15"));
|
|
|
|
assert.ok(pmc.recent.has_conversation("1,2"));
|
|
|
|
assert.ok(pmc.recent.has_conversation("1,2,15"));
|
|
|
|
// Check that we canonicalize to sorted order. This isn't
|
|
|
|
// functionality we rely on, but seems worth testing.
|
|
|
|
assert.ok(pmc.recent.has_conversation("2,1,15"));
|
|
|
|
|
|
|
|
// Since the current filter does not match the DM view, there may be
|
|
|
|
// messages in the conversation which are not fetched yet.
|
|
|
|
assert.ok(!pmc.recent.has_conversation("1,3"));
|
|
|
|
assert.ok(!pmc.recent.has_conversation("2"));
|
|
|
|
assert.ok(!pmc.recent.has_conversation("72"));
|
|
|
|
});
|