2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-09-29 22:20:46 +02:00
|
|
|
const {parseISO} = require("date-fns");
|
2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
2020-09-29 22:20:46 +02:00
|
|
|
const MockDate = require("mockdate");
|
2020-07-25 02:02:35 +02:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {$t} = require("./lib/i18n");
|
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
2024-02-13 02:08:24 +01:00
|
|
|
const {current_user, page_params, realm, user_settings} = require("./lib/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const message_user_ids = mock_esm("../src/message_user_ids");
|
2023-11-30 11:11:27 +01:00
|
|
|
const settings_data = mock_esm("../src/settings_data", {
|
|
|
|
user_can_access_all_other_users: () => true,
|
|
|
|
});
|
2014-02-01 16:14:40 +01:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
const muted_users = zrequire("muted_users");
|
2020-09-29 22:20:46 +02:00
|
|
|
const people = zrequire("people");
|
2024-07-08 19:00:08 +02:00
|
|
|
const user_groups = zrequire("user_groups");
|
2020-02-25 12:46:14 +01:00
|
|
|
|
2020-03-24 22:42:58 +01:00
|
|
|
const welcome_bot = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "welcome-bot@example.com",
|
2020-03-24 22:42:58 +01:00
|
|
|
user_id: 4,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Welcome Bot",
|
2020-03-24 22:42:58 +01:00
|
|
|
is_bot: true,
|
|
|
|
// cross realm bots have no owner
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const me = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "me@example.com",
|
2017-01-19 23:04:52 +01:00
|
|
|
user_id: 30,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Me Myself",
|
2020-10-27 01:41:00 +01:00
|
|
|
timezone: "America/Los_Angeles",
|
2018-11-02 20:08:24 +01:00
|
|
|
is_admin: false,
|
|
|
|
is_guest: false,
|
2021-04-22 21:07:51 +02:00
|
|
|
is_moderator: false,
|
2018-11-02 20:08:24 +01:00
|
|
|
is_bot: false,
|
2021-04-29 15:49:01 +02:00
|
|
|
role: 400,
|
2020-06-24 14:25:17 +02:00
|
|
|
// no avatar, so client should construct a /avatar/{user_id} URL.
|
2017-01-19 23:04:52 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const isaac = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "isaac@example.com",
|
|
|
|
delivery_email: "isaac-delivery@example.com",
|
2018-02-23 16:16:55 +01:00
|
|
|
user_id: 32,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Isaac Newton",
|
2018-02-23 16:16:55 +01:00
|
|
|
};
|
|
|
|
|
2023-09-25 06:07:27 +02:00
|
|
|
const unknown_user = people.make_user(1500, "unknown@example.com", "Unknown user");
|
|
|
|
|
2017-01-19 23:04:52 +01:00
|
|
|
function initialize() {
|
|
|
|
people.init();
|
2021-03-08 18:12:40 +01:00
|
|
|
people.add_active_user({...me});
|
2017-01-20 23:16:28 +01:00
|
|
|
people.initialize_current_user(me.user_id);
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.set_muted_users([]);
|
2023-09-25 06:07:27 +02:00
|
|
|
|
|
|
|
people._add_user(unknown_user);
|
2017-01-19 23:04:52 +01:00
|
|
|
}
|
|
|
|
|
2024-07-08 19:00:08 +02:00
|
|
|
const nobody = {
|
|
|
|
name: "role:nobody",
|
|
|
|
id: 1,
|
|
|
|
members: new Set([]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
const everyone = {
|
|
|
|
name: "role:everyone",
|
|
|
|
id: 2,
|
|
|
|
members: new Set([30]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
|
|
|
|
user_groups.initialize({realm_user_groups: [nobody, everyone]});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
function test_people(label, f) {
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2021-03-08 18:12:40 +01:00
|
|
|
initialize();
|
2022-07-10 01:06:33 +02:00
|
|
|
f(helpers);
|
2021-03-08 18:12:40 +01:00
|
|
|
});
|
|
|
|
}
|
2014-03-12 19:59:17 +01:00
|
|
|
|
2020-05-26 18:59:34 +02:00
|
|
|
/*
|
|
|
|
TEST SETUP NOTES:
|
|
|
|
|
|
|
|
We don't add all these users right away,
|
|
|
|
but they are convenient to have for various
|
|
|
|
tests, and we just add them as needed after
|
|
|
|
calling something like `people.init()`.
|
|
|
|
|
|
|
|
Note that we deliberately make it so that
|
|
|
|
alphabetical order mismatches id order,
|
|
|
|
since that can uncover bugs where we neglect
|
|
|
|
to be rigorous about sort order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const realm_admin = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "realm_admin@example.com",
|
|
|
|
full_name: "Realm Admin",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 32,
|
2020-06-03 23:30:34 +02:00
|
|
|
is_owner: false,
|
2020-05-26 18:59:34 +02:00
|
|
|
is_admin: true,
|
|
|
|
is_guest: false,
|
2021-04-22 21:07:51 +02:00
|
|
|
is_moderator: false,
|
2021-05-28 12:51:50 +02:00
|
|
|
is_billing_admin: true,
|
2020-05-26 18:59:34 +02:00
|
|
|
is_bot: false,
|
2021-04-29 15:49:01 +02:00
|
|
|
role: 200,
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const guest = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "guest@example.com",
|
|
|
|
full_name: "Guest User",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 33,
|
2020-06-03 23:30:34 +02:00
|
|
|
is_owner: false,
|
2020-05-26 18:59:34 +02:00
|
|
|
is_admin: false,
|
|
|
|
is_guest: true,
|
2021-04-22 21:07:51 +02:00
|
|
|
is_moderator: false,
|
2021-05-28 12:51:50 +02:00
|
|
|
is_billing_admin: false,
|
2020-05-26 18:59:34 +02:00
|
|
|
is_bot: false,
|
2021-04-29 15:49:01 +02:00
|
|
|
role: 600,
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
2020-06-03 23:30:34 +02:00
|
|
|
const realm_owner = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "realm_owner@example.com",
|
|
|
|
full_name: "Realm Owner",
|
2020-06-03 23:30:34 +02:00
|
|
|
user_id: 34,
|
|
|
|
is_owner: true,
|
|
|
|
is_admin: true,
|
|
|
|
is_guest: false,
|
2021-04-22 21:07:51 +02:00
|
|
|
is_moderator: false,
|
2021-05-28 12:51:50 +02:00
|
|
|
is_billing_admin: false,
|
2020-06-03 23:30:34 +02:00
|
|
|
is_bot: false,
|
2021-04-29 15:49:01 +02:00
|
|
|
role: 100,
|
2020-06-03 23:30:34 +02:00
|
|
|
};
|
|
|
|
|
2020-05-26 18:59:34 +02:00
|
|
|
const bot_botson = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "botson-bot@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 35,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Bot Botson",
|
2020-05-26 18:59:34 +02:00
|
|
|
is_bot: true,
|
|
|
|
bot_owner_id: isaac.user_id,
|
2022-03-31 08:31:45 +02:00
|
|
|
role: 300,
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
2021-04-22 21:07:51 +02:00
|
|
|
const moderator = {
|
|
|
|
email: "moderator@example.com",
|
|
|
|
full_name: "Moderator",
|
|
|
|
user_id: 36,
|
|
|
|
is_owner: false,
|
|
|
|
is_admin: false,
|
|
|
|
is_guest: false,
|
2021-05-28 12:51:50 +02:00
|
|
|
is_billing_admin: false,
|
2021-04-22 21:07:51 +02:00
|
|
|
is_moderator: true,
|
|
|
|
is_bot: false,
|
|
|
|
role: 300,
|
|
|
|
};
|
|
|
|
|
2023-12-12 20:47:41 +01:00
|
|
|
const bot_with_inaccessible_owner = {
|
2023-12-04 11:46:50 +01:00
|
|
|
email: "inaccessible-owner-bot@example.com",
|
|
|
|
user_id: 37,
|
|
|
|
full_name: "Inaccessible owner bot",
|
|
|
|
is_bot: true,
|
|
|
|
bot_owner_id: 38,
|
|
|
|
role: 300,
|
|
|
|
};
|
|
|
|
|
2020-05-26 18:59:34 +02:00
|
|
|
const steven = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "steven@example.com",
|
|
|
|
delivery_email: "steven-delivery@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 77,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Steven",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const alice1 = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice1@example.com",
|
2021-11-11 14:56:40 +01:00
|
|
|
delivery_email: "alice1-delivery@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 202,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const bob = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "bob@example.com",
|
2021-11-11 14:56:40 +01:00
|
|
|
delivery_email: "bob-delivery@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 203,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Bob van Roberts",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const charles = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "charles@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 301,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Charles Dickens",
|
2021-10-14 00:48:32 +02:00
|
|
|
avatar_url: "http://charles.com/foo.png",
|
2020-05-26 18:59:34 +02:00
|
|
|
is_guest: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const maria = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "Athens@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 302,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Maria Athens",
|
2020-06-24 14:25:17 +02:00
|
|
|
// With client_gravatar enabled, requests that client compute gravatar
|
|
|
|
avatar_url: null,
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const ashton = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "ashton@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 303,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Ashton Smith",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const linus = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "ltorvalds@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 304,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Linus Torvalds",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const emp401 = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "emp401@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 401,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "whatever 401",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const emp402 = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "EMP402@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 402,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "whatever 402",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const debbie = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "deBBie71@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 501,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Debra Henton",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const stephen1 = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "stephen-the-author@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 601,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Stephen King",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const stephen2 = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "stephen-the-explorer@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 602,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Stephen King",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const noah = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "emnoa@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 1200,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Nöôáàh Ëmerson",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const plain_noah = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "otheremnoa@example.com",
|
2020-05-26 18:59:34 +02:00
|
|
|
user_id: 1201,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Nooaah Emerson",
|
2020-05-26 18:59:34 +02:00
|
|
|
};
|
|
|
|
|
2021-05-19 14:41:18 +02:00
|
|
|
const all1 = {
|
|
|
|
email: "all1@example.com",
|
|
|
|
user_id: 1202,
|
|
|
|
full_name: "all",
|
|
|
|
};
|
|
|
|
|
|
|
|
const all2 = {
|
|
|
|
email: "all2@example.com",
|
|
|
|
user_id: 1203,
|
|
|
|
full_name: "all",
|
|
|
|
};
|
|
|
|
|
2023-09-30 03:10:43 +02:00
|
|
|
const stewie = {
|
|
|
|
email: "stewie@example.com",
|
|
|
|
user_id: 1204,
|
|
|
|
full_name: "Stewart Gilligan",
|
|
|
|
profile_data: {
|
|
|
|
1: "(888) 888-8888",
|
|
|
|
2: "(555) 555-5555",
|
|
|
|
3: "he/him",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-12-23 22:42:45 +01:00
|
|
|
function get_all_persons() {
|
|
|
|
return people.filter_all_persons(() => true);
|
|
|
|
}
|
|
|
|
|
2023-12-04 11:46:50 +01:00
|
|
|
test_people("basics", ({override}) => {
|
2019-12-23 22:42:45 +01:00
|
|
|
const persons = get_all_persons();
|
2017-01-19 23:04:52 +01:00
|
|
|
|
2020-03-21 19:53:16 +01:00
|
|
|
assert.deepEqual(people.get_realm_users(), [me]);
|
|
|
|
|
2023-09-25 06:07:27 +02:00
|
|
|
assert.equal(persons.length, 2);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(persons[0].full_name, "Me Myself");
|
2016-11-01 19:36:50 +01:00
|
|
|
|
2020-03-21 19:53:16 +01:00
|
|
|
let realm_persons = people.get_realm_users();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(realm_persons[0].full_name, "Me Myself");
|
2020-01-01 23:43:30 +01:00
|
|
|
|
2020-03-21 19:53:16 +01:00
|
|
|
realm_persons = people.get_realm_users();
|
2020-02-08 06:05:56 +01:00
|
|
|
assert.equal(realm_persons.length, 1);
|
2023-05-16 12:46:50 +02:00
|
|
|
assert.equal(people.get_realm_active_human_user_ids().length, 1);
|
2016-11-01 19:45:53 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const full_name = "Isaac Newton";
|
|
|
|
const email = "isaac@example.com";
|
2017-03-26 19:32:54 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!people.is_known_user_id(32));
|
|
|
|
assert.ok(!people.is_valid_full_name_and_user_id(full_name, 32));
|
2020-02-16 14:16:46 +01:00
|
|
|
assert.equal(people.get_user_id_from_name(full_name), undefined);
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(isaac);
|
2018-02-23 16:16:55 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(people.get_actual_name_from_user_id(32), full_name);
|
2020-02-16 14:16:46 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(people.is_valid_full_name_and_user_id(full_name, 32));
|
|
|
|
assert.ok(people.is_known_user_id(32));
|
2020-03-21 20:19:30 +01:00
|
|
|
assert.equal(people.get_active_human_count(), 2);
|
2014-02-01 16:14:40 +01:00
|
|
|
|
2020-02-16 14:16:46 +01:00
|
|
|
assert.equal(people.get_user_id_from_name(full_name), 32);
|
|
|
|
|
|
|
|
let person = people.get_by_email(email);
|
2014-02-01 16:14:40 +01:00
|
|
|
assert.equal(person.full_name, full_name);
|
2020-03-21 20:19:30 +01:00
|
|
|
|
2020-03-21 19:53:16 +01:00
|
|
|
realm_persons = people.get_realm_users();
|
2020-02-08 06:05:56 +01:00
|
|
|
assert.equal(realm_persons.length, 2);
|
2016-11-01 19:45:53 +01:00
|
|
|
|
2020-01-01 23:43:30 +01:00
|
|
|
const active_user_ids = people.get_active_user_ids().sort();
|
|
|
|
assert.deepEqual(active_user_ids, [me.user_id, isaac.user_id]);
|
2017-10-26 21:00:30 +02:00
|
|
|
assert.equal(people.is_active_user_for_popover(isaac.user_id), true);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(people.is_valid_email_for_compose(isaac.email));
|
2017-10-07 20:10:10 +02:00
|
|
|
|
2023-01-29 02:57:25 +01:00
|
|
|
let bot_user_ids = people.get_bot_ids();
|
|
|
|
assert.equal(bot_user_ids.length, 0);
|
|
|
|
|
2016-12-15 22:44:42 +01:00
|
|
|
// Now deactivate isaac
|
|
|
|
people.deactivate(isaac);
|
2020-05-30 04:21:15 +02:00
|
|
|
assert.equal(people.get_non_active_human_ids().length, 1);
|
2020-03-21 13:58:40 +01:00
|
|
|
assert.equal(people.get_active_human_count(), 1);
|
2017-10-26 21:00:30 +02:00
|
|
|
assert.equal(people.is_active_user_for_popover(isaac.user_id), false);
|
2020-07-22 13:39:38 +02:00
|
|
|
assert.equal(people.is_valid_email_for_compose(isaac.email), true);
|
2017-10-14 00:33:36 +02:00
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(bot_botson);
|
2017-10-26 21:00:30 +02:00
|
|
|
assert.equal(people.is_active_user_for_popover(bot_botson.user_id), true);
|
2023-01-29 02:57:25 +01:00
|
|
|
bot_user_ids = people.get_bot_ids();
|
|
|
|
assert.deepEqual(bot_user_ids, [bot_botson.user_id]);
|
2014-03-12 19:59:17 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(people.get_bot_owner_user(bot_botson).full_name, "Isaac Newton");
|
2020-03-24 22:42:58 +01:00
|
|
|
|
2022-06-27 22:34:21 +02:00
|
|
|
assert.equal(people.can_admin_user(me), true);
|
|
|
|
assert.equal(people.can_admin_user(bot_botson), false);
|
|
|
|
|
2020-03-24 22:42:58 +01:00
|
|
|
// Add our cross-realm bot. It won't add to our human
|
|
|
|
// count, and it has no owner.
|
2020-05-27 03:09:17 +02:00
|
|
|
people.add_cross_realm_user(welcome_bot);
|
2020-03-24 22:42:58 +01:00
|
|
|
assert.equal(people.get_bot_owner_user(welcome_bot), undefined);
|
|
|
|
assert.equal(people.get_active_human_count(), 1);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(people.get_by_email(welcome_bot.email).full_name, "Welcome Bot");
|
2020-03-24 22:42:58 +01:00
|
|
|
|
2023-12-04 11:46:50 +01:00
|
|
|
override(settings_data, "user_can_access_all_other_users", () => false);
|
|
|
|
assert.equal(
|
2023-12-12 20:47:41 +01:00
|
|
|
people.get_bot_owner_user(bot_with_inaccessible_owner).full_name,
|
2023-12-04 11:46:50 +01:00
|
|
|
"translated: Unknown user",
|
|
|
|
);
|
|
|
|
|
2020-03-21 19:53:16 +01:00
|
|
|
// get_realm_users() will include our active bot,
|
|
|
|
// but will exclude isaac (who is deactivated)
|
|
|
|
assert.deepEqual(
|
2020-07-15 00:34:28 +02:00
|
|
|
people
|
|
|
|
.get_realm_users()
|
|
|
|
.map((u) => u.user_id)
|
|
|
|
.sort(),
|
|
|
|
[me.user_id, bot_botson.user_id],
|
2020-03-21 19:53:16 +01:00
|
|
|
);
|
|
|
|
|
2023-01-29 02:57:25 +01:00
|
|
|
// get_bot_ids() includes all bot users.
|
|
|
|
bot_user_ids = people.get_bot_ids();
|
|
|
|
assert.deepEqual(bot_user_ids, [bot_botson.user_id, welcome_bot.user_id]);
|
|
|
|
|
2020-03-21 13:58:40 +01:00
|
|
|
// The bot doesn't add to our human count.
|
|
|
|
assert.equal(people.get_active_human_count(), 1);
|
|
|
|
|
2017-10-26 21:00:30 +02:00
|
|
|
// Invalid user ID returns false and warns.
|
2023-07-12 07:22:30 +02:00
|
|
|
blueslip.expect("warn", "Unexpectedly invalid user_id in user popover query");
|
2017-10-26 21:00:30 +02:00
|
|
|
assert.equal(people.is_active_user_for_popover(123412), false);
|
2017-10-25 01:24:03 +02:00
|
|
|
|
2016-12-15 22:44:42 +01:00
|
|
|
// We can still get their info for non-realm needs.
|
|
|
|
person = people.get_by_email(email);
|
|
|
|
assert.equal(person.email, email);
|
|
|
|
|
2017-01-19 23:04:52 +01:00
|
|
|
// The current user should still be there
|
2020-07-15 01:29:15 +02:00
|
|
|
person = people.get_by_email("me@example.com");
|
|
|
|
assert.equal(person.full_name, "Me Myself");
|
2017-06-02 04:47:53 +02:00
|
|
|
|
|
|
|
// Test undefined people
|
2023-09-25 06:01:26 +02:00
|
|
|
assert.equal(people.is_cross_realm_email("invalid@example.com"), false);
|
2017-06-02 04:47:53 +02:00
|
|
|
|
|
|
|
// Test is_my_user_id function
|
|
|
|
assert.equal(people.is_my_user_id(me.user_id), true);
|
|
|
|
assert.equal(people.is_my_user_id(isaac.user_id), false);
|
2018-07-28 20:13:20 +02:00
|
|
|
|
|
|
|
// Reactivating issac
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(isaac);
|
2023-05-16 12:46:50 +02:00
|
|
|
const active_humans = people.get_realm_active_human_user_ids();
|
2020-03-21 18:09:45 +01:00
|
|
|
assert.equal(active_humans.length, 2);
|
2020-01-01 23:43:30 +01:00
|
|
|
assert.deepEqual(
|
2020-03-21 18:09:45 +01:00
|
|
|
active_humans.sort((p) => p.user_id),
|
2020-07-15 00:34:28 +02:00
|
|
|
[me.user_id, isaac.user_id],
|
|
|
|
);
|
2022-02-22 15:36:14 +01:00
|
|
|
|
|
|
|
// get_users_from_ids
|
|
|
|
assert.deepEqual(people.get_users_from_ids([me.user_id, isaac.user_id]), [me, isaac]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-01-19 23:04:52 +01:00
|
|
|
|
2021-12-15 18:48:33 +01:00
|
|
|
test_people("sort_but_pin_current_user_on_top with me", () => {
|
|
|
|
people.add_active_user(maria);
|
|
|
|
people.add_active_user(steven);
|
|
|
|
|
|
|
|
// We need the actual object from people.js, not the
|
|
|
|
// "me" object we made a copy of.
|
|
|
|
const my_user = people.get_by_user_id(me.user_id);
|
|
|
|
const users = [steven, debbie, maria, my_user];
|
|
|
|
|
|
|
|
people.sort_but_pin_current_user_on_top(users);
|
|
|
|
|
|
|
|
assert.deepEqual(users, [my_user, debbie, maria, steven]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test_people("sort_but_pin_current_user_on_top without me", () => {
|
|
|
|
people.add_active_user(maria);
|
|
|
|
people.add_active_user(steven);
|
|
|
|
|
|
|
|
const users = [steven, maria];
|
|
|
|
|
|
|
|
people.sort_but_pin_current_user_on_top(users);
|
|
|
|
|
|
|
|
assert.deepEqual(users, [maria, steven]);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("check_active_non_active_users", () => {
|
|
|
|
people.add_active_user(bot_botson);
|
|
|
|
people.add_active_user(isaac);
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
let active_users = people.get_realm_users();
|
|
|
|
let non_active_users = people.get_non_active_realm_users();
|
|
|
|
assert.equal(active_users.length, 3);
|
|
|
|
assert.equal(non_active_users.length, 0);
|
|
|
|
|
|
|
|
people.add_active_user(maria);
|
|
|
|
people.add_active_user(linus);
|
|
|
|
active_users = people.get_realm_users();
|
|
|
|
assert.equal(active_users.length, 5);
|
|
|
|
// Invalid ID
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "No user found");
|
2020-05-26 22:34:15 +02:00
|
|
|
people.is_person_active(1000001);
|
|
|
|
assert.equal(people.is_person_active(maria.user_id), true);
|
|
|
|
assert.equal(people.is_person_active(linus.user_id), true);
|
|
|
|
|
|
|
|
people.deactivate(maria);
|
|
|
|
non_active_users = people.get_non_active_realm_users();
|
|
|
|
active_users = people.get_realm_users();
|
|
|
|
assert.equal(non_active_users.length, 1);
|
|
|
|
assert.equal(active_users.length, 4);
|
|
|
|
assert.equal(people.is_person_active(maria.user_id), false);
|
|
|
|
|
|
|
|
people.deactivate(linus);
|
|
|
|
people.add_active_user(maria);
|
|
|
|
non_active_users = people.get_non_active_realm_users();
|
|
|
|
active_users = people.get_realm_users();
|
|
|
|
assert.equal(non_active_users.length, 1);
|
|
|
|
assert.equal(active_users.length, 4);
|
|
|
|
assert.equal(people.is_person_active(maria.user_id), true);
|
|
|
|
assert.equal(people.is_person_active(linus.user_id), false);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("pm_lookup_key", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(people.pm_lookup_key("30"), "30");
|
|
|
|
assert.equal(people.pm_lookup_key("32,30"), "32");
|
|
|
|
assert.equal(people.pm_lookup_key("101,32,30"), "32,101");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-08-01 15:51:56 +02:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("get_recipients", () => {
|
|
|
|
people.add_active_user(isaac);
|
2021-05-13 09:32:06 +02:00
|
|
|
people.add_active_user(linus);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(people.get_recipients("30"), "Me Myself");
|
|
|
|
assert.equal(people.get_recipients("30,32"), "Isaac Newton");
|
2021-05-13 09:32:06 +02:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.add_muted_user(304);
|
2021-05-13 09:32:06 +02:00
|
|
|
assert.equal(people.get_recipients("304,32"), "Isaac Newton, translated: Muted user");
|
|
|
|
});
|
|
|
|
|
|
|
|
test_people("get_full_name", () => {
|
|
|
|
people.add_active_user(isaac);
|
|
|
|
const names = people.get_full_name(isaac.user_id);
|
|
|
|
assert.equal(names, "Isaac Newton");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-04-03 19:34:14 +02:00
|
|
|
|
2021-05-11 16:33:52 +02:00
|
|
|
test_people("get_full_names_for_poll_option", () => {
|
2021-03-08 18:12:40 +01:00
|
|
|
people.add_active_user(isaac);
|
2021-05-11 16:33:52 +02:00
|
|
|
const names = people.get_full_names_for_poll_option([me.user_id, isaac.user_id]);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(names, "Me Myself, Isaac Newton");
|
2018-02-23 16:16:55 +01:00
|
|
|
});
|
|
|
|
|
2023-12-01 11:53:45 +01:00
|
|
|
test_people("get_display_full_names", ({override}) => {
|
2021-05-10 15:35:02 +02:00
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
people.add_active_user(steven);
|
|
|
|
people.add_active_user(bob);
|
|
|
|
people.add_active_user(charles);
|
2023-09-15 20:00:56 +02:00
|
|
|
people.add_active_user(guest);
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_enable_guest_user_indicator = true;
|
2023-09-15 20:00:56 +02:00
|
|
|
|
2023-12-01 11:53:45 +01:00
|
|
|
let user_ids = [me.user_id, steven.user_id, bob.user_id, charles.user_id, guest.user_id];
|
2021-05-10 15:45:33 +02:00
|
|
|
let names = people.get_display_full_names(user_ids);
|
2021-05-10 15:35:02 +02:00
|
|
|
|
|
|
|
// This doesn't do anything special for the current user. The caller has
|
|
|
|
// to take care of such cases and do the appropriate.
|
2023-09-15 20:00:56 +02:00
|
|
|
assert.deepEqual(names, [
|
|
|
|
"Me Myself",
|
|
|
|
"Steven",
|
|
|
|
"Bob van Roberts",
|
|
|
|
"Charles Dickens",
|
|
|
|
"translated: Guest User (guest)",
|
|
|
|
]);
|
2021-05-10 15:45:33 +02:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.add_muted_user(charles.user_id);
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_enable_guest_user_indicator = false;
|
2021-05-10 15:45:33 +02:00
|
|
|
names = people.get_display_full_names(user_ids);
|
2023-09-15 20:00:56 +02:00
|
|
|
assert.deepEqual(names, [
|
|
|
|
"Me Myself",
|
|
|
|
"Steven",
|
|
|
|
"Bob van Roberts",
|
|
|
|
"translated: Muted user",
|
|
|
|
"Guest User",
|
|
|
|
]);
|
2023-10-10 13:24:47 +02:00
|
|
|
|
|
|
|
muted_users.add_muted_user(guest.user_id);
|
|
|
|
names = people.get_display_full_names(user_ids);
|
|
|
|
assert.deepEqual(names, [
|
|
|
|
"Me Myself",
|
|
|
|
"Steven",
|
|
|
|
"Bob van Roberts",
|
|
|
|
"translated: Muted user",
|
|
|
|
"translated: Muted user",
|
|
|
|
]);
|
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_enable_guest_user_indicator = true;
|
2023-10-10 13:24:47 +02:00
|
|
|
names = people.get_display_full_names(user_ids);
|
|
|
|
assert.deepEqual(names, [
|
|
|
|
"Me Myself",
|
|
|
|
"Steven",
|
|
|
|
"Bob van Roberts",
|
|
|
|
"translated: Muted user",
|
|
|
|
"translated: Muted user (guest)",
|
|
|
|
]);
|
2023-12-01 11:53:45 +01:00
|
|
|
|
|
|
|
override(settings_data, "user_can_access_all_other_users", () => false);
|
|
|
|
const inaccessible_user_id = 99;
|
|
|
|
user_ids = [me.user_id, steven.user_id, inaccessible_user_id];
|
|
|
|
names = people.get_display_full_names(user_ids, true);
|
|
|
|
assert.deepEqual(names, ["Me Myself", "Steven", "translated: Unknown user"]);
|
2021-05-10 15:35:02 +02:00
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("my_custom_profile_data", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = people.get_by_email(me.email);
|
2020-07-15 01:29:15 +02:00
|
|
|
person.profile_data = {3: "My address", 4: "My phone number"};
|
|
|
|
assert.equal(people.my_custom_profile_data(3), "My address");
|
|
|
|
assert.equal(people.my_custom_profile_data(4), "My phone number");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-02-26 20:09:07 +01:00
|
|
|
|
2023-09-30 03:10:43 +02:00
|
|
|
test_people("get_custom_fields_by_type", () => {
|
|
|
|
people.add_active_user(stewie);
|
|
|
|
const person = people.get_by_user_id(stewie.user_id);
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.custom_profile_field_types = {
|
2023-09-30 03:10:43 +02:00
|
|
|
SHORT_TEXT: {
|
|
|
|
id: 1,
|
|
|
|
name: "Short text",
|
|
|
|
},
|
|
|
|
PRONOUNS: {
|
|
|
|
id: 8,
|
|
|
|
name: "Pronouns",
|
|
|
|
},
|
|
|
|
};
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.custom_profile_fields = [
|
2023-09-30 03:10:43 +02:00
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: "Phone number (mobile)",
|
|
|
|
type: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
name: "Phone number (office)",
|
|
|
|
type: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 3,
|
|
|
|
name: "Pronouns",
|
|
|
|
type: 8,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const SHORT_TEXT_ID = 1;
|
|
|
|
assert.deepEqual(people.get_custom_fields_by_type(person.user_id, SHORT_TEXT_ID), [
|
|
|
|
"(888) 888-8888",
|
|
|
|
"(555) 555-5555",
|
|
|
|
]);
|
|
|
|
assert.deepEqual(people.get_custom_fields_by_type(person.user_id, 8), ["he/him"]);
|
|
|
|
assert.deepEqual(people.get_custom_fields_by_type(person.user_id, 100), []);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("bot_custom_profile_data", () => {
|
2019-04-07 17:29:53 +02:00
|
|
|
// If this test fails, then try opening organization settings > bots
|
|
|
|
// http://localhost:9991/#organization/bot-list-admin
|
|
|
|
// and then try to edit any of the bots.
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(bot_botson);
|
2020-05-26 18:59:34 +02:00
|
|
|
assert.equal(people.get_custom_profile_data(bot_botson.user_id, 3), null);
|
2019-04-07 17:29:53 +02:00
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("user_timezone", () => {
|
|
|
|
MockDate.set(parseISO("20130208T080910").getTime());
|
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.twenty_four_hour_time = true;
|
2023-11-18 03:50:33 +01:00
|
|
|
assert.equal(people.get_user_time(me.user_id), "00:09");
|
2017-06-02 06:12:41 +02:00
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.twenty_four_hour_time = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(people.get_user_time(me.user_id), "12:09 AM");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-02 06:12:41 +02:00
|
|
|
|
2022-11-21 20:33:53 +01:00
|
|
|
test_people("utcToZonedTime", ({override}) => {
|
|
|
|
MockDate.set(parseISO("20130208T080910").getTime());
|
|
|
|
user_settings.twenty_four_hour_time = true;
|
|
|
|
|
2023-09-25 06:07:27 +02:00
|
|
|
assert.deepEqual(people.get_user_time(unknown_user.user_id), undefined);
|
2023-11-18 03:50:33 +01:00
|
|
|
assert.equal(people.get_user_time(me.user_id), "00:09");
|
2022-11-21 20:33:53 +01:00
|
|
|
|
2022-11-23 22:46:45 +01:00
|
|
|
override(people.get_by_user_id(me.user_id), "timezone", "Eriador/Rivendell");
|
2023-11-30 21:08:36 +01:00
|
|
|
blueslip.expect(
|
|
|
|
"warn",
|
|
|
|
"Error formatting time in Eriador/Rivendell: RangeError: Invalid time zone specified: Eriador/Rivendell",
|
|
|
|
);
|
2022-11-21 20:33:53 +01:00
|
|
|
people.get_user_time(me.user_id);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("user_type", () => {
|
2020-05-26 18:59:34 +02:00
|
|
|
people.init();
|
2018-11-02 20:08:24 +01:00
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(me);
|
|
|
|
people.add_active_user(realm_admin);
|
|
|
|
people.add_active_user(guest);
|
2020-06-03 23:30:34 +02:00
|
|
|
people.add_active_user(realm_owner);
|
2021-04-22 21:07:51 +02:00
|
|
|
people.add_active_user(moderator);
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(bot_botson);
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(people.get_user_type(me.user_id), $t({defaultMessage: "Member"}));
|
|
|
|
assert.equal(people.get_user_type(realm_admin.user_id), $t({defaultMessage: "Administrator"}));
|
|
|
|
assert.equal(people.get_user_type(guest.user_id), $t({defaultMessage: "Guest"}));
|
|
|
|
assert.equal(people.get_user_type(realm_owner.user_id), $t({defaultMessage: "Owner"}));
|
2021-04-22 21:07:51 +02:00
|
|
|
assert.equal(people.get_user_type(moderator.user_id), $t({defaultMessage: "Moderator"}));
|
2022-03-31 08:31:45 +02:00
|
|
|
assert.equal(people.get_user_type(bot_botson.user_id), $t({defaultMessage: "Moderator"}));
|
2018-11-02 20:08:24 +01:00
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("updates", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const person = people.get_by_email("me@example.com");
|
|
|
|
people.set_full_name(person, "Me the Third");
|
|
|
|
assert.equal(people.my_full_name(), "Me the Third");
|
|
|
|
assert.equal(person.full_name, "Me the Third");
|
|
|
|
assert.equal(people.get_user_id_from_name("Me the Third"), me.user_id);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2014-02-20 19:50:14 +01:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("get_by_user_id", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let person = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "mary@example.com",
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 42,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Mary",
|
2016-10-31 15:56:57 +01:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(person);
|
2020-07-15 01:29:15 +02:00
|
|
|
person = people.get_by_email("mary@example.com");
|
|
|
|
assert.equal(person.full_name, "Mary");
|
2020-02-05 14:30:59 +01:00
|
|
|
person = people.get_by_user_id(42);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(person.email, "mary@example.com");
|
2016-10-31 15:56:57 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
people.set_full_name(person, "Mary New");
|
2020-02-05 14:30:59 +01:00
|
|
|
person = people.get_by_user_id(42);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(person.full_name, "Mary New");
|
2016-10-31 15:56:57 +01:00
|
|
|
|
2016-12-15 22:44:42 +01:00
|
|
|
// deactivate() should eventually just take a user_id, but
|
|
|
|
// now it takes a full person object. Note that deactivate()
|
|
|
|
// won't actually make the user disappear completely.
|
|
|
|
people.deactivate(person);
|
2020-02-05 14:30:59 +01:00
|
|
|
person = people.get_by_user_id(42);
|
2016-12-15 22:44:42 +01:00
|
|
|
assert.equal(person.user_id, 42);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-10-31 15:56:57 +01:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("set_custom_profile_field_data", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = people.get_by_email(me.email);
|
2021-03-08 18:12:40 +01:00
|
|
|
person.profile_data = {};
|
2020-07-15 00:34:28 +02:00
|
|
|
const field = {
|
|
|
|
id: 3,
|
|
|
|
name: "Custom long field",
|
|
|
|
type: "text",
|
|
|
|
value: "Field value",
|
|
|
|
rendered_value: "<p>Field value</p>",
|
|
|
|
};
|
2018-03-10 14:41:44 +01:00
|
|
|
people.set_custom_profile_field_data(person.user_id, field);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(person.profile_data[field.id].value, "Field value");
|
|
|
|
assert.equal(person.profile_data[field.id].rendered_value, "<p>Field value</p>");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-10 14:41:44 +01:00
|
|
|
|
2022-12-20 16:52:25 +01:00
|
|
|
test_people("is_current_user_only_owner", () => {
|
|
|
|
const person = people.get_by_email(me.email);
|
|
|
|
person.is_owner = false;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_owner = false;
|
2022-12-20 16:52:25 +01:00
|
|
|
assert.ok(!people.is_current_user_only_owner());
|
|
|
|
|
|
|
|
person.is_owner = true;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_owner = true;
|
2022-12-20 16:52:25 +01:00
|
|
|
assert.ok(people.is_current_user_only_owner());
|
|
|
|
|
|
|
|
people.add_active_user(realm_owner);
|
|
|
|
assert.ok(!people.is_current_user_only_owner());
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("recipient_counts", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = 99;
|
2020-07-20 22:18:43 +02:00
|
|
|
assert.equal(people.get_recipient_count({user_id}), 0);
|
2017-01-31 21:02:15 +01:00
|
|
|
people.incr_recipient_count(user_id);
|
|
|
|
people.incr_recipient_count(user_id);
|
2020-07-20 22:18:43 +02:00
|
|
|
assert.equal(people.get_recipient_count({user_id}), 2);
|
2016-11-04 14:34:58 +01:00
|
|
|
|
|
|
|
assert.equal(people.get_recipient_count({pm_recipient_count: 5}), 5);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-11-03 21:59:18 +01:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("filtered_users", () => {
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(charles);
|
|
|
|
people.add_active_user(maria);
|
|
|
|
people.add_active_user(ashton);
|
|
|
|
people.add_active_user(linus);
|
|
|
|
people.add_active_user(noah);
|
|
|
|
people.add_active_user(plain_noah);
|
2016-11-14 21:37:36 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const search_term = "a";
|
2022-02-08 18:56:40 +01:00
|
|
|
const users = people.get_realm_users();
|
2024-05-16 07:41:44 +02:00
|
|
|
let filtered_people = people.filter_people_by_search_terms(users, search_term);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(filtered_people.size, 2);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(filtered_people.has(ashton.user_id));
|
|
|
|
assert.ok(filtered_people.has(maria.user_id));
|
|
|
|
assert.ok(!filtered_people.has(charles.user_id));
|
2016-10-18 19:21:38 +02:00
|
|
|
|
2024-05-16 07:41:44 +02:00
|
|
|
filtered_people = people.filter_people_by_search_terms(users, "");
|
|
|
|
assert.equal(filtered_people.size, 7);
|
2016-10-18 19:21:38 +02:00
|
|
|
|
2024-05-16 07:41:44 +02:00
|
|
|
filtered_people = people.filter_people_by_search_terms(users, "ltorv");
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(filtered_people.size, 1);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(filtered_people.has(linus.user_id));
|
2016-11-14 21:37:36 +01:00
|
|
|
|
2024-05-16 07:41:44 +02:00
|
|
|
filtered_people = people.filter_people_by_search_terms(users, "ch di, maria");
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(filtered_people.size, 2);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(filtered_people.has(charles.user_id));
|
|
|
|
assert.ok(filtered_people.has(maria.user_id));
|
2016-11-14 22:34:46 +01:00
|
|
|
|
2017-06-21 03:33:30 +02:00
|
|
|
// Test filtering of names with diacritics
|
|
|
|
// This should match Nöôáàh by ignoring diacritics, and also match Nooaah
|
2024-05-16 07:41:44 +02:00
|
|
|
filtered_people = people.filter_people_by_search_terms(users, "noOa");
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(filtered_people.size, 2);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(filtered_people.has(noah.user_id));
|
|
|
|
assert.ok(filtered_people.has(plain_noah.user_id));
|
2017-06-21 03:33:30 +02:00
|
|
|
|
|
|
|
// This should match ëmerson, but not emerson
|
2024-05-16 07:41:44 +02:00
|
|
|
filtered_people = people.filter_people_by_search_terms(users, "ëm");
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(filtered_people.size, 1);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(filtered_people.has(noah.user_id));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-11-15 22:55:37 +01:00
|
|
|
|
2024-07-03 19:19:55 +02:00
|
|
|
test_people("dm_matches_search_string", () => {
|
|
|
|
people.add_active_user(charles);
|
|
|
|
people.add_active_user(maria);
|
|
|
|
people.add_active_user(ashton);
|
|
|
|
people.add_active_user(linus);
|
|
|
|
people.add_active_user(noah);
|
|
|
|
people.add_active_user(plain_noah);
|
|
|
|
|
|
|
|
let result = people.dm_matches_search_string([ashton], "a");
|
|
|
|
assert.ok(result);
|
|
|
|
// Maria's email starts with `a`.
|
|
|
|
result = people.dm_matches_search_string([maria], "a");
|
|
|
|
assert.ok(result);
|
|
|
|
// Neither Charles' full name or email start with `a`.
|
|
|
|
result = people.dm_matches_search_string([charles], "a");
|
|
|
|
assert.ok(!result);
|
|
|
|
|
|
|
|
// Empty search terms should always return true
|
|
|
|
result = people.dm_matches_search_string([charles], "");
|
|
|
|
assert.ok(result);
|
|
|
|
result = people.dm_matches_search_string([charles, maria, noah], "");
|
|
|
|
assert.ok(result);
|
|
|
|
|
|
|
|
// Match with email.
|
|
|
|
result = people.dm_matches_search_string([linus], "ltorv");
|
|
|
|
assert.ok(result);
|
|
|
|
|
|
|
|
// Test filtering of names with diacritics. This should match
|
|
|
|
// Nöôáàh by ignoring diacritics, and also match Nooaah.
|
|
|
|
result = people.dm_matches_search_string([noah], "noOa");
|
|
|
|
assert.ok(result);
|
|
|
|
result = people.dm_matches_search_string([plain_noah], "noOa");
|
|
|
|
assert.ok(result);
|
|
|
|
|
|
|
|
// This should match ëmerson, but not emerson.
|
|
|
|
result = people.dm_matches_search_string([noah], "ëm");
|
|
|
|
assert.ok(result);
|
|
|
|
result = people.dm_matches_search_string([plain_noah], "ëm");
|
|
|
|
assert.ok(!result);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("multi_user_methods", () => {
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(emp401);
|
|
|
|
people.add_active_user(emp402);
|
2016-11-15 22:55:37 +01:00
|
|
|
|
2020-05-26 18:59:34 +02:00
|
|
|
// The order of user_ids is relevant here.
|
|
|
|
assert.equal(emp401.user_id, 401);
|
|
|
|
assert.equal(emp402.user_id, 402);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let emails_string = people.user_ids_string_to_emails_string("402,401");
|
|
|
|
assert.equal(emails_string, "emp401@example.com,emp402@example.com");
|
2016-11-15 22:55:37 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
emails_string = people.slug_to_emails("402,401");
|
|
|
|
assert.equal(emails_string, "emp401@example.com,emp402@example.com");
|
2020-07-03 17:02:30 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
emails_string = people.slug_to_emails("402,401-group");
|
|
|
|
assert.equal(emails_string, "emp401@example.com,emp402@example.com");
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
emails_string = "emp402@example.com,EMP401@EXAMPLE.COM";
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids_string = people.emails_strings_to_user_ids_string(emails_string);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(user_ids_string, "401,402");
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
|
2017-04-03 19:21:18 +02:00
|
|
|
user_ids_string = people.reply_to_to_user_ids_string(emails_string);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(user_ids_string, "401,402");
|
2017-04-03 19:21:18 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const slug = people.emails_to_slug(emails_string);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(slug, "401,402-group");
|
2017-04-03 19:21:18 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(people.reply_to_to_user_ids_string("invalid@example.com"), undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
|
2022-06-02 01:36:05 +02:00
|
|
|
test_people("emails_to_full_names_string", () => {
|
|
|
|
people.add_active_user(charles);
|
|
|
|
people.add_active_user(maria);
|
|
|
|
assert.equal(
|
|
|
|
people.emails_to_full_names_string([charles.email, maria.email]),
|
|
|
|
`${charles.full_name}, ${maria.full_name}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
people.emails_to_full_names_string([
|
|
|
|
charles.email,
|
|
|
|
"unknown-email@example.com",
|
|
|
|
maria.email,
|
|
|
|
]),
|
2023-12-02 11:23:36 +01:00
|
|
|
`${charles.full_name}, translated: Unknown user, ${maria.full_name}`,
|
2022-06-02 01:36:05 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-06-11 21:45:05 +02:00
|
|
|
test_people("concat_direct_message_group", () => {
|
2020-05-26 18:46:23 +02:00
|
|
|
/*
|
|
|
|
We assume that user_ids passed in
|
2024-06-11 21:45:05 +02:00
|
|
|
to concat_direct_message_group have
|
|
|
|
already been validated, so we don't
|
|
|
|
need actual people for these tests
|
|
|
|
to pass.
|
2020-05-26 18:46:23 +02:00
|
|
|
|
|
|
|
That may change in the future.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const user_ids = [303, 301, 302];
|
|
|
|
|
2024-06-11 21:45:05 +02:00
|
|
|
assert.equal(people.concat_direct_message_group(user_ids, 304), "301,302,303,304");
|
2020-05-26 18:46:23 +02:00
|
|
|
|
bug fix: Fix sorting for group-pm edge cases.
If you have a group PM where some users have
three-digit user_ids and some with four-digit
user_ids (or similar), a huddle could effectively
be ignored when determining the order of
search search suggestions.
Basically, we need a way to canonically sort
user_ids in "huddle" strings, and it's somewhat
arbitrary whether you sort lexically or sort
numerically, but you do need to be consistent
about it.
And JS is not exactly helpful here:
> [99, 101].sort()
[ 101, 99 ]
This is a pretty obscure bug with pretty low
user-facing consequences, and it was never
reported to us as far as I know, but the fix
here is pretty straightforward.
We have had similar bugs of slightly more consequence
in the past. The reason this bug has shown
up multiple times in our codebase is that every
component that deals with huddles has slightly
different forces that determine how it wants
to serialize the huddle. It's just one of those
annoying things. Plus, bugs with group PMs
do tend to escape detection, since most people
spend most of their time either on streams
or in 1:1 PMs.
2020-05-27 01:29:50 +02:00
|
|
|
// IMPORTANT: we always want to sort
|
2024-06-11 21:45:05 +02:00
|
|
|
// ids numerically to create direct message group strings.
|
|
|
|
assert.equal(people.concat_direct_message_group(user_ids, 99), "99,301,302,303");
|
2020-05-26 18:46:23 +02:00
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("message_methods", () => {
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(charles);
|
|
|
|
people.add_active_user(maria);
|
2020-06-24 14:25:17 +02:00
|
|
|
people.add_active_user(ashton);
|
2017-02-06 20:48:01 +01:00
|
|
|
|
2020-05-26 18:59:34 +02:00
|
|
|
// We don't rely on Maria to have all flags set explicitly--
|
|
|
|
// undefined values are just treated as falsy.
|
|
|
|
assert.equal(maria.is_guest, undefined);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
people.small_avatar_url_for_person(maria),
|
2024-08-02 20:00:55 +02:00
|
|
|
"https://secure.gravatar.com/avatar/6dbdd7946b58d8b11351fcb27e5cdd55?d=identicon",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-11-06 07:23:26 +01:00
|
|
|
assert.equal(
|
|
|
|
people.medium_avatar_url_for_person(maria),
|
|
|
|
"https://secure.gravatar.com/avatar/6dbdd7946b58d8b11351fcb27e5cdd55?d=identicon&s=500",
|
|
|
|
);
|
2023-05-17 17:20:59 +02:00
|
|
|
assert.equal(people.medium_avatar_url_for_person(charles), "/avatar/301/medium?version=0");
|
|
|
|
assert.equal(people.medium_avatar_url_for_person(ashton), "/avatar/303/medium?version=0");
|
2020-05-22 21:04:03 +02:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.add_muted_user(30);
|
2023-09-06 23:25:36 +02:00
|
|
|
assert.deepEqual(people.sender_info_for_recent_view_row([30]), [
|
2020-05-22 21:04:03 +02:00
|
|
|
{
|
2024-08-02 20:00:55 +02:00
|
|
|
avatar_url_small: "/avatar/30",
|
2021-05-12 08:13:26 +02:00
|
|
|
is_muted: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "me@example.com",
|
2021-03-08 18:12:40 +01:00
|
|
|
full_name: me.full_name,
|
2020-05-22 21:04:03 +02:00
|
|
|
is_admin: false,
|
|
|
|
is_bot: false,
|
|
|
|
is_guest: false,
|
2021-04-22 21:07:51 +02:00
|
|
|
is_moderator: false,
|
2021-04-29 15:49:01 +02:00
|
|
|
role: 400,
|
2020-10-27 01:41:00 +01:00
|
|
|
timezone: "America/Los_Angeles",
|
2020-05-22 21:04:03 +02:00
|
|
|
user_id: 30,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2020-07-15 00:34:28 +02:00
|
|
|
display_recipient: [{id: maria.user_id}, {id: me.user_id}, {id: charles.user_id}],
|
2017-04-03 20:02:02 +02:00
|
|
|
sender_id: charles.user_id,
|
2017-02-06 20:48:01 +01:00
|
|
|
};
|
2023-04-11 21:04:33 +02:00
|
|
|
assert.equal(people.pm_with_url(message), "#narrow/dm/301,302-group");
|
|
|
|
assert.equal(people.pm_perma_link(message), "#narrow/dm/30,301,302-group");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(people.pm_reply_to(message), "Athens@example.com,charles@example.com");
|
2024-08-02 20:00:55 +02:00
|
|
|
assert.equal(people.small_avatar_url(message), "http://charles.com/foo.png");
|
2017-02-06 20:48:01 +01:00
|
|
|
|
|
|
|
message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2020-07-15 00:34:28 +02:00
|
|
|
display_recipient: [{id: maria.user_id}, {id: me.user_id}],
|
2020-07-15 01:29:15 +02:00
|
|
|
avatar_url: "legacy.png",
|
2017-02-06 20:48:01 +01:00
|
|
|
};
|
2023-04-11 21:04:33 +02:00
|
|
|
assert.equal(people.pm_with_url(message), "#narrow/dm/302-Maria-Athens");
|
|
|
|
assert.equal(people.pm_perma_link(message), "#narrow/dm/30,302-dm");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(people.pm_reply_to(message), "Athens@example.com");
|
2024-08-02 20:00:55 +02:00
|
|
|
assert.equal(people.small_avatar_url(message), "legacy.png");
|
2017-02-06 20:48:01 +01:00
|
|
|
|
2017-11-03 17:25:47 +01:00
|
|
|
message = {
|
|
|
|
avatar_url: undefined,
|
|
|
|
sender_id: maria.user_id,
|
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
people.small_avatar_url(message),
|
2024-08-02 20:00:55 +02:00
|
|
|
"https://secure.gravatar.com/avatar/6dbdd7946b58d8b11351fcb27e5cdd55?d=identicon",
|
2017-11-03 17:25:47 +01:00
|
|
|
);
|
|
|
|
|
2023-06-16 15:23:45 +02:00
|
|
|
blueslip.expect("error", "Unknown user_id in maybe_get_user_by_id");
|
2017-11-03 17:25:47 +01:00
|
|
|
message = {
|
|
|
|
avatar_url: undefined,
|
2020-07-15 01:29:15 +02:00
|
|
|
sender_email: "foo@example.com",
|
2017-11-03 17:25:47 +01:00
|
|
|
sender_id: 9999999,
|
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
people.small_avatar_url(message),
|
2024-08-02 20:00:55 +02:00
|
|
|
"https://secure.gravatar.com/avatar/b48def645758b95537d4424c84d1a9ff?d=identicon",
|
2017-11-03 17:25:47 +01:00
|
|
|
);
|
|
|
|
|
2020-06-24 14:25:17 +02:00
|
|
|
message = {
|
|
|
|
sender_id: ashton.user_id,
|
|
|
|
};
|
2024-08-02 20:00:55 +02:00
|
|
|
assert.equal(people.small_avatar_url(message), `/avatar/${ashton.user_id}`);
|
2020-06-24 14:25:17 +02:00
|
|
|
|
2017-02-06 20:48:01 +01:00
|
|
|
message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2020-07-15 00:34:28 +02:00
|
|
|
display_recipient: [{id: me.user_id}],
|
2017-02-06 20:48:01 +01:00
|
|
|
};
|
2023-04-11 21:04:33 +02:00
|
|
|
assert.equal(people.pm_with_url(message), "#narrow/dm/30-Me-Myself");
|
|
|
|
assert.equal(people.pm_perma_link(message), "#narrow/dm/30-dm");
|
2017-06-02 04:47:53 +02:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
message = {type: "stream"};
|
2017-06-02 04:47:53 +02:00
|
|
|
assert.equal(people.pm_with_user_ids(message), undefined);
|
2018-10-18 22:05:28 +02:00
|
|
|
assert.equal(people.all_user_ids_in_pm(message), undefined);
|
2017-06-02 04:47:53 +02:00
|
|
|
|
|
|
|
// Test undefined user_ids
|
|
|
|
assert.equal(people.pm_reply_to(message), undefined);
|
|
|
|
assert.equal(people.pm_reply_user_string(message), undefined);
|
|
|
|
assert.equal(people.pm_with_url(message), undefined);
|
|
|
|
|
|
|
|
// Test sender_is_bot
|
2020-05-26 18:59:34 +02:00
|
|
|
const bot = bot_botson;
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(bot);
|
2017-06-02 04:47:53 +02:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
message = {sender_id: bot.user_id};
|
2017-06-02 04:47:53 +02:00
|
|
|
assert.equal(people.sender_is_bot(message), true);
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
message = {sender_id: maria.user_id};
|
2017-06-02 04:47:53 +02:00
|
|
|
assert.equal(people.sender_is_bot(message), undefined);
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
message = {sender_id: undefined};
|
2017-06-02 04:47:53 +02:00
|
|
|
assert.equal(people.sender_is_bot(message), false);
|
2018-10-31 18:15:29 +01:00
|
|
|
|
|
|
|
// Test sender_is_guest
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(guest);
|
2018-10-31 18:15:29 +01:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
message = {sender_id: guest.user_id};
|
2018-10-31 18:15:29 +01:00
|
|
|
assert.equal(people.sender_is_guest(message), true);
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
message = {sender_id: maria.user_id};
|
2018-10-31 18:15:29 +01:00
|
|
|
assert.equal(people.sender_is_guest(message), undefined);
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
message = {sender_id: charles.user_id};
|
2018-10-31 18:15:29 +01:00
|
|
|
assert.equal(people.sender_is_guest(message), false);
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
message = {sender_id: undefined};
|
2018-10-31 18:15:29 +01:00
|
|
|
assert.equal(people.sender_is_guest(message), false);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-02 04:47:53 +02:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
test_people("extract_people_from_message", () => {
|
2024-07-16 20:01:26 +02:00
|
|
|
const message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2017-06-02 04:47:53 +02:00
|
|
|
sender_full_name: maria.full_name,
|
|
|
|
sender_id: maria.user_id,
|
|
|
|
sender_email: maria.email,
|
|
|
|
};
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!people.is_known_user_id(maria.user_id));
|
2017-11-06 17:03:01 +01:00
|
|
|
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "Added user late");
|
2017-06-02 04:47:53 +02:00
|
|
|
people.extract_people_from_message(message);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(people.is_known_user_id(maria.user_id));
|
2022-07-10 01:06:33 +02:00
|
|
|
blueslip.reset();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-11-06 15:48:44 +01:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("maybe_incr_recipient_count", () => {
|
2020-01-01 13:02:34 +01:00
|
|
|
const maria_recip = {
|
|
|
|
id: maria.user_id,
|
2017-11-06 15:48:44 +01:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(maria);
|
2017-11-06 15:48:44 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2020-01-01 13:02:34 +01:00
|
|
|
display_recipient: [maria_recip],
|
2017-11-06 15:48:44 +01:00
|
|
|
sent_by_me: true,
|
|
|
|
};
|
|
|
|
assert.equal(people.get_recipient_count(maria), 0);
|
|
|
|
people.maybe_incr_recipient_count(message);
|
|
|
|
assert.equal(people.get_recipient_count(maria), 1);
|
2017-06-02 04:47:53 +02:00
|
|
|
|
2017-11-06 15:48:44 +01:00
|
|
|
// Test all the no-op conditions to get test
|
|
|
|
// coverage.
|
2017-06-02 04:47:53 +02:00
|
|
|
message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2017-11-06 15:48:44 +01:00
|
|
|
sent_by_me: false,
|
2020-01-01 13:02:34 +01:00
|
|
|
display_recipient: [maria_recip],
|
2017-11-06 15:48:44 +01:00
|
|
|
};
|
|
|
|
people.maybe_incr_recipient_count(message);
|
|
|
|
assert.equal(people.get_recipient_count(maria), 1);
|
|
|
|
|
|
|
|
message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2017-11-06 15:48:44 +01:00
|
|
|
};
|
|
|
|
people.maybe_incr_recipient_count(message);
|
2017-06-02 04:47:53 +02:00
|
|
|
assert.equal(people.get_recipient_count(maria), 1);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-02-06 20:48:01 +01:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("slugs", () => {
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(debbie);
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
|
2020-05-26 18:59:34 +02:00
|
|
|
const slug = people.emails_to_slug(debbie.email);
|
2022-10-25 14:38:45 +02:00
|
|
|
assert.equal(slug, "501-Debra-Henton");
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const email = people.slug_to_emails(slug);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(email, "debbie71@example.com");
|
2017-06-02 04:47:53 +02:00
|
|
|
|
|
|
|
// Test undefined slug
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(people.emails_to_slug("does@not.exist"), undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-01-31 20:44:51 +01:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test_people("get_people_for_search_bar", ({override}) => {
|
2021-03-28 17:57:53 +02:00
|
|
|
let user_ids;
|
2021-02-24 13:30:27 +01:00
|
|
|
|
2021-03-28 17:57:53 +02:00
|
|
|
override(message_user_ids, "user_ids", () => user_ids);
|
2020-01-02 15:17:10 +01:00
|
|
|
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const i of _.range(20)) {
|
2020-01-02 15:17:10 +01:00
|
|
|
const person = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "whatever@email.com",
|
|
|
|
full_name: "James Jones",
|
2020-01-02 15:17:10 +01:00
|
|
|
user_id: 1000 + i,
|
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(person);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2020-01-02 15:17:10 +01:00
|
|
|
|
2021-03-28 17:57:53 +02:00
|
|
|
user_ids = [];
|
2020-07-15 01:29:15 +02:00
|
|
|
const big_results = people.get_people_for_search_bar("James");
|
2020-01-02 15:17:10 +01:00
|
|
|
|
|
|
|
assert.equal(big_results.length, 20);
|
|
|
|
|
2021-03-28 17:57:53 +02:00
|
|
|
user_ids = [1001, 1002, 1003, 1004, 1005, 1006];
|
2020-07-15 01:29:15 +02:00
|
|
|
const small_results = people.get_people_for_search_bar("Jones");
|
2020-01-02 15:17:10 +01:00
|
|
|
|
|
|
|
// As long as there are 5+ results among the user_ids
|
2021-03-28 17:57:53 +02:00
|
|
|
// in message_user_ids, we will get a small result and not
|
2020-03-28 01:25:56 +01:00
|
|
|
// search all people.
|
2020-01-02 15:17:10 +01:00
|
|
|
assert.equal(small_results.length, 6);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("updates", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const old_email = "FOO@example.com";
|
|
|
|
const new_email = "bar@example.com";
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = 502;
|
2017-01-31 20:44:51 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let person = {
|
2017-01-31 20:44:51 +01:00
|
|
|
email: old_email,
|
2020-07-20 22:18:43 +02:00
|
|
|
user_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Foo Barson",
|
2017-01-31 20:44:51 +01:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(person);
|
2017-01-31 20:44:51 +01:00
|
|
|
|
|
|
|
// Do sanity checks on our data.
|
|
|
|
assert.equal(people.get_by_email(old_email).user_id, user_id);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!people.is_cross_realm_email(old_email));
|
2017-01-31 20:44:51 +01:00
|
|
|
|
|
|
|
assert.equal(people.get_by_email(new_email), undefined);
|
|
|
|
|
|
|
|
// DO THE EMAIL UPDATE HERE.
|
|
|
|
people.update_email(user_id, new_email);
|
|
|
|
|
|
|
|
// Now look up using the new email.
|
|
|
|
assert.equal(people.get_by_email(new_email).user_id, user_id);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!people.is_cross_realm_email(new_email));
|
2017-01-31 20:44:51 +01:00
|
|
|
|
2019-12-23 22:42:45 +01:00
|
|
|
const all_people = get_all_persons();
|
2023-09-25 06:07:27 +02:00
|
|
|
assert.equal(all_people.length, 3);
|
2017-01-31 20:44:51 +01:00
|
|
|
|
2020-10-07 11:24:24 +02:00
|
|
|
person = all_people.find((p) => p.email === new_email);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(person.full_name, "Foo Barson");
|
2017-01-31 20:44:51 +01:00
|
|
|
|
|
|
|
// Test shim where we can still retrieve user info using the
|
|
|
|
// old email.
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.expect(
|
|
|
|
"warn",
|
2020-10-07 13:17:55 +02:00
|
|
|
"Obsolete email passed to get_by_email: FOO@example.com new email = bar@example.com",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2017-01-31 20:44:51 +01:00
|
|
|
person = people.get_by_email(old_email);
|
|
|
|
assert.equal(person.user_id, user_id);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-02-10 03:18:57 +01:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("update_email_in_reply_to", () => {
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(charles);
|
|
|
|
people.add_active_user(maria);
|
2017-02-10 03:18:57 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let reply_to = " charles@example.com, athens@example.com";
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(people.update_email_in_reply_to(reply_to, 9999, "whatever"), reply_to);
|
2017-02-10 03:18:57 +01:00
|
|
|
assert.equal(
|
2020-07-15 01:29:15 +02:00
|
|
|
people.update_email_in_reply_to(reply_to, maria.user_id, "maria@example.com"),
|
|
|
|
"charles@example.com,maria@example.com",
|
2017-02-10 03:18:57 +01:00
|
|
|
);
|
2017-06-02 04:47:53 +02:00
|
|
|
|
2023-09-25 06:01:26 +02:00
|
|
|
reply_to = " charles@example.com, athens@example.com, invalid@example.com";
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(people.update_email_in_reply_to(reply_to, 9999, "whatever"), reply_to);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-02-10 03:18:57 +01:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("track_duplicate_full_names", () => {
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(maria);
|
|
|
|
people.add_active_user(stephen1);
|
2020-02-16 14:16:46 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!people.is_duplicate_full_name("Stephen King"));
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(people.get_user_id_from_name("Stephen King"), stephen1.user_id);
|
2020-02-16 14:16:46 +01:00
|
|
|
|
|
|
|
// Now duplicate the Stephen King name.
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user({...stephen2});
|
2020-02-16 14:16:46 +01:00
|
|
|
|
|
|
|
// For duplicate names we won't try to guess which
|
|
|
|
// user_id the person means; the UI should use
|
|
|
|
// other codepaths for disambiguation.
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(people.get_user_id_from_name("Stephen King"), undefined);
|
2020-02-16 14:16:46 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(people.is_duplicate_full_name("Stephen King"));
|
|
|
|
assert.ok(!people.is_duplicate_full_name("Maria Athens"));
|
|
|
|
assert.ok(!people.is_duplicate_full_name("Some Random Name"));
|
2020-05-26 18:59:34 +02:00
|
|
|
|
|
|
|
// It is somewhat janky that we have to clone
|
|
|
|
// stephen2 here. It would be nice if people.set_full_name
|
|
|
|
// just took a user_id as the first parameter.
|
2020-07-15 01:29:15 +02:00
|
|
|
people.set_full_name({...stephen2}, "Stephen King JP");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!people.is_duplicate_full_name("Stephen King"));
|
|
|
|
assert.ok(!people.is_duplicate_full_name("Stephen King JP"));
|
2018-08-08 21:56:07 +02:00
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("get_mention_syntax", () => {
|
2024-04-03 17:38:44 +02:00
|
|
|
// blueslip warning is not raised for wildcard mentions without a user_id
|
|
|
|
assert.equal(people.get_mention_syntax("all"), "@**all**");
|
|
|
|
assert.equal(people.get_mention_syntax("everyone", undefined, true), "@_**everyone**");
|
2024-04-24 21:34:45 +02:00
|
|
|
assert.equal(people.get_mention_syntax("stream"), "@**channel**");
|
|
|
|
assert.equal(people.get_mention_syntax("channel"), "@**channel**");
|
2024-04-03 17:38:44 +02:00
|
|
|
assert.equal(people.get_mention_syntax("topic"), "@**topic**");
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(stephen1);
|
|
|
|
people.add_active_user(stephen2);
|
|
|
|
people.add_active_user(maria);
|
2018-10-13 02:25:38 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(people.is_duplicate_full_name("Stephen King"));
|
2020-05-26 18:59:34 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "get_mention_syntax called without user_id.");
|
|
|
|
assert.equal(people.get_mention_syntax("Stephen King"), "@**Stephen King**");
|
2020-05-26 18:59:34 +02:00
|
|
|
blueslip.reset();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(people.get_mention_syntax("Stephen King", 601), "@**Stephen King|601**");
|
2024-04-03 17:38:44 +02:00
|
|
|
assert.equal(people.get_mention_syntax("Stephen King", 602, true), "@_**Stephen King|602**");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(people.get_mention_syntax("Maria Athens", 603), "@**Maria Athens**");
|
2021-05-19 14:41:18 +02:00
|
|
|
|
|
|
|
// Following tests handle a special case when `full_name` matches with a wildcard.
|
|
|
|
//
|
|
|
|
// At this point, there is no duplicate full name, `all`, so we should still get
|
|
|
|
// mention syntax with `user_id` appended to it.
|
|
|
|
people.add_active_user(all1);
|
|
|
|
assert.equal(people.get_mention_syntax("all", 1202), "@**all|1202**");
|
|
|
|
|
|
|
|
people.add_active_user(all2);
|
2024-04-03 17:38:44 +02:00
|
|
|
assert.ok(people.is_duplicate_full_name("all"));
|
|
|
|
assert.equal(people.get_mention_syntax("all", 1203, true), "@_**all|1203**");
|
2018-10-13 02:25:38 +02:00
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("initialize", () => {
|
2017-06-02 04:47:53 +02:00
|
|
|
people.init();
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
const params = {};
|
|
|
|
|
|
|
|
params.realm_non_active_users = [
|
2017-11-06 14:56:06 +01:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "retiree@example.com",
|
2017-11-06 14:56:06 +01:00
|
|
|
user_id: 15,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Retiree",
|
2017-11-06 14:56:06 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
params.realm_users = [
|
2017-06-02 04:47:53 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2017-06-02 04:47:53 +02:00
|
|
|
user_id: 16,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice",
|
2017-06-02 04:47:53 +02:00
|
|
|
},
|
|
|
|
];
|
2020-02-25 12:16:26 +01:00
|
|
|
params.cross_realm_bots = [
|
2017-06-02 04:47:53 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "bot@example.com",
|
2017-06-02 04:47:53 +02:00
|
|
|
user_id: 17,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Test Bot",
|
2017-06-02 04:47:53 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
const my_user_id = 42;
|
|
|
|
people.initialize(my_user_id, params);
|
2017-06-02 04:47:53 +02:00
|
|
|
|
2017-10-26 21:00:30 +02:00
|
|
|
assert.equal(people.is_active_user_for_popover(17), true);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(people.is_cross_realm_email("bot@example.com"));
|
|
|
|
assert.ok(people.is_valid_email_for_compose("bot@example.com"));
|
|
|
|
assert.ok(people.is_valid_email_for_compose("alice@example.com"));
|
2020-07-22 13:39:38 +02:00
|
|
|
assert.ok(people.is_valid_email_for_compose("retiree@example.com"));
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!people.is_valid_email_for_compose("totally-bogus-username@example.com"));
|
|
|
|
assert.ok(people.is_valid_bulk_emails_for_compose(["bot@example.com", "alice@example.com"]));
|
|
|
|
assert.ok(!people.is_valid_bulk_emails_for_compose(["not@valid.com", "alice@example.com"]));
|
|
|
|
assert.ok(people.is_my_user_id(42));
|
2017-06-02 04:47:53 +02:00
|
|
|
|
2020-02-05 14:30:59 +01:00
|
|
|
const fetched_retiree = people.get_by_user_id(15);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(fetched_retiree.full_name, "Retiree");
|
2017-11-06 14:56:06 +01:00
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
assert.equal(page_params.realm_users, undefined);
|
|
|
|
assert.equal(page_params.cross_realm_bots, undefined);
|
|
|
|
assert.equal(page_params.realm_non_active_users, undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2019-07-11 18:54:28 +02:00
|
|
|
|
2024-04-05 19:11:54 +02:00
|
|
|
test_people("predicate_for_user_settings_filters", () => {
|
2020-01-13 17:45:53 +01:00
|
|
|
/*
|
|
|
|
This function calls matches_user_settings_search,
|
|
|
|
so that is where we do more thorough testing.
|
|
|
|
This test is just a sanity check for now.
|
|
|
|
*/
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2020-01-13 17:45:53 +01:00
|
|
|
|
2024-04-05 19:11:54 +02:00
|
|
|
const fred_smith = {full_name: "Fred Smith", role: 100};
|
2020-01-13 17:45:53 +01:00
|
|
|
|
2024-04-05 19:11:54 +02:00
|
|
|
// Test only when text_search filter is true
|
|
|
|
assert.equal(
|
|
|
|
people.predicate_for_user_settings_filters(fred_smith, {text_search: "fr", role_code: 0}),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
// Test only when role_code filter is true
|
|
|
|
assert.equal(
|
|
|
|
people.predicate_for_user_settings_filters(fred_smith, {text_search: "", role_code: 100}),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
// Test only when text_search filter is false
|
|
|
|
assert.equal(
|
|
|
|
people.predicate_for_user_settings_filters(fred_smith, {text_search: "ab", role_code: 0}),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
// Test only when role_code filter is false
|
|
|
|
assert.equal(
|
|
|
|
people.predicate_for_user_settings_filters(fred_smith, {text_search: "", role_code: 200}),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
// Test when both text_search and role_code filter are true
|
|
|
|
assert.equal(
|
|
|
|
people.predicate_for_user_settings_filters(fred_smith, {
|
|
|
|
text_search: "smi",
|
|
|
|
role_code: 100,
|
|
|
|
}),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
// Test when both text_search and role_code filter are false
|
|
|
|
assert.equal(
|
|
|
|
people.predicate_for_user_settings_filters(fred_smith, {text_search: "de", role_code: 300}),
|
|
|
|
false,
|
|
|
|
);
|
2020-01-13 17:45:53 +01:00
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("matches_user_settings_search", () => {
|
2019-12-28 17:10:17 +01:00
|
|
|
const match = people.matches_user_settings_search;
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2019-12-30 15:44:15 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(match({email: "fred@example.com"}, "fred"), false);
|
|
|
|
assert.equal(match({full_name: "Fred Smith"}, "fr"), true);
|
2019-12-30 15:44:15 +01:00
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = true;
|
2020-02-25 12:46:14 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(match({delivery_email: "fred@example.com"}, "fr"), true);
|
2021-10-26 15:43:39 +02:00
|
|
|
assert.equal(
|
|
|
|
match(
|
|
|
|
{
|
|
|
|
delivery_email: "bogus",
|
|
|
|
email: "fred@example.com",
|
|
|
|
},
|
|
|
|
"fr",
|
|
|
|
),
|
|
|
|
false,
|
|
|
|
);
|
2019-12-28 17:10:17 +01:00
|
|
|
|
2021-10-26 15:43:39 +02:00
|
|
|
assert.equal(match({delivery_email: "fred@example.com"}, "fr"), true);
|
|
|
|
assert.equal(match({email: "fred@example.com"}, "fr"), false);
|
2019-12-28 17:10:17 +01:00
|
|
|
|
|
|
|
// test normal stuff
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(match({email: "fred@example.com"}, "st"), false);
|
|
|
|
assert.equal(match({full_name: "Fred Smith"}, "st"), false);
|
|
|
|
assert.equal(match({full_name: "Joe Frederick"}, "st"), false);
|
2019-12-28 17:10:17 +01:00
|
|
|
|
2021-10-26 15:43:39 +02:00
|
|
|
assert.equal(match({delivery_email: "fred@example.com"}, "fr"), true);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(match({full_name: "Fred Smith"}, "fr"), true);
|
|
|
|
assert.equal(match({full_name: "Joe Frederick"}, "fr"), true);
|
2019-12-28 17:10:17 +01:00
|
|
|
|
|
|
|
// test in-string matches...we may want not to be so liberal
|
|
|
|
// here about matching, as it's noisy for large realms (who
|
|
|
|
// need search the most)
|
2021-10-26 15:43:39 +02:00
|
|
|
assert.equal(match({delivery_email: "fred@example.com"}, "re"), true);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(match({full_name: "Fred Smith"}, "re"), true);
|
|
|
|
assert.equal(match({full_name: "Joe Frederick"}, "re"), true);
|
2019-12-28 17:10:17 +01:00
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("is_valid_full_name_and_user_id", () => {
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!people.is_valid_full_name_and_user_id("bogus", 99));
|
|
|
|
assert.ok(!people.is_valid_full_name_and_user_id(me.full_name, 99));
|
|
|
|
assert.ok(people.is_valid_full_name_and_user_id(me.full_name, me.user_id));
|
2020-02-16 14:16:46 +01:00
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("emails_strings_to_user_ids_array", () => {
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(steven);
|
|
|
|
people.add_active_user(maria);
|
2019-07-11 18:54:28 +02:00
|
|
|
|
|
|
|
let user_ids = people.emails_strings_to_user_ids_array(`${steven.email},${maria.email}`);
|
|
|
|
assert.deepEqual(user_ids, [steven.user_id, maria.user_id]);
|
|
|
|
|
2023-07-12 07:22:30 +02:00
|
|
|
blueslip.expect("warn", "Unknown emails");
|
2020-07-15 01:29:15 +02:00
|
|
|
user_ids = people.emails_strings_to_user_ids_array("dummyuser@example.com");
|
2019-07-11 18:54:28 +02:00
|
|
|
assert.equal(user_ids, undefined);
|
|
|
|
});
|
2020-04-06 20:14:36 +02:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("get_visible_email", () => {
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(steven);
|
|
|
|
people.add_active_user(maria);
|
2020-04-06 20:14:36 +02:00
|
|
|
|
|
|
|
let email = people.get_visible_email(steven);
|
|
|
|
assert.equal(email, steven.delivery_email);
|
|
|
|
|
|
|
|
email = people.get_visible_email(maria);
|
|
|
|
assert.equal(email, maria.email);
|
|
|
|
});
|
2020-04-21 21:28:56 +02:00
|
|
|
|
2021-03-08 18:12:40 +01:00
|
|
|
test_people("get_active_message_people", () => {
|
2021-03-28 17:57:53 +02:00
|
|
|
message_user_ids.user_ids = () => [steven.user_id, maria.user_id, alice1.user_id];
|
2020-04-21 21:28:56 +02:00
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(steven);
|
|
|
|
people.add_active_user(maria);
|
|
|
|
people.add_active_user(alice1);
|
2020-04-21 21:28:56 +02:00
|
|
|
|
|
|
|
let active_message_people = people.get_active_message_people();
|
2020-05-26 18:59:34 +02:00
|
|
|
assert.deepEqual(active_message_people, [steven, maria, alice1]);
|
2020-04-21 21:28:56 +02:00
|
|
|
|
2020-05-26 18:59:34 +02:00
|
|
|
people.deactivate(alice1);
|
2020-04-21 21:28:56 +02:00
|
|
|
active_message_people = people.get_active_message_people();
|
|
|
|
assert.deepEqual(active_message_people, [steven, maria]);
|
|
|
|
});
|
2020-09-29 22:20:46 +02:00
|
|
|
|
2024-06-11 21:45:05 +02:00
|
|
|
test_people("direct_message_group_string", () => {
|
|
|
|
assert.equal(people.direct_message_group_string({type: "stream"}), undefined);
|
2021-03-19 13:48:16 +01:00
|
|
|
|
2024-06-11 21:45:05 +02:00
|
|
|
function direct_message_group(user_ids) {
|
|
|
|
return people.direct_message_group_string({
|
2021-03-19 13:48:16 +01:00
|
|
|
type: "private",
|
|
|
|
display_recipient: user_ids.map((id) => ({id})),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
people.add_active_user(maria);
|
|
|
|
people.add_active_user(bob);
|
|
|
|
|
2024-06-11 21:45:05 +02:00
|
|
|
assert.equal(direct_message_group([]), undefined);
|
|
|
|
assert.equal(direct_message_group([me.user_id, maria.user_id]), undefined);
|
|
|
|
assert.equal(direct_message_group([me.user_id, maria.user_id, bob.user_id]), "203,302");
|
2021-03-19 13:48:16 +01:00
|
|
|
});
|
|
|
|
|
2023-05-17 11:23:16 +02:00
|
|
|
test_people("get_realm_active_human_users", () => {
|
|
|
|
let humans = people.get_realm_active_human_users();
|
|
|
|
assert.equal(humans.length, 1);
|
|
|
|
assert.deepEqual(humans, [me]);
|
|
|
|
|
|
|
|
people.add_active_user(maria);
|
|
|
|
people.add_active_user(bot_botson);
|
|
|
|
humans = people.get_realm_active_human_users();
|
|
|
|
assert.equal(humans.length, 2);
|
|
|
|
assert.deepEqual(humans, [me, maria]);
|
|
|
|
|
|
|
|
people.deactivate(maria);
|
|
|
|
humans = people.get_realm_active_human_users();
|
|
|
|
assert.equal(humans.length, 1);
|
|
|
|
assert.deepEqual(humans, [me]);
|
|
|
|
});
|
|
|
|
|
2023-09-13 19:30:52 +02:00
|
|
|
test_people("should_show_guest_user_indicator", () => {
|
|
|
|
people.add_active_user(charles);
|
|
|
|
people.add_active_user(guest);
|
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_enable_guest_user_indicator = false;
|
2023-09-13 19:30:52 +02:00
|
|
|
assert.equal(people.should_add_guest_user_indicator(charles.user_id), false);
|
|
|
|
assert.equal(people.should_add_guest_user_indicator(guest.user_id), false);
|
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_enable_guest_user_indicator = true;
|
2023-09-13 19:30:52 +02:00
|
|
|
assert.equal(people.should_add_guest_user_indicator(charles.user_id), false);
|
|
|
|
assert.equal(people.should_add_guest_user_indicator(guest.user_id), true);
|
|
|
|
});
|
|
|
|
|
2023-11-30 11:11:27 +01:00
|
|
|
test_people("get_user_by_id_assert_valid", ({override}) => {
|
|
|
|
people.add_active_user(charles);
|
|
|
|
const inaccessible_user_id = 99;
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_bot_domain = "zulipdev.com";
|
2023-11-30 11:11:27 +01:00
|
|
|
override(settings_data, "user_can_access_all_other_users", () => false);
|
|
|
|
|
|
|
|
let user = people.get_user_by_id_assert_valid(inaccessible_user_id);
|
|
|
|
assert.equal(user.full_name, "translated: Unknown user");
|
|
|
|
assert.equal(user.user_id, inaccessible_user_id);
|
|
|
|
assert.ok(user.is_inaccessible_user);
|
|
|
|
assert.equal(user.email, "user99@zulipdev.com");
|
|
|
|
|
|
|
|
user = people.get_user_by_id_assert_valid(charles.user_id);
|
|
|
|
assert.equal(user.full_name, charles.full_name);
|
|
|
|
assert.ok(!user.is_inaccessible_user);
|
|
|
|
assert.equal(user.email, charles.email);
|
|
|
|
|
|
|
|
override(settings_data, "user_can_access_all_other_users", () => true);
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
people.get_user_by_id_assert_valid(199);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
message: "Unknown user_id in get_by_user_id: 199",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
user = people.get_user_by_id_assert_valid(charles.user_id);
|
|
|
|
assert.equal(user.full_name, charles.full_name);
|
|
|
|
assert.ok(!user.is_inaccessible_user);
|
|
|
|
assert.equal(user.email, charles.email);
|
|
|
|
});
|
|
|
|
|
2024-07-08 19:00:08 +02:00
|
|
|
test_people("user_can_initiate_direct_message_thread", () => {
|
|
|
|
people.add_active_user(welcome_bot);
|
|
|
|
realm.realm_direct_message_initiator_group = nobody.id;
|
|
|
|
assert.ok(!people.user_can_initiate_direct_message_thread("32"));
|
|
|
|
// Can send if only bots and self are present.
|
|
|
|
assert.ok(people.user_can_initiate_direct_message_thread("4,30"));
|
|
|
|
|
|
|
|
realm.realm_direct_message_initiator_group = everyone.id;
|
|
|
|
assert.ok(people.user_can_initiate_direct_message_thread("32"));
|
|
|
|
});
|
|
|
|
|
2020-09-29 22:20:46 +02:00
|
|
|
// reset to native Date()
|
2022-05-07 02:53:53 +02:00
|
|
|
run_test("reset MockDate", () => {
|
|
|
|
MockDate.reset();
|
|
|
|
});
|