2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = require("./people");
|
2020-02-13 22:34:29 +01:00
|
|
|
const util = require("./util");
|
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
|
|
|
|
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;
|
2020-07-15 01:29:15 +02:00
|
|
|
const retry_spinner = row.find(".refresh-failed-message");
|
|
|
|
retry_spinner.toggleClass("rotating", true);
|
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
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
retry_spinner.toggleClass("rotating", false);
|
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) {
|
2020-04-09 17:43:30 +02:00
|
|
|
exports.message_send_error(message.id, response);
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2020-07-15 01:29:15 +02:00
|
|
|
retry_spinner.toggleClass("rotating", false);
|
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
|
|
|
}
|
|
|
|
|
2020-01-22 11:42:05 +01:00
|
|
|
exports.build_display_recipient = function (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.
|
|
|
|
//
|
|
|
|
// TODO: It might be cleaner for the webapp for such
|
|
|
|
// 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;
|
2020-01-22 11:42:05 +01:00
|
|
|
};
|
|
|
|
|
2020-02-12 09:32:25 +01:00
|
|
|
exports.insert_local_message = function (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
|
|
|
|
2020-06-11 14:32:12 +02:00
|
|
|
message.display_recipient = exports.build_display_recipient(message);
|
2020-01-22 11:42:05 +01:00
|
|
|
local_message.insert_message(message);
|
2020-04-09 19:22:30 +02:00
|
|
|
return message;
|
2020-01-22 11:37:57 +01:00
|
|
|
};
|
2014-01-16 20:18:55 +01:00
|
|
|
|
2018-05-16 22:05:11 +02:00
|
|
|
exports.is_slash_command = function (content) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return !content.startsWith("/me") && content.startsWith("/");
|
2018-05-16 22:05:11 +02:00
|
|
|
};
|
|
|
|
|
2020-04-09 20:48:27 +02:00
|
|
|
exports.try_deliver_locally = function (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
|
|
|
}
|
|
|
|
|
2018-05-16 22:05:11 +02:00
|
|
|
if (exports.is_slash_command(message_request.content)) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-05-16 22:05:11 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 09:45:12 +02:00
|
|
|
if (!current_msg_list.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
|
|
|
}
|
|
|
|
|
2020-04-09 19:22:30 +02:00
|
|
|
const message = exports.insert_local_message(message_request, local_id_float);
|
|
|
|
return message;
|
2013-12-19 17:03:08 +01:00
|
|
|
};
|
|
|
|
|
2020-04-09 20:48:27 +02:00
|
|
|
exports.edit_locally = function (message, request) {
|
2019-04-22 20:13:23 +02:00
|
|
|
// Responsible for doing the rendering work of locally editing the
|
|
|
|
// content ofa message. This is used in several code paths:
|
|
|
|
// * 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
|
|
|
|
2014-03-04 16:47:44 +01:00
|
|
|
home_msg_list.view.rerender_messages([message]);
|
2016-04-25 23:45:25 +02:00
|
|
|
if (current_msg_list === message_list.narrowed) {
|
|
|
|
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();
|
2014-01-02 19:39:22 +01:00
|
|
|
};
|
|
|
|
|
2020-04-09 20:48:27 +02:00
|
|
|
exports.reify_message_id = function (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
|
|
|
|
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);
|
2017-07-19 14:39:28 +02:00
|
|
|
notifications.reify_message_id(opts);
|
2020-05-28 09:48:58 +02:00
|
|
|
recent_topics.reify_message_id_if_available(opts);
|
2013-12-19 17:03:08 +01:00
|
|
|
};
|
|
|
|
|
2020-04-09 20:48:27 +02:00
|
|
|
exports.process_from_server = function (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
|
|
|
}
|
|
|
|
|
2020-04-09 21:09:17 +02:00
|
|
|
exports.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.
|
2014-02-11 16:19:42 +01:00
|
|
|
home_msg_list.view.rerender_messages(msgs_to_rerender);
|
2016-04-25 23:45:25 +02:00
|
|
|
if (current_msg_list === message_list.narrowed) {
|
|
|
|
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;
|
2013-12-19 17:03:08 +01:00
|
|
|
};
|
|
|
|
|
2020-04-09 20:48:27 +02:00
|
|
|
exports._patch_waiting_for_ack = function (data) {
|
2019-06-21 07:33:02 +02:00
|
|
|
// Only for testing
|
|
|
|
waiting_for_ack = data;
|
|
|
|
};
|
|
|
|
|
2020-04-09 17:43:30 +02:00
|
|
|
exports.message_send_error = function (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);
|
2013-12-19 17:03:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function abort_message(message) {
|
|
|
|
// Remove in all lists in which it exists
|
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 msg_list of [message_list.all, home_msg_list, current_msg_list]) {
|
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
|
|
|
}
|
|
|
|
|
2018-05-15 22:03:14 +02:00
|
|
|
exports.initialize = function () {
|
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);
|
2018-05-15 22:03:14 +02:00
|
|
|
};
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.echo = exports;
|