2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-10 17:01:31 +01:00
|
|
|
import * as alert_words from "./alert_words";
|
2021-03-30 06:23:09 +02:00
|
|
|
import {all_messages_data} from "./all_messages_data";
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as compose from "./compose";
|
2021-09-23 17:43:30 +02:00
|
|
|
import * as compose_ui from "./compose_ui";
|
2021-06-14 11:08:54 +02:00
|
|
|
import * as drafts from "./drafts";
|
2021-02-28 00:49:57 +01:00
|
|
|
import * as local_message from "./local_message";
|
2021-02-28 00:49:36 +01:00
|
|
|
import * as markdown from "./markdown";
|
2021-03-26 14:39:40 +01:00
|
|
|
import * as message_events from "./message_events";
|
2021-02-28 21:31:33 +01:00
|
|
|
import * as message_list from "./message_list";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 01:10:03 +01:00
|
|
|
import * as message_store from "./message_store";
|
2021-02-28 00:47:56 +01:00
|
|
|
import * as narrow_state from "./narrow_state";
|
2021-02-28 01:06:34 +01:00
|
|
|
import * as notifications from "./notifications";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-10 16:53:37 +01:00
|
|
|
import * as people from "./people";
|
2021-02-28 00:44:12 +01:00
|
|
|
import * as pm_list from "./pm_list";
|
2021-02-28 01:03:09 +01:00
|
|
|
import * as popovers from "./popovers";
|
2021-06-10 14:18:46 +02:00
|
|
|
import * as recent_topics_data from "./recent_topics_data";
|
2021-02-28 00:42:30 +01:00
|
|
|
import * as rows from "./rows";
|
2021-02-28 00:50:19 +01:00
|
|
|
import * as sent_messages from "./sent_messages";
|
2021-02-28 21:31:02 +01:00
|
|
|
import * as stream_list from "./stream_list";
|
2021-02-28 00:54:32 +01:00
|
|
|
import * as stream_topic_history from "./stream_topic_history";
|
2021-02-28 00:51:22 +01:00
|
|
|
import * as transmit from "./transmit";
|
2021-02-28 21:33:10 +01:00
|
|
|
import * as ui from "./ui";
|
2021-02-10 16:53:37 +01:00
|
|
|
import * as util from "./util";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2018-11-30 00:48:13 +01:00
|
|
|
// Docs: https://zulip.readthedocs.io/en/latest/subsystems/sending-messages.html
|
2013-12-04 17:16:08 +01:00
|
|
|
|
2020-02-12 05:59:50 +01:00
|
|
|
const waiting_for_id = new Map();
|
2020-02-12 06:02:56 +01:00
|
|
|
let waiting_for_ack = new Map();
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2021-05-07 08:49:29 +02:00
|
|
|
// These retry spinner functions return true if and only if the
|
|
|
|
// spinner already is in the requested state, which can be used to
|
|
|
|
// avoid sending duplicate requests.
|
|
|
|
function show_retry_spinner(row) {
|
|
|
|
const retry_spinner = row.find(".refresh-failed-message");
|
|
|
|
|
|
|
|
if (!retry_spinner.hasClass("rotating")) {
|
|
|
|
retry_spinner.toggleClass("rotating", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hide_retry_spinner(row) {
|
|
|
|
const retry_spinner = row.find(".refresh-failed-message");
|
|
|
|
|
|
|
|
if (retry_spinner.hasClass("rotating")) {
|
|
|
|
retry_spinner.toggleClass("rotating", false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-26 14:39:40 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-12 23:21:05 +02:00
|
|
|
function failed_message_success(message_id) {
|
|
|
|
message_store.get(message_id).failed_request = false;
|
|
|
|
ui.show_failed_message_success(message_id);
|
|
|
|
}
|
|
|
|
|
2014-01-03 20:39:12 +01:00
|
|
|
function resend_message(message, row) {
|
|
|
|
message.content = message.raw_content;
|
2021-05-07 08:49:29 +02:00
|
|
|
if (show_retry_spinner(row)) {
|
|
|
|
// retry already in in progress
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2014-01-03 20:39:12 +01:00
|
|
|
// Always re-set queue_id if we've gotten a new one
|
|
|
|
// since the time when the message object was initially created
|
2017-04-24 21:40:16 +02:00
|
|
|
message.queue_id = page_params.queue_id;
|
2014-01-03 20:39:12 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const 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
|
|
|
|
|
|
|
function on_success(data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = data.id;
|
|
|
|
const locally_echoed = true;
|
2014-01-03 20:39:12 +01:00
|
|
|
|
2021-05-07 08:49:29 +02:00
|
|
|
hide_retry_spinner(row);
|
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
|
|
|
|
|
|
|
compose.send_message_success(local_id, message_id, locally_echoed);
|
2014-01-03 20:39:12 +01:00
|
|
|
|
|
|
|
// Resend succeeded, so mark as no longer failed
|
2020-07-12 23:21:05 +02:00
|
|
|
failed_message_success(message_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
|
|
|
}
|
|
|
|
|
|
|
|
function on_error(response) {
|
2021-02-10 16:53:37 +01:00
|
|
|
message_send_error(message.id, response);
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2021-05-07 08:49:29 +02:00
|
|
|
hide_retry_spinner(row);
|
2019-03-19 21:29:53 +01:00
|
|
|
}, 300);
|
2014-01-03 20:39:12 +01:00
|
|
|
blueslip.log("Manual resend of message failed");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
sent_messages.start_resend(local_id);
|
2018-02-20 13:08:50 +01:00
|
|
|
transmit.send_message(message, on_success, on_error);
|
2014-01-03 20:39:12 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function build_display_recipient(message) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (message.type === "stream") {
|
2020-01-23 21:44:23 +01:00
|
|
|
return message.stream;
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 21:44:23 +01:00
|
|
|
// Build a display recipient with the full names of each
|
|
|
|
// recipient. Note that it's important that use
|
|
|
|
// util.extract_pm_recipients, which filters out any spurious
|
|
|
|
// ", " at the end of the recipient list
|
|
|
|
const emails = util.extract_pm_recipients(message.private_message_recipient);
|
|
|
|
|
2020-01-31 21:46:03 +01:00
|
|
|
let sender_in_display_recipients = false;
|
2020-07-02 01:39:34 +02:00
|
|
|
const display_recipient = emails.map((email) => {
|
2020-01-23 21:44:23 +01:00
|
|
|
email = email.trim();
|
|
|
|
const person = people.get_by_email(email);
|
|
|
|
if (person === undefined) {
|
|
|
|
// For unknown users, we return a skeleton object.
|
2020-01-23 21:47:08 +01:00
|
|
|
//
|
|
|
|
// This allows us to support zephyr mirroring situations
|
|
|
|
// where the server might dynamically create users in
|
|
|
|
// response to messages being sent to their email address.
|
|
|
|
//
|
2021-05-14 00:16:30 +02:00
|
|
|
// TODO: It might be cleaner for the web app for such
|
2020-01-23 21:47:08 +01:00
|
|
|
// dynamic user creation to happen inside a separate API
|
|
|
|
// call when the pill is constructed, and then enforcing
|
|
|
|
// the requirement that we have an actual user object in
|
|
|
|
// `people.js` when sending messages.
|
2020-01-23 21:44:23 +01:00
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
email,
|
2020-01-23 21:44:23 +01:00
|
|
|
full_name: email,
|
|
|
|
unknown_local_echo_user: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-31 21:46:03 +01:00
|
|
|
if (person.user_id === message.sender_id) {
|
|
|
|
sender_in_display_recipients = true;
|
2020-01-23 07:52:08 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 21:44:23 +01:00
|
|
|
// NORMAL PATH
|
|
|
|
//
|
|
|
|
// This should match the format of display_recipient
|
|
|
|
// objects generated by the backend code in models.py,
|
|
|
|
// which is why we create a new object with a `.id` field
|
|
|
|
// rather than a `.user_id` field.
|
|
|
|
return {
|
|
|
|
id: person.user_id,
|
|
|
|
email: person.email,
|
|
|
|
full_name: person.full_name,
|
|
|
|
};
|
|
|
|
});
|
2020-01-23 07:52:08 +01:00
|
|
|
|
2020-01-31 21:46:03 +01:00
|
|
|
if (!sender_in_display_recipients) {
|
2020-01-23 07:52:08 +01:00
|
|
|
// Ensure that the current user is included in
|
|
|
|
// display_recipient for group PMs.
|
|
|
|
display_recipient.push({
|
|
|
|
id: message.sender_id,
|
|
|
|
email: message.sender_email,
|
|
|
|
full_name: message.sender_full_name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return display_recipient;
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|
2020-01-22 11:42:05 +01:00
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function insert_local_message(message_request, local_id_float) {
|
2020-01-22 11:42:05 +01:00
|
|
|
// 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
|
2020-07-16 22:40:18 +02:00
|
|
|
const message = {...message_request};
|
2020-01-22 11:42:05 +01:00
|
|
|
|
|
|
|
// Locally delivered messages cannot be unread (since we sent them), nor
|
|
|
|
// can they alert the user.
|
|
|
|
message.unread = false;
|
|
|
|
|
|
|
|
message.raw_content = message.content;
|
|
|
|
|
|
|
|
// NOTE: This will parse synchronously. We're not using the async pipeline
|
|
|
|
markdown.apply_markdown(message);
|
2018-02-24 13:42:27 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
message.content_type = "text/html";
|
2020-01-22 11:42:05 +01:00
|
|
|
message.sender_email = people.my_current_email();
|
|
|
|
message.sender_full_name = people.my_full_name();
|
|
|
|
message.avatar_url = page_params.avatar_url;
|
2021-02-05 21:20:14 +01:00
|
|
|
message.timestamp = Date.now() / 1000;
|
2020-02-12 09:32:25 +01:00
|
|
|
message.local_id = local_id_float.toString();
|
2020-01-22 11:42:05 +01:00
|
|
|
message.locally_echoed = true;
|
2020-02-12 09:32:25 +01:00
|
|
|
message.id = local_id_float;
|
2020-01-22 11:42:05 +01:00
|
|
|
markdown.add_topic_links(message);
|
|
|
|
|
2020-02-12 05:59:50 +01:00
|
|
|
waiting_for_id.set(message.local_id, message);
|
2020-02-12 06:02:56 +01:00
|
|
|
waiting_for_ack.set(message.local_id, message);
|
2020-01-22 11:42:05 +01:00
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
message.display_recipient = build_display_recipient(message);
|
2021-03-26 14:39:40 +01:00
|
|
|
insert_message(message);
|
2020-04-09 19:22:30 +02:00
|
|
|
return message;
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|
2014-01-16 20:18:55 +01:00
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function is_slash_command(content) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return !content.startsWith("/me") && content.startsWith("/");
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|
2018-05-16 22:05:11 +02:00
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function try_deliver_locally(message_request) {
|
2017-07-29 02:51:33 +02:00
|
|
|
if (markdown.contains_backend_only_syntax(message_request.content)) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2014-01-16 20:18:55 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 19:05:31 +02:00
|
|
|
if (narrow_state.active() && !narrow_state.filter().can_apply_locally(true)) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2014-01-16 20:18:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
if (is_slash_command(message_request.content)) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-05-16 22:05:11 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
if (!message_lists.current.data.fetch_status.has_found_newest()) {
|
2020-02-24 20:50:12 +01:00
|
|
|
// If the current message list doesn't yet have the latest
|
|
|
|
// messages before the one we just sent, local echo would make
|
|
|
|
// it appear as though there were no messages between what we
|
|
|
|
// have and the new message we just sent, when in fact we're
|
|
|
|
// in the process of fetching those from the server. In this
|
|
|
|
// case, it's correct to skip local echo; we'll get the
|
|
|
|
// message we just sent placed appropriately when we get it
|
|
|
|
// from either server_events or message_fetch.
|
2020-02-22 12:09:46 +01:00
|
|
|
blueslip.info("Skipping local echo until newest messages get loaded.");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-02-22 12:09:46 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 09:32:25 +01:00
|
|
|
const local_id_float = local_message.get_next_id_float();
|
2017-07-17 22:25:25 +02:00
|
|
|
|
2020-02-12 09:32:25 +01:00
|
|
|
if (!local_id_float) {
|
2017-07-17 22:25:25 +02:00
|
|
|
// This can happen for legit reasons.
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2014-01-24 17:15:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-08 02:59:08 +02:00
|
|
|
// Save a locally echoed message in drafts, so it cannot be
|
|
|
|
// lost. It will be cleared if the message is sent successfully.
|
|
|
|
// We ask the drafts system to not notify the user, since they'd
|
|
|
|
// be quite distracting in the very common case that the message
|
|
|
|
// sends normally.
|
|
|
|
const draft_id = drafts.update_draft({no_notify: true});
|
2021-06-14 11:08:54 +02:00
|
|
|
message_request.draft_id = draft_id;
|
|
|
|
|
2021-09-23 17:43:30 +02:00
|
|
|
// Now that we've committed to delivering the message locally, we
|
|
|
|
// shrink the compose-box if it is in the full-screen state. This
|
|
|
|
// would have happened anyway in clear_compose_box, however, we
|
|
|
|
// need to this operation before inserting the local message into
|
|
|
|
// the feed. Otherwise, the out-of-view notification will be
|
|
|
|
// always triggered on the top of compose-box, regardless of
|
|
|
|
// whether the message would be visible after shrinking compose,
|
|
|
|
// because compose occludes the whole screen.
|
|
|
|
if (compose_ui.is_full_size()) {
|
|
|
|
compose_ui.make_compose_box_original_size();
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
const message = insert_local_message(message_request, local_id_float);
|
2020-04-09 19:22:30 +02:00
|
|
|
return message;
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function edit_locally(message, request) {
|
2019-04-22 20:13:23 +02:00
|
|
|
// Responsible for doing the rendering work of locally editing the
|
2022-02-08 00:13:33 +01:00
|
|
|
// content of a message. This is used in several code paths:
|
2019-04-22 20:13:23 +02:00
|
|
|
// * Editing a message where a message was locally echoed but
|
|
|
|
// it got an error back from the server
|
|
|
|
// * Locally echoing any content-only edits to fully sent messages
|
|
|
|
// * Restoring the original content should the server return an
|
|
|
|
// error after having locally echoed content-only messages.
|
|
|
|
// The details of what should be changed are encoded in the request.
|
2019-11-20 23:20:39 +01:00
|
|
|
const raw_content = request.raw_content;
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_content_edited = raw_content !== undefined && message.raw_content !== raw_content;
|
2018-05-03 21:12:39 +02:00
|
|
|
|
2020-05-06 12:07:34 +02:00
|
|
|
if (request.new_topic !== undefined || request.new_stream_id !== undefined) {
|
|
|
|
const new_stream_id = request.new_stream_id;
|
2019-11-20 23:20:39 +01:00
|
|
|
const new_topic = request.new_topic;
|
2020-06-15 19:52:00 +02:00
|
|
|
stream_topic_history.remove_messages({
|
2017-07-26 14:05:25 +02:00
|
|
|
stream_id: message.stream_id,
|
2020-02-19 00:04:12 +01:00
|
|
|
topic_name: message.topic,
|
2020-06-15 19:52:00 +02:00
|
|
|
num_messages: 1,
|
2020-08-04 11:12:42 +02:00
|
|
|
max_removed_msg_id: message.id,
|
2017-07-26 14:05:25 +02:00
|
|
|
});
|
|
|
|
|
2020-05-06 12:07:34 +02:00
|
|
|
if (new_stream_id !== undefined) {
|
|
|
|
message.stream_id = new_stream_id;
|
|
|
|
}
|
|
|
|
if (new_topic !== undefined) {
|
|
|
|
message.topic = new_topic;
|
|
|
|
}
|
2017-07-26 14:05:25 +02:00
|
|
|
|
2020-03-22 18:40:05 +01:00
|
|
|
stream_topic_history.add_message({
|
2017-07-26 14:05:25 +02:00
|
|
|
stream_id: message.stream_id,
|
2020-02-19 00:04:12 +01:00
|
|
|
topic_name: message.topic,
|
2017-07-26 14:05:25 +02:00
|
|
|
message_id: message.id,
|
|
|
|
});
|
2014-01-02 19:39:22 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 21:12:39 +02:00
|
|
|
if (message_content_edited) {
|
|
|
|
message.raw_content = raw_content;
|
2019-04-22 20:13:23 +02:00
|
|
|
if (request.content !== undefined) {
|
|
|
|
// This happens in the code path where message editing
|
|
|
|
// failed and we're trying to undo the local echo. We use
|
|
|
|
// the saved content and flags rather than rendering; this
|
|
|
|
// is important in case
|
|
|
|
// markdown.contains_backend_only_syntax(message) is true.
|
|
|
|
message.content = request.content;
|
|
|
|
message.mentioned = request.mentioned;
|
|
|
|
message.mentioned_me_directly = request.mentioned_me_directly;
|
|
|
|
message.alerted = request.alerted;
|
|
|
|
} else {
|
2020-08-11 01:47:49 +02:00
|
|
|
// Otherwise, we Markdown-render the message; this resets
|
2019-04-22 20:13:23 +02:00
|
|
|
// all flags, so we need to restore those flags that are
|
|
|
|
// properties of how the user has interacted with the
|
|
|
|
// message, and not its rendering.
|
|
|
|
markdown.apply_markdown(message);
|
|
|
|
if (request.starred !== undefined) {
|
|
|
|
message.starred = request.starred;
|
|
|
|
}
|
|
|
|
if (request.historical !== undefined) {
|
|
|
|
message.historical = request.historical;
|
|
|
|
}
|
|
|
|
if (request.collapsed !== undefined) {
|
|
|
|
message.collapsed = request.collapsed;
|
|
|
|
}
|
|
|
|
}
|
2018-05-03 21:12:39 +02:00
|
|
|
}
|
2017-01-20 15:21:28 +01:00
|
|
|
|
2019-04-22 20:13:23 +02:00
|
|
|
// We don't have logic to adjust unread counts, because message
|
|
|
|
// reaching this code path must either have been sent by us or the
|
|
|
|
// topic isn't being edited, so unread counts can't have changed.
|
2014-01-02 19:39:22 +01:00
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.home.view.rerender_messages([message]);
|
|
|
|
if (message_lists.current === message_list.narrowed) {
|
2016-04-25 23:45:25 +02:00
|
|
|
message_list.narrowed.view.rerender_messages([message]);
|
2014-01-02 19:39:22 +01:00
|
|
|
}
|
|
|
|
stream_list.update_streams_sidebar();
|
2016-11-29 16:53:43 +01:00
|
|
|
pm_list.update_private_messages();
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|
2014-01-02 19:39:22 +01:00
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function reify_message_id(local_id, server_id) {
|
2020-02-12 05:59:50 +01:00
|
|
|
const message = waiting_for_id.get(local_id);
|
|
|
|
waiting_for_id.delete(local_id);
|
2013-12-19 17:03:08 +01:00
|
|
|
|
|
|
|
// reify_message_id is called both on receiving a self-sent message
|
|
|
|
// from the server, and on receiving the response to the send request
|
|
|
|
// Reification is only needed the first time the server id is found
|
|
|
|
if (message === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
message.id = server_id;
|
2017-07-17 16:52:57 +02:00
|
|
|
message.locally_echoed = false;
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2021-06-14 11:08:54 +02:00
|
|
|
if (message.draft_id) {
|
|
|
|
// Delete the draft if message was locally echoed
|
|
|
|
drafts.draft_model.deleteDraft(message.draft_id);
|
|
|
|
}
|
|
|
|
|
2020-10-07 09:17:30 +02:00
|
|
|
const opts = {old_id: Number.parseFloat(local_id), new_id: server_id};
|
2017-07-19 12:49:49 +02:00
|
|
|
|
|
|
|
message_store.reify_message_id(opts);
|
2021-03-28 19:08:25 +02:00
|
|
|
update_message_lists(opts);
|
2017-07-19 14:39:28 +02:00
|
|
|
notifications.reify_message_id(opts);
|
2021-06-10 14:18:46 +02:00
|
|
|
recent_topics_data.reify_message_id_if_available(opts);
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2021-03-28 19:08:25 +02:00
|
|
|
export function update_message_lists({old_id, new_id}) {
|
2021-03-30 06:23:09 +02:00
|
|
|
if (all_messages_data !== undefined) {
|
|
|
|
all_messages_data.change_message_id(old_id, new_id);
|
|
|
|
}
|
|
|
|
for (const msg_list of [message_lists.home, message_list.narrowed]) {
|
2021-03-28 19:08:25 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function process_from_server(messages) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const msgs_to_rerender = [];
|
|
|
|
const non_echo_messages = [];
|
2017-09-26 19:22:52 +02:00
|
|
|
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const message of messages) {
|
2013-12-19 17:03:08 +01:00
|
|
|
// In case we get the sent message before we get the send ACK, reify here
|
|
|
|
|
2020-04-09 21:09:17 +02:00
|
|
|
const local_id = message.local_id;
|
|
|
|
const client_message = waiting_for_ack.get(local_id);
|
2017-09-26 19:22:52 +02:00
|
|
|
if (client_message === undefined) {
|
|
|
|
// For messages that weren't locally echoed, we go through
|
|
|
|
// the "main" codepath that doesn't have to id reconciliation.
|
|
|
|
// We simply return non-echo messages to our caller.
|
|
|
|
non_echo_messages.push(message);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
continue;
|
2017-09-26 19:22:52 +02:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
reify_message_id(local_id, message.id);
|
2017-09-26 19:22:52 +02:00
|
|
|
|
2020-07-12 23:21:05 +02:00
|
|
|
if (message_store.get(message.id).failed_request) {
|
|
|
|
failed_message_success(message.id);
|
|
|
|
}
|
|
|
|
|
2017-09-26 19:22:52 +02:00
|
|
|
if (client_message.content !== message.content) {
|
|
|
|
client_message.content = message.content;
|
2020-04-09 21:09:17 +02:00
|
|
|
sent_messages.mark_disparity(local_id);
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
2017-09-26 21:12:27 +02:00
|
|
|
|
2017-12-16 23:25:31 +01:00
|
|
|
message_store.update_booleans(client_message, message.flags);
|
2017-10-12 22:11:43 +02:00
|
|
|
|
|
|
|
// We don't try to highlight alert words locally, so we have to
|
|
|
|
// do it now. (Note that we will indeed highlight alert words in
|
|
|
|
// messages that we sent to ourselves, since we might want to test
|
|
|
|
// that our alert words are set up correctly.)
|
|
|
|
alert_words.process_message(client_message);
|
|
|
|
|
2017-09-26 22:01:39 +02:00
|
|
|
// Previously, the message had the "local echo" timestamp set
|
|
|
|
// by the browser; if there was some round-trip delay to the
|
|
|
|
// server, the actual server-side timestamp could be slightly
|
|
|
|
// different. This corrects the frontend timestamp to match
|
|
|
|
// the backend.
|
2017-09-26 21:12:27 +02:00
|
|
|
client_message.timestamp = message.timestamp;
|
|
|
|
|
2020-02-14 13:39:04 +01:00
|
|
|
client_message.topic_links = message.topic_links;
|
2019-06-21 06:31:52 +02:00
|
|
|
client_message.is_me_message = message.is_me_message;
|
2018-02-12 15:12:40 +01:00
|
|
|
client_message.submessages = message.submessages;
|
|
|
|
|
2017-09-26 19:22:52 +02:00
|
|
|
msgs_to_rerender.push(client_message);
|
2020-04-09 21:09:17 +02:00
|
|
|
waiting_for_ack.delete(local_id);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2017-09-26 21:04:23 +02:00
|
|
|
if (msgs_to_rerender.length > 0) {
|
2017-09-26 22:01:39 +02:00
|
|
|
// In theory, we could just rerender messages where there were
|
|
|
|
// changes in either the rounded timestamp we display or the
|
|
|
|
// message content, but in practice, there's no harm to just
|
|
|
|
// doing it unconditionally.
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.home.view.rerender_messages(msgs_to_rerender);
|
|
|
|
if (message_lists.current === message_list.narrowed) {
|
2016-04-25 23:45:25 +02:00
|
|
|
message_list.narrowed.view.rerender_messages(msgs_to_rerender);
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 21:04:23 +02:00
|
|
|
|
2017-09-26 19:22:52 +02:00
|
|
|
return non_echo_messages;
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function _patch_waiting_for_ack(data) {
|
2019-06-21 07:33:02 +02:00
|
|
|
// Only for testing
|
|
|
|
waiting_for_ack = data;
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|
2019-06-21 07:33:02 +02:00
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function message_send_error(message_id, error_response) {
|
2013-12-19 17:03:08 +01:00
|
|
|
// Error sending message, show inline
|
2020-04-09 17:43:30 +02:00
|
|
|
message_store.get(message_id).failed_request = true;
|
|
|
|
ui.show_message_failed(message_id, error_response);
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
|
|
|
function abort_message(message) {
|
|
|
|
// Remove in all lists in which it exists
|
2021-03-30 06:23:09 +02:00
|
|
|
all_messages_data.remove([message.id]);
|
|
|
|
for (const msg_list of [message_lists.home, message_lists.current]) {
|
2020-11-12 22:03:45 +01:00
|
|
|
msg_list.remove_and_rerender([message.id]);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:53:37 +01:00
|
|
|
export function initialize() {
|
2021-02-03 23:23:32 +01:00
|
|
|
function on_failed_action(selector, callback) {
|
|
|
|
$("#main_div").on("click", selector, function (e) {
|
2013-12-19 17:03:08 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
popovers.hide_all();
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(this).closest(".message_row");
|
2020-04-09 21:31:15 +02:00
|
|
|
const local_id = rows.local_echo_id(row);
|
2013-12-19 17:03:08 +01:00
|
|
|
// Message should be waiting for ack and only have a local id,
|
|
|
|
// otherwise send would not have failed
|
2020-04-09 21:31:15 +02:00
|
|
|
const message = waiting_for_ack.get(local_id);
|
2013-12-19 17:03:08 +01:00
|
|
|
if (message === undefined) {
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.warn(
|
|
|
|
"Got resend or retry on failure request but did not find message in ack list " +
|
|
|
|
local_id,
|
|
|
|
);
|
2013-12-19 17:03:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-03 20:39:12 +01:00
|
|
|
callback(message, row);
|
2013-12-19 17:03:08 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
on_failed_action(".remove-failed-message", abort_message);
|
|
|
|
on_failed_action(".refresh-failed-message", resend_message);
|
2021-02-10 16:53:37 +01:00
|
|
|
}
|