Simplify notifier-platform code for closing notifications (#32609)

* Simplify notifier-platform code for closing notifications

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Add test

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2026-02-23 17:40:38 +00:00
committed by GitHub
parent 9c050c58b7
commit e907d4978d
3 changed files with 18 additions and 13 deletions
-8
View File
@@ -245,14 +245,6 @@ export default abstract class BasePlatform {
public loudNotification(ev: MatrixEvent, room: Room): void {} public loudNotification(ev: MatrixEvent, room: Room): void {}
public clearNotification(notif: Notification): void {
// Some browsers don't support this, e.g Safari on iOS
// https://developer.mozilla.org/en-US/docs/Web/API/Notification/close
if (notif.close) {
notif.close();
}
}
/** /**
* Returns true if the platform requires URL previews in tooltips, otherwise false. * Returns true if the platform requires URL previews in tooltips, otherwise false.
* @returns {boolean} whether the platform requires URL previews in tooltips * @returns {boolean} whether the platform requires URL previews in tooltips
+1 -3
View File
@@ -433,11 +433,9 @@ class NotifierClass extends TypedEventEmitter<keyof EmittedEvents, EmittedEvents
// do this. Instead, clear all notifications for a room once // do this. Instead, clear all notifications for a room once
// there are no notifs left in that room., which is not quite // there are no notifs left in that room., which is not quite
// as good but it's something. // as good but it's something.
const plaf = PlatformPeg.get();
if (!plaf) return;
if (this.notifsByRoom[room.roomId] === undefined) return; if (this.notifsByRoom[room.roomId] === undefined) return;
for (const notif of this.notifsByRoom[room.roomId]) { for (const notif of this.notifsByRoom[room.roomId]) {
plaf.clearNotification(notif); notif.close();
} }
delete this.notifsByRoom[room.roomId]; delete this.notifsByRoom[room.roomId];
} }
+17 -2
View File
@@ -149,7 +149,7 @@ describe("Notifier", () => {
MockPlatform = mockPlatformPeg({ MockPlatform = mockPlatformPeg({
supportsNotifications: jest.fn().mockReturnValue(true), supportsNotifications: jest.fn().mockReturnValue(true),
maySendNotifications: jest.fn().mockReturnValue(true), maySendNotifications: jest.fn().mockReturnValue(true),
displayNotification: jest.fn(), displayNotification: jest.fn().mockReturnValue({ close: jest.fn() }),
loudNotification: jest.fn(), loudNotification: jest.fn(),
}); });
@@ -171,7 +171,7 @@ describe("Notifier", () => {
const event = new MatrixEvent({ const event = new MatrixEvent({
sender: "@alice:server.org", sender: "@alice:server.org",
type: "m.room.message", type: "m.room.message",
room_id: "!room:server.org", room_id: roomId,
content: { content: {
body: "hey", body: "hey",
}, },
@@ -271,6 +271,21 @@ describe("Notifier", () => {
expect(MockPlatform.displayNotification).toHaveBeenCalledWith(testRoom.name, "hey", null, testRoom, event); expect(MockPlatform.displayNotification).toHaveBeenCalledWith(testRoom.name, "hey", null, testRoom, event);
}); });
it("closes a desktop notification when room is marked read", () => {
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
emitLiveEvent(event);
expect(MockPlatform.displayNotification).toHaveBeenCalledWith(testRoom.name, "hey", null, testRoom, event);
mockClient!.emit(RoomEvent.Receipt, event, testRoom);
expect(
(
MockPlatform.displayNotification.mock.results[0].value as ReturnType<
typeof MockPlatform.displayNotification
>
).close,
).toHaveBeenCalled();
});
it("creates a loud notification when enabled", () => { it("creates a loud notification when enabled", () => {
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null); mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
emitLiveEvent(event); emitLiveEvent(event);