js: Convert static/js/buddy_data.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-27 16:12:35 -08:00 committed by Tim Abbott
parent 818b5aacb6
commit 170905c065
14 changed files with 59 additions and 63 deletions

View File

@ -130,7 +130,6 @@
"avatar": false, "avatar": false,
"blueslip": false, "blueslip": false,
"bot_data": false, "bot_data": false,
"buddy_data": false,
"buddy_list": false, "buddy_list": false,
"compose_actions": false, "compose_actions": false,
"compose_ui": false, "compose_ui": false,

View File

@ -645,7 +645,7 @@ test_ui("update_presence_info", () => {
}, },
}; };
buddy_data.matches_filter = () => true; buddy_data.__Rewire__("matches_filter", () => true);
const alice_li = $.create("alice stub"); const alice_li = $.create("alice stub");
buddy_list_add(alice.user_id, alice_li); buddy_list_add(alice.user_id, alice_li);

View File

@ -280,7 +280,7 @@ run_test("bulk_data_hacks", () => {
// Make our shrink limit higher, and go back to an empty search. // Make our shrink limit higher, and go back to an empty search.
// We won't get all 1000 users, just the present ones. // We won't get all 1000 users, just the present ones.
buddy_data.max_size_before_shrinking = 50000; buddy_data.__Rewire__("max_size_before_shrinking", 50000);
user_ids = buddy_data.get_filtered_and_sorted_user_ids(""); user_ids = buddy_data.get_filtered_and_sorted_user_ids("");
assert.equal(user_ids.length, 700); assert.equal(user_ids.length, 700);

View File

@ -20,7 +20,6 @@ rewiremock("../../static/js/message_viewport").with({
rewiremock.enable(); rewiremock.enable();
const people = zrequire("people"); const people = zrequire("people");
zrequire("buddy_data");
const buddy_list = zrequire("buddy_list"); const buddy_list = zrequire("buddy_list");
zrequire("ui"); zrequire("ui");

View File

@ -30,7 +30,6 @@ rewiremock("../../static/js/pm_list_dom").with(pm_list_dom);
rewiremock.enable(); rewiremock.enable();
zrequire("buddy_data");
const people = zrequire("people"); const people = zrequire("people");
const pm_conversations = zrequire("pm_conversations"); const pm_conversations = zrequire("pm_conversations");
const pm_list = zrequire("pm_list"); const pm_list = zrequire("pm_list");

View File

@ -48,7 +48,6 @@ rewiremock.enable();
zrequire("narrow"); zrequire("narrow");
const people = zrequire("people"); const people = zrequire("people");
zrequire("buddy_data");
const user_status = zrequire("user_status"); const user_status = zrequire("user_status");
const message_edit = zrequire("message_edit"); const message_edit = zrequire("message_edit");

View File

@ -2,6 +2,7 @@
const _ = require("lodash"); const _ = require("lodash");
const buddy_data = require("./buddy_data");
const channel = require("./channel"); const channel = require("./channel");
const keydown_util = require("./keydown_util"); const keydown_util = require("./keydown_util");
const {ListCursor} = require("./list_cursor"); const {ListCursor} = require("./list_cursor");

View File

@ -1,11 +1,10 @@
"use strict"; import * as compose_fade from "./compose_fade";
import * as hash_util from "./hash_util";
import * as people from "./people";
import * as presence from "./presence";
import * as user_status from "./user_status";
import * as util from "./util";
const compose_fade = require("./compose_fade");
const hash_util = require("./hash_util");
const people = require("./people");
const presence = require("./presence");
const user_status = require("./user_status");
const util = require("./util");
/* /*
This is the main model code for building the buddy list. This is the main model code for building the buddy list.
@ -15,7 +14,7 @@ const util = require("./util");
*/ */
exports.max_size_before_shrinking = 600; export const max_size_before_shrinking = 600;
const fade_config = { const fade_config = {
get_user_id(item) { get_user_id(item) {
@ -29,8 +28,8 @@ const fade_config = {
}, },
}; };
exports.get_user_circle_class = function (user_id) { export function get_user_circle_class(user_id) {
const status = exports.buddy_status(user_id); const status = buddy_status(user_id);
switch (status) { switch (status) {
case "active": case "active":
@ -43,10 +42,10 @@ exports.get_user_circle_class = function (user_id) {
default: default:
return "user_circle_empty"; return "user_circle_empty";
} }
}; }
exports.status_description = function (user_id) { export function status_description(user_id) {
const status = exports.buddy_status(user_id); const status = buddy_status(user_id);
switch (status) { switch (status) {
case "active": case "active":
@ -59,15 +58,15 @@ exports.status_description = function (user_id) {
default: default:
return i18n.t("Offline"); return i18n.t("Offline");
} }
}; }
exports.level = function (user_id) { export function level(user_id) {
if (people.is_my_user_id(user_id)) { if (people.is_my_user_id(user_id)) {
// Always put current user at the top. // Always put current user at the top.
return 0; return 0;
} }
const status = exports.buddy_status(user_id); const status = buddy_status(user_id);
switch (status) { switch (status) {
case "active": case "active":
@ -79,9 +78,9 @@ exports.level = function (user_id) {
default: default:
return 3; return 3;
} }
}; }
exports.buddy_status = function (user_id) { export function buddy_status(user_id) {
if (user_status.is_away(user_id)) { if (user_status.is_away(user_id)) {
if (people.is_my_user_id(user_id)) { if (people.is_my_user_id(user_id)) {
return "away_me"; return "away_me";
@ -92,11 +91,11 @@ exports.buddy_status = function (user_id) {
// get active/idle/etc. // get active/idle/etc.
return presence.get_status(user_id); return presence.get_status(user_id);
}; }
exports.compare_function = function (a, b) { export function compare_function(a, b) {
const level_a = exports.level(a); const level_a = level(a);
const level_b = exports.level(b); const level_b = level(b);
const diff = level_a - level_b; const diff = level_a - level_b;
if (diff !== 0) { if (diff !== 0) {
return diff; return diff;
@ -110,13 +109,13 @@ exports.compare_function = function (a, b) {
const full_name_b = person_b ? person_b.full_name : ""; const full_name_b = person_b ? person_b.full_name : "";
return util.strcmp(full_name_a, full_name_b); return util.strcmp(full_name_a, full_name_b);
}; }
exports.sort_users = function (user_ids) { export function sort_users(user_ids) {
// TODO sort by unread count first, once we support that // TODO sort by unread count first, once we support that
user_ids.sort(exports.compare_function); user_ids.sort(compare_function);
return user_ids; return user_ids;
}; }
function filter_user_ids(user_filter_text, user_ids) { function filter_user_ids(user_filter_text, user_ids) {
if (user_filter_text === "") { if (user_filter_text === "") {
@ -134,17 +133,17 @@ function filter_user_ids(user_filter_text, user_ids) {
return Array.from(user_id_dict.keys()); return Array.from(user_id_dict.keys());
} }
exports.matches_filter = function (user_filter_text, user_id) { export function matches_filter(user_filter_text, user_id) {
// This is a roundabout way of checking a user if you look // This is a roundabout way of checking a user if you look
// too hard at it, but it should be fine for now. // too hard at it, but it should be fine for now.
return filter_user_ids(user_filter_text, [user_id]).length === 1; return filter_user_ids(user_filter_text, [user_id]).length === 1;
}; }
function get_num_unread(user_id) { function get_num_unread(user_id) {
return unread.num_unread_for_person(user_id.toString()); return unread.num_unread_for_person(user_id.toString());
} }
exports.get_my_user_status = function (user_id) { export function get_my_user_status(user_id) {
if (!people.is_my_user_id(user_id)) { if (!people.is_my_user_id(user_id)) {
return undefined; return undefined;
} }
@ -154,9 +153,9 @@ exports.get_my_user_status = function (user_id) {
} }
return i18n.t("(you)"); return i18n.t("(you)");
}; }
exports.user_last_seen_time_status = function (user_id) { export function user_last_seen_time_status(user_id) {
const status = presence.get_status(user_id); const status = presence.get_status(user_id);
if (status === "active") { if (status === "active") {
return i18n.t("Active now"); return i18n.t("Active now");
@ -178,13 +177,13 @@ exports.user_last_seen_time_status = function (user_id) {
return i18n.t("More than 2 weeks ago"); return i18n.t("More than 2 weeks ago");
} }
return timerender.last_seen_status_from_date(last_active_date); return timerender.last_seen_status_from_date(last_active_date);
}; }
exports.info_for = function (user_id) { export function info_for(user_id) {
const user_circle_class = exports.get_user_circle_class(user_id); const user_circle_class = get_user_circle_class(user_id);
const person = people.get_by_user_id(user_id); const person = people.get_by_user_id(user_id);
const my_user_status = exports.get_my_user_status(user_id); const my_user_status = get_my_user_status(user_id);
const user_circle_status = exports.status_description(user_id); const user_circle_status = status_description(user_id);
return { return {
href: hash_util.pm_with_uri(person.email), href: hash_util.pm_with_uri(person.email),
@ -196,7 +195,7 @@ exports.info_for = function (user_id) {
user_circle_class, user_circle_class,
user_circle_status, user_circle_status,
}; };
}; }
function get_last_seen(active_status, last_seen) { function get_last_seen(active_status, last_seen) {
if (active_status === "active") { if (active_status === "active") {
@ -207,7 +206,7 @@ function get_last_seen(active_status, last_seen) {
return last_seen_text; return last_seen_text;
} }
exports.get_title_data = function (user_ids_string, is_group) { export function get_title_data(user_ids_string, is_group) {
if (is_group === true) { if (is_group === true) {
// For groups, just return a string with recipient names. // For groups, just return a string with recipient names.
return { return {
@ -245,7 +244,7 @@ exports.get_title_data = function (user_ids_string, is_group) {
// For buddy list and individual PMS. Since is_group=False, it's // For buddy list and individual PMS. Since is_group=False, it's
// a single, human, user. // a single, human, user.
const active_status = presence.get_status(user_id); const active_status = presence.get_status(user_id);
const last_seen = exports.user_last_seen_time_status(user_id); const last_seen = user_last_seen_time_status(user_id);
// Users has a status. // Users has a status.
if (user_status.get_status_text(user_id)) { if (user_status.get_status_text(user_id)) {
@ -262,21 +261,21 @@ exports.get_title_data = function (user_ids_string, is_group) {
second_line: get_last_seen(active_status, last_seen), second_line: get_last_seen(active_status, last_seen),
third_line: "", third_line: "",
}; };
}; }
exports.get_item = function (user_id) { export function get_item(user_id) {
const info = exports.info_for(user_id); const info = info_for(user_id);
compose_fade.update_user_info([info], fade_config); compose_fade.update_user_info([info], fade_config);
return info; return info;
}; }
function user_is_recently_active(user_id) { function user_is_recently_active(user_id) {
// return true if the user has a green/orange circle // return true if the user has a green/orange circle
return exports.level(user_id) <= 2; return level(user_id) <= 2;
} }
function maybe_shrink_list(user_ids, user_filter_text) { function maybe_shrink_list(user_ids, user_filter_text) {
if (user_ids.length <= exports.max_size_before_shrinking) { if (user_ids.length <= max_size_before_shrinking) {
return user_ids; return user_ids;
} }
@ -323,20 +322,20 @@ function get_user_id_list(user_filter_text) {
return user_ids; return user_ids;
} }
exports.get_filtered_and_sorted_user_ids = function (user_filter_text) { export function get_filtered_and_sorted_user_ids(user_filter_text) {
let user_ids; let user_ids;
user_ids = get_user_id_list(user_filter_text); user_ids = get_user_id_list(user_filter_text);
user_ids = maybe_shrink_list(user_ids, user_filter_text); user_ids = maybe_shrink_list(user_ids, user_filter_text);
return exports.sort_users(user_ids); return sort_users(user_ids);
}; }
exports.get_items_for_users = function (user_ids) { export function get_items_for_users(user_ids) {
const user_info = user_ids.map((user_id) => exports.info_for(user_id)); const user_info = user_ids.map((user_id) => info_for(user_id));
compose_fade.update_user_info(user_info, fade_config); compose_fade.update_user_info(user_info, fade_config);
return user_info; return user_info;
}; }
exports.huddle_fraction_present = function (huddle) { export function huddle_fraction_present(huddle) {
const user_ids = huddle.split(",").map((s) => Number.parseInt(s, 10)); const user_ids = huddle.split(",").map((s) => Number.parseInt(s, 10));
let num_present = 0; let num_present = 0;
@ -353,6 +352,4 @@ exports.huddle_fraction_present = function (huddle) {
return 0.5; return 0.5;
} }
return undefined; return undefined;
}; }
window.buddy_data = exports;

View File

@ -3,6 +3,7 @@
const render_user_presence_row = require("../templates/user_presence_row.hbs"); const render_user_presence_row = require("../templates/user_presence_row.hbs");
const render_user_presence_rows = require("../templates/user_presence_rows.hbs"); const render_user_presence_rows = require("../templates/user_presence_rows.hbs");
const buddy_data = require("./buddy_data");
const message_viewport = require("./message_viewport"); const message_viewport = require("./message_viewport");
class BuddyListConf { class BuddyListConf {

View File

@ -33,7 +33,6 @@ import "../notifications";
import "../message_events"; import "../message_events";
import "../server_events"; import "../server_events";
import "../zulip"; import "../zulip";
import "../buddy_data";
import "../padded_widget"; import "../padded_widget";
import "../buddy_list"; import "../buddy_list";
import "../activity"; import "../activity";

View File

@ -6,6 +6,7 @@ import WinChan from "winchan";
import render_buddy_list_tooltip from "../templates/buddy_list_tooltip.hbs"; import render_buddy_list_tooltip from "../templates/buddy_list_tooltip.hbs";
import render_buddy_list_tooltip_content from "../templates/buddy_list_tooltip_content.hbs"; import render_buddy_list_tooltip_content from "../templates/buddy_list_tooltip_content.hbs";
import * as buddy_data from "./buddy_data";
import * as channel from "./channel"; import * as channel from "./channel";
import * as compose from "./compose"; import * as compose from "./compose";
import * as compose_state from "./compose_state"; import * as compose_state from "./compose_state";

View File

@ -8,7 +8,6 @@ declare let admin: any;
declare let avatar: any; declare let avatar: any;
declare let blueslip: any; declare let blueslip: any;
declare let bot_data: any; declare let bot_data: any;
declare let buddy_data: any;
declare let buddy_list: any; declare let buddy_list: any;
declare let compose_actions: any; declare let compose_actions: any;
declare let composebox_typeahead: any; declare let composebox_typeahead: any;

View File

@ -1,3 +1,4 @@
import * as buddy_data from "./buddy_data";
import * as hash_util from "./hash_util"; import * as hash_util from "./hash_util";
import * as narrow_state from "./narrow_state"; import * as narrow_state from "./narrow_state";
import * as people from "./people"; import * as people from "./people";

View File

@ -13,6 +13,7 @@ import render_user_info_popover_content from "../templates/user_info_popover_con
import render_user_info_popover_title from "../templates/user_info_popover_title.hbs"; import render_user_info_popover_title from "../templates/user_info_popover_title.hbs";
import render_user_profile_modal from "../templates/user_profile_modal.hbs"; import render_user_profile_modal from "../templates/user_profile_modal.hbs";
import * as buddy_data from "./buddy_data";
import * as compose_state from "./compose_state"; import * as compose_state from "./compose_state";
import * as condense from "./condense"; import * as condense from "./condense";
import * as feature_flags from "./feature_flags"; import * as feature_flags from "./feature_flags";