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.

compose: Remove create_message_object.

Evy Kassirerd2f7b1a7acce
diff --git a/web/src/compose.js b/web/src/compose.js
index 09b4c1f0adc..e87ee89ad20 100644
--- a/web/src/compose.js
+++ b/web/src/compose.js
@@ -80,43 +80,6 @@ export function render_preview_area() {
$preview_message_area.show();
}
-export function create_message_object(message_content = compose_state.message_content()) {
- // Changes here must also be kept in sync with echo.try_deliver_locally
- const message = {
- type: compose_state.get_message_type(),
- content: message_content,
- sender_id: current_user.user_id,
- queue_id: server_events_state.queue_id,
- stream_id: undefined,
- };
- message.topic = "";
-
- if (message.type === "private") {
- // TODO: this should be collapsed with the code in composebox_typeahead.ts
- const recipient = compose_state.private_message_recipient_emails();
- const emails = util.extract_pm_recipients(recipient);
- message.to = emails;
- message.reply_to = recipient;
- message.private_message_recipient = recipient;
- message.to_user_ids = people.email_list_to_user_ids_string(emails);
-
- // Note: The `undefined` case is for situations like
- // the is_zephyr_mirror_realm case where users may
- // be automatically created when you try to send a
- // direct message to their email address.
- if (message.to_user_ids !== undefined) {
- message.to = people.user_ids_string_to_ids_array(message.to_user_ids);
- }
- } else {
- const topic = compose_state.topic();
- message.topic = util.is_topic_name_considered_empty(topic) ? "" : topic;
- const stream_id = compose_state.stream_id();
- message.stream_id = stream_id;
- message.to = stream_id;
- }
- return message;
-}
-
export function clear_compose_box() {
/* Before clearing the compose box, we reset it to the
* default/normal size. Note that for locally echoed messages, we
@@ -167,7 +130,41 @@ export function send_message_success(request, data) {
}
}
-export let send_message = (request = create_message_object()) => {
+export let send_message = () => {
+ // Changes here must also be kept in sync with echo.try_deliver_locally
+ const request = {
+ type: compose_state.get_message_type(),
+ content: compose_state.message_content(),
+ sender_id: current_user.user_id,
+ queue_id: server_events_state.queue_id,
+ stream_id: undefined,
+ };
+ request.topic = "";
+
+ if (request.type === "private") {
+ // TODO: this should be collapsed with the code in composebox_typeahead.ts
+ const recipient = compose_state.private_message_recipient_emails();
+ const emails = util.extract_pm_recipients(recipient);
+ request.to = emails;
+ request.reply_to = recipient;
+ request.private_message_recipient = recipient;
+ request.to_user_ids = people.email_list_to_user_ids_string(emails);
+
+ // Note: The `undefined` case is for situations like
+ // the is_zephyr_mirror_realm case where users may
+ // be automatically created when you try to send a
+ // direct message to their email address.
+ if (request.to_user_ids !== undefined) {
+ request.to = people.user_ids_string_to_ids_array(request.to_user_ids);
+ }
+ } else {
+ const topic = compose_state.topic();
+ request.topic = util.is_topic_name_considered_empty(topic) ? "" : topic;
+ const stream_id = compose_state.stream_id();
+ request.stream_id = stream_id;
+ request.to = stream_id;
+ }
+
compose_state.set_recipient_edited_manually(false);
compose_state.set_is_content_unedited_restored_draft(false);
if (request.type === "private") {
@@ -344,12 +341,10 @@ export function do_post_send_tasks() {
}
function schedule_message_to_custom_date() {
- const compose_message_object = create_message_object();
-
const deliver_at = scheduled_messages.get_formatted_selected_send_later_time();
const scheduled_delivery_timestamp = scheduled_messages.get_selected_send_later_timestamp();
- const message_type = compose_message_object.type;
+ const message_type = compose_state.get_message_type();
let req_type;
if (message_type === "private") {
@@ -358,11 +353,28 @@ function schedule_message_to_custom_date() {
req_type = message_type;
}
+ let message_to;
+ if (message_type === "private") {
+ const recipient = compose_state.private_message_recipient_emails();
+ const emails = util.extract_pm_recipients(recipient);
+ const to_user_ids = people.email_list_to_user_ids_string(emails);
+ message_to = util.extract_pm_recipients(recipient);
+ // Note: The `undefined` case is for situations like
+ // the is_zephyr_mirror_realm case where users may
+ // be automatically created when you try to send a
+ // direct message to their email address.
+ if (to_user_ids !== undefined) {
+ message_to = people.user_ids_string_to_ids_array(to_user_ids);
+ }
+ } else {
+ message_to = compose_state.stream_id();
+ }
+
const scheduled_message_data = {
type: req_type,
- to: JSON.stringify(compose_message_object.to),
- topic: compose_message_object.topic,
- content: compose_message_object.content,
+ to: JSON.stringify(message_to),
+ topic: message_type === "stream" ? compose_state.topic() : "",
+ content: compose_state.message_content(),
scheduled_delivery_timestamp,
};
diff --git a/web/src/echo.ts b/web/src/echo.ts
index 70055ea0276..2af358cb384 100644
--- a/web/src/echo.ts
+++ b/web/src/echo.ts
@@ -261,7 +261,7 @@ export function insert_local_message(
): Message {
// 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
+ // Keep this in sync with changes to compose.send_message
const raw_content = message_request.content;
const topic = message_request.topic;
diff --git a/web/src/people.ts b/web/src/people.ts
index a77d0a71d22..322cb834cd1 100644
--- a/web/src/people.ts
+++ b/web/src/people.ts
@@ -426,7 +426,7 @@ export function emails_string_to_user_ids(emails_string: string): number[] {
return user_ids_string ? user_ids_string_to_ids_array(user_ids_string) : [];
}
-export let email_list_to_user_ids_string = (emails: string[]): string | undefined => {
+export function email_list_to_user_ids_string(emails: string[]): string | undefined {
let user_ids = util.try_parse_as_truthy(
emails.map((email) => {
const person = get_by_email(email);
@@ -442,12 +442,6 @@ export let email_list_to_user_ids_string = (emails: string[]): string | undefine
user_ids = util.sorted_ids(user_ids);
return user_ids.join(",");
-};
-
-export function rewire_email_list_to_user_ids_string(
- value: typeof email_list_to_user_ids_string,
-): void {
- email_list_to_user_ids_string = value;
}
export function get_full_names_for_poll_option(user_ids: number[]): string {
diff --git a/web/tests/compose.test.cjs b/web/tests/compose.test.cjs
index bc5c9f294f2..07e3622ca19 100644
--- a/web/tests/compose.test.cjs
+++ b/web/tests/compose.test.cjs
@@ -850,36 +850,6 @@ test_ui("on_events", ({override, override_rewire}) => {
})();
});
-test_ui("create_message_object", ({override, override_rewire}) => {
- mock_banners();
-
- const fake_compose_box = new FakeComposeBox();
-
- compose_state.set_stream_id(social.stream_id);
-
- fake_compose_box.set_topic_val("lunch");
- fake_compose_box.set_textarea_val("burrito");
-
- compose_state.set_message_type("stream");
-
- let message = compose.create_message_object();
- assert.equal(message.to, social.stream_id);
- assert.equal(message.topic, "lunch");
- assert.equal(message.content, "burrito");
-
- compose_state.set_message_type("private");
- override(compose_pm_pill, "get_emails", () => "[email protected],[email protected]");
-
- message = compose.create_message_object();
- assert.deepEqual(message.to, [alice.user_id, bob.user_id]);
- assert.equal(message.to_user_ids, "31,32");
- assert.equal(message.content, "burrito");
-
- override_rewire(people, "email_list_to_user_ids_string", () => undefined);
- message = compose.create_message_object();
- assert.deepEqual(message.to, [alice.email, bob.email]);
-});
-
test_ui("DM policy disabled", ({override, override_rewire}) => {
// Disable dms in the organisation
override(realm, "realm_direct_message_permission_group", nobody.id);