message_events: Use the new message_ids param of /messages.

This helps us better update the views where we don't have the
messages that were updated cached or the filter cannot be
applied locally.

Tested using browser breakpoints for search `zulip` in starred narrow
for `!filter.can_apply_locally()` and setting message_fetch
constants to single digit for `messages_to_fetch.length > 0`.
This commit is contained in:
Aman Agrawal 2024-09-28 17:09:57 +00:00 committed by Tim Abbott
parent 3f726e25e4
commit 69c1b7c64e
1 changed files with 37 additions and 16 deletions

View File

@ -113,26 +113,47 @@ export function update_views_filtered_on_message_property(
} }
} }
// In most cases, we will only have one message to fetch which if (!filter.can_apply_locally()) {
// can happen without rerendering the view. channel.get({
// In case of multiple messages, we just rerender the view url: "/json/messages",
// since it is likely to use similar amount of resources to data: {
// fetching the messages and rerendering the view. message_ids: JSON.stringify(message_ids),
if (messages_to_fetch.length > 1 || !filter.can_apply_locally()) { narrow: JSON.stringify(filter.terms()),
// TODO: Might be better to check if `/messages/matches_narrow` },
// rather than doing a full rerender. success(data) {
message_view.show(filter.terms(), { const messages_to_add = [];
then_select_id: msg_list.selected_id(), const messages_to_remove = new Set(message_ids);
then_select_offset: msg_list.selected_row().get_offset_to_window().top, for (const raw_message of data.messages) {
trigger: "message property update", messages_to_remove.delete(raw_message.id);
force_rerender: true, let message = message_store.get(raw_message.id);
if (!message) {
message = message_helper.process_new_message(raw_message);
}
messages_to_add.push(message);
}
msg_list.data.remove([...messages_to_remove]);
msg_list.data.add_messages(messages_to_add);
msg_list.rerender();
},
}); });
} else if (messages_to_fetch.length === 1) { } else if (messages_to_fetch.length > 0) {
// Fetch the message and update the view. // Fetch the message and update the view.
channel.get({ channel.get({
url: "/json/messages/" + messages_to_fetch[0], url: "/json/messages",
data: {
message_ids: JSON.stringify(messages_to_fetch),
// We don't filter by narrow here since we can
// apply the filter locally and the fetched message
// can be used to update other message lists and
// cached message data structures as well.
},
success(data) { success(data) {
message_helper.process_new_message(data.message); // `messages_to_fetch` might already be cached locally when
// we reach here but `message_helper.process_new_message`
// already handles that case.
for (const raw_message of data.messages) {
message_helper.process_new_message(raw_message);
}
update_views_filtered_on_message_property( update_views_filtered_on_message_property(
message_ids, message_ids,
property_term_type, property_term_type,