From 6d663127072db7919ed5150d73770d8f8d14ad93 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Thu, 21 Nov 2024 19:08:28 +0530 Subject: [PATCH] message_fetch: Account for recently sent messages in data verification. --- web/src/message_fetch.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.ts index 1bba1df5b7..b5b2d14b0a 100644 --- a/web/src/message_fetch.ts +++ b/web/src/message_fetch.ts @@ -656,6 +656,9 @@ export function maybe_load_newer_messages(opts: {msg_list: MessageList}): void { export function verify_cached_data(data: MessageListData): void { type EventDetails = { type: string; + message?: { + id: number; + }; // ...many more properties. }; let events_since_restoring_cached_data: EventDetails[] = []; @@ -715,11 +718,26 @@ export function verify_cached_data(data: MessageListData): void { if (has_found_newest) { assert(data.found_newest); } - assert(data.messages.length === messages.length); + const cached_msg_ids = new Set(messages.map((msg) => msg.id)); - for (const msg of data.messages) { - assert(cached_msg_ids.has(msg.id)); + const server_msg_ids = new Set(data.messages.map((msg) => msg.id)); + const msgs_not_found = server_msg_ids.difference(cached_msg_ids); + msgs_not_found.union(cached_msg_ids.difference(server_msg_ids)); + if (msgs_not_found.size > 0) { + // Check if the missing messages were recently added. + for (const event of events_since_restoring_cached_data) { + if ( + event.type === "message" && + event.message !== undefined && + msgs_not_found.has(event.message.id) + ) { + continue; + } else { + assert(msgs_not_found.size === 0); + } + } } + assert(msgs_not_found.size === 0); $(document).off("server_event.zulip"); } catch (error) { setTimeout(() => {