echo: Cut dependency on `message_events.js`.

This commit is contained in:
Lalit 2023-06-28 16:32:28 +05:30 committed by Tim Abbott
parent 9f1fcfb3a7
commit 9447381d03
4 changed files with 20 additions and 29 deletions

View File

@ -22,6 +22,7 @@ import {$t, $t_html} from "./i18n";
import * as loading from "./loading";
import * as markdown from "./markdown";
import * as message_edit from "./message_edit";
import * as message_events from "./message_events";
import * as narrow from "./narrow";
import {page_params} from "./page_params";
import * as people from "./people";
@ -214,7 +215,7 @@ export function send_message(request = create_message_object()) {
let local_id;
let locally_echoed;
const message = echo.try_deliver_locally(request);
const message = echo.try_deliver_locally(request, message_events.insert_new_messages);
if (message) {
// We are rendering this message locally with an id
// like 92l99.01 that corresponds to a reasonable

View File

@ -7,7 +7,6 @@ import * as compose_ui from "./compose_ui";
import * as drafts from "./drafts";
import * as local_message from "./local_message";
import * as markdown from "./markdown";
import * as message_events from "./message_events";
import * as message_lists from "./message_lists";
import * as message_live_update from "./message_live_update";
import * as message_store from "./message_store";
@ -53,13 +52,6 @@ function hide_retry_spinner($row) {
return true;
}
function insert_message(message) {
// It is a little bit funny to go through the message_events
// codepath, but it's sort of the idea behind local echo that
// we are simulating server events before they actually arrive.
message_events.insert_new_messages([message], true);
}
function show_message_failed(message_id, failed_msg) {
// Failed to send message, so display inline retry/cancel
message_live_update.update_message_in_all_views(message_id, ($row) => {
@ -181,7 +173,7 @@ export function build_display_recipient(message) {
return display_recipient;
}
export function insert_local_message(message_request, local_id_float) {
export function insert_local_message(message_request, local_id_float, insert_new_messages) {
// Shallow clone of message request object that is turned into something suitable
// for zulip.js:add_message
// Keep this in sync with changes to compose.create_message_object
@ -207,7 +199,8 @@ export function insert_local_message(message_request, local_id_float) {
message.display_recipient = build_display_recipient(message);
insert_message(message);
insert_new_messages([message], true);
return message;
}
@ -215,7 +208,7 @@ export function is_slash_command(content) {
return !content.startsWith("/me") && content.startsWith("/");
}
export function try_deliver_locally(message_request) {
export function try_deliver_locally(message_request, insert_new_messages) {
if (markdown.contains_backend_only_syntax(message_request.content)) {
return undefined;
}
@ -268,7 +261,7 @@ export function try_deliver_locally(message_request) {
compose_ui.make_compose_box_original_size();
}
const message = insert_local_message(message_request, local_id_float);
const message = insert_local_message(message_request, local_id_float, insert_new_messages);
return message;
}

View File

@ -182,16 +182,14 @@ test_ui("send_message", ({override, override_rewire, mock_template}) => {
override(compose_pm_pill, "get_emails", () => "alice@example.com");
const server_message_id = 127;
override_rewire(echo, "insert_message", (message) => {
assert.equal(message.timestamp, fake_now);
});
override(markdown, "apply_markdown", () => {});
override(markdown, "add_topic_links", () => {});
override_rewire(echo, "try_deliver_locally", (message_request) => {
const local_id_float = 123.04;
return echo.insert_local_message(message_request, local_id_float);
return echo.insert_local_message(message_request, local_id_float, (messages) =>
assert.equal(messages[0].timestamp, fake_now),
);
});
override(transmit, "send_message", (payload, success) => {

View File

@ -208,7 +208,7 @@ run_test("update_message_lists", () => {
assert.equal(view_args.new, 402);
});
run_test("insert_local_message streams", ({override, override_rewire}) => {
run_test("insert_local_message streams", ({override}) => {
const fake_now = 555;
MockDate.set(new Date(fake_now * 1000));
@ -226,14 +226,14 @@ run_test("insert_local_message streams", ({override, override_rewire}) => {
add_topic_links_called = true;
});
override_rewire(echo, "insert_message", (message) => {
const insert_new_messages = ([message]) => {
assert.equal(message.display_recipient, "general");
assert.equal(message.timestamp, fake_now);
assert.equal(message.sender_email, "iago@zulip.com");
assert.equal(message.sender_full_name, "Iago");
assert.equal(message.sender_id, 123);
insert_message_called = true;
});
};
const message_request = {
type: "stream",
@ -242,14 +242,14 @@ run_test("insert_local_message streams", ({override, override_rewire}) => {
sender_full_name: "Iago",
sender_id: 123,
};
echo.insert_local_message(message_request, local_id_float);
echo.insert_local_message(message_request, local_id_float, insert_new_messages);
assert.ok(apply_markdown_called);
assert.ok(add_topic_links_called);
assert.ok(insert_message_called);
});
run_test("insert_local_message direct message", ({override, override_rewire}) => {
run_test("insert_local_message direct message", ({override}) => {
const local_id_float = 102.01;
page_params.user_id = 123;
@ -270,10 +270,10 @@ run_test("insert_local_message direct message", ({override, override_rewire}) =>
let apply_markdown_called = false;
let insert_message_called = false;
override_rewire(echo, "insert_message", (message) => {
const insert_new_messages = ([message]) => {
assert.equal(message.display_recipient.length, 3);
insert_message_called = true;
});
};
override(markdown, "apply_markdown", () => {
apply_markdown_called = true;
@ -290,18 +290,17 @@ run_test("insert_local_message direct message", ({override, override_rewire}) =>
sender_full_name: "Iago",
sender_id: 123,
};
echo.insert_local_message(message_request, local_id_float);
echo.insert_local_message(message_request, local_id_float, insert_new_messages);
assert.ok(add_topic_links_called);
assert.ok(apply_markdown_called);
assert.ok(insert_message_called);
});
run_test("test reify_message_id", ({override, override_rewire}) => {
run_test("test reify_message_id", ({override}) => {
const local_id_float = 103.01;
override(markdown, "apply_markdown", () => {});
override(markdown, "add_topic_links", () => {});
override_rewire(echo, "insert_message", () => {});
const message_request = {
type: "stream",
@ -311,7 +310,7 @@ run_test("test reify_message_id", ({override, override_rewire}) => {
sender_id: 123,
draft_id: 100,
};
echo.insert_local_message(message_request, local_id_float);
echo.insert_local_message(message_request, local_id_float, () => {});
let message_store_reify_called = false;
let notifications_reify_called = false;