From 4c29b6e10e4bd8acbdb01702481d3693895bde63 Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Tue, 27 Oct 2026 22:01:18 +0530 Subject: [PATCH] remove unread notification when unread count does not appear in room sync response --- src/client/syncWriter/utils.ts | 56 ++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/client/syncWriter/utils.ts b/src/client/syncWriter/utils.ts index 3e017586..632f4d76 100644 --- a/src/client/syncWriter/utils.ts +++ b/src/client/syncWriter/utils.ts @@ -336,29 +336,41 @@ export const writeUnreadNotifications = async ( ): Promise => { const existing = await ct.storage.unreadNotifications.read.getMultiple(joinRooms); - const unreadNotificationsObjects: UnreadNotificationsObject[] = joinRooms.flatMap( - (roomId, index) => { - const joinRoom = join[roomId]; + const unreadNotificationsObjects: UnreadNotificationsObject[] = []; + const roomsToDelete: string[] = []; - const existingUnread = existing[index]?.unread; - const unread = joinRoomUtil.roomUnreadNotifications(joinRoom); + joinRooms.forEach((roomId, index) => { + const joinRoom = join[roomId]; + const existingUnread = existing[index]?.unread; + const unread = joinRoomUtil.roomUnreadNotifications(joinRoom); - if ( - !unread || - (existingUnread?.highlight_count === unread.highlight_count && - existingUnread?.notification_count === unread.notification_count) - ) { - return []; + if (!unread) { + if (existingUnread) { + roomsToDelete.push(roomId); } - const obj: UnreadNotificationsObject = { - id: roomId, - unread, - }; - return obj; + return; } - ); - await ct.storage.unreadNotifications.write.putMultiple(unreadNotificationsObjects); + if ( + existingUnread?.highlight_count === unread.highlight_count && + existingUnread?.notification_count === unread.notification_count + ) { + return; + } + + unreadNotificationsObjects.push({ + id: roomId, + unread, + }); + }); + + if (roomsToDelete.length > 0) { + await ct.storage.unreadNotifications.write.deleteMultiple(roomsToDelete); + } + + if (unreadNotificationsObjects.length > 0) { + await ct.storage.unreadNotifications.write.putMultiple(unreadNotificationsObjects); + } }; export const writeUnreadThreadNotifications = async ( @@ -399,8 +411,12 @@ export const writeUnreadThreadNotifications = async ( const toRemoveKeys = notificationsToRemove.map((n) => getUnreadThreadNotificationId(n.roomId, n.threadId) ); - await ct.storage.unreadThreadNotifications.write.deleteMultiple(toRemoveKeys); - await ct.storage.unreadThreadNotifications.write.putMultiple(unreadThreadNotificationsObjects); + if (toRemoveKeys.length > 0) { + await ct.storage.unreadThreadNotifications.write.deleteMultiple(toRemoveKeys); + } + if (unreadThreadNotificationsObjects.length > 0) { + await ct.storage.unreadThreadNotifications.write.putMultiple(unreadThreadNotificationsObjects); + } }; export const writeInviteRoomState = async (