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

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-27 16:10:03 -08:00 committed by Tim Abbott
parent ac65935b16
commit 7145cb4a0d
46 changed files with 118 additions and 61 deletions

View File

@ -154,7 +154,6 @@
"message_events": false,
"message_fetch": false,
"message_list": false,
"message_store": false,
"message_util": false,
"narrow": false,
"padded_widget": false,

View File

@ -14,9 +14,12 @@ const stream_topic_history = {__esModule: true};
rewiremock("../../static/js/stream_topic_history").with(stream_topic_history);
const message_store = set_global("message_store", {
const message_store = {
__esModule: true,
user_ids: () => [],
});
};
rewiremock("../../static/js/message_store").with(message_store);
const page_params = set_global("page_params", {});
const channel = {__esModule: true};

View File

@ -30,7 +30,7 @@ rewiremock("../../static/js/sent_messages").with({
},
});
set_global("message_store", {
rewiremock("../../static/js/message_store").with({
get: () => ({failed_request: true}),
update_booleans: () => {},
});

View File

@ -2,13 +2,18 @@
const {strict: assert} = require("assert");
const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const message_store = set_global("message_store", {});
const message_store = {__esModule: true};
rewiremock("../../static/js/message_store").with(message_store);
const page_params = set_global("page_params", {});
rewiremock.enable();
zrequire("unread");
const stream_data = zrequire("stream_data");
const people = zrequire("people");
@ -1553,3 +1558,4 @@ run_test("error_cases", () => {
const predicate = get_predicate([["pm-with", "Joe@example.com"]]);
assert(!predicate({type: "private"}));
});
rewiremock.disable();

View File

@ -22,7 +22,9 @@ rewiremock.enable();
const rows = zrequire("rows");
const lightbox = zrequire("lightbox");
const message_store = set_global("message_store", {});
const message_store = {__esModule: true};
rewiremock("../../static/js/message_store").with(message_store);
rows.__Rewire__("is_draft_row", () => false);

View File

@ -39,7 +39,6 @@ const fenced_code = zrequire("fenced_code", "shared/js/fenced_code");
const markdown_config = zrequire("markdown_config");
const marked = zrequire("marked", "third/marked/lib/marked");
const markdown = zrequire("markdown");
zrequire("message_store");
const people = zrequire("people");
const stream_data = zrequire("stream_data");
const user_groups = zrequire("user_groups");

View File

@ -22,7 +22,6 @@ rewiremock("katex").with({
rewiremock.enable();
const markdown_config = zrequire("markdown_config");
zrequire("message_store");
const markdown = zrequire("markdown");

View File

@ -40,7 +40,8 @@ rewiremock("../../static/js/message_scroll").with({
update_top_of_narrow_notices: () => {},
});
const message_util = set_global("message_util", {});
const message_store = set_global("message_store", {});
const message_store = {__esModule: true};
rewiremock("../../static/js/message_store").with(message_store);
const narrow_state = {__esModule: true};
rewiremock("../../static/js/narrow_state").with(narrow_state);
const pm_list = {__esModule: true};

View File

@ -7,7 +7,8 @@ const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const message_store = set_global("message_store", {});
const message_store = {__esModule: true};
rewiremock("../../static/js/message_store").with(message_store);
set_global("page_params", {});
rewiremock("../../static/js/muting").with({

View File

@ -5,13 +5,17 @@ const {strict: assert} = require("assert");
const {parseISO} = require("date-fns");
const _ = require("lodash");
const MockDate = require("mockdate");
const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const message_store = set_global("message_store", {});
const message_store = {__esModule: true};
rewiremock("../../static/js/message_store").with(message_store);
const page_params = set_global("page_params", {});
rewiremock.enable();
const people = zrequire("people");
const settings_config = zrequire("settings_config");
const visibility = settings_config.email_address_visibility_values;
@ -1113,3 +1117,4 @@ run_test("get_active_message_people", () => {
// reset to native Date()
MockDate.reset();
rewiremock.disable();

View File

@ -54,12 +54,16 @@ const message = {
],
};
const message_store = set_global("message_store", {
const message_store = {
__esModule: true,
get(message_id) {
assert.equal(message_id, 1001);
return message;
},
});
};
rewiremock("../../static/js/message_store").with(message_store);
set_global("current_msg_list", {
selected_message() {

View File

@ -2,13 +2,15 @@
const {strict: assert} = require("assert");
const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
let next_id = 0;
const messages = [];
set_global("message_store", {
rewiremock("../../static/js/message_store").with({
get: (msg_id) => messages[msg_id - 1],
});
set_global("message_list", {
@ -19,6 +21,8 @@ set_global("message_list", {
},
});
rewiremock.enable();
const rs = zrequire("recent_senders");
zrequire("message_util.js");
@ -228,3 +232,4 @@ run_test("process_message_for_senders", () => {
// no changes should take place in this case.
rs.update_topics_of_deleted_message_ids([-1]);
});
rewiremock.disable();

View File

@ -146,7 +146,7 @@ set_global("message_list", {
},
},
});
set_global("message_store", {
rewiremock("../../static/js/message_store").with({
get: (msg_id) => messages[msg_id - 1],
});
rewiremock("../../static/js/stream_data").with({

View File

@ -2,6 +2,8 @@
const {strict: assert} = require("assert");
const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
@ -9,12 +11,14 @@ const page_params = set_global("page_params", {
search_pills_enabled: true,
});
set_global("message_store", {
rewiremock("../../static/js/message_store").with({
user_ids: () => [],
});
set_global("narrow", {});
rewiremock.enable();
const settings_config = zrequire("settings_config");
page_params.realm_email_address_visibility =
settings_config.email_address_visibility_values.admins_only.code;
@ -966,3 +970,4 @@ run_test("multiple_operators_without_pills", () => {
expected = ["is:alerted sender:ted@zulip.com abc"];
assert.deepEqual(suggestions.strings, expected);
});
rewiremock.disable();

View File

@ -2,17 +2,21 @@
const {strict: assert} = require("assert");
const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const page_params = set_global("page_params", {
search_pills_enabled: false,
});
set_global("message_store", {
rewiremock("../../static/js/message_store").with({
user_ids: () => [],
});
set_global("narrow", {});
rewiremock.enable();
const settings_config = zrequire("settings_config");
page_params.realm_email_address_visibility =
settings_config.email_address_visibility_values.admins_only.code;
@ -884,3 +888,4 @@ run_test("queries_with_spaces", () => {
expected = ["stream:offi", "stream:office"];
assert.deepEqual(suggestions.strings, expected);
});
rewiremock.disable();

View File

@ -53,7 +53,6 @@ set_global("$", {
rewiremock.enable();
zrequire("message_store");
const server_events = zrequire("server_events");
set_global("$", $);

View File

@ -10,7 +10,9 @@ const {run_test} = require("../zjsunit/test");
const channel = {__esModule: true};
rewiremock("../../static/js/channel").with(channel);
const widgetize = set_global("widgetize", {});
const message_store = set_global("message_store", {});
const message_store = {__esModule: true};
rewiremock("../../static/js/message_store").with(message_store);
rewiremock.enable();

View File

@ -15,7 +15,9 @@ let page_params = set_global("page_params", {
rewiremock("../../static/js/narrow_state").with({});
set_global("current_msg_list", {});
set_global("home_msg_list", {});
const message_store = set_global("message_store", {});
const message_store = {__esModule: true};
rewiremock("../../static/js/message_store").with(message_store);
rewiremock.enable();

View File

@ -30,7 +30,6 @@ import "../ui";
import "../composebox_typeahead";
import "../hotkey";
import "../notifications";
import "../message_store";
import "../message_util";
import "../message_events";
import "../message_fetch";

View File

@ -14,6 +14,7 @@ import * as hashchange from "./hashchange";
import * as message_edit from "./message_edit";
import * as message_edit_history from "./message_edit_history";
import * as message_flags from "./message_flags";
import * as message_store from "./message_store";
import * as muting_ui from "./muting_ui";
import * as notifications from "./notifications";
import * as overlays from "./overlays";

View File

@ -13,6 +13,7 @@ const channel = require("./channel");
const compose = require("./compose");
const compose_pm_pill = require("./compose_pm_pill");
const compose_state = require("./compose_state");
const message_store = require("./message_store");
const people = require("./people");
const rows = require("./rows");
const settings_data = require("./settings_data");

View File

@ -2,6 +2,7 @@ import * as alert_words from "./alert_words";
import * as compose from "./compose";
import * as local_message from "./local_message";
import * as markdown from "./markdown";
import * as message_store from "./message_store";
import * as narrow_state from "./narrow_state";
import * as notifications from "./notifications";
import * as people from "./people";

View File

@ -8,6 +8,7 @@ const render_emoji_popover_content = require("../templates/emoji_popover_content
const render_emoji_popover_search_results = require("../templates/emoji_popover_search_results.hbs");
const render_emoji_showcase = require("../templates/emoji_showcase.hbs");
const message_store = require("./message_store");
const popovers = require("./popovers");
const rows = require("./rows");

View File

@ -1,6 +1,7 @@
import Handlebars from "handlebars/runtime";
import _ from "lodash";
import * as message_store from "./message_store";
import * as people from "./people";
import * as stream_data from "./stream_data";
import * as util from "./util";

View File

@ -1,3 +1,4 @@
import * as message_store from "./message_store";
import * as rows from "./rows";
let is_floating_recipient_bar_showing = false;

View File

@ -27,7 +27,6 @@ declare let input_pill: any;
declare let message_events: any;
declare let message_fetch: any;
declare let message_list: any;
declare let message_store: any;
declare let message_util: any;
declare let narrow: any;
declare let padded_widget: any;

View File

@ -1,4 +1,5 @@
import {LightboxCanvas} from "./lightbox_canvas";
import * as message_store from "./message_store";
import * as overlays from "./overlays";
import * as people from "./people";
import * as popovers from "./popovers";

View File

@ -6,6 +6,8 @@ import * as emoji from "../shared/js/emoji";
import * as fenced_code from "../shared/js/fenced_code";
import marked from "../third/marked/lib/marked";
import * as message_store from "./message_store";
// This contains zulip's frontend Markdown implementation; see
// docs/subsystems/markdown.md for docs on our Markdown syntax. The other
// main piece in rendering Markdown client-side is

View File

@ -9,6 +9,7 @@ import * as condense from "./condense";
import * as echo from "./echo";
import * as loading from "./loading";
import * as markdown from "./markdown";
import * as message_store from "./message_store";
import * as message_viewport from "./message_viewport";
import * as resize from "./resize";
import * as rows from "./rows";

View File

@ -8,6 +8,7 @@ const condense = require("./condense");
const huddle_data = require("./huddle_data");
const message_edit = require("./message_edit");
const message_edit_history = require("./message_edit_history");
const message_store = require("./message_store");
const narrow_state = require("./narrow_state");
const notifications = require("./notifications");
const pm_list = require("./pm_list");

View File

@ -4,6 +4,7 @@ const channel = require("./channel");
const {Filter} = require("./filter");
const huddle_data = require("./huddle_data");
const message_scroll = require("./message_scroll");
const message_store = require("./message_store");
const people = require("./people");
const pm_list = require("./pm_list");
const stream_data = require("./stream_data");

View File

@ -1,6 +1,7 @@
import _ from "lodash";
import * as channel from "./channel";
import * as message_store from "./message_store";
import * as starred_messages from "./starred_messages";
function send_flag_update(message, flag, op) {

View File

@ -11,6 +11,7 @@ import * as compose_fade from "./compose_fade";
import * as condense from "./condense";
import * as hash_util from "./hash_util";
import * as message_edit from "./message_edit";
import * as message_store from "./message_store";
import * as message_viewport from "./message_viewport";
import * as narrow_state from "./narrow_state";
import * as people from "./people";

View File

@ -1,3 +1,4 @@
import * as message_store from "./message_store";
import * as people from "./people";
function rerender_messages_view() {

View File

@ -1,11 +1,9 @@
"use strict";
const alert_words = require("./alert_words");
const people = require("./people");
const pm_conversations = require("./pm_conversations");
const recent_senders = require("./recent_senders");
const stream_topic_history = require("./stream_topic_history");
const util = require("./util");
import * as alert_words from "./alert_words";
import * as people from "./people";
import * as pm_conversations from "./pm_conversations";
import * as recent_senders from "./recent_senders";
import * as stream_topic_history from "./stream_topic_history";
import * as util from "./util";
const stored_messages = new Map();
@ -24,11 +22,11 @@ const stored_messages = new Map();
*/
const message_user_ids = new Set();
exports.user_ids = function () {
export function user_ids() {
return Array.from(message_user_ids);
};
}
exports.get = function get(message_id) {
export function get(message_id) {
if (message_id === undefined || message_id === null) {
blueslip.error("message_store.get got bad value: " + message_id);
return undefined;
@ -43,15 +41,15 @@ exports.get = function get(message_id) {
}
return stored_messages.get(message_id);
};
}
exports.each = function (f) {
export function each(f) {
for (const [id, message] of stored_messages) {
f(message, id);
}
};
}
exports.get_pm_emails = function (message) {
export function get_pm_emails(message) {
const user_ids = people.pm_with_user_ids(message);
const emails = user_ids
.map((user_id) => {
@ -65,9 +63,9 @@ exports.get_pm_emails = function (message) {
.sort();
return emails.join(", ");
};
}
exports.get_pm_full_names = function (message) {
export function get_pm_full_names(message) {
const user_ids = people.pm_with_user_ids(message);
const names = user_ids
.map((user_id) => {
@ -81,9 +79,9 @@ exports.get_pm_full_names = function (message) {
.sort();
return names.join(", ");
};
}
exports.process_message_for_recent_private_messages = function (message) {
export function process_message_for_recent_private_messages(message) {
const user_ids = people.pm_with_user_ids(message);
if (!user_ids) {
return;
@ -94,9 +92,9 @@ exports.process_message_for_recent_private_messages = function (message) {
}
pm_conversations.recent.insert(user_ids, message.id);
};
}
exports.set_message_booleans = function (message) {
export function set_message_booleans(message) {
const flags = message.flags || [];
function convert_flag(flag_name) {
@ -115,9 +113,9 @@ exports.set_message_booleans = function (message) {
// just a distraction, so we delete it. (All the downstream code
// uses booleans.)
delete message.flags;
};
}
exports.init_booleans = function (message) {
export function init_booleans(message) {
// This initializes booleans for the local-echo path where
// we don't have flags from the server yet. (We want to
// explicitly set flags to false to be consistent with other
@ -129,9 +127,9 @@ exports.init_booleans = function (message) {
message.mentioned_me_directly = false;
message.collapsed = false;
message.alerted = false;
};
}
exports.update_booleans = function (message, flags) {
export function update_booleans(message, flags) {
// When we get server flags for local echo or message edits,
// we are vulnerable to race conditions, so only update flags
// that are driven by message content.
@ -142,9 +140,9 @@ exports.update_booleans = function (message, flags) {
message.mentioned = convert_flag("mentioned") || convert_flag("wildcard_mentioned");
message.mentioned_me_directly = convert_flag("mentioned");
message.alerted = convert_flag("has_alert_word");
};
}
exports.add_message_metadata = function (message) {
export function add_message_metadata(message) {
const cached_msg = stored_messages.get(message.id);
if (cached_msg !== undefined) {
// Copy the match topic and content over if they exist on
@ -188,12 +186,12 @@ exports.add_message_metadata = function (message) {
case "private":
message.is_private = true;
message.reply_to = util.normalize_recipients(exports.get_pm_emails(message));
message.display_reply_to = exports.get_pm_full_names(message);
message.reply_to = util.normalize_recipients(get_pm_emails(message));
message.display_reply_to = get_pm_full_names(message);
message.pm_with_url = people.pm_with_url(message);
message.to_user_ids = people.pm_reply_user_string(message);
exports.process_message_for_recent_private_messages(message);
process_message_for_recent_private_messages(message);
if (people.is_my_user_id(message.sender_id)) {
for (const recip of message.display_recipient) {
@ -209,20 +207,20 @@ exports.add_message_metadata = function (message) {
}
stored_messages.set(message.id, message);
return message;
};
}
exports.update_property = function (property, value, info) {
export function update_property(property, value, info) {
switch (property) {
case "sender_full_name":
case "small_avatar_url":
exports.each((msg) => {
each((msg) => {
if (msg.sender_id && msg.sender_id === info.user_id) {
msg[property] = value;
}
});
break;
case "stream_name":
exports.each((msg) => {
each((msg) => {
if (msg.stream_id && msg.stream_id === info.stream_id) {
msg.display_recipient = value;
msg.stream = value;
@ -230,9 +228,9 @@ exports.update_property = function (property, value, info) {
});
break;
}
};
}
exports.reify_message_id = function (opts) {
export function reify_message_id(opts) {
const old_id = opts.old_id;
const new_id = opts.new_id;
if (stored_messages.has(old_id)) {
@ -249,6 +247,4 @@ exports.reify_message_id = function (opts) {
}
}
}
};
window.message_store = exports;
}

View File

@ -1,6 +1,7 @@
"use strict";
const loading = require("./loading");
const message_store = require("./message_store");
const resize = require("./resize");
const unread_ui = require("./unread_ui");

View File

@ -10,6 +10,7 @@ const hashchange = require("./hashchange");
const message_edit = require("./message_edit");
const {MessageListData} = require("./message_list_data");
const message_scroll = require("./message_scroll");
const message_store = require("./message_store");
const narrow_state = require("./narrow_state");
const notifications = require("./notifications");
const people = require("./people");

View File

@ -6,6 +6,7 @@ import render_notification from "../templates/notification.hbs";
import * as alert_words from "./alert_words";
import * as channel from "./channel";
import * as favicon from "./favicon";
import * as message_store from "./message_store";
import * as muting from "./muting";
import * as narrow_state from "./narrow_state";
import * as navigate from "./navigate";

View File

@ -5,6 +5,7 @@ import _ from "lodash";
import * as typeahead from "../shared/js/typeahead";
import {FoldDict} from "./fold_dict";
import * as message_store from "./message_store";
import * as reload_state from "./reload_state";
import * as settings_data from "./settings_data";
import * as util from "./util";

View File

@ -6,6 +6,7 @@ const emoji = require("../shared/js/emoji");
const render_message_reaction = require("../templates/message_reaction.hbs");
const channel = require("./channel");
const message_store = require("./message_store");
const people = require("./people");
exports.view = {}; // function namespace

View File

@ -8,6 +8,7 @@ const drafts = require("./drafts");
const hash_util = require("./hash_util");
const ListWidget = require("./list_widget");
const {localstorage} = require("./localstorage");
const message_store = require("./message_store");
const muting = require("./muting");
const narrow_state = require("./narrow_state");
const navigate = require("./navigate");

View File

@ -1,3 +1,4 @@
import * as message_store from "./message_store";
// We don't need an andSelf() here because we already know
// that our next element is *not* a message_row, so this
// isn't going to end up empty unless we're at the bottom or top.

View File

@ -4,6 +4,7 @@ const _ = require("lodash");
const channel = require("./channel");
const echo = require("./echo");
const message_store = require("./message_store");
const reload = require("./reload");
const reload_state = require("./reload_state");
const sent_messages = require("./sent_messages");

View File

@ -1,4 +1,5 @@
import * as channel from "./channel";
import * as message_store from "./message_store";
export function get_message_events(message) {
if (message.locally_echoed) {

View File

@ -1,6 +1,7 @@
"use strict";
const {FoldDict} = require("./fold_dict");
const message_store = require("./message_store");
const muting = require("./muting");
const people = require("./people");
const stream_data = require("./stream_data");

View File

@ -2,6 +2,7 @@
const channel = require("./channel");
const message_flags = require("./message_flags");
const message_store = require("./message_store");
const message_viewport = require("./message_viewport");
const notifications = require("./notifications");
const overlays = require("./overlays");