From bd31de7b54ced6b6204602a08d4c4c8b7595c29e Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Thu, 12 Mar 2026 14:25:54 -0400 Subject: [PATCH 1/2] fix: notification handling with null safety and improved logic --- .changeset/fix-notification-handling.md | 5 +++ .../pages/client/BackgroundNotifications.tsx | 18 ++-------- src/app/state/room/roomToUnread.ts | 2 +- src/app/utils/room.ts | 33 +++++++++++++++---- 4 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 .changeset/fix-notification-handling.md diff --git a/.changeset/fix-notification-handling.md b/.changeset/fix-notification-handling.md new file mode 100644 index 00000000..537a1d52 --- /dev/null +++ b/.changeset/fix-notification-handling.md @@ -0,0 +1,5 @@ +--- +'default': patch +--- + +Fix notification handling with null safety and improved logic diff --git a/src/app/pages/client/BackgroundNotifications.tsx b/src/app/pages/client/BackgroundNotifications.tsx index 9ff5ab57..1109a9cc 100644 --- a/src/app/pages/client/BackgroundNotifications.tsx +++ b/src/app/pages/client/BackgroundNotifications.tsx @@ -297,7 +297,8 @@ export function BackgroundNotifications() { return; } - if (!isNotificationEvent(mEvent)) { + const userId = mx.getUserId(); + if (!isNotificationEvent(mEvent, room, userId)) { return; } @@ -326,20 +327,7 @@ export function BackgroundNotifications() { // For "Mention & Keywords": respect the push rule (only notify if it matches). const shouldForceDMNotification = isDM && notificationType !== NotificationType.MentionsAndKeywords; - // For reactions: Only notify if someone reacted to your own message - let shouldForceReactionNotification = false; - if (eventType === 'm.reaction') { - const relatesTo = mEvent.getContent()['m.relates_to']; - const reactedToEventId = relatesTo?.event_id; - if (reactedToEventId) { - const reactedToEvent = room.findEventById(reactedToEventId); - if (reactedToEvent && reactedToEvent.getSender() === mx.getUserId()) { - shouldForceReactionNotification = true; - } - } - } - const shouldNotify = - pushActions?.notify || shouldForceDMNotification || shouldForceReactionNotification; + const shouldNotify = pushActions?.notify || shouldForceDMNotification; if (!shouldNotify) { return; diff --git a/src/app/state/room/roomToUnread.ts b/src/app/state/room/roomToUnread.ts index e70271d2..ff57a4d7 100644 --- a/src/app/state/room/roomToUnread.ts +++ b/src/app/state/room/roomToUnread.ts @@ -230,7 +230,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo } // Handle live events (new messages arriving in real-time) - if (data.liveEvent && isNotificationEvent(mEvent)) { + if (data.liveEvent && isNotificationEvent(mEvent, room, mx.getUserId())) { if (mEvent.getSender() === mx.getUserId()) return; const unreadInfo = getUnreadInfo(room, { applyFixup: shouldApplyUnreadFixup(), mDirects }); setUnreadAtom({ diff --git a/src/app/utils/room.ts b/src/app/utils/room.ts index cb30a1af..9f588caa 100644 --- a/src/app/utils/room.ts +++ b/src/app/utils/room.ts @@ -214,7 +214,7 @@ const NOTIFICATION_EVENT_TYPES = [ 'm.sticker', 'm.reaction', ]; -export const isNotificationEvent = (mEvent: MatrixEvent) => { +export const isNotificationEvent = (mEvent: MatrixEvent, room?: Room, userId?: string) => { const eType = mEvent.getType(); if (!NOTIFICATION_EVENT_TYPES.includes(eType)) { return false; @@ -222,7 +222,28 @@ export const isNotificationEvent = (mEvent: MatrixEvent) => { if (eType === 'm.room.member') return false; if (mEvent.isRedacted()) return false; - return mEvent.getRelation()?.rel_type !== 'm.replace'; + const relation = mEvent.getRelation(); + const relationType = relation?.rel_type; + + // Filter out edits - they shouldn't count as new notifications + if (relationType === 'm.replace') return false; + + // For reactions: only count them if they're reactions to the current user's messages + if (relationType === 'm.annotation') { + if (!room || !userId || !relation) { + // If we don't have room/userId/relation context, filter out all reactions (safe default) + return false; + } + // Get the event being reacted to + const reactedToEventId = relation.event_id; + if (!reactedToEventId) return false; + + const reactedToEvent = room.findEventById(reactedToEventId); + // Only count as notification if the reacted-to message was sent by current user + return reactedToEvent?.getSender() === userId; + } + + return true; }; export const roomHaveNotification = (room: Room): boolean => { @@ -254,7 +275,7 @@ export const roomHaveUnread = (mx: MatrixClient, room: Room) => { if (event.getId() === readUpToId) { return false; } - if (isNotificationEvent(event)) { + if (isNotificationEvent(event, room, userId)) { return true; } } @@ -298,7 +319,7 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn .reverse() .find( (event) => - !event.isSending() && event.getSender() !== userId && isNotificationEvent(event) + !event.isSending() && event.getSender() !== userId && isNotificationEvent(event, room, userId) ); const latestNotificationId = latestNotification?.getId(); if (latestNotificationId && room.hasUserReadEvent(userId, latestNotificationId)) { @@ -321,7 +342,7 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn const event = liveEvents[i]; if (!event) break; if (event.getId() === readUpToId) break; - if (isNotificationEvent(event) && event.getSender() !== userId) { + if (isNotificationEvent(event, room, userId) && event.getSender() !== userId) { fallbackTotal += 1; const pushActions = pushProcessor.actionsForEvent(event); if (pushActions?.tweaks?.highlight) fallbackHighlight += 1; @@ -343,7 +364,7 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn const liveEvents = room.getLiveTimeline().getEvents(); const hasActivity = liveEvents.some( - (event) => event.getSender() !== userId && isNotificationEvent(event) + (event) => event.getSender() !== userId && isNotificationEvent(event, room, userId) ); if (hasActivity) { From 1d12be5f31109189d76caf0777055e4e4d07aff0 Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Thu, 12 Mar 2026 14:53:44 -0400 Subject: [PATCH 2/2] fix: correct isNotificationEvent function usage - Update BackgroundNotifications.tsx to use single-argument isNotificationEvent - Update roomToUnread.ts to use single-argument isNotificationEvent - Auto-format files with prettier - All TypeScript and ESLint checks passing --- src/app/pages/client/BackgroundNotifications.tsx | 3 +-- src/app/state/room/roomToUnread.ts | 2 +- src/app/utils/room.ts | 12 +++++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/app/pages/client/BackgroundNotifications.tsx b/src/app/pages/client/BackgroundNotifications.tsx index 1109a9cc..9ca53b5c 100644 --- a/src/app/pages/client/BackgroundNotifications.tsx +++ b/src/app/pages/client/BackgroundNotifications.tsx @@ -297,8 +297,7 @@ export function BackgroundNotifications() { return; } - const userId = mx.getUserId(); - if (!isNotificationEvent(mEvent, room, userId)) { + if (!isNotificationEvent(mEvent)) { return; } diff --git a/src/app/state/room/roomToUnread.ts b/src/app/state/room/roomToUnread.ts index ff57a4d7..e70271d2 100644 --- a/src/app/state/room/roomToUnread.ts +++ b/src/app/state/room/roomToUnread.ts @@ -230,7 +230,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo } // Handle live events (new messages arriving in real-time) - if (data.liveEvent && isNotificationEvent(mEvent, room, mx.getUserId())) { + if (data.liveEvent && isNotificationEvent(mEvent)) { if (mEvent.getSender() === mx.getUserId()) return; const unreadInfo = getUnreadInfo(room, { applyFixup: shouldApplyUnreadFixup(), mDirects }); setUnreadAtom({ diff --git a/src/app/utils/room.ts b/src/app/utils/room.ts index 9f588caa..21fa6e29 100644 --- a/src/app/utils/room.ts +++ b/src/app/utils/room.ts @@ -224,10 +224,10 @@ export const isNotificationEvent = (mEvent: MatrixEvent, room?: Room, userId?: s if (mEvent.isRedacted()) return false; const relation = mEvent.getRelation(); const relationType = relation?.rel_type; - + // Filter out edits - they shouldn't count as new notifications if (relationType === 'm.replace') return false; - + // For reactions: only count them if they're reactions to the current user's messages if (relationType === 'm.annotation') { if (!room || !userId || !relation) { @@ -237,12 +237,12 @@ export const isNotificationEvent = (mEvent: MatrixEvent, room?: Room, userId?: s // Get the event being reacted to const reactedToEventId = relation.event_id; if (!reactedToEventId) return false; - + const reactedToEvent = room.findEventById(reactedToEventId); // Only count as notification if the reacted-to message was sent by current user return reactedToEvent?.getSender() === userId; } - + return true; }; @@ -319,7 +319,9 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn .reverse() .find( (event) => - !event.isSending() && event.getSender() !== userId && isNotificationEvent(event, room, userId) + !event.isSending() && + event.getSender() !== userId && + isNotificationEvent(event, room, userId) ); const latestNotificationId = latestNotification?.getId(); if (latestNotificationId && room.hasUserReadEvent(userId, latestNotificationId)) {