From dd7a5ac4c5c04e9ed8c1b71dda74c3c1ef2992d7 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Wed, 24 Jul 2024 03:52:43 +0000 Subject: [PATCH] recent_view_ui: Fix dm rows not updated for deleted messages. Tested by deleting a DM and navigating to recent view. No error is thrown and dm row is correctly placed. Co-authored-by: Tim Abbott --- web/src/message_util.ts | 6 ++++++ web/src/recent_view_ui.ts | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/web/src/message_util.ts b/web/src/message_util.ts index b624e15936..6440661d02 100644 --- a/web/src/message_util.ts +++ b/web/src/message_util.ts @@ -94,6 +94,12 @@ export function get_messages_in_topic(stream_id: number, topic: string): Message ); } +export function get_messages_in_dm_conversations(user_ids_strings: Set): Message[] { + return all_messages_data + .all_messages() + .filter((x) => x.type === "private" && user_ids_strings.has(x.to_user_ids)); +} + export function get_max_message_id_in_stream(stream_id: number): number { let max_message_id = 0; for (const msg of all_messages_data.all_messages()) { diff --git a/web/src/recent_view_ui.ts b/web/src/recent_view_ui.ts index e2db87fa73..9a277a1984 100644 --- a/web/src/recent_view_ui.ts +++ b/web/src/recent_view_ui.ts @@ -794,6 +794,31 @@ export function update_topics_of_deleted_message_ids(message_ids: number[]): voi msgs_to_process.push(...msgs); } + const dm_conversations_to_rerender = new Set(); + for (const msg_id of message_ids) { + const msg = message_store.get(msg_id); + if (msg === undefined) { + continue; + } + + if (msg.type === "private") { + const key = recent_view_util.get_key_from_message(msg); + // Important to assert since we use the key in get_messages_in_dm_conversation. + assert(key === msg.to_user_ids); + dm_conversations_to_rerender.add(key); + } + } + + for (const key of dm_conversations_to_rerender) { + recent_view_data.conversations.delete(key); + } + if (dm_conversations_to_rerender.size > 0) { + const dm_messages_to_process = message_util.get_messages_in_dm_conversations( + dm_conversations_to_rerender, + ); + msgs_to_process.push(...dm_messages_to_process); + } + if (msgs_to_process.length > 0) { process_messages(msgs_to_process); } else {