From b0174f3acc1448d32c10e8977e64195b4e5265d1 Mon Sep 17 00:00:00 2001 From: ginnyTheCat Date: Sun, 11 Sep 2022 15:21:59 +0200 Subject: [PATCH] Delete notifications after messages have been read or deleted (#830) * Delete read notifications * Delete notifications for deleted messages * Correctly remove notification --- src/client/state/Notifications.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/client/state/Notifications.js b/src/client/state/Notifications.js index d332f1f..da4521d 100644 --- a/src/client/state/Notifications.js +++ b/src/client/state/Notifications.js @@ -43,6 +43,8 @@ class Notifications extends EventEmitter { this.roomList = roomList; this.roomIdToNoti = new Map(); + this.roomIdToPopupNotis = new Map(); + this.eventIdToPopupNoti = new Map(); // this._initNoti(); this._listenEvents(); @@ -258,17 +260,38 @@ class Notifications extends EventEmitter { const noti = new window.Notification(title, { body: mEvent.getContent().body, icon, + tag: mEvent.getId(), silent: settings.isNotificationSounds, }); if (settings.isNotificationSounds) { noti.onshow = () => this._playNotiSound(); } noti.onclick = () => selectRoom(room.roomId, mEvent.getId()); + + this.eventIdToPopupNoti.set(mEvent.getId(), noti); + if (this.roomIdToPopupNotis.has(room.roomId)) { + this.roomIdToPopupNotis.get(room.roomId).push(noti); + } else { + this.roomIdToPopupNotis.set(room.roomId, [noti]); + } } else { this._playNotiSound(); } } + _deletePopupNoti(eventId) { + this.eventIdToPopupNoti.get(eventId)?.close(); + this.eventIdToPopupNoti.delete(eventId); + } + + _deletePopupRoomNotis(roomId) { + this.roomIdToPopupNotis.get(roomId)?.forEach((n) => { + this.eventIdToPopupNoti.delete(n.tag); + n.close(); + }); + this.roomIdToPopupNotis.delete(roomId); + } + _playNotiSound() { if (!this._notiAudio) { this._notiAudio = document.getElementById('notificationSound'); @@ -285,6 +308,8 @@ class Notifications extends EventEmitter { _listenEvents() { this.matrixClient.on('Room.timeline', (mEvent, room) => { + if (mEvent.isRedaction()) this._deletePopupNoti(mEvent.event.redacts); + if (room.isSpaceRoom()) return; if (!isNotifEvent(mEvent)) return; @@ -355,6 +380,8 @@ class Notifications extends EventEmitter { if (readerUserId !== this.matrixClient.getUserId()) return; this.deleteNoti(room.roomId); + + this._deletePopupRoomNotis(room.roomId); } });