2023-04-07 04:58:17 +02:00
|
|
|
/* Main compose box module for sending messages. */
|
|
|
|
|
2022-01-31 11:57:30 +01:00
|
|
|
import autosize from "autosize";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-02-28 00:51:57 +01:00
|
|
|
import _ from "lodash";
|
|
|
|
|
2023-06-01 02:26:36 +02:00
|
|
|
import render_success_message_scheduled_banner from "../templates/compose_banner/success_message_scheduled_banner.hbs";
|
2023-11-21 10:39:13 +01:00
|
|
|
import render_wildcard_mention_not_allowed_error from "../templates/compose_banner/wildcard_mention_not_allowed_error.hbs";
|
2023-06-01 02:26:36 +02:00
|
|
|
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as channel from "./channel";
|
2022-08-30 22:24:54 +02:00
|
|
|
import * as compose_banner from "./compose_banner";
|
2023-10-16 12:55:24 +02:00
|
|
|
import * as compose_notifications from "./compose_notifications";
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as compose_state from "./compose_state";
|
2021-02-28 01:26:48 +01:00
|
|
|
import * as compose_ui from "./compose_ui";
|
2021-06-25 15:16:29 +02:00
|
|
|
import * as compose_validate from "./compose_validate";
|
2022-10-07 18:58:30 +02:00
|
|
|
import * as drafts from "./drafts";
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as echo from "./echo";
|
2023-10-05 21:02:54 +02:00
|
|
|
import {$t_html} from "./i18n";
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as loading from "./loading";
|
|
|
|
import * as markdown from "./markdown";
|
2023-06-28 13:02:28 +02:00
|
|
|
import * as message_events from "./message_events";
|
2023-12-02 13:34:05 +01:00
|
|
|
import * as onboarding_steps from "./onboarding_steps";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as rendered_markdown from "./rendered_markdown";
|
2023-10-04 00:18:21 +02:00
|
|
|
import * as scheduled_messages from "./scheduled_messages";
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as sent_messages from "./sent_messages";
|
2021-02-28 01:11:47 +01:00
|
|
|
import * as server_events from "./server_events";
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as transmit from "./transmit";
|
2021-07-28 16:00:58 +02:00
|
|
|
import {user_settings} from "./user_settings";
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as util from "./util";
|
|
|
|
import * as zcommand from "./zcommand";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2018-11-30 00:48:13 +01:00
|
|
|
// Docs: https://zulip.readthedocs.io/en/latest/subsystems/sending-messages.html
|
2012-10-03 22:53:15 +02:00
|
|
|
|
2021-02-28 00:51:57 +01:00
|
|
|
export function clear_invites() {
|
2023-04-01 01:42:40 +02:00
|
|
|
$(
|
|
|
|
`#compose_banners .${CSS.escape(compose_banner.CLASSNAMES.recipient_not_subscribed)}`,
|
|
|
|
).remove();
|
2021-02-28 00:51:57 +01:00
|
|
|
}
|
2017-04-14 15:09:13 +02:00
|
|
|
|
2021-02-28 00:51:57 +01:00
|
|
|
export function clear_private_stream_alert() {
|
2023-04-01 01:42:40 +02:00
|
|
|
$(`#compose_banners .${CSS.escape(compose_banner.CLASSNAMES.private_stream_warning)}`).remove();
|
2021-02-28 00:51:57 +01:00
|
|
|
}
|
2017-10-14 15:18:21 +02:00
|
|
|
|
2021-02-28 00:51:57 +01:00
|
|
|
export function clear_preview_area() {
|
2023-11-02 19:29:54 +01:00
|
|
|
$("textarea#compose-textarea").show();
|
|
|
|
$("textarea#compose-textarea").trigger("focus");
|
2021-04-20 19:50:01 +02:00
|
|
|
$("#compose .undo_markdown_preview").hide();
|
|
|
|
$("#compose .preview_message_area").hide();
|
|
|
|
$("#compose .preview_content").empty();
|
|
|
|
$("#compose .markdown_preview").show();
|
2023-11-02 19:29:54 +01:00
|
|
|
autosize.update($("textarea#compose-textarea"));
|
2022-01-30 16:00:45 +01:00
|
|
|
|
|
|
|
// While in preview mode we disable unneeded compose_control_buttons,
|
2023-09-14 16:09:33 +02:00
|
|
|
// so here we are re-enabling those compose_control_buttons
|
2022-01-30 16:00:45 +01:00
|
|
|
$("#compose").removeClass("preview_mode");
|
|
|
|
$("#compose .preview_mode_disabled .compose_control_button").attr("tabindex", 0);
|
2021-02-28 00:51:57 +01:00
|
|
|
}
|
2016-08-29 22:37:27 +02:00
|
|
|
|
2021-02-28 00:51:57 +01:00
|
|
|
export function create_message_object() {
|
2018-11-04 17:04:17 +01:00
|
|
|
// Topics are optional, and we provide a placeholder if one isn't given.
|
2019-11-02 00:06:25 +01:00
|
|
|
let topic = compose_state.topic();
|
2018-11-15 18:34:16 +01:00
|
|
|
if (topic === "") {
|
2023-10-02 22:56:07 +02:00
|
|
|
topic = compose_state.empty_topic_placeholder();
|
2013-05-20 22:52:03 +02:00
|
|
|
}
|
2013-10-23 16:46:18 +02:00
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
// Changes here must also be kept in sync with echo.try_deliver_locally
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = {
|
2017-04-24 20:35:26 +02:00
|
|
|
type: compose_state.get_message_type(),
|
2020-07-29 22:15:14 +02:00
|
|
|
content: compose_state.message_content(),
|
2017-02-17 22:15:38 +01:00
|
|
|
sender_id: page_params.user_id,
|
2017-04-24 21:40:16 +02:00
|
|
|
queue_id: page_params.queue_id,
|
2023-09-26 20:28:39 +02:00
|
|
|
stream_id: undefined,
|
2017-02-17 22:15:38 +01:00
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
message.topic = "";
|
2013-04-17 19:34:20 +02:00
|
|
|
|
|
|
|
if (message.type === "private") {
|
|
|
|
// TODO: this should be collapsed with the code in composebox_typeahead.js
|
2019-12-02 17:53:55 +01:00
|
|
|
const recipient = compose_state.private_message_recipient();
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = util.extract_pm_recipients(recipient);
|
2017-02-24 23:51:23 +01:00
|
|
|
message.to = emails;
|
2017-02-24 16:20:25 +01:00
|
|
|
message.reply_to = recipient;
|
|
|
|
message.private_message_recipient = recipient;
|
2017-02-24 23:51:23 +01:00
|
|
|
message.to_user_ids = people.email_list_to_user_ids_string(emails);
|
2019-05-23 22:18:58 +02:00
|
|
|
|
2023-06-16 13:23:39 +02:00
|
|
|
// 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.
|
2019-05-23 22:18:58 +02:00
|
|
|
if (message.to_user_ids !== undefined) {
|
2019-06-06 21:49:01 +02:00
|
|
|
message.to = people.user_ids_string_to_ids_array(message.to_user_ids);
|
2019-05-23 22:18:58 +02:00
|
|
|
}
|
2013-04-17 19:34:20 +02:00
|
|
|
} else {
|
2020-02-19 02:44:17 +01:00
|
|
|
message.topic = topic;
|
2023-06-26 19:25:57 +02:00
|
|
|
const stream_id = compose_state.stream_id();
|
|
|
|
message.stream_id = stream_id;
|
|
|
|
message.to = stream_id;
|
2013-04-17 19:34:20 +02:00
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
2018-02-06 22:54:53 +01:00
|
|
|
|
2021-02-28 00:51:57 +01:00
|
|
|
export function clear_compose_box() {
|
2021-09-23 17:43:30 +02:00
|
|
|
/* Before clearing the compose box, we reset it to the
|
|
|
|
* default/normal size. Note that for locally echoed messages, we
|
|
|
|
* will have already done this action before echoing the message
|
|
|
|
* to avoid the compose box triggering "new message out of view"
|
|
|
|
* notifications incorrectly. */
|
|
|
|
if (compose_ui.is_full_size()) {
|
|
|
|
compose_ui.make_compose_box_original_size();
|
|
|
|
}
|
2023-11-02 19:29:54 +01:00
|
|
|
$("textarea#compose-textarea").val("").trigger("focus");
|
2021-07-10 19:33:45 +02:00
|
|
|
compose_validate.check_overflow_text();
|
2022-03-16 01:27:00 +01:00
|
|
|
compose_validate.clear_topic_resolved_warning();
|
2023-11-02 19:29:54 +01:00
|
|
|
$("textarea#compose-textarea").removeData("draft-id");
|
|
|
|
compose_ui.autosize_textarea($("textarea#compose-textarea"));
|
2022-12-10 01:20:07 +01:00
|
|
|
compose_banner.clear_errors();
|
|
|
|
compose_banner.clear_warnings();
|
2023-06-23 20:45:04 +02:00
|
|
|
compose_banner.clear_uploads();
|
2021-11-30 06:26:05 +01:00
|
|
|
compose_ui.hide_compose_spinner();
|
2023-10-04 00:18:21 +02:00
|
|
|
scheduled_messages.reset_selected_schedule_timestamp();
|
2022-07-16 14:40:59 +02:00
|
|
|
$(".compose_control_button_container:has(.add-poll)").removeClass("disabled-on-hover");
|
2013-12-03 20:59:23 +01:00
|
|
|
}
|
|
|
|
|
2023-10-16 12:55:24 +02:00
|
|
|
export function send_message_success(request, data) {
|
|
|
|
if (!request.locally_echoed) {
|
2023-11-02 19:29:54 +01:00
|
|
|
if ($("textarea#compose-textarea").data("draft-id")) {
|
|
|
|
drafts.draft_model.deleteDraft($("textarea#compose-textarea").data("draft-id"));
|
2023-01-25 23:17:51 +01:00
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
clear_compose_box();
|
|
|
|
}
|
|
|
|
|
2023-10-16 12:55:24 +02:00
|
|
|
echo.reify_message_id(request.local_id, data.id);
|
|
|
|
|
|
|
|
if (request.type === "stream") {
|
|
|
|
if (data.automatic_new_visibility_policy) {
|
2023-12-02 13:34:05 +01:00
|
|
|
if (!onboarding_steps.ONE_TIME_NOTICES_TO_DISPLAY.has("visibility_policy_banner")) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-16 12:55:24 +02:00
|
|
|
// topic has been automatically unmuted or followed. No need to
|
|
|
|
// suggest the user to unmute. Show the banner and return.
|
|
|
|
compose_notifications.notify_automatic_new_visibility_policy(request, data);
|
2023-12-02 13:34:05 +01:00
|
|
|
onboarding_steps.post_onboarding_step_as_read("visibility_policy_banner");
|
2023-10-16 12:55:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const muted_narrow = compose_notifications.get_muted_narrow(request);
|
|
|
|
if (muted_narrow) {
|
|
|
|
compose_notifications.notify_unmute(muted_narrow, request.stream_id, request.topic);
|
|
|
|
}
|
|
|
|
}
|
2021-02-28 00:51:57 +01:00
|
|
|
}
|
2017-07-13 15:01:02 +02:00
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
export function send_message(request = create_message_object()) {
|
2023-01-09 16:45:16 +01:00
|
|
|
compose_state.set_recipient_edited_manually(false);
|
2013-09-11 20:42:12 +02:00
|
|
|
if (request.type === "private") {
|
|
|
|
request.to = JSON.stringify(request.to);
|
|
|
|
} else {
|
|
|
|
request.to = JSON.stringify([request.to]);
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let local_id;
|
|
|
|
let locally_echoed;
|
2017-05-09 19:34:42 +02:00
|
|
|
|
2023-06-28 13:02:28 +02:00
|
|
|
const message = echo.try_deliver_locally(request, message_events.insert_new_messages);
|
2020-04-09 19:22:30 +02:00
|
|
|
if (message) {
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
// We are rendering this message locally with an id
|
|
|
|
// like 92l99.01 that corresponds to a reasonable
|
|
|
|
// approximation of the id we'll get from the server
|
|
|
|
// in terms of sorting messages.
|
2020-04-09 19:22:30 +02:00
|
|
|
local_id = message.local_id;
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
locally_echoed = true;
|
|
|
|
} else {
|
|
|
|
// We are not rendering this message locally, but we
|
|
|
|
// track the message's life cycle with an id like
|
|
|
|
// loc-1, loc-2, loc-3,etc.
|
|
|
|
locally_echoed = false;
|
|
|
|
local_id = sent_messages.get_new_local_id();
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
2017-07-14 19:30:23 +02:00
|
|
|
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
request.local_id = local_id;
|
2017-07-17 16:52:57 +02:00
|
|
|
request.locally_echoed = locally_echoed;
|
2023-05-03 00:01:10 +02:00
|
|
|
request.resend = false;
|
2017-07-17 16:52:57 +02:00
|
|
|
|
2013-11-06 18:57:38 +01:00
|
|
|
function success(data) {
|
2023-10-16 12:55:24 +02:00
|
|
|
send_message_success(request, data);
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
2013-11-04 23:58:51 +01:00
|
|
|
|
2023-11-21 10:39:13 +01:00
|
|
|
function error(response, server_error_code) {
|
|
|
|
// Error callback for failed message send attempts.
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
if (!locally_echoed) {
|
2023-11-21 10:39:13 +01:00
|
|
|
if (server_error_code === "TOPIC_WILDCARD_MENTION_NOT_ALLOWED") {
|
|
|
|
// The topic wildcard mention permission code path has
|
|
|
|
// a special error.
|
|
|
|
const new_row = render_wildcard_mention_not_allowed_error({
|
|
|
|
banner_type: compose_banner.ERROR,
|
|
|
|
classname: compose_banner.CLASSNAMES.wildcards_not_allowed,
|
|
|
|
});
|
|
|
|
compose_banner.append_compose_banner_to_banner_list(new_row, $("#compose_banners"));
|
|
|
|
} else {
|
|
|
|
compose_banner.show_error_message(
|
|
|
|
response,
|
|
|
|
compose_banner.CLASSNAMES.generic_compose_error,
|
|
|
|
$("#compose_banners"),
|
|
|
|
$("textarea#compose-textarea"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-22 06:00:28 +01:00
|
|
|
// For messages that were not locally echoed, we're
|
|
|
|
// responsible for hiding the compose spinner to restore
|
|
|
|
// the compose box so one can send a next message.
|
|
|
|
//
|
|
|
|
// (Restoring this state is handled by clear_compose_box
|
|
|
|
// for locally echoed messages.)
|
|
|
|
compose_ui.hide_compose_spinner();
|
2013-12-19 17:03:08 +01:00
|
|
|
return;
|
2013-12-03 20:59:23 +01:00
|
|
|
}
|
2013-10-31 15:56:30 +01:00
|
|
|
|
2020-04-09 17:43:30 +02:00
|
|
|
echo.message_send_error(message.id, response);
|
2022-10-07 18:58:30 +02:00
|
|
|
|
|
|
|
// We might not have updated the draft count because we assumed the
|
|
|
|
// message would send. Ensure that the displayed count is correct.
|
|
|
|
drafts.sync_count();
|
2013-09-11 20:42:12 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 13:08:50 +01:00
|
|
|
transmit.send_message(request, success, error);
|
2020-07-15 00:34:28 +02:00
|
|
|
server_events.assert_get_events_running(
|
|
|
|
"Restarting get_events because it was not running during send",
|
|
|
|
);
|
2013-12-03 20:59:23 +01:00
|
|
|
|
2017-05-09 19:34:42 +02:00
|
|
|
if (locally_echoed) {
|
2013-12-03 20:59:23 +01:00
|
|
|
clear_compose_box();
|
2023-03-17 19:54:09 +01:00
|
|
|
// Schedule a timer to display a spinner when the message is
|
|
|
|
// taking a longtime to send.
|
|
|
|
setTimeout(() => echo.display_slow_send_loading_spinner(message), 5000);
|
2013-12-03 20:59:23 +01:00
|
|
|
}
|
2021-02-28 00:51:57 +01:00
|
|
|
}
|
2012-10-29 18:06:53 +01:00
|
|
|
|
2022-08-12 11:37:47 +02:00
|
|
|
export function enter_with_preview_open(ctrl_pressed = false) {
|
|
|
|
if (
|
|
|
|
(user_settings.enter_sends && !ctrl_pressed) ||
|
|
|
|
(!user_settings.enter_sends && ctrl_pressed)
|
|
|
|
) {
|
|
|
|
// If this enter should send, we attempt to send the message.
|
2021-02-28 00:51:57 +01:00
|
|
|
finish();
|
2017-02-19 07:43:02 +01:00
|
|
|
} else {
|
2022-08-16 23:34:11 +02:00
|
|
|
// Otherwise, we return to the normal compose state.
|
|
|
|
clear_preview_area();
|
2017-02-19 07:43:02 +01:00
|
|
|
}
|
2021-02-28 00:51:57 +01:00
|
|
|
}
|
2017-02-19 07:43:02 +01:00
|
|
|
|
2022-07-28 09:31:57 +02:00
|
|
|
// Common entrypoint for asking the server to send the message
|
|
|
|
// currently drafted in the compose box, including for scheduled
|
|
|
|
// messages.
|
2023-05-04 15:43:44 +02:00
|
|
|
export function finish(scheduling_message = false) {
|
2022-07-28 09:31:57 +02:00
|
|
|
if (compose_ui.compose_spinner_visible) {
|
|
|
|
// Avoid sending a message twice in parallel in races where
|
|
|
|
// the user clicks the `Send` button very quickly twice or
|
|
|
|
// presses enter and the send button simultaneously.
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:51:57 +01:00
|
|
|
clear_preview_area();
|
|
|
|
clear_invites();
|
|
|
|
clear_private_stream_alert();
|
2023-02-02 03:38:52 +01:00
|
|
|
compose_banner.clear_message_sent_banners();
|
2013-09-16 21:13:11 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_content = compose_state.message_content();
|
2018-06-02 13:59:02 +02:00
|
|
|
|
|
|
|
// Skip normal validation for zcommands, since they aren't
|
|
|
|
// actual messages with recipients; users only send them
|
|
|
|
// from the compose box for convenience sake.
|
|
|
|
if (zcommand.process(message_content)) {
|
2021-02-28 00:51:57 +01:00
|
|
|
do_post_send_tasks();
|
2018-06-02 13:59:02 +02:00
|
|
|
clear_compose_box();
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-06-02 13:59:02 +02:00
|
|
|
}
|
|
|
|
|
2021-11-30 06:26:05 +01:00
|
|
|
compose_ui.show_compose_spinner();
|
|
|
|
|
2023-05-04 15:43:44 +02:00
|
|
|
if (!compose_validate.validate(scheduling_message)) {
|
2021-11-30 06:26:05 +01:00
|
|
|
// If the message failed validation, hide compose spinner.
|
|
|
|
compose_ui.hide_compose_spinner();
|
2012-10-29 22:37:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-04 21:31:23 +01:00
|
|
|
|
2023-05-04 15:43:44 +02:00
|
|
|
if (scheduling_message) {
|
2023-04-14 21:34:41 +02:00
|
|
|
schedule_message_to_custom_date();
|
2018-01-04 21:31:23 +01:00
|
|
|
} else {
|
2021-02-28 00:51:57 +01:00
|
|
|
send_message();
|
2018-01-04 21:31:23 +01:00
|
|
|
}
|
2021-02-28 00:51:57 +01:00
|
|
|
do_post_send_tasks();
|
2018-06-02 13:38:40 +02:00
|
|
|
return true;
|
2021-02-28 00:51:57 +01:00
|
|
|
}
|
2018-06-02 13:38:40 +02:00
|
|
|
|
2021-02-28 00:51:57 +01:00
|
|
|
export function do_post_send_tasks() {
|
|
|
|
clear_preview_area();
|
2012-11-07 19:59:35 +01:00
|
|
|
// TODO: Do we want to fire the event even if the send failed due
|
|
|
|
// to a server-side error?
|
2020-12-11 04:26:23 +01:00
|
|
|
$(document).trigger("compose_finished.zulip");
|
2021-02-28 00:51:57 +01:00
|
|
|
}
|
2012-10-18 20:16:56 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function render_and_show_preview($preview_spinner, $preview_content_box, content) {
|
2019-02-05 15:21:30 +01:00
|
|
|
function show_preview(rendered_content, raw_content) {
|
|
|
|
// content is passed to check for status messages ("/me ...")
|
|
|
|
// and will be undefined in case of errors
|
2019-11-02 00:06:25 +01:00
|
|
|
let rendered_preview_html;
|
2020-07-15 00:34:28 +02:00
|
|
|
if (raw_content !== undefined && markdown.is_status_message(raw_content)) {
|
2019-02-05 15:21:30 +01:00
|
|
|
// Handle previews of /me messages
|
2020-07-15 00:34:28 +02:00
|
|
|
rendered_preview_html =
|
|
|
|
"<p><strong>" +
|
2021-02-03 23:48:26 +01:00
|
|
|
_.escape(page_params.full_name) +
|
2020-07-15 00:34:28 +02:00
|
|
|
"</strong>" +
|
|
|
|
rendered_content.slice("<p>/me".length);
|
2019-02-05 15:21:30 +01:00
|
|
|
} else {
|
|
|
|
rendered_preview_html = rendered_content;
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$preview_content_box.html(util.clean_user_content_links(rendered_preview_html));
|
|
|
|
rendered_markdown.update_elements($preview_content_box);
|
2019-02-05 15:21:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (content.length === 0) {
|
2021-04-14 00:59:25 +02:00
|
|
|
show_preview($t_html({defaultMessage: "Nothing to preview"}));
|
2019-02-05 15:21:30 +01:00
|
|
|
} else {
|
2020-07-16 23:29:01 +02:00
|
|
|
if (markdown.contains_backend_only_syntax(content)) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $spinner = $preview_spinner.expectOne();
|
|
|
|
loading.make_indicator($spinner);
|
2019-02-05 15:21:30 +01:00
|
|
|
} else {
|
2020-06-29 23:20:51 +02:00
|
|
|
// For messages that don't appear to contain syntax that
|
2020-08-11 01:47:49 +02:00
|
|
|
// is only supported by our backend Markdown processor, we
|
|
|
|
// render using the frontend Markdown processor (but still
|
2020-06-29 23:20:51 +02:00
|
|
|
// render server-side to ensure the preview is accurate;
|
|
|
|
// if the `markdown.contains_backend_only_syntax` logic is
|
|
|
|
// wrong, users will see a brief flicker of the locally
|
|
|
|
// echoed frontend rendering before receiving the
|
|
|
|
// authoritative backend rendering from the server).
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_obj = {
|
2019-02-05 15:21:30 +01:00
|
|
|
raw_content: content,
|
|
|
|
};
|
|
|
|
markdown.apply_markdown(message_obj);
|
|
|
|
}
|
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/render",
|
2020-07-20 22:18:43 +02:00
|
|
|
data: {content},
|
|
|
|
success(response_data) {
|
2019-02-05 15:21:30 +01:00
|
|
|
if (markdown.contains_backend_only_syntax(content)) {
|
2022-01-25 11:36:19 +01:00
|
|
|
loading.destroy_indicator($preview_spinner);
|
2019-02-05 15:21:30 +01:00
|
|
|
}
|
|
|
|
show_preview(response_data.rendered, content);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error() {
|
2019-02-05 15:21:30 +01:00
|
|
|
if (markdown.contains_backend_only_syntax(content)) {
|
2022-01-25 11:36:19 +01:00
|
|
|
loading.destroy_indicator($preview_spinner);
|
2019-02-05 15:21:30 +01:00
|
|
|
}
|
2021-04-14 00:59:25 +02:00
|
|
|
show_preview($t_html({defaultMessage: "Failed to generate preview"}));
|
2019-02-05 15:21:30 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-02-28 00:51:57 +01:00
|
|
|
}
|
2019-02-05 15:21:30 +01:00
|
|
|
|
2023-04-14 21:34:41 +02:00
|
|
|
function schedule_message_to_custom_date() {
|
2023-04-20 04:34:08 +02:00
|
|
|
const compose_message_object = create_message_object();
|
|
|
|
|
2023-10-04 00:18:21 +02:00
|
|
|
const deliver_at = scheduled_messages.get_formatted_selected_send_later_time();
|
|
|
|
const scheduled_delivery_timestamp = scheduled_messages.get_selected_send_later_timestamp();
|
2023-04-20 04:34:08 +02:00
|
|
|
|
|
|
|
const message_type = compose_message_object.type;
|
|
|
|
let req_type;
|
|
|
|
|
|
|
|
if (message_type === "private") {
|
|
|
|
req_type = "direct";
|
|
|
|
} else {
|
|
|
|
req_type = message_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
const scheduled_message_data = {
|
|
|
|
type: req_type,
|
|
|
|
to: JSON.stringify(compose_message_object.to),
|
|
|
|
topic: compose_message_object.topic,
|
|
|
|
content: compose_message_object.content,
|
|
|
|
scheduled_delivery_timestamp,
|
|
|
|
};
|
|
|
|
|
2023-06-01 02:26:36 +02:00
|
|
|
const $banner_container = $("#compose_banners");
|
|
|
|
const success = function (data) {
|
2023-11-02 19:29:54 +01:00
|
|
|
drafts.draft_model.deleteDraft($("textarea#compose-textarea").data("draft-id"));
|
2023-06-01 02:26:36 +02:00
|
|
|
clear_compose_box();
|
|
|
|
const new_row = render_success_message_scheduled_banner({
|
|
|
|
scheduled_message_id: data.scheduled_message_id,
|
|
|
|
deliver_at,
|
|
|
|
});
|
|
|
|
compose_banner.clear_message_sent_banners();
|
|
|
|
compose_banner.append_compose_banner_to_banner_list(new_row, $banner_container);
|
|
|
|
};
|
|
|
|
|
|
|
|
const error = function (xhr) {
|
|
|
|
const response = channel.xhr_error_message("Error sending message", xhr);
|
|
|
|
compose_ui.hide_compose_spinner();
|
|
|
|
compose_banner.show_error_message(
|
|
|
|
response,
|
|
|
|
compose_banner.CLASSNAMES.generic_compose_error,
|
|
|
|
$banner_container,
|
2023-11-02 19:29:54 +01:00
|
|
|
$("textarea#compose-textarea"),
|
2023-06-01 02:26:36 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
channel.post({
|
|
|
|
url: "/json/scheduled_messages",
|
|
|
|
data: scheduled_message_data,
|
|
|
|
success,
|
|
|
|
error,
|
|
|
|
});
|
2023-04-14 21:34:41 +02:00
|
|
|
}
|