Skip to content

Commit 466442c

Browse files
authored
Merge pull request #233 from Just-Insane/fix/notification-handling
Fix notification handling (edits, reactions)
2 parents 4e4521a + 1d12be5 commit 466442c

3 files changed

Lines changed: 35 additions & 20 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'default': patch
3+
---
4+
5+
Fix notification handling with null safety and improved logic

src/app/pages/client/BackgroundNotifications.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -326,20 +326,7 @@ export function BackgroundNotifications() {
326326
// For "Mention & Keywords": respect the push rule (only notify if it matches).
327327
const shouldForceDMNotification =
328328
isDM && notificationType !== NotificationType.MentionsAndKeywords;
329-
// For reactions: Only notify if someone reacted to your own message
330-
let shouldForceReactionNotification = false;
331-
if (eventType === 'm.reaction') {
332-
const relatesTo = mEvent.getContent()['m.relates_to'];
333-
const reactedToEventId = relatesTo?.event_id;
334-
if (reactedToEventId) {
335-
const reactedToEvent = room.findEventById(reactedToEventId);
336-
if (reactedToEvent && reactedToEvent.getSender() === mx.getUserId()) {
337-
shouldForceReactionNotification = true;
338-
}
339-
}
340-
}
341-
const shouldNotify =
342-
pushActions?.notify || shouldForceDMNotification || shouldForceReactionNotification;
329+
const shouldNotify = pushActions?.notify || shouldForceDMNotification;
343330

344331
if (!shouldNotify) {
345332
return;

src/app/utils/room.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,36 @@ const NOTIFICATION_EVENT_TYPES = [
214214
'm.sticker',
215215
'm.reaction',
216216
];
217-
export const isNotificationEvent = (mEvent: MatrixEvent) => {
217+
export const isNotificationEvent = (mEvent: MatrixEvent, room?: Room, userId?: string) => {
218218
const eType = mEvent.getType();
219219
if (!NOTIFICATION_EVENT_TYPES.includes(eType)) {
220220
return false;
221221
}
222222
if (eType === 'm.room.member') return false;
223223

224224
if (mEvent.isRedacted()) return false;
225-
return mEvent.getRelation()?.rel_type !== 'm.replace';
225+
const relation = mEvent.getRelation();
226+
const relationType = relation?.rel_type;
227+
228+
// Filter out edits - they shouldn't count as new notifications
229+
if (relationType === 'm.replace') return false;
230+
231+
// For reactions: only count them if they're reactions to the current user's messages
232+
if (relationType === 'm.annotation') {
233+
if (!room || !userId || !relation) {
234+
// If we don't have room/userId/relation context, filter out all reactions (safe default)
235+
return false;
236+
}
237+
// Get the event being reacted to
238+
const reactedToEventId = relation.event_id;
239+
if (!reactedToEventId) return false;
240+
241+
const reactedToEvent = room.findEventById(reactedToEventId);
242+
// Only count as notification if the reacted-to message was sent by current user
243+
return reactedToEvent?.getSender() === userId;
244+
}
245+
246+
return true;
226247
};
227248

228249
export const roomHaveNotification = (room: Room): boolean => {
@@ -254,7 +275,7 @@ export const roomHaveUnread = (mx: MatrixClient, room: Room) => {
254275
if (event.getId() === readUpToId) {
255276
return false;
256277
}
257-
if (isNotificationEvent(event)) {
278+
if (isNotificationEvent(event, room, userId)) {
258279
return true;
259280
}
260281
}
@@ -298,7 +319,9 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn
298319
.reverse()
299320
.find(
300321
(event) =>
301-
!event.isSending() && event.getSender() !== userId && isNotificationEvent(event)
322+
!event.isSending() &&
323+
event.getSender() !== userId &&
324+
isNotificationEvent(event, room, userId)
302325
);
303326
const latestNotificationId = latestNotification?.getId();
304327
if (latestNotificationId && room.hasUserReadEvent(userId, latestNotificationId)) {
@@ -321,7 +344,7 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn
321344
const event = liveEvents[i];
322345
if (!event) break;
323346
if (event.getId() === readUpToId) break;
324-
if (isNotificationEvent(event) && event.getSender() !== userId) {
347+
if (isNotificationEvent(event, room, userId) && event.getSender() !== userId) {
325348
fallbackTotal += 1;
326349
const pushActions = pushProcessor.actionsForEvent(event);
327350
if (pushActions?.tweaks?.highlight) fallbackHighlight += 1;
@@ -343,7 +366,7 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn
343366
const liveEvents = room.getLiveTimeline().getEvents();
344367

345368
const hasActivity = liveEvents.some(
346-
(event) => event.getSender() !== userId && isNotificationEvent(event)
369+
(event) => event.getSender() !== userId && isNotificationEvent(event, room, userId)
347370
);
348371

349372
if (hasActivity) {

0 commit comments

Comments
 (0)