diff --git a/src/app/state/room/roomToNotifications.ts b/src/app/state/room/roomToNotifications.ts index 199a1d9a..5d0268e9 100644 --- a/src/app/state/room/roomToNotifications.ts +++ b/src/app/state/room/roomToNotifications.ts @@ -3,7 +3,7 @@ import { useCallback, useEffect } from 'react'; import { UnreadNotificationCounts } from 'neurone/lib/api/types'; import { useRoomsNotificationPreferencesContext } from '../../hooks/useRoomsNotificationPreferences'; import { ClientContext } from '../../../client/context'; -import { AsyncStatus, useAsyncCallbackValue } from '../../hooks/useAsyncCallback'; +import { useObservableValue } from '../../hooks/useObservableValue'; export type Notifications = UnreadNotificationCounts & { threads: Map; @@ -18,25 +18,10 @@ export const useBindRoomToNotificationsAtom = ( ) => { const setNotificationAtom = useSetAtom(notificationsAtom); const roomsNotificationPreferences = useRoomsNotificationPreferencesContext(); - - const [unreadState] = useAsyncCallbackValue( - useCallback(async () => { - const notifications = await ct.storage.unreadNotifications.observableRead.getAll(); - const threadNotifications = - await ct.storage.unreadThreadNotifications.observableRead.getAll(); - - return { - notifications, - threadNotifications, - }; - }, [ct]) - ); - - const unread = unreadState.status === AsyncStatus.Success ? unreadState.data : undefined; + const notifications = useObservableValue(ct.notifications); + const threadNotifications = useObservableValue(ct.threadNotifications); const processNotifications = useCallback(() => { - const notifications = unread?.notifications.read ?? []; - const threadNotifications = unread?.threadNotifications.read ?? []; const { mute } = roomsNotificationPreferences; const roomToThreadsNotifications = new Map>(); @@ -66,18 +51,9 @@ export const useBindRoomToNotificationsAtom = ( }); setNotificationAtom(roomToNotifications); - }, [unread, setNotificationAtom, roomsNotificationPreferences]); + }, [notifications, threadNotifications, setNotificationAtom, roomsNotificationPreferences]); useEffect(() => { - if (unread) { - processNotifications(); - } - - const unsubscribeN = unread?.notifications.subscribe(() => processNotifications()); - const unsubscribeTn = unread?.threadNotifications.subscribe(() => processNotifications()); - return () => { - unsubscribeN?.(); - unsubscribeTn?.(); - }; - }, [unread, processNotifications]); + processNotifications(); + }, [processNotifications]); }; diff --git a/src/client/context.ts b/src/client/context.ts index 68065c69..f89bfd26 100644 --- a/src/client/context.ts +++ b/src/client/context.ts @@ -21,6 +21,8 @@ import { LeaveRoomStateObject, ReadReceiptObject, sessionValue, + UnreadNotificationsObject, + UnreadThreadNotificationsObject, } from './storage/collections'; import { Storage } from './storage'; import { SyncContext, SyncValue } from './sync'; @@ -122,6 +124,8 @@ export type ClientContext = { roomToVersion: ObservableValue>; roomToLatestEvent: ObservableValue>; userReadReceipt: ObservableValue; + notifications: ObservableValue; + threadNotifications: ObservableValue; roomToTimeline: ObservableValue>; pendingMemberRooms: ObservableValue; @@ -864,6 +868,18 @@ export const createUserReceiptObservable = async ( return receiptObservable; }; +export const createNotificationsObservable = async (storage: Storage, disposer: Disposer) => { + const notifications = await storage.unreadNotifications.observableRead.getAll(); + const threadNotifications = await storage.unreadThreadNotifications.observableRead.getAll(); + + disposer.collect(notifications, threadNotifications); + + return { + notifications, + threadNotifications, + }; +}; + export const createLogoutObservable = ( syncObservable: ObservableValue, disposer: Disposer diff --git a/src/client/initialize.ts b/src/client/initialize.ts index 62c8b0c4..a54f99b7 100644 --- a/src/client/initialize.ts +++ b/src/client/initialize.ts @@ -27,6 +27,7 @@ import { createBackupInfoObservable, createUserReceiptObservable, createToDeviceEventsObservable, + createNotificationsObservable, } from './context'; import { createStorage } from './storage'; import { getAuthJSONRequest } from './request'; @@ -82,6 +83,10 @@ export const initializeClient = async (session: Session): Promise ): Promise => { + const notificationsToRemove: UnreadThreadNotificationsObject[] = []; + const unreadThreadNotificationsObjects: UnreadThreadNotificationsObject[] = joinRooms.flatMap( (roomId) => { const joinRoom = join[roomId]; - const unreadThreadNotifications = joinRoomUtil.roomUnreadThreadNotifications(joinRoom); + const unreadThreadNotifications = joinRoomUtil.roomUnreadThreadNotifications(joinRoom) ?? {}; - if (!unreadThreadNotifications) { - return []; - } + const threadIds = Object.keys(unreadThreadNotifications); - return Object.keys(unreadThreadNotifications).map((threadId) => { + const toRemove = ct.threadNotifications.read.filter( + (n) => n.roomId === roomId && !threadIds.includes(n.threadId) + ); + notificationsToRemove.push(...toRemove); + + return threadIds.map((threadId) => { const unread = unreadThreadNotifications[threadId]; const obj: UnreadThreadNotificationsObject = { @@ -391,6 +396,10 @@ 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); };