refactor: Extract echo.update_message_lists.

We lift the code out of message_store.reify_ids
into its only calling module (echo.js).
This commit is contained in:
Steve Howell 2021-03-28 17:08:25 +00:00 committed by Tim Abbott
parent 6bad6c8837
commit 415e6a486f
4 changed files with 48 additions and 54 deletions

View File

@ -5,6 +5,7 @@ const {strict: assert} = require("assert");
const MockDate = require("mockdate"); const MockDate = require("mockdate");
const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace"); const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
const {make_stub} = require("../zjsunit/stub");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
const {page_params} = require("../zjsunit/zpage_params"); const {page_params} = require("../zjsunit/zpage_params");
@ -169,6 +170,28 @@ run_test("build_display_recipient", () => {
assert.equal(iago.id, 123); assert.equal(iago.id, 123);
}); });
run_test("update_message_lists", () => {
home_msg_list.view = {};
const stub = make_stub();
const view_stub = make_stub();
home_msg_list.change_message_id = stub.f;
home_msg_list.view.change_message_id = view_stub.f;
echo.update_message_lists({old_id: 401, new_id: 402});
assert.equal(stub.num_calls, 1);
const args = stub.get_args("old", "new");
assert.equal(args.old, 401);
assert.equal(args.new, 402);
assert.equal(view_stub.num_calls, 1);
const view_args = view_stub.get_args("old", "new");
assert.equal(view_args.old, 401);
assert.equal(view_args.new, 402);
});
run_test("insert_local_message streams", (override) => { run_test("insert_local_message streams", (override) => {
const fake_now = 555; const fake_now = 555;
MockDate.set(new Date(fake_now * 1000)); MockDate.set(new Date(fake_now * 1000));

View File

@ -3,7 +3,6 @@
const {strict: assert} = require("assert"); const {strict: assert} = require("assert");
const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace"); const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");
const {make_stub} = require("../zjsunit/stub");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip"); const blueslip = require("../zjsunit/zblueslip");
const {page_params} = require("../zjsunit/zpage_params"); const {page_params} = require("../zjsunit/zpage_params");
@ -19,7 +18,6 @@ mock_esm("../../static/js/recent_senders", {
}); });
set_global("document", "document-stub"); set_global("document", "document-stub");
set_global("home_msg_list", {});
page_params.realm_allow_message_editing = true; page_params.realm_allow_message_editing = true;
page_params.is_admin = true; page_params.is_admin = true;
@ -238,6 +236,17 @@ test("errors", () => {
); );
}); });
test("reify_message_id", () => {
const message = {type: "private", id: 500};
message_store.update_message_cache(message);
assert.equal(message_store.get_cached_message(500), message);
message_store.reify_message_id({old_id: 500, new_id: 501});
assert.equal(message_store.get_cached_message(500), undefined);
assert.equal(message_store.get_cached_message(501), message);
});
test("update_booleans", () => { test("update_booleans", () => {
const message = {}; const message = {};
@ -324,44 +333,6 @@ test("update_property", () => {
assert.equal(message2.display_recipient, denmark.name); assert.equal(message2.display_recipient, denmark.name);
}); });
test("message_id_change", () => {
const message = {
sender_email: "me@example.com",
sender_id: me.user_id,
type: "private",
display_recipient: convert_recipients([me, bob, cindy]),
flags: ["has_alert_word"],
id: 401,
};
message_helper.process_new_message(message);
const opts = {
old_id: 401,
new_id: 402,
};
{
const stub = make_stub();
home_msg_list.change_message_id = stub.f;
message_store.reify_message_id(opts);
assert.equal(stub.num_calls, 1);
const msg_id = stub.get_args("old", "new");
assert.equal(msg_id.old, 401);
assert.equal(msg_id.new, 402);
}
home_msg_list.view = {};
{
const stub = make_stub();
home_msg_list.view.change_message_id = stub.f;
message_store.reify_message_id(opts);
assert.equal(stub.num_calls, 1);
const msg_id = stub.get_args("old", "new");
assert.equal(msg_id.old, 401);
assert.equal(msg_id.new, 402);
}
});
test("errors", () => { test("errors", () => {
blueslip.expect("error", "message_store.get got bad value: undefined"); blueslip.expect("error", "message_store.get got bad value: undefined");
message_store.get(undefined); message_store.get(undefined);

View File

@ -307,10 +307,23 @@ export function reify_message_id(local_id, server_id) {
const opts = {old_id: Number.parseFloat(local_id), new_id: server_id}; const opts = {old_id: Number.parseFloat(local_id), new_id: server_id};
message_store.reify_message_id(opts); message_store.reify_message_id(opts);
update_message_lists(opts);
notifications.reify_message_id(opts); notifications.reify_message_id(opts);
recent_topics.reify_message_id_if_available(opts); recent_topics.reify_message_id_if_available(opts);
} }
export function update_message_lists({old_id, new_id}) {
for (const msg_list of [message_list.all, home_msg_list, message_list.narrowed]) {
if (msg_list !== undefined) {
msg_list.change_message_id(old_id, new_id);
if (msg_list.view !== undefined) {
msg_list.view.change_message_id(old_id, new_id);
}
}
}
}
export function process_from_server(messages) { export function process_from_server(messages) {
const msgs_to_rerender = []; const msgs_to_rerender = [];
const non_echo_messages = []; const non_echo_messages = [];

View File

@ -1,5 +1,4 @@
import * as blueslip from "./blueslip"; import * as blueslip from "./blueslip";
import * as message_list from "./message_list";
import * as people from "./people"; import * as people from "./people";
const stored_messages = new Map(); const stored_messages = new Map();
@ -137,21 +136,9 @@ export function update_property(property, value, info) {
} }
} }
export function reify_message_id(opts) { export function reify_message_id({old_id, new_id}) {
const old_id = opts.old_id;
const new_id = opts.new_id;
if (stored_messages.has(old_id)) { if (stored_messages.has(old_id)) {
stored_messages.set(new_id, stored_messages.get(old_id)); stored_messages.set(new_id, stored_messages.get(old_id));
stored_messages.delete(old_id); stored_messages.delete(old_id);
} }
for (const msg_list of [message_list.all, home_msg_list, message_list.narrowed]) {
if (msg_list !== undefined) {
msg_list.change_message_id(old_id, new_id);
if (msg_list.view !== undefined) {
msg_list.view.change_message_id(old_id, new_id);
}
}
}
} }