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
|
|
|
|
2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
|
|
|
|
2024-11-13 07:05:32 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace.cjs");
|
|
|
|
const {run_test} = require("./lib/test.cjs");
|
|
|
|
const {page_params} = require("./lib/zpage_params.cjs");
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2024-06-12 12:04:08 +02:00
|
|
|
mock_esm("../src/settings_data", {
|
|
|
|
user_can_access_all_other_users: () => true,
|
|
|
|
});
|
2023-02-22 23:04:10 +01:00
|
|
|
const timerender = mock_esm("../src/timerender");
|
2021-02-28 01:14:36 +01:00
|
|
|
|
2021-03-19 14:38:22 +01:00
|
|
|
const compose_fade_helper = zrequire("compose_fade_helper");
|
2021-06-27 21:38:26 +02:00
|
|
|
const muted_users = zrequire("muted_users");
|
2023-08-23 02:18:53 +02:00
|
|
|
const narrow_state = zrequire("narrow_state");
|
2021-03-19 14:38:22 +01:00
|
|
|
const peer_data = zrequire("peer_data");
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-02-10 04:53:22 +01:00
|
|
|
const presence = zrequire("presence");
|
2021-03-19 14:38:22 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
2021-02-10 04:53:22 +01:00
|
|
|
const user_status = zrequire("user_status");
|
|
|
|
const buddy_data = zrequire("buddy_data");
|
2024-09-25 00:55:31 +02:00
|
|
|
const {Filter} = zrequire("filter");
|
|
|
|
const message_lists = zrequire("message_lists");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_current_user, set_realm} = zrequire("state_data");
|
2024-10-09 08:44:21 +02:00
|
|
|
const {initialize_user_settings} = zrequire("user_settings");
|
|
|
|
|
2024-10-09 22:44:13 +02:00
|
|
|
const realm = {};
|
|
|
|
set_realm(realm);
|
|
|
|
const current_user = {};
|
|
|
|
set_current_user(current_user);
|
2024-10-09 08:44:21 +02:00
|
|
|
const user_settings = {};
|
|
|
|
initialize_user_settings({user_settings});
|
2018-04-20 18:22:28 +02:00
|
|
|
|
|
|
|
// The buddy_data module is mostly tested indirectly through
|
2024-11-13 07:05:32 +01:00
|
|
|
// activity.test.cjs, but we should feel free to add direct tests
|
2018-04-20 18:22:28 +02:00
|
|
|
// here.
|
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
const selma = {
|
|
|
|
user_id: 1000,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Human Selma",
|
|
|
|
email: "selma@example.com",
|
2018-12-18 19:25:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const me = {
|
|
|
|
user_id: 1001,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Human Myself",
|
|
|
|
email: "self@example.com",
|
2018-12-18 19:25:14 +01:00
|
|
|
};
|
|
|
|
|
2020-05-27 14:46:44 +02:00
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@zulip.com",
|
2020-05-27 14:46:44 +02:00
|
|
|
user_id: 1002,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice Smith",
|
2020-05-27 14:46:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const fred = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "fred@zulip.com",
|
2020-05-27 14:46:44 +02:00
|
|
|
user_id: 1003,
|
|
|
|
full_name: "Fred Flintstone",
|
|
|
|
};
|
|
|
|
|
|
|
|
const jill = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "jill@zulip.com",
|
2020-05-27 14:46:44 +02:00
|
|
|
user_id: 1004,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Jill Hill",
|
2020-05-27 14:46:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const mark = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "mark@zulip.com",
|
2020-05-27 14:46:44 +02:00
|
|
|
user_id: 1005,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Marky Mark",
|
2020-05-27 14:46:44 +02:00
|
|
|
};
|
|
|
|
|
2019-02-28 16:09:03 +01:00
|
|
|
const old_user = {
|
|
|
|
user_id: 9999,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Old User",
|
|
|
|
email: "old_user@example.com",
|
2019-02-28 16:09:03 +01:00
|
|
|
};
|
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
const bot = {
|
|
|
|
user_id: 55555,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Red Herring Bot",
|
|
|
|
email: "bot@example.com",
|
2018-12-18 19:25:14 +01:00
|
|
|
is_bot: true,
|
2019-08-04 14:57:32 +02:00
|
|
|
bot_owner_id: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const bot_with_owner = {
|
|
|
|
user_id: 55556,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Blue Herring Bot",
|
|
|
|
email: "bot_with_owner@example.com",
|
2019-08-04 14:57:32 +02:00
|
|
|
is_bot: true,
|
|
|
|
bot_owner_id: 1001,
|
2020-07-15 01:29:15 +02:00
|
|
|
bot_owner_full_name: "Human Myself",
|
2018-12-18 19:25:14 +01:00
|
|
|
};
|
|
|
|
|
2021-03-14 10:18:07 +01:00
|
|
|
function add_canned_users() {
|
|
|
|
people.add_active_user(alice);
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(bot);
|
|
|
|
people.add_active_user(bot_with_owner);
|
2020-05-27 14:46:44 +02:00
|
|
|
people.add_active_user(fred);
|
|
|
|
people.add_active_user(jill);
|
|
|
|
people.add_active_user(mark);
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(old_user);
|
2021-03-14 10:18:07 +01:00
|
|
|
people.add_active_user(selma);
|
|
|
|
}
|
2018-12-18 18:50:58 +01:00
|
|
|
|
2021-03-14 10:18:07 +01:00
|
|
|
function test(label, f) {
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2024-10-09 21:10:22 +02:00
|
|
|
helpers.override(user_settings, "presence_enabled", true);
|
2021-03-19 14:38:22 +01:00
|
|
|
compose_fade_helper.clear_focused_recipient();
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
peer_data.clear_for_testing();
|
2021-03-14 10:18:07 +01:00
|
|
|
user_status.initialize({user_status: {}});
|
|
|
|
presence.presence_info.clear();
|
|
|
|
people.init();
|
|
|
|
people.add_active_user(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.set_muted_users([]);
|
2024-09-25 00:55:31 +02:00
|
|
|
message_lists.set_current(undefined);
|
2022-07-10 01:06:33 +02:00
|
|
|
|
|
|
|
f(helpers);
|
|
|
|
|
|
|
|
presence.clear_internal_data();
|
2021-03-14 10:18:07 +01:00
|
|
|
});
|
2018-09-08 14:41:41 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 16:08:27 +01:00
|
|
|
function set_presence(user_id, status) {
|
|
|
|
presence.presence_info.set(user_id, {
|
|
|
|
status,
|
|
|
|
last_active: 9999,
|
|
|
|
});
|
|
|
|
}
|
2018-04-20 18:22:28 +02:00
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
test("user_circle, level", ({override}) => {
|
2021-03-14 10:18:07 +01:00
|
|
|
add_canned_users();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_presence(selma.user_id, "active");
|
|
|
|
assert.equal(buddy_data.get_user_circle_class(selma.user_id), "user_circle_green");
|
2022-09-21 15:49:36 +02:00
|
|
|
assert.equal(buddy_data.level(selma.user_id), 1);
|
2021-05-16 17:10:07 +02:00
|
|
|
|
2022-09-21 15:49:36 +02:00
|
|
|
set_presence(selma.user_id, "idle");
|
|
|
|
assert.equal(buddy_data.get_user_circle_class(selma.user_id), "user_circle_idle");
|
|
|
|
assert.equal(buddy_data.level(selma.user_id), 2);
|
|
|
|
|
|
|
|
set_presence(selma.user_id, "offline");
|
|
|
|
assert.equal(buddy_data.get_user_circle_class(selma.user_id), "user_circle_empty");
|
|
|
|
assert.equal(buddy_data.level(selma.user_id), 3);
|
2019-02-17 02:10:42 +01:00
|
|
|
|
2021-05-16 17:10:07 +02:00
|
|
|
set_presence(me.user_id, "active");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(buddy_data.get_user_circle_class(me.user_id), "user_circle_green");
|
2021-05-16 17:10:07 +02:00
|
|
|
assert.equal(buddy_data.level(me.user_id), 0);
|
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "presence_enabled", false);
|
2022-09-21 15:49:36 +02:00
|
|
|
assert.equal(buddy_data.get_user_circle_class(me.user_id), "user_circle_empty");
|
|
|
|
assert.equal(buddy_data.level(me.user_id), 0);
|
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "presence_enabled", true);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(buddy_data.get_user_circle_class(me.user_id), "user_circle_green");
|
2022-09-21 15:49:36 +02:00
|
|
|
assert.equal(buddy_data.level(me.user_id), 0);
|
2021-05-16 17:10:07 +02:00
|
|
|
|
|
|
|
set_presence(fred.user_id, "idle");
|
2022-01-27 22:22:59 +01:00
|
|
|
assert.equal(buddy_data.get_user_circle_class(fred.user_id), "user_circle_idle");
|
2021-05-16 17:10:07 +02:00
|
|
|
assert.equal(buddy_data.level(fred.user_id), 2);
|
|
|
|
|
|
|
|
set_presence(fred.user_id, undefined);
|
2022-09-21 15:49:36 +02:00
|
|
|
assert.equal(buddy_data.get_user_circle_class(fred.user_id), "user_circle_empty");
|
|
|
|
assert.equal(buddy_data.level(fred.user_id), 3);
|
2019-02-17 02:10:42 +01:00
|
|
|
});
|
|
|
|
|
2024-10-09 21:21:41 +02:00
|
|
|
test("title_data", ({override}) => {
|
2024-08-30 00:55:09 +02:00
|
|
|
page_params.presence_history_limit_days_for_web_app = 365;
|
2021-03-14 10:18:07 +01:00
|
|
|
add_canned_users();
|
|
|
|
|
2019-08-04 14:57:32 +02:00
|
|
|
// Groups
|
2019-11-02 00:06:25 +01:00
|
|
|
let is_group = true;
|
|
|
|
const user_ids_string = "9999,1000";
|
|
|
|
let expected_group_data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
first_line: "Human Selma, Old User",
|
|
|
|
second_line: "",
|
|
|
|
third_line: "",
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(buddy_data.get_title_data(user_ids_string, is_group), expected_group_data);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
is_group = "";
|
2019-08-04 14:57:32 +02:00
|
|
|
|
|
|
|
// Bots with owners.
|
|
|
|
expected_group_data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
first_line: "Blue Herring Bot",
|
|
|
|
second_line: "translated: Owner: Human Myself",
|
|
|
|
third_line: "",
|
2024-11-13 18:42:04 +01:00
|
|
|
is_deactivated: false,
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
buddy_data.get_title_data(bot_with_owner.user_id, is_group),
|
|
|
|
expected_group_data,
|
|
|
|
);
|
2019-08-04 14:57:32 +02:00
|
|
|
|
|
|
|
// Bots without owners.
|
|
|
|
expected_group_data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
first_line: "Red Herring Bot",
|
|
|
|
second_line: "",
|
|
|
|
third_line: "",
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(buddy_data.get_title_data(bot.user_id, is_group), expected_group_data);
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
// Individual users.
|
2019-01-25 16:40:18 +01:00
|
|
|
user_status.set_status_text({
|
|
|
|
user_id: me.user_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
status_text: "out to lunch",
|
2019-01-25 16:40:18 +01:00
|
|
|
});
|
2019-08-04 14:57:32 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let expected_data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
first_line: "Human Myself",
|
|
|
|
second_line: "out to lunch",
|
|
|
|
third_line: "translated: Active now",
|
2021-04-02 13:05:26 +02:00
|
|
|
show_you: true,
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
2024-10-09 21:21:41 +02:00
|
|
|
override(current_user, "user_id", me.user_id);
|
2019-08-04 14:57:32 +02:00
|
|
|
assert.deepEqual(buddy_data.get_title_data(me.user_id, is_group), expected_data);
|
|
|
|
|
|
|
|
expected_data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
first_line: "Old User",
|
2024-08-30 00:55:09 +02:00
|
|
|
second_line: "translated: Not active in the last year",
|
2020-07-15 01:29:15 +02:00
|
|
|
third_line: "",
|
2021-04-02 13:05:26 +02:00
|
|
|
show_you: false,
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(buddy_data.get_title_data(old_user.user_id, is_group), expected_data);
|
2024-11-13 18:42:04 +01:00
|
|
|
|
|
|
|
// Deactivated users.
|
|
|
|
people.deactivate(selma);
|
|
|
|
expected_data = {
|
|
|
|
first_line: "Human Selma",
|
|
|
|
second_line: "translated: This user has been deactivated.",
|
|
|
|
third_line: "",
|
|
|
|
show_you: false,
|
|
|
|
is_deactivated: true,
|
|
|
|
};
|
|
|
|
assert.deepEqual(buddy_data.get_title_data(selma.user_id, is_group), expected_data);
|
|
|
|
|
|
|
|
// Deactivated bots.
|
|
|
|
people.deactivate(bot_with_owner);
|
|
|
|
expected_group_data = {
|
|
|
|
first_line: "Blue Herring Bot",
|
|
|
|
second_line: "translated: Owner: Human Myself",
|
|
|
|
third_line: "translated: This bot has been deactivated.",
|
|
|
|
is_deactivated: true,
|
|
|
|
};
|
|
|
|
assert.deepEqual(
|
|
|
|
buddy_data.get_title_data(bot_with_owner.user_id, is_group),
|
|
|
|
expected_group_data,
|
|
|
|
);
|
2019-01-25 16:40:18 +01:00
|
|
|
});
|
|
|
|
|
2024-08-09 20:38:24 +02:00
|
|
|
test("filters deactivated users", () => {
|
2021-03-14 10:18:07 +01:00
|
|
|
add_canned_users();
|
2021-03-08 16:08:27 +01:00
|
|
|
|
|
|
|
set_presence(selma.user_id, "active");
|
|
|
|
set_presence(me.user_id, "active");
|
|
|
|
|
2024-08-09 20:38:24 +02:00
|
|
|
let user_ids = buddy_data.get_filtered_and_sorted_user_ids("selm");
|
2018-12-18 19:25:14 +01:00
|
|
|
|
2018-12-18 19:29:08 +01:00
|
|
|
assert.deepEqual(user_ids, [selma.user_id]);
|
2024-08-09 20:38:24 +02:00
|
|
|
assert.ok(buddy_data.matches_filter("selm", selma.user_id));
|
|
|
|
|
|
|
|
// Deactivated users are excluded.
|
|
|
|
people.deactivate(selma);
|
|
|
|
|
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids("selm");
|
|
|
|
assert.deepEqual(user_ids, []);
|
|
|
|
|
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids();
|
|
|
|
assert.equal(user_ids.includes(selma.user_id), false);
|
|
|
|
assert.ok(!buddy_data.matches_filter("selm", selma.user_id));
|
2018-06-02 01:42:05 +02:00
|
|
|
});
|
2018-04-20 18:22:28 +02:00
|
|
|
|
2021-04-23 17:30:00 +02:00
|
|
|
test("muted users excluded from search", () => {
|
2021-05-16 17:24:11 +02:00
|
|
|
people.add_active_user(selma);
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.add_muted_user(selma.user_id);
|
2021-04-23 17:30:00 +02:00
|
|
|
|
|
|
|
let user_ids = buddy_data.get_filtered_and_sorted_user_ids();
|
|
|
|
assert.equal(user_ids.includes(selma.user_id), false);
|
2023-08-24 01:33:39 +02:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids("selm");
|
2021-04-23 17:30:00 +02:00
|
|
|
assert.deepEqual(user_ids, []);
|
2023-08-24 01:33:39 +02:00
|
|
|
assert.ok(!buddy_data.matches_filter("selm", selma.user_id));
|
2021-04-23 17:30:00 +02:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.remove_muted_user(selma.user_id);
|
2023-08-24 01:33:39 +02:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids("selm");
|
2021-05-16 17:24:11 +02:00
|
|
|
assert.deepEqual(user_ids, [selma.user_id]);
|
2023-08-24 01:33:39 +02:00
|
|
|
assert.ok(buddy_data.matches_filter("selm", selma.user_id));
|
2021-04-23 17:30:00 +02:00
|
|
|
});
|
|
|
|
|
2022-07-09 23:25:05 +02:00
|
|
|
test("bulk_data_hacks", ({override_rewire}) => {
|
2021-03-14 10:18:07 +01:00
|
|
|
// sanity check
|
|
|
|
assert.equal(mark.user_id, 1005);
|
|
|
|
|
|
|
|
for (const i of _.range(mark.user_id + 1, 2000)) {
|
|
|
|
const person = {
|
|
|
|
user_id: i,
|
|
|
|
full_name: `Human ${i}`,
|
|
|
|
email: `person${i}@example.com`,
|
|
|
|
};
|
|
|
|
people.add_active_user(person);
|
|
|
|
}
|
|
|
|
add_canned_users();
|
2021-03-08 16:08:27 +01:00
|
|
|
|
|
|
|
// Make 400 of the users active
|
|
|
|
set_presence(selma.user_id, "active");
|
|
|
|
set_presence(me.user_id, "active");
|
|
|
|
|
|
|
|
for (const user_id of _.range(1000, 1400)) {
|
|
|
|
set_presence(user_id, "active");
|
|
|
|
}
|
|
|
|
|
|
|
|
// And then 300 not active
|
|
|
|
for (const user_id of _.range(1400, 1700)) {
|
|
|
|
set_presence(user_id, "offline");
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids;
|
2018-04-20 18:22:28 +02:00
|
|
|
|
2018-04-23 23:13:52 +02:00
|
|
|
// Even though we have 1000 users, we only get the 400 active
|
2018-04-20 18:22:28 +02:00
|
|
|
// users. This is a consequence of buddy_data.maybe_shrink_list.
|
2018-04-23 23:13:52 +02:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids();
|
2018-04-20 18:22:28 +02:00
|
|
|
assert.equal(user_ids.length, 400);
|
2018-04-23 23:13:52 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids("");
|
2018-04-23 23:13:52 +02:00
|
|
|
assert.equal(user_ids.length, 400);
|
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
// We don't match on "so", because it's not at the start of a
|
2018-04-23 23:13:52 +02:00
|
|
|
// word in the name/email.
|
2020-07-15 01:29:15 +02:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids("so");
|
2018-04-23 23:13:52 +02:00
|
|
|
assert.equal(user_ids.length, 0);
|
|
|
|
|
|
|
|
// We match on "h" for the first name, and the result limit
|
2023-08-24 01:33:39 +02:00
|
|
|
// is relaxed for searches.
|
2020-07-15 01:29:15 +02:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids("h");
|
2023-08-24 01:33:39 +02:00
|
|
|
assert.equal(user_ids.length, 997);
|
2018-04-23 23:13:52 +02:00
|
|
|
|
|
|
|
// We match on "p" for the email.
|
2020-07-15 01:29:15 +02:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids("p");
|
2020-05-27 14:46:44 +02:00
|
|
|
assert.equal(user_ids.length, 994);
|
2018-04-23 23:13:52 +02:00
|
|
|
|
|
|
|
// Make our shrink limit higher, and go back to an empty search.
|
|
|
|
// We won't get all 1000 users, just the present ones.
|
2022-07-09 23:25:05 +02:00
|
|
|
override_rewire(buddy_data, "max_size_before_shrinking", 50000);
|
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids("");
|
2018-04-23 23:13:52 +02:00
|
|
|
assert.equal(user_ids.length, 700);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-12-20 15:55:20 +01:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
test("always show me", () => {
|
2021-06-28 13:20:16 +02:00
|
|
|
assert.deepEqual(buddy_data.get_filtered_and_sorted_user_ids(""), [me.user_id]);
|
|
|
|
|
|
|
|
// try to make us show twice
|
2022-07-10 01:06:33 +02:00
|
|
|
presence.presence_info.set(me.user_id, {status: "active"});
|
2021-06-28 13:20:16 +02:00
|
|
|
assert.deepEqual(buddy_data.get_filtered_and_sorted_user_ids(""), [me.user_id]);
|
|
|
|
});
|
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
test("always show pm users", ({override_rewire}) => {
|
|
|
|
people.add_active_user(selma);
|
|
|
|
override_rewire(narrow_state, "pm_ids_set", () => new Set([selma.user_id]));
|
|
|
|
assert.deepEqual(buddy_data.get_filtered_and_sorted_user_ids(""), [me.user_id, selma.user_id]);
|
|
|
|
});
|
|
|
|
|
2024-09-18 21:45:52 +02:00
|
|
|
test("show offline channel subscribers for small channels", ({override_rewire}) => {
|
|
|
|
// Add a bunch of users to hit max_size_before_shrinking, which will hide some
|
|
|
|
// offline users.
|
|
|
|
for (const i of _.range(2000, 2700)) {
|
|
|
|
const person = {
|
|
|
|
user_id: i,
|
|
|
|
full_name: `Human ${i}`,
|
|
|
|
email: `person${i}@example.com`,
|
|
|
|
};
|
|
|
|
people.add_active_user(person);
|
|
|
|
}
|
|
|
|
for (const user_id of _.range(2000, 2700)) {
|
|
|
|
set_presence(user_id, "offline");
|
|
|
|
}
|
|
|
|
|
|
|
|
people.add_active_user(alice);
|
|
|
|
set_presence(alice.user_id, "active");
|
|
|
|
people.add_active_user(fred);
|
|
|
|
set_presence(fred.user_id, "offline");
|
|
|
|
people.add_active_user(selma);
|
|
|
|
set_presence(selma.user_id, "offline");
|
|
|
|
people.add_active_user(jill);
|
|
|
|
set_presence(jill.user_id, "offline");
|
|
|
|
|
|
|
|
const stream_id = 1001;
|
|
|
|
const sub = {name: "Rome", subscribed: true, stream_id};
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
peer_data.set_subscribers(stream_id, [
|
|
|
|
selma.user_id,
|
|
|
|
alice.user_id,
|
|
|
|
fred.user_id,
|
|
|
|
jill.user_id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
override_rewire(narrow_state, "stream_id", () => stream_id);
|
|
|
|
assert.deepEqual(buddy_data.get_filtered_and_sorted_user_ids(""), [
|
|
|
|
me.user_id,
|
|
|
|
alice.user_id,
|
|
|
|
fred.user_id,
|
|
|
|
selma.user_id,
|
|
|
|
jill.user_id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Make the max channel size lower, so that we hide the offline users
|
|
|
|
override_rewire(buddy_data, "max_channel_size_to_show_all_subscribers", 2);
|
|
|
|
assert.deepEqual(buddy_data.get_filtered_and_sorted_user_ids(""), [me.user_id, alice.user_id]);
|
|
|
|
});
|
|
|
|
|
2024-09-25 00:55:31 +02:00
|
|
|
test("get_conversation_participants", ({override_rewire}) => {
|
|
|
|
people.add_active_user(selma);
|
|
|
|
|
|
|
|
const rome_sub = {name: "Rome", subscribed: true, stream_id: 1001};
|
|
|
|
stream_data.add_sub(rome_sub);
|
|
|
|
peer_data.set_subscribers(rome_sub.stream_id, [selma.user_id]);
|
|
|
|
|
|
|
|
const filter = new Filter([
|
|
|
|
{operator: "channel", operand: rome_sub.channel_id},
|
|
|
|
{operator: "topic", operand: "Foo"},
|
|
|
|
]);
|
|
|
|
message_lists.set_current({
|
|
|
|
data: {
|
|
|
|
filter,
|
|
|
|
},
|
|
|
|
all_messages() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
sender_id: selma.user_id,
|
|
|
|
id: rome_sub.stream_id,
|
|
|
|
content: "example content",
|
|
|
|
topic: "Foo",
|
|
|
|
type: "stream",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
|
|
|
});
|
|
|
|
override_rewire(narrow_state, "stream_id", () => rome_sub.stream_id);
|
|
|
|
override_rewire(narrow_state, "topic", () => "Foo");
|
|
|
|
|
|
|
|
assert.deepEqual(buddy_data.get_conversation_participants(), new Set([selma.user_id]));
|
|
|
|
});
|
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
test("level", ({override}) => {
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "server_presence_offline_threshold_seconds", 200);
|
2023-02-21 11:52:30 +01:00
|
|
|
|
2021-03-14 10:18:07 +01:00
|
|
|
add_canned_users();
|
2018-12-20 15:55:20 +01:00
|
|
|
assert.equal(buddy_data.level(me.user_id), 0);
|
|
|
|
assert.equal(buddy_data.level(selma.user_id), 3);
|
|
|
|
|
|
|
|
const server_time = 9999;
|
|
|
|
const info = {
|
|
|
|
website: {
|
|
|
|
status: "active",
|
|
|
|
timestamp: server_time,
|
|
|
|
},
|
|
|
|
};
|
2020-02-07 17:19:03 +01:00
|
|
|
presence.update_info_from_event(me.user_id, info, server_time);
|
|
|
|
presence.update_info_from_event(selma.user_id, info, server_time);
|
2018-12-20 15:55:20 +01:00
|
|
|
|
|
|
|
assert.equal(buddy_data.level(me.user_id), 0);
|
|
|
|
assert.equal(buddy_data.level(selma.user_id), 1);
|
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "presence_enabled", false);
|
2022-09-21 15:49:36 +02:00
|
|
|
set_presence(selma.user_id, "offline");
|
2018-12-20 15:55:20 +01:00
|
|
|
|
2018-12-19 21:16:03 +01:00
|
|
|
// Selma gets demoted to level 3, but "me"
|
|
|
|
// stays in level 0.
|
2018-12-20 15:55:20 +01:00
|
|
|
assert.equal(buddy_data.level(me.user_id), 0);
|
2018-12-19 21:16:03 +01:00
|
|
|
assert.equal(buddy_data.level(selma.user_id), 3);
|
2018-12-20 15:55:20 +01:00
|
|
|
});
|
2019-02-28 16:09:03 +01:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
test("compare_function", () => {
|
|
|
|
const first_user_shown_higher = -1;
|
|
|
|
const second_user_shown_higher = 1;
|
|
|
|
|
|
|
|
const stream_id = 1001;
|
|
|
|
const sub = {name: "Rome", subscribed: true, stream_id};
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(fred);
|
|
|
|
|
|
|
|
// Alice is higher because of alphabetical sorting.
|
|
|
|
peer_data.set_subscribers(stream_id, []);
|
|
|
|
assert.equal(
|
|
|
|
second_user_shown_higher,
|
2024-10-02 07:09:29 +02:00
|
|
|
buddy_data.compare_function(fred.user_id, alice.user_id, sub, new Set(), new Set()),
|
2023-08-23 02:18:53 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Fred is higher because they're in the narrow and Alice isn't.
|
|
|
|
peer_data.set_subscribers(stream_id, [fred.user_id]);
|
|
|
|
assert.equal(
|
|
|
|
first_user_shown_higher,
|
2024-10-02 07:09:29 +02:00
|
|
|
buddy_data.compare_function(fred.user_id, alice.user_id, sub, new Set(), new Set()),
|
2023-08-23 02:18:53 +02:00
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
second_user_shown_higher,
|
2024-10-02 07:09:29 +02:00
|
|
|
buddy_data.compare_function(alice.user_id, fred.user_id, sub, new Set(), new Set()),
|
2023-08-23 02:18:53 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Fred is higher because they're in the DM conversation and Alice isn't.
|
|
|
|
assert.equal(
|
|
|
|
first_user_shown_higher,
|
|
|
|
buddy_data.compare_function(
|
|
|
|
fred.user_id,
|
|
|
|
alice.user_id,
|
|
|
|
undefined,
|
|
|
|
new Set([fred.user_id]),
|
2024-10-02 07:09:29 +02:00
|
|
|
new Set(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Fred is higher because they're in the conversation and Alice isn't.
|
|
|
|
assert.equal(
|
|
|
|
first_user_shown_higher,
|
|
|
|
buddy_data.compare_function(
|
|
|
|
fred.user_id,
|
|
|
|
alice.user_id,
|
|
|
|
undefined,
|
|
|
|
new Set(),
|
|
|
|
new Set([fred.user_id]),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
second_user_shown_higher,
|
|
|
|
buddy_data.compare_function(
|
|
|
|
alice.user_id,
|
|
|
|
fred.user_id,
|
|
|
|
undefined,
|
|
|
|
new Set(),
|
|
|
|
new Set([fred.user_id]),
|
2023-08-23 02:18:53 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Alice is higher because of alphabetical sorting.
|
|
|
|
assert.equal(
|
|
|
|
second_user_shown_higher,
|
2024-10-02 07:09:29 +02:00
|
|
|
buddy_data.compare_function(fred.user_id, alice.user_id, undefined, new Set(), new Set()),
|
2023-08-23 02:18:53 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// The user is part of a DM conversation, though that's not explicitly in the DM list.
|
|
|
|
assert.equal(
|
|
|
|
first_user_shown_higher,
|
2024-10-02 07:09:29 +02:00
|
|
|
buddy_data.compare_function(
|
|
|
|
me.user_id,
|
|
|
|
alice.user_id,
|
|
|
|
undefined,
|
|
|
|
new Set([fred.user_id]),
|
|
|
|
new Set(),
|
|
|
|
),
|
2023-08-23 02:18:53 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
test("user_last_seen_time_status", ({override}) => {
|
2024-08-30 00:55:09 +02:00
|
|
|
page_params.presence_history_limit_days_for_web_app = 365;
|
2021-03-08 16:08:27 +01:00
|
|
|
set_presence(selma.user_id, "active");
|
|
|
|
set_presence(me.user_id, "active");
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(buddy_data.user_last_seen_time_status(selma.user_id), "translated: Active now");
|
2019-02-28 16:09:03 +01:00
|
|
|
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_is_zephyr_mirror_realm", true);
|
2021-06-03 20:43:19 +02:00
|
|
|
assert.equal(
|
|
|
|
buddy_data.user_last_seen_time_status(old_user.user_id),
|
2023-04-07 18:24:15 +02:00
|
|
|
"translated: Activity unknown",
|
2021-06-03 20:43:19 +02:00
|
|
|
);
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_is_zephyr_mirror_realm", false);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
buddy_data.user_last_seen_time_status(old_user.user_id),
|
2024-08-30 00:55:09 +02:00
|
|
|
"translated: Not active in the last year",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2019-02-28 16:09:03 +01:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
presence.presence_info.set(old_user.user_id, {last_active: 1526137743});
|
2019-02-28 16:09:03 +01:00
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
override(timerender, "last_seen_status_from_date", (date) => {
|
2021-02-05 21:20:14 +01:00
|
|
|
assert.deepEqual(date, new Date(1526137743000));
|
2023-04-07 18:24:15 +02:00
|
|
|
return "translated: Active May 12";
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2019-02-28 16:09:03 +01:00
|
|
|
|
2021-06-03 20:43:19 +02:00
|
|
|
assert.equal(
|
|
|
|
buddy_data.user_last_seen_time_status(old_user.user_id),
|
2023-04-07 18:24:15 +02:00
|
|
|
"translated: Active May 12",
|
2021-06-03 20:43:19 +02:00
|
|
|
);
|
2021-06-03 20:45:59 +02:00
|
|
|
|
|
|
|
set_presence(selma.user_id, "idle");
|
|
|
|
assert.equal(buddy_data.user_last_seen_time_status(selma.user_id), "translated: Idle");
|
2019-02-28 16:09:03 +01:00
|
|
|
});
|
2020-02-04 15:38:37 +01:00
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
test("get_items_for_users", ({override}) => {
|
2021-05-16 17:17:44 +02:00
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(fred);
|
2022-09-21 15:49:36 +02:00
|
|
|
set_presence(alice.user_id, "offline");
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "emojiset", "google");
|
|
|
|
override(user_settings, "user_list_style", 2);
|
2023-09-19 08:13:40 +02:00
|
|
|
|
2021-06-27 19:04:17 +02:00
|
|
|
const status_emoji_info = {
|
2022-07-10 01:06:33 +02:00
|
|
|
emoji_alt_code: false,
|
2021-06-27 19:04:17 +02:00
|
|
|
emoji_name: "car",
|
|
|
|
emoji_code: "1f697",
|
|
|
|
reaction_type: "unicode_emoji",
|
|
|
|
};
|
2021-05-16 17:17:44 +02:00
|
|
|
|
2023-09-19 08:13:40 +02:00
|
|
|
const status_emoji_info_event = {
|
|
|
|
id: 1,
|
|
|
|
type: "user_status",
|
|
|
|
status_text: "",
|
|
|
|
...status_emoji_info,
|
|
|
|
};
|
|
|
|
|
2021-05-16 17:17:44 +02:00
|
|
|
const user_ids = [me.user_id, alice.user_id, fred.user_id];
|
2022-07-10 01:06:33 +02:00
|
|
|
for (const user_id of user_ids) {
|
2023-09-19 08:13:40 +02:00
|
|
|
user_status.set_status_emoji({user_id, ...status_emoji_info_event});
|
2022-07-10 01:06:33 +02:00
|
|
|
}
|
|
|
|
|
2022-08-12 22:41:06 +02:00
|
|
|
const user_list_style = {
|
|
|
|
COMPACT: false,
|
|
|
|
WITH_STATUS: true,
|
|
|
|
WITH_AVATAR: false,
|
|
|
|
};
|
|
|
|
|
2021-05-16 17:17:44 +02:00
|
|
|
assert.deepEqual(buddy_data.get_items_for_users(user_ids), [
|
|
|
|
{
|
2023-04-11 21:04:33 +02:00
|
|
|
href: "#narrow/dm/1001-Human-Myself",
|
2021-05-16 17:17:44 +02:00
|
|
|
is_current_user: true,
|
|
|
|
name: "Human Myself",
|
|
|
|
num_unread: 0,
|
2024-10-24 08:10:56 +02:00
|
|
|
profile_picture: "/avatar/1001",
|
2021-06-27 19:04:17 +02:00
|
|
|
status_emoji_info,
|
2022-08-12 22:41:06 +02:00
|
|
|
status_text: undefined,
|
2024-11-05 22:45:51 +01:00
|
|
|
has_status_text: false,
|
2021-05-16 17:17:44 +02:00
|
|
|
user_circle_class: "user_circle_green",
|
|
|
|
user_id: 1001,
|
2022-08-12 22:41:06 +02:00
|
|
|
user_list_style,
|
2023-09-13 19:30:52 +02:00
|
|
|
should_add_guest_user_indicator: false,
|
2021-05-16 17:17:44 +02:00
|
|
|
},
|
|
|
|
{
|
2023-04-11 21:04:33 +02:00
|
|
|
href: "#narrow/dm/1002-Alice-Smith",
|
2021-05-16 17:17:44 +02:00
|
|
|
is_current_user: false,
|
|
|
|
name: "Alice Smith",
|
|
|
|
num_unread: 0,
|
2024-10-24 08:10:56 +02:00
|
|
|
profile_picture: "/avatar/1002",
|
2021-06-27 19:04:17 +02:00
|
|
|
status_emoji_info,
|
2022-08-12 22:41:06 +02:00
|
|
|
status_text: undefined,
|
2024-11-05 22:45:51 +01:00
|
|
|
has_status_text: false,
|
2022-09-21 15:49:36 +02:00
|
|
|
user_circle_class: "user_circle_empty",
|
2021-05-16 17:17:44 +02:00
|
|
|
user_id: 1002,
|
2022-08-12 22:41:06 +02:00
|
|
|
user_list_style,
|
2023-09-13 19:30:52 +02:00
|
|
|
should_add_guest_user_indicator: false,
|
2021-05-16 17:17:44 +02:00
|
|
|
},
|
|
|
|
{
|
2023-04-11 21:04:33 +02:00
|
|
|
href: "#narrow/dm/1003-Fred-Flintstone",
|
2021-05-16 17:17:44 +02:00
|
|
|
is_current_user: false,
|
|
|
|
name: "Fred Flintstone",
|
|
|
|
num_unread: 0,
|
2024-10-24 08:10:56 +02:00
|
|
|
profile_picture: "/avatar/1003",
|
2021-06-27 19:04:17 +02:00
|
|
|
status_emoji_info,
|
2022-08-12 22:41:06 +02:00
|
|
|
status_text: undefined,
|
2024-11-05 22:45:51 +01:00
|
|
|
has_status_text: false,
|
2021-05-16 17:17:44 +02:00
|
|
|
user_circle_class: "user_circle_empty",
|
|
|
|
user_id: 1003,
|
2022-08-12 22:41:06 +02:00
|
|
|
user_list_style,
|
2023-09-13 19:30:52 +02:00
|
|
|
should_add_guest_user_indicator: false,
|
2021-05-16 17:17:44 +02:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2024-06-05 22:22:17 +02:00
|
|
|
test("unknown user id in presence", () => {
|
|
|
|
// This test is to make sure that we don't generate errors
|
|
|
|
// when we have a user_id in presence that we don't have
|
|
|
|
// information about.
|
|
|
|
// Such scenarios can happen if we receive presence info involving
|
|
|
|
// a new user before the user creation event reaches us.
|
|
|
|
presence.presence_info.set(999, {status: "active"});
|
|
|
|
const user_ids = buddy_data.get_filtered_and_sorted_user_ids();
|
|
|
|
|
|
|
|
// This user id should not be present in the filtered list,
|
|
|
|
// it's meant to be ignored until we know about the user.
|
|
|
|
assert.equal(user_ids.includes(999), false);
|
2020-02-04 15:38:37 +01:00
|
|
|
});
|