Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

js: Convert people module to ES6.

Priyank Patel551a19c90dc3
diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.js
index 5e3afe2fd75..ee6b5f6b78b 100644
--- a/frontend_tests/node_tests/composebox_typeahead.js
+++ b/frontend_tests/node_tests/composebox_typeahead.js
@@ -783,13 +783,12 @@ run_test("initialize", () => {
assert.equal(appended_name, "Othello, the Moor of Venice");
let appended_names = [];
- people.get_by_user_id = function (user_id) {
+ people.__Rewire__("get_by_user_id", (user_id) => {
const users = {100: hamlet, 104: lear};
return users[user_id];
- };
- people.my_current_email = function () {
- return "[email protected]";
- };
+ });
+
+ people.__Rewire__("my_current_email", () => "[email protected]");
compose_pm_pill.set_from_typeahead = function (item) {
appended_names.push(item.full_name);
};
diff --git a/frontend_tests/node_tests/people.js b/frontend_tests/node_tests/people.js
index 1f871310db6..64c990eb195 100644
--- a/frontend_tests/node_tests/people.js
+++ b/frontend_tests/node_tests/people.js
@@ -737,20 +737,20 @@ run_test("extract_people_from_message", () => {
assert(!people.is_known_user_id(maria.user_id));
let reported;
- people.report_late_add = function (user_id, email) {
+ people.__Rewire__("report_late_add", (user_id, email) => {
assert.equal(user_id, maria.user_id);
assert.equal(email, maria.email);
reported = true;
- };
+ });
people.extract_people_from_message(message);
assert(people.is_known_user_id(maria.user_id));
assert(reported);
// Get line coverage
- people.report_late_add = function () {
+ people.__Rewire__("report_late_add", () => {
throw Error("unexpected late add");
- };
+ });
message = {
type: "private",
diff --git a/frontend_tests/node_tests/people_errors.js b/frontend_tests/node_tests/people_errors.js
index 98bca91ee29..a96122092da 100644
--- a/frontend_tests/node_tests/people_errors.js
+++ b/frontend_tests/node_tests/people_errors.js
@@ -104,12 +104,8 @@ run_test("blueslip", () => {
const reply_to = people.pm_reply_to(message);
assert(reply_to.includes("?"));
- people.pm_with_user_ids = function () {
- return [42];
- };
- people.get_by_user_id = function () {
- return;
- };
+ people.__Rewire__("pm_with_user_ids", () => [42]);
+ people.__Rewire__("get_by_user_id", () => {});
blueslip.expect("error", "Unknown people in message");
const uri = people.pm_with_url({});
assert.equal(uri.indexOf("unk"), uri.length - 3);
diff --git a/static/js/people.js b/static/js/people.js
index 6b282b302fe..efee16df02f 100644
--- a/static/js/people.js
+++ b/static/js/people.js
@@ -1,15 +1,13 @@
-"use strict";
+import md5 from "blueimp-md5";
+import _ from "lodash";
+import moment from "moment-timezone";
+import "unorm"; // String.prototype.normalize polyfill for IE11
-const md5 = require("blueimp-md5");
-const _ = require("lodash");
-const moment = require("moment-timezone");
+import * as typeahead from "../shared/js/typeahead";
-require("unorm"); // String.prototype.normalize polyfill for IE11
-const typeahead = require("../shared/js/typeahead");
-
-const FoldDict = require("./fold_dict").FoldDict;
-const settings_data = require("./settings_data");
-const util = require("./util");
+import {FoldDict} from "./fold_dict";
+import * as settings_data from "./settings_data";
+import * as util from "./util";
let people_dict;
let people_by_name_dict;
@@ -23,7 +21,7 @@ let my_user_id;
// We have an init() function so that our automated tests
// can easily clear data.
-exports.init = function () {
+export function init() {
// The following three dicts point to the same objects
// (all people we've seen), but people_dict can have duplicate
// keys related to email changes. We want to deprecate
@@ -42,24 +40,24 @@ exports.init = function () {
// This maintains a set of ids of people with same full names.
duplicate_full_name_data = new FoldDict();
-};
+}
// WE INITIALIZE DATA STRUCTURES HERE!
-exports.init();
+init();
function split_to_ints(lst) {
return lst.split(",").map((s) => parseInt(s, 10));
}
-exports.get_by_user_id = function (user_id, ignore_missing) {
+export function get_by_user_id(user_id, ignore_missing) {
if (!people_by_user_id_dict.has(user_id) && !ignore_missing) {
blueslip.error("Unknown user_id in get_by_user_id: " + user_id);
return;
}
return people_by_user_id_dict.get(user_id);
-};
+}
-exports.get_by_email = function (email) {
+export function get_by_email(email) {
const person = people_dict.get(email);
if (!person) {
@@ -73,9 +71,9 @@ exports.get_by_email = function (email) {
}
return person;
-};
+}
-exports.get_bot_owner_user = function (user) {
+export function get_bot_owner_user(user) {
const owner_id = user.bot_owner_id;
if (owner_id === undefined || owner_id === null) {
@@ -83,11 +81,11 @@ exports.get_bot_owner_user = function (user) {
return;
}
- return exports.get_by_user_id(owner_id);
-};
+ return get_by_user_id(owner_id);
+}
-exports.id_matches_email_operand = function (user_id, email) {
- const person = exports.get_by_email(email);
+export function id_matches_email_operand(user_id, email) {
+ const person = get_by_email(email);
if (!person) {
// The user may type bad data into the search bar, so
@@ -97,9 +95,9 @@ exports.id_matches_email_operand = function (user_id, email) {
}
return person.user_id === user_id;
-};
+}
-exports.update_email = function (user_id, new_email) {
+export function update_email(user_id, new_email) {
const person = people_by_user_id_dict.get(user_id);
person.email = new_email;
people_dict.set(new_email, person);
@@ -107,17 +105,17 @@ exports.update_email = function (user_id, new_email) {
// For legacy reasons we don't delete the old email
// keys in our dictionaries, so that reverse lookups
// still work correctly.
-};
+}
-exports.get_visible_email = function (user) {
+export function get_visible_email(user) {
if (user.delivery_email) {
return user.delivery_email;
}
return user.email;
-};
+}
-exports.get_user_id = function (email) {
- const person = exports.get_by_email(email);
+export function get_user_id(email) {
+ const person = get_by_email(email);
if (person === undefined) {
const error_msg = "Unknown email for get_user_id: " + email;
blueslip.error(error_msg);
@@ -130,9 +128,9 @@ exports.get_user_id = function (email) {
}
return user_id;
-};
+}
-exports.is_known_user_id = function (user_id) {
+export function is_known_user_id(user_id) {
/*
For certain low-stakes operations, such as emoji reactions,
we may get a user_id that we don't know about, because the
@@ -141,7 +139,7 @@ exports.is_known_user_id = function (user_id) {
expedient thing we can check.)
*/
return people_by_user_id_dict.has(user_id);
-};
+}
function sort_numerically(user_ids) {
user_ids.sort((a, b) => a - b);
@@ -149,7 +147,7 @@ function sort_numerically(user_ids) {
return user_ids;
}
-exports.huddle_string = function (message) {
+export function huddle_string(message) {
if (message.type !== "private") {
return;
}
@@ -157,7 +155,7 @@ exports.huddle_string = function (message) {
let user_ids = message.display_recipient.map((recip) => recip.id);
function is_huddle_recip(user_id) {
- return user_id && people_by_user_id_dict.has(user_id) && !exports.is_my_user_id(user_id);
+ return user_id && people_by_user_id_dict.has(user_id) && !is_my_user_id(user_id);
}
user_ids = user_ids.filter(is_huddle_recip);
@@ -169,9 +167,9 @@ exports.huddle_string = function (message) {
user_ids = sort_numerically(user_ids);
return user_ids.join(",");
-};
+}
-exports.user_ids_string_to_emails_string = function (user_ids_string) {
+export function user_ids_string_to_emails_string(user_ids_string) {
const user_ids = split_to_ints(user_ids_string);
let emails = user_ids.map((user_id) => {
@@ -189,32 +187,32 @@ exports.user_ids_string_to_emails_string = function (user_ids_string) {
emails.sort();
return emails.join(",");
-};
+}
-exports.user_ids_string_to_ids_array = function (user_ids_string) {
+export function user_ids_string_to_ids_array(user_ids_string) {
const user_ids = user_ids_string.split(",");
const ids = user_ids.map((id) => Number(id));
return ids;
-};
+}
-exports.emails_strings_to_user_ids_array = function (emails_string) {
- const user_ids_string = exports.emails_strings_to_user_ids_string(emails_string);
+export function emails_strings_to_user_ids_array(emails_string) {
+ const user_ids_string = emails_strings_to_user_ids_string(emails_string);
if (user_ids_string === undefined) {
return;
}
- const user_ids_array = exports.user_ids_string_to_ids_array(user_ids_string);
+ const user_ids_array = user_ids_string_to_ids_array(user_ids_string);
return user_ids_array;
-};
+}
-exports.reply_to_to_user_ids_string = function (emails_string) {
+export function reply_to_to_user_ids_string(emails_string) {
// This is basically emails_strings_to_user_ids_string
// without blueslip warnings, since it can be called with
// invalid data.
const emails = emails_string.split(",");
let user_ids = emails.map((email) => {
- const person = exports.get_by_email(email);
+ const person = get_by_email(email);
return person && person.user_id;
});
@@ -225,24 +223,24 @@ exports.reply_to_to_user_ids_string = function (emails_string) {
user_ids = sort_numerically(user_ids);
return user_ids.join(",");
-};
+}
-exports.get_user_time_preferences = function (user_id) {
- const user_timezone = exports.get_by_user_id(user_id).timezone;
+export function get_user_time_preferences(user_id) {
+ const user_timezone = get_by_user_id(user_id).timezone;
if (user_timezone) {
return settings_data.get_time_preferences(user_timezone);
}
-};
+}
-exports.get_user_time = function (user_id) {
- const user_pref = exports.get_user_time_preferences(user_id);
+export function get_user_time(user_id) {
+ const user_pref = get_user_time_preferences(user_id);
if (user_pref) {
return moment().tz(user_pref.timezone).format(user_pref.format);
}
-};
+}
-exports.get_user_type = function (user_id) {
- const user_profile = exports.get_by_user_id(user_id);
+export function get_user_type(user_id) {
+ const user_profile = get_by_user_id(user_id);
if (user_profile.is_owner) {
return i18n.t("Owner");
@@ -254,16 +252,16 @@ exports.get_user_type = function (user_id) {
return i18n.t("Bot");
}
return i18n.t("Member");
-};
+}
-exports.emails_strings_to_user_ids_string = function (emails_string) {
+export function emails_strings_to_user_ids_string(emails_string) {
const emails = emails_string.split(",");
- return exports.email_list_to_user_ids_string(emails);
-};
+ return email_list_to_user_ids_string(emails);
+}
-exports.email_list_to_user_ids_string = function (emails) {
+export function email_list_to_user_ids_string(emails) {
let user_ids = emails.map((email) => {
- const person = exports.get_by_email(email);
+ const person = get_by_email(email);
return person && person.user_id;
});
@@ -275,9 +273,9 @@ exports.email_list_to_user_ids_string = function (emails) {
user_ids = sort_numerically(user_ids);
return user_ids.join(",");
-};
+}
-exports.safe_full_names = function (user_ids) {
+export function safe_full_names(user_ids) {
let names = user_ids.map((user_id) => {
const person = people_by_user_id_dict.get(user_id);
return person && person.full_name;
@@ -286,39 +284,39 @@ exports.safe_full_names = function (user_ids) {
names = names.filter(Boolean);
return names.join(", ");
-};
+}
-exports.get_full_name = function (user_id) {
+export function get_full_name(user_id) {
return people_by_user_id_dict.get(user_id).full_name;
-};
+}
-exports.get_recipients = function (user_ids_string) {
+export function get_recipients(user_ids_string) {
// See message_store.get_pm_full_names() for a similar function.
const user_ids = split_to_ints(user_ids_string);
- const other_ids = user_ids.filter((user_id) => !exports.is_my_user_id(user_id));
+ const other_ids = user_ids.filter((user_id) => !is_my_user_id(user_id));
if (other_ids.length === 0) {
// private message with oneself
- return exports.my_full_name();
+ return my_full_name();
}
- const names = other_ids.map(exports.get_full_name).sort();
+ const names = other_ids.map(get_full_name).sort();
return names.join(", ");
-};
+}
-exports.pm_reply_user_string = function (message) {
- const user_ids = exports.pm_with_user_ids(message);
+export function pm_reply_user_string(message) {
+ const user_ids = pm_with_user_ids(message);
if (!user_ids) {
return;
}
return user_ids.join(",");
-};
+}
-exports.pm_reply_to = function (message) {
- const user_ids = exports.pm_with_user_ids(message);
+export function pm_reply_to(message) {
+ const user_ids = pm_with_user_ids(message);
if (!user_ids) {
return;
@@ -338,13 +336,13 @@ exports.pm_reply_to = function (message) {
const reply_to = emails.join(",");
return reply_to;
-};
+}
function sorted_other_user_ids(user_ids) {
// This excludes your own user id unless you're the only user
// (i.e. you sent a message to yourself).
- const other_user_ids = user_ids.filter((user_id) => !exports.is_my_user_id(user_id));
+ const other_user_ids = user_ids.filter((user_id) => !is_my_user_id(user_id));
if (other_user_ids.length >= 1) {
user_ids = other_user_ids;
@@ -357,7 +355,7 @@ function sorted_other_user_ids(user_ids) {
return user_ids;
}
-exports.concat_huddle = function (user_ids, user_id) {
+export function concat_huddle(user_ids, user_id) {
/*
We assume user_ids and user_id have already
been validated by the caller.
@@ -367,9 +365,9 @@ exports.concat_huddle = function (user_ids, user_id) {
*/
const sorted_ids = sort_numerically([...user_ids, user_id]);
return sorted_ids.join(",");
-};
+}
-exports.pm_lookup_key = function (user_ids_string) {
+export function pm_lookup_key(user_ids_string) {
/*
The server will sometimes include our own user id
in keys for PMs, but we only want our user id if
@@ -378,9 +376,9 @@ exports.pm_lookup_key = function (user_ids_string) {
let user_ids = split_to_ints(user_ids_string);
user_ids = sorted_other_user_ids(user_ids);
return user_ids.join(",");
-};
+}
-exports.all_user_ids_in_pm = function (message) {
+export function all_user_ids_in_pm(message) {
if (message.type !== "private") {
return;
}
@@ -394,9 +392,9 @@ exports.all_user_ids_in_pm = function (message) {
user_ids = sort_numerically(user_ids);
return user_ids;
-};
+}
-exports.pm_with_user_ids = function (message) {
+export function pm_with_user_ids(message) {
if (message.type !== "private") {
return;
}
@@ -409,9 +407,9 @@ exports.pm_with_user_ids = function (message) {
const user_ids = message.display_recipient.map((recip) => recip.id);
return sorted_other_user_ids(user_ids);
-};
+}
-exports.group_pm_with_user_ids = function (message) {
+export function group_pm_with_user_ids(message) {
if (message.type !== "private") {
return;
}
@@ -422,7 +420,7 @@ exports.group_pm_with_user_ids = function (message) {
}
const user_ids = message.display_recipient.map((recip) => recip.id);
- const is_user_present = user_ids.some((user_id) => exports.is_my_user_id(user_id));
+ const is_user_present = user_ids.some((user_id) => is_my_user_id(user_id));
if (is_user_present) {
user_ids.sort();
if (user_ids.length > 2) {
@@ -430,10 +428,10 @@ exports.group_pm_with_user_ids = function (message) {
}
}
return false;
-};
+}
-exports.pm_perma_link = function (message) {
- const user_ids = exports.all_user_ids_in_pm(message);
+export function pm_perma_link(message) {
+ const user_ids = all_user_ids_in_pm(message);
if (!user_ids) {
return;
@@ -450,10 +448,10 @@ exports.pm_perma_link = function (message) {
const slug = user_ids.join(",") + "-" + suffix;
const uri = "#narrow/pm-with/" + slug;
return uri;
-};
+}
-exports.pm_with_url = function (message) {
- const user_ids = exports.pm_with_user_ids(message);
+export function pm_with_url(message) {
+ const user_ids = pm_with_user_ids(message);
if (!user_ids) {
return;
@@ -464,7 +462,7 @@ exports.pm_with_url = function (message) {
if (user_ids.length > 1) {
suffix = "group";
} else {
- const person = exports.get_by_user_id(user_ids[0]);
+ const person = get_by_user_id(user_ids[0]);
if (person && person.email) {
suffix = person.email.split("@")[0].toLowerCase();
} else {
@@ -476,9 +474,9 @@ exports.pm_with_url = function (message) {
const slug = user_ids.join(",") + "-" + suffix;
const uri = "#narrow/pm-with/" + slug;
return uri;
-};
+}
-exports.update_email_in_reply_to = function (reply_to, user_id, new_email) {
+export function update_email_in_reply_to(reply_to, user_id, new_email) {
// We try to replace an old email with a new email in a reply_to,
// but we try to avoid changing the reply_to if we don't have to,
// and we don't warn on any errors.
@@ -504,9 +502,9 @@ exports.update_email_in_reply_to = function (reply_to, user_id, new_email) {
});
return emails.join(",");
-};
+}
-exports.pm_with_operand_ids = function (operand) {
+export function pm_with_operand_ids(operand) {
let emails = operand.split(",");
emails = emails.map((email) => email.trim());
let persons = emails.map((email) => people_dict.get(email));
@@ -525,10 +523,10 @@ exports.pm_with_operand_ids = function (operand) {
user_ids = sort_numerically(user_ids);
return user_ids;
-};
+}
-exports.emails_to_slug = function (emails_string) {
- let slug = exports.reply_to_to_user_ids_string(emails_string);
+export function emails_to_slug(emails_string) {
+ let slug = reply_to_to_user_ids_string(emails_string);
if (!slug) {
return;
@@ -545,9 +543,9 @@ exports.emails_to_slug = function (emails_string) {
}
return slug;
-};
+}
-exports.slug_to_emails = function (slug) {
+export function slug_to_emails(slug) {
/*
It's not super important to be flexible about
PM-related slugs, since you would rarely post
@@ -563,12 +561,12 @@ exports.slug_to_emails = function (slug) {
const m = /^([\d,]+)(-.*)?/.exec(slug);
if (m) {
let user_ids_string = m[1];
- user_ids_string = exports.exclude_me_from_string(user_ids_string);
- return exports.user_ids_string_to_emails_string(user_ids_string);
+ user_ids_string = exclude_me_from_string(user_ids_string);
+ return user_ids_string_to_emails_string(user_ids_string);
}
-};
+}
-exports.exclude_me_from_string = function (user_ids_string) {
+export function exclude_me_from_string(user_ids_string) {
// Exclude me from a user_ids_string UNLESS I'm the
// only one in it.
let user_ids = split_to_ints(user_ids_string);
@@ -580,62 +578,62 @@ exports.exclude_me_from_string = function (user_ids_string) {
return user_ids.join(",");
}
- user_ids = user_ids.filter((user_id) => !exports.is_my_user_id(user_id));
+ user_ids = user_ids.filter((user_id) => !is_my_user_id(user_id));
return user_ids.join(",");
-};
+}
-exports.format_small_avatar_url = function (raw_url) {
+export function format_small_avatar_url(raw_url) {
const url = raw_url + "&s=50";
return url;
-};
+}
-exports.sender_is_bot = function (message) {
+export function sender_is_bot(message) {
if (message.sender_id) {
- const person = exports.get_by_user_id(message.sender_id);
+ const person = get_by_user_id(message.sender_id);
return person.is_bot;
}
return false;
-};
+}
-exports.sender_is_guest = function (message) {
+export function sender_is_guest(message) {
if (message.sender_id) {
- const person = exports.get_by_user_id(message.sender_id);
+ const person = get_by_user_id(message.sender_id);
return person.is_guest;
}
return false;
-};
+}
function gravatar_url_for_email(email) {
const hash = md5(email.toLowerCase());
const avatar_url = "https://secure.gravatar.com/avatar/" + hash + "?d=identicon";
- const small_avatar_url = exports.format_small_avatar_url(avatar_url);
+ const small_avatar_url = format_small_avatar_url(avatar_url);
return small_avatar_url;
}
-exports.small_avatar_url_for_person = function (person) {
+export function small_avatar_url_for_person(person) {
if (person.avatar_url) {
- return exports.format_small_avatar_url(person.avatar_url);
+ return format_small_avatar_url(person.avatar_url);
}
if (person.avatar_url === null) {
return gravatar_url_for_email(person.email);
}
- return exports.format_small_avatar_url("/avatar/" + person.user_id);
-};
+ return format_small_avatar_url("/avatar/" + person.user_id);
+}
-exports.sender_info_with_small_avatar_urls_for_sender_ids = function (sender_ids) {
+export function sender_info_with_small_avatar_urls_for_sender_ids(sender_ids) {
const senders_info = [];
for (const id of sender_ids) {
- const sender = {...exports.get_by_user_id(id)};
- sender.avatar_url_small = exports.small_avatar_url_for_person(sender);
+ const sender = {...get_by_user_id(id)};
+ sender.avatar_url_small = small_avatar_url_for_person(sender);
senders_info.push(sender);
}
return senders_info;
-};
+}
-exports.small_avatar_url = function (message) {
+export function small_avatar_url(message) {
// Try to call this function in all places where we need 25px
// avatar images, so that the browser can help
// us avoid unnecessary network trips. (For user-uploaded avatars,
@@ -649,20 +647,20 @@ exports.small_avatar_url = function (message) {
// We should always have message.sender_id, except for in the
// tutorial, where it's ok to fall back to the url in the fake
// messages.
- person = exports.get_by_user_id(message.sender_id);
+ person = get_by_user_id(message.sender_id);
}
// The first time we encounter a sender in a message, we may
// not have person.avatar_url set, but if we do, then use that.
if (person && person.avatar_url) {
- return exports.small_avatar_url_for_person(person);
+ return small_avatar_url_for_person(person);
}
// Try to get info from the message if we didn't have a `person` object
// or if the avatar was missing. We do this verbosely to avoid false
// positives on line coverage (we don't do branch checking).
if (message.avatar_url) {
- return exports.format_small_avatar_url(message.avatar_url);
+ return format_small_avatar_url(message.avatar_url);
}
if (person && person.avatar_url === undefined) {
@@ -671,7 +669,7 @@ exports.small_avatar_url = function (message) {
// required to take advantage of the user_avatar_url_field_optional
// optimization, which saves a huge amount of network traffic on
// servers with 10,000s of user accounts.
- return exports.format_small_avatar_url("/avatar/" + person.user_id);
+ return format_small_avatar_url("/avatar/" + person.user_id);
}
// For computing the user's email, we first trust the person
@@ -685,31 +683,31 @@ exports.small_avatar_url = function (message) {
}
return gravatar_url_for_email(email);
-};
+}
-exports.is_valid_email_for_compose = function (email) {
- if (exports.is_cross_realm_email(email)) {
+export function is_valid_email_for_compose(email) {
+ if (is_cross_realm_email(email)) {
return true;
}
- const person = exports.get_by_email(email);
+ const person = get_by_email(email);
if (!person) {
return false;
}
return active_user_dict.has(person.user_id);
-};
+}
-exports.is_valid_bulk_emails_for_compose = function (emails) {
+export function is_valid_bulk_emails_for_compose(emails) {
// Returns false if at least one of the emails is invalid.
return emails.every((email) => {
- if (!exports.is_valid_email_for_compose(email)) {
+ if (!is_valid_email_for_compose(email)) {
return false;
}
return true;
});
-};
+}
-exports.is_active_user_for_popover = function (user_id) {
+export function is_active_user_for_popover(user_id) {
// For popover menus, we include cross-realm bots as active
// users.
@@ -727,9 +725,9 @@ exports.is_active_user_for_popover = function (user_id) {
}
return false;
-};
+}
-exports.filter_all_persons = function (pred) {
+export function filter_all_persons(pred) {
const ret = [];
for (const person of people_by_user_id_dict.values()) {
if (pred(person)) {
@@ -737,9 +735,9 @@ exports.filter_all_persons = function (pred) {
}
}
return ret;
-};
+}
-exports.filter_all_users = function (pred) {
+export function filter_all_users(pred) {
const ret = [];
for (const person of active_user_dict.values()) {
if (pred(person)) {
@@ -747,14 +745,14 @@ exports.filter_all_users = function (pred) {
}
}
return ret;
-};
+}
-exports.get_realm_users = function () {
+export function get_realm_users() {
// includes humans and bots from your realm
return Array.from(active_user_dict.values());
-};
+}
-exports.get_active_human_ids = function () {
+export function get_active_human_ids() {
const human_ids = [];
for (const user of active_user_dict.values()) {
@@ -764,9 +762,9 @@ exports.get_active_human_ids = function () {
}
return human_ids;
-};
+}
-exports.get_non_active_human_ids = function () {
+export function get_non_active_human_ids() {
const human_ids = [];
for (const user of non_active_user_dict.values()) {
@@ -776,9 +774,9 @@ exports.get_non_active_human_ids = function () {
}
return human_ids;
-};
+}
-exports.get_active_human_count = function () {
+export function get_active_human_count() {
let count = 0;
for (const person of active_user_dict.values()) {
if (!person.is_bot) {
@@ -786,26 +784,26 @@ exports.get_active_human_count = function () {
}
}
return count;
-};
+}
-exports.get_active_user_ids = function () {
+export function get_active_user_ids() {
// This includes active users and active bots.
return Array.from(active_user_dict.keys());
-};
+}
-exports.get_non_active_realm_users = function () {
+export function get_non_active_realm_users() {
return Array.from(non_active_user_dict.values());
-};
+}
-exports.is_cross_realm_email = function (email) {
- const person = exports.get_by_email(email);
+export function is_cross_realm_email(email) {
+ const person = get_by_email(email);
if (!person) {
return;
}
return cross_realm_dict.has(person.user_id);
-};
+}
-exports.get_recipient_count = function (person) {
+export function get_recipient_count(person) {
// We can have fake person objects like the "all"
// pseudo-person in at-mentions. They will have
// the pm_recipient_count on the object itself.
@@ -829,14 +827,14 @@ exports.get_recipient_count = function (person) {
const count = pm_recipient_count_dict.get(person.user_id);
return count || 0;
-};
+}
-exports.incr_recipient_count = function (user_id) {
+export function incr_recipient_count(user_id) {
const old_count = pm_recipient_count_dict.get(user_id) || 0;
pm_recipient_count_dict.set(user_id, old_count + 1);
-};
+}
-exports.get_message_people = function () {
+export function get_message_people() {
/*
message_people are roughly the people who have
actually sent messages that are currently
@@ -857,20 +855,20 @@ exports.get_message_people = function () {
.filter(Boolean);
return message_people;
-};
+}
-exports.get_active_message_people = function () {
- const message_people = exports.get_message_people();
+export function get_active_message_people() {
+ const message_people = get_message_people();
const active_message_people = message_people.filter((item) =>
active_user_dict.has(item.user_id),
);
return active_message_people;
-};
+}
-exports.get_people_for_search_bar = function (query) {
- const pred = exports.build_person_matcher(query);
+export function get_people_for_search_bar(query) {
+ const pred = build_person_matcher(query);
- const message_people = exports.get_message_people();
+ const message_people = get_message_people();
const small_results = message_people.filter(pred);
@@ -878,10 +876,10 @@ exports.get_people_for_search_bar = function (query) {
return small_results;
}
- return exports.filter_all_persons(pred);
-};
+ return filter_all_persons(pred);
+}
-exports.build_termlet_matcher = function (termlet) {
+export function build_termlet_matcher(termlet) {
termlet = termlet.trim();
const is_ascii = /^[a-z]+$/.test(termlet);
@@ -896,13 +894,13 @@ exports.build_termlet_matcher = function (termlet) {
return names.some((name) => name.startsWith(termlet));
};
-};
+}
-exports.build_person_matcher = function (query) {
+export function build_person_matcher(query) {
query = query.trim();
const termlets = query.toLowerCase().split(/\s+/);
- const termlet_matchers = termlets.map(exports.build_termlet_matcher);
+ const termlet_matchers = termlets.map(build_termlet_matcher);
return function (user) {
const email = user.email.toLowerCase();
@@ -913,19 +911,19 @@ exports.build_person_matcher = function (query) {
return termlet_matchers.every((matcher) => matcher(user));
};
-};
+}
-exports.filter_people_by_search_terms = function (users, search_terms) {
+export function filter_people_by_search_terms(users, search_terms) {
const filtered_users = new Map();
// Build our matchers outside the loop to avoid some
// search overhead that is not user-specific.
- const matchers = search_terms.map((search_term) => exports.build_person_matcher(search_term));
+ const matchers = search_terms.map((search_term) => build_person_matcher(search_term));
// Loop through users and populate filtered_users only
// if they include search_terms
for (const user of users) {
- const person = exports.get_by_email(user.email);
+ const person = get_by_email(user.email);
// Get person object (and ignore errors)
if (!person || !person.full_name) {
continue;
@@ -940,9 +938,9 @@ exports.filter_people_by_search_terms = function (users, search_terms) {
}
return filtered_users;
-};
+}
-exports.is_valid_full_name_and_user_id = (full_name, user_id) => {
+export const is_valid_full_name_and_user_id = (full_name, user_id) => {
const person = people_by_user_id_dict.get(user_id);
if (!person) {
@@ -952,7 +950,7 @@ exports.is_valid_full_name_and_user_id = (full_name, user_id) => {
return person.full_name === full_name;
};
-exports.get_actual_name_from_user_id = (user_id) => {
+export const get_actual_name_from_user_id = (user_id) => {
/*
If you are dealing with user-entered data, you
should validate the user_id BEFORE calling
@@ -968,7 +966,7 @@ exports.get_actual_name_from_user_id = (user_id) => {
return person.full_name;
};
-exports.get_user_id_from_name = function (full_name) {
+export function get_user_id_from_name(full_name) {
// get_user_id_from_name('Alice Smith') === 42
/*
@@ -990,12 +988,12 @@ exports.get_user_id_from_name = function (full_name) {
return;
}
- if (exports.is_duplicate_full_name(full_name)) {
+ if (is_duplicate_full_name(full_name)) {
return;
}
return person.user_id;
-};
+}
function people_cmp(person1, person2) {
const name_cmp = util.strcmp(person1.full_name, person2.full_name);
@@ -1007,7 +1005,7 @@ function people_cmp(person1, person2) {
return util.strcmp(person1.email, person2.email);
}
-exports.get_people_for_stream_create = function () {
+export function get_people_for_stream_create() {
/*
If you are thinking of reusing this function,
a better option in most cases is to just
@@ -1021,7 +1019,7 @@ exports.get_people_for_stream_create = function () {
*/
const people_minus_you = [];
for (const person of active_user_dict.values()) {
- if (!exports.is_my_user_id(person.user_id)) {
+ if (!is_my_user_id(person.user_id)) {
people_minus_you.push({
email: person.email,
user_id: person.user_id,
@@ -1030,9 +1028,9 @@ exports.get_people_for_stream_create = function () {
}
}
return people_minus_you.sort(people_cmp);
-};
+}
-exports.track_duplicate_full_name = function (full_name, user_id, to_remove) {
+export function track_duplicate_full_name(full_name, user_id, to_remove) {
let ids;
if (duplicate_full_name_data.has(full_name)) {
ids = duplicate_full_name_data.get(full_name);
@@ -1046,15 +1044,15 @@ exports.track_duplicate_full_name = function (full_name, user_id, to_remove) {
ids.delete(user_id);
}
duplicate_full_name_data.set(full_name, ids);
-};
+}
-exports.is_duplicate_full_name = function (full_name) {
+export function is_duplicate_full_name(full_name) {
const ids = duplicate_full_name_data.get(full_name);
return ids && ids.size > 1;
-};
+}
-exports.get_mention_syntax = function (full_name, user_id, silent) {
+export function get_mention_syntax(full_name, user_id, silent) {
let mention = "";
if (silent) {
mention += "@_**";
@@ -1065,14 +1063,14 @@ exports.get_mention_syntax = function (full_name, user_id, silent) {
if (!user_id) {
blueslip.warn("get_mention_syntax called without user_id.");
}
- if (exports.is_duplicate_full_name(full_name) && user_id) {
+ if (is_duplicate_full_name(full_name) && user_id) {
mention += "|" + user_id;
}
mention += "**";
return mention;
-};
+}
-exports._add_user = function add(person) {
+export function _add_user(person) {
/*
This is common code to add any user, even
users who may be deactivated or outside
@@ -1090,18 +1088,18 @@ exports._add_user = function add(person) {
blueslip.warn("No user_id provided for " + person.email);
}
- exports.track_duplicate_full_name(person.full_name, person.user_id);
+ track_duplicate_full_name(person.full_name, person.user_id);
people_dict.set(person.email, person);
people_by_name_dict.set(person.full_name, person);
-};
+}
-exports.add_active_user = function (person) {
+export function add_active_user(person) {
active_user_dict.set(person.user_id, person);
- exports._add_user(person);
+ _add_user(person);
non_active_user_dict.delete(person.user_id);
-};
+}
-exports.is_person_active = (user_id) => {
+export const is_person_active = (user_id) => {
if (!people_by_user_id_dict.has(user_id)) {
blueslip.error("No user found.", user_id);
}
@@ -1109,22 +1107,22 @@ exports.is_person_active = (user_id) => {
return active_user_dict.has(user_id);
};
-exports.add_cross_realm_user = function (person) {
+export function add_cross_realm_user(person) {
if (!people_dict.has(person.email)) {
- exports._add_user(person);
+ _add_user(person);
}
cross_realm_dict.set(person.user_id, person);
-};
+}
-exports.deactivate = function (person) {
+export function deactivate(person) {
// We don't fully remove a person from all of our data
// structures, because deactivated users can be part
// of somebody's PM list.
active_user_dict.delete(person.user_id);
non_active_user_dict.set(person.user_id, person);
-};
+}
-exports.report_late_add = function (user_id, email) {
+export function report_late_add(user_id, email) {
// This function is extracted to make unit testing easier,
// plus we may fine-tune our reporting here for different
// types of realms.
@@ -1135,9 +1133,9 @@ exports.report_late_add = function (user_id, email) {
} else {
blueslip.error(msg);
}
-};
+}
-exports.extract_people_from_message = function (message) {
+export function extract_people_from_message(message) {
let involved_people;
switch (message.type) {
@@ -1171,9 +1169,9 @@ exports.extract_people_from_message = function (message) {
continue;
}
- exports.report_late_add(user_id, person.email);
+ report_late_add(user_id, person.email);
- exports._add_user({
+ _add_user({
email: person.email,
user_id,
full_name: person.full_name,
@@ -1181,19 +1179,19 @@ exports.extract_people_from_message = function (message) {
is_bot: person.is_bot || false,
});
}
-};
+}
function safe_lower(s) {
return (s || "").toLowerCase();
}
-exports.matches_user_settings_search = function (person, value) {
+export function matches_user_settings_search(person, value) {
const email = settings_data.email_for_user_settings(person);
return safe_lower(person.full_name).includes(value) || safe_lower(email).includes(value);
-};
+}
-exports.filter_for_user_settings_search = function (persons, query) {
+export function filter_for_user_settings_search(persons, query) {
/*
TODO: For large realms, we can optimize this a couple
different ways. For realms that don't show
@@ -1205,10 +1203,10 @@ exports.filter_for_user_settings_search = function (persons, query) {
See #13554 for more context.
*/
- return persons.filter((person) => exports.matches_user_settings_search(person, query));
-};
+ return persons.filter((person) => matches_user_settings_search(person, query));
+}
-exports.maybe_incr_recipient_count = function (message) {
+export function maybe_incr_recipient_count(message) {
if (message.type !== "private") {
return;
}
@@ -1224,22 +1222,22 @@ exports.maybe_incr_recipient_count = function (message) {
}
const user_id = recip.id;
- exports.incr_recipient_count(user_id);
+ incr_recipient_count(user_id);
}
-};
+}
-exports.set_full_name = function (person_obj, new_full_name) {
+export function set_full_name(person_obj, new_full_name) {
if (people_by_name_dict.has(person_obj.full_name)) {
people_by_name_dict.delete(person_obj.full_name);
}
// Remove previous and add new full name to the duplicate full name tracker.
- exports.track_duplicate_full_name(person_obj.full_name, person_obj.user_id, true);
- exports.track_duplicate_full_name(new_full_name, person_obj.user_id);
+ track_duplicate_full_name(person_obj.full_name, person_obj.user_id, true);
+ track_duplicate_full_name(new_full_name, person_obj.user_id);
people_by_name_dict.set(new_full_name, person_obj);
person_obj.full_name = new_full_name;
-};
+}
-exports.set_custom_profile_field_data = function (user_id, field) {
+export function set_custom_profile_field_data(user_id, field) {
if (field.id === undefined) {
blueslip.error("Trying to set undefined field id");
return;
@@ -1248,49 +1246,49 @@ exports.set_custom_profile_field_data = function (user_id, field) {
value: field.value,
rendered_value: field.rendered_value,
};
-};
+}
-exports.is_current_user = function (email) {
+export function is_current_user(email) {
if (email === null || email === undefined) {
return false;
}
- return email.toLowerCase() === exports.my_current_email().toLowerCase();
-};
+ return email.toLowerCase() === my_current_email().toLowerCase();
+}
-exports.initialize_current_user = function (user_id) {
+export function initialize_current_user(user_id) {
my_user_id = user_id;
-};
+}
-exports.my_full_name = function () {
+export function my_full_name() {
return people_by_user_id_dict.get(my_user_id).full_name;
-};
+}
-exports.my_current_email = function () {
+export function my_current_email() {
return people_by_user_id_dict.get(my_user_id).email;
-};
+}
-exports.my_current_user_id = function () {
+export function my_current_user_id() {
return my_user_id;
-};
+}
-exports.my_custom_profile_data = function (field_id) {
+export function my_custom_profile_data(field_id) {
if (field_id === undefined) {
blueslip.error("Undefined field id");
return;
}
- return exports.get_custom_profile_data(my_user_id, field_id);
-};
+ return get_custom_profile_data(my_user_id, field_id);
+}
-exports.get_custom_profile_data = function (user_id, field_id) {
+export function get_custom_profile_data(user_id, field_id) {
const profile_data = people_by_user_id_dict.get(user_id).profile_data;
if (profile_data === undefined) {
return null;
}
return profile_data[field_id];
-};
+}
-exports.is_my_user_id = function (user_id) {
+export function is_my_user_id(user_id) {
if (!user_id) {
return false;
}
@@ -1301,21 +1299,21 @@ exports.is_my_user_id = function (user_id) {
}
return user_id === my_user_id;
-};
+}
-exports.initialize = function (my_user_id, params) {
+export function initialize(my_user_id, params) {
for (const person of params.realm_users) {
- exports.add_active_user(person);
+ add_active_user(person);
}
for (const person of params.realm_non_active_users) {
non_active_user_dict.set(person.user_id, person);
- exports._add_user(person);
+ _add_user(person);
}
for (const person of params.cross_realm_bots) {
- exports.add_cross_realm_user(person);
+ add_cross_realm_user(person);
}
- exports.initialize_current_user(my_user_id);
-};
+ initialize_current_user(my_user_id);
+}