message_events: Update code to handle messages not available locally.

Previously, if the client recieved an "update_messages" event with
"message_id" not present locally, then the event was completely
ignored. But, this can happen when moving messages especially
when doing a partial move due to time limit error.

This commit updates the code to have the code which requires the
message to be present locally run only if it is present instead
of just ignoring the event completely such that the updates for
moving the messages can be done.
This commit is contained in:
Sahil Batra
2023-04-25 20:19:43 +05:30
committed by Tim Abbott
parent 57a89e5228
commit 168680af23

View File

@@ -177,65 +177,69 @@ export function update_messages(events) {
for (const event of events) { for (const event of events) {
const anchor_message = message_store.get(event.message_id); const anchor_message = message_store.get(event.message_id);
if (anchor_message === undefined) { if (anchor_message !== undefined) {
continue; // Logic for updating the specific edited message only
} // needs to run if we had a local copy of the message.
delete anchor_message.local_edit_timestamp; delete anchor_message.local_edit_timestamp;
msgs_to_rerender.push(anchor_message); msgs_to_rerender.push(anchor_message);
message_store.update_booleans(anchor_message, event.flags); message_store.update_booleans(anchor_message, event.flags);
condense.un_cache_message_content_height(anchor_message.id); condense.un_cache_message_content_height(anchor_message.id);
if (event.rendered_content !== undefined) { if (event.rendered_content !== undefined) {
anchor_message.content = event.rendered_content; anchor_message.content = event.rendered_content;
}
if (event.is_me_message !== undefined) {
anchor_message.is_me_message = event.is_me_message;
}
// mark the current message edit attempt as complete.
message_edit.end_message_edit(event.message_id);
// Save the content edit to the front end anchor_message.edit_history
// before topic edits to ensure that combined topic / content
// edits have edit_history logged for both before any
// potential narrowing as part of the topic edit loop.
if (event.orig_content !== undefined) {
if (page_params.realm_allow_edit_history) {
// Note that we do this for topic edits separately, below.
// If an event changed both content and topic, we'll generate
// two client-side events, which is probably good for display.
const edit_history_entry = {
user_id: event.user_id,
prev_content: event.orig_content,
prev_rendered_content: event.orig_rendered_content,
prev_rendered_content_version: event.prev_rendered_content_version,
timestamp: event.edit_timestamp,
};
// Add message's edit_history in message dict
// For messages that are edited, edit_history needs to
// be added to message in frontend.
if (anchor_message.edit_history === undefined) {
anchor_message.edit_history = [];
}
anchor_message.edit_history = [edit_history_entry, ...anchor_message.edit_history];
} }
any_message_content_edited = true;
// Update raw_content, so that editing a few times in a row is fast. if (event.is_me_message !== undefined) {
anchor_message.raw_content = event.content; anchor_message.is_me_message = event.is_me_message;
} }
if (unread.update_message_for_mention(anchor_message, any_message_content_edited)) { // mark the current message edit attempt as complete.
const topic_key = recent_topics_util.get_topic_key( message_edit.end_message_edit(event.message_id);
anchor_message.stream_id,
anchor_message.topic, // Save the content edit to the front end anchor_message.edit_history
); // before topic edits to ensure that combined topic / content
recent_topics_ui.inplace_rerender(topic_key); // edits have edit_history logged for both before any
// potential narrowing as part of the topic edit loop.
if (event.orig_content !== undefined) {
if (page_params.realm_allow_edit_history) {
// Note that we do this for topic edits separately, below.
// If an event changed both content and topic, we'll generate
// two client-side events, which is probably good for display.
const edit_history_entry = {
user_id: event.user_id,
prev_content: event.orig_content,
prev_rendered_content: event.orig_rendered_content,
prev_rendered_content_version: event.prev_rendered_content_version,
timestamp: event.edit_timestamp,
};
// Add message's edit_history in message dict
// For messages that are edited, edit_history needs to
// be added to message in frontend.
if (anchor_message.edit_history === undefined) {
anchor_message.edit_history = [];
}
anchor_message.edit_history = [
edit_history_entry,
...anchor_message.edit_history,
];
}
any_message_content_edited = true;
// Update raw_content, so that editing a few times in a row is fast.
anchor_message.raw_content = event.content;
}
if (unread.update_message_for_mention(anchor_message, any_message_content_edited)) {
const topic_key = recent_topics_util.get_topic_key(
anchor_message.stream_id,
anchor_message.topic,
);
recent_topics_ui.inplace_rerender(topic_key);
}
} }
// new_topic will be undefined if the topic is unchanged. // new_topic will be undefined if the topic is unchanged.
@@ -469,16 +473,18 @@ export function update_messages(events) {
} }
} }
// Mark the message as edited for the UI. The rendering_only if (anchor_message !== undefined) {
// flag is used to indicated update_message events that are // Mark the message as edited for the UI. The rendering_only
// triggered by server latency optimizations, not user // flag is used to indicated update_message events that are
// interactions; these should not generate edit history updates. // triggered by server latency optimizations, not user
if (!event.rendering_only) { // interactions; these should not generate edit history updates.
anchor_message.last_edit_timestamp = event.edit_timestamp; if (!event.rendering_only) {
} anchor_message.last_edit_timestamp = event.edit_timestamp;
}
notifications.received_messages([anchor_message]); notifications.received_messages([anchor_message]);
alert_words.process_message(anchor_message); alert_words.process_message(anchor_message);
}
if (topic_edited || stream_changed) { if (topic_edited || stream_changed) {
// if topic is changed // if topic is changed
@@ -486,7 +492,9 @@ export function update_messages(events) {
let post_edit_topic = new_topic; let post_edit_topic = new_topic;
if (!topic_edited) { if (!topic_edited) {
pre_edit_topic = anchor_message.topic; if (anchor_message !== undefined) {
pre_edit_topic = anchor_message.topic;
}
post_edit_topic = pre_edit_topic; post_edit_topic = pre_edit_topic;
} }
@@ -507,6 +515,7 @@ export function update_messages(events) {
// Rerender "Message edit history" if it was open to the edited message. // Rerender "Message edit history" if it was open to the edited message.
if ( if (
anchor_message !== undefined &&
$("#message-edit-history").parents(".micromodal").hasClass("modal--open") && $("#message-edit-history").parents(".micromodal").hasClass("modal--open") &&
anchor_message.id === Number.parseInt($("#message-history").attr("data-message-id"), 10) anchor_message.id === Number.parseInt($("#message-history").attr("data-message-id"), 10)
) { ) {