diff --git a/spec/unit/room.spec.ts b/spec/unit/room.spec.ts index f2c7aee2b27..5ddda62d71f 100644 --- a/spec/unit/room.spec.ts +++ b/spec/unit/room.spec.ts @@ -2624,11 +2624,11 @@ describe("Room", function() { room.setThreadUnreadNotificationCount("123", NotificationCountType.Total, 666); room.setThreadUnreadNotificationCount("123", NotificationCountType.Highlight, 123); - expect(room.getThreadsAggregateNotificationType()).toBe(NotificationCountType.Highlight); + expect(room.threadsAggregateNotificationType).toBe(NotificationCountType.Highlight); room.resetThreadUnreadNotificationCount(); - expect(room.getThreadsAggregateNotificationType()).toBe(null); + expect(room.threadsAggregateNotificationType).toBe(null); expect(room.getThreadUnreadNotificationCount("123", NotificationCountType.Total)).toBe(0); expect(room.getThreadUnreadNotificationCount("123", NotificationCountType.Highlight)).toBe(0); @@ -2636,11 +2636,11 @@ describe("Room", function() { it("sets the room threads notification type", () => { room.setThreadUnreadNotificationCount("123", NotificationCountType.Total, 666); - expect(room.getThreadsAggregateNotificationType()).toBe(NotificationCountType.Total); + expect(room.threadsAggregateNotificationType).toBe(NotificationCountType.Total); room.setThreadUnreadNotificationCount("123", NotificationCountType.Highlight, 123); - expect(room.getThreadsAggregateNotificationType()).toBe(NotificationCountType.Highlight); + expect(room.threadsAggregateNotificationType).toBe(NotificationCountType.Highlight); room.setThreadUnreadNotificationCount("123", NotificationCountType.Total, 333); - expect(room.getThreadsAggregateNotificationType()).toBe(NotificationCountType.Highlight); + expect(room.threadsAggregateNotificationType).toBe(NotificationCountType.Highlight); }); }); @@ -2669,6 +2669,37 @@ describe("Room", function() { }); }); + describe("threadsAggregateNotificationType", () => { + it("defaults to null", () => { + expect(room.threadsAggregateNotificationType).toBeNull(); + }); + + it("counts multiple threads", () => { + room.setThreadUnreadNotificationCount("$123", NotificationCountType.Total, 1); + expect(room.threadsAggregateNotificationType).toBe(NotificationCountType.Total); + + room.setThreadUnreadNotificationCount("$456", NotificationCountType.Total, 1); + expect(room.threadsAggregateNotificationType).toBe(NotificationCountType.Total); + + room.setThreadUnreadNotificationCount("$123", NotificationCountType.Highlight, 1); + expect(room.threadsAggregateNotificationType).toBe(NotificationCountType.Highlight); + + room.setThreadUnreadNotificationCount("$123", NotificationCountType.Highlight, 0); + expect(room.threadsAggregateNotificationType).toBe(NotificationCountType.Total); + }); + + it("allows reset", () => { + room.setThreadUnreadNotificationCount("$123", NotificationCountType.Total, 1); + room.setThreadUnreadNotificationCount("$456", NotificationCountType.Total, 1); + room.setThreadUnreadNotificationCount("$123", NotificationCountType.Highlight, 1); + expect(room.threadsAggregateNotificationType).toBe(NotificationCountType.Highlight); + + room.resetThreadUnreadNotificationCount(); + + expect(room.threadsAggregateNotificationType).toBeNull(); + }); + }); + it("should load pending events from from the store and decrypt if needed", async () => { const client = new TestClient(userA).client; client.crypto = { diff --git a/src/models/room.ts b/src/models/room.ts index 202ef728534..de285dca78f 100644 --- a/src/models/room.ts +++ b/src/models/room.ts @@ -201,7 +201,6 @@ export class Room extends ReadReceipt { private txnToEvent: Record = {}; // Pending in-flight requests { string: MatrixEvent } private notificationCounts: NotificationCount = {}; private readonly threadNotifications = new Map(); - private roomThreadsNotificationType: NotificationCountType | null = null; private readonly timelineSets: EventTimelineSet[]; public readonly threadsTimelineSets: EventTimelineSet[] = []; // any filtered timeline sets we're maintaining for this room @@ -1251,22 +1250,6 @@ export class Room extends ReadReceipt { this.threadNotifications.set(threadId, notification); - // Remember what the global threads notification count type is - // for all the threads in the room - if (count > 0) { - switch (this.roomThreadsNotificationType) { - case NotificationCountType.Highlight: - break; - case NotificationCountType.Total: - if (type === NotificationCountType.Highlight) { - this.roomThreadsNotificationType = type; - } - break; - default: - this.roomThreadsNotificationType = type; - } - } - this.emit( RoomEvent.UnreadNotifications, notification, @@ -1278,8 +1261,16 @@ export class Room extends ReadReceipt { * @experimental * @returns the notification count type for all the threads in the room */ - public getThreadsAggregateNotificationType(): NotificationCountType | null { - return this.roomThreadsNotificationType; + public get threadsAggregateNotificationType(): NotificationCountType | null { + let type: NotificationCountType | null = null; + for (const threadNotification of this.threadNotifications.values()) { + if ((threadNotification.highlight ?? 0) > 0) { + return NotificationCountType.Highlight; + } else if ((threadNotification.total ?? 0) > 0 && !type) { + type = NotificationCountType.Total; + } + } + return type; } /** @@ -1287,7 +1278,6 @@ export class Room extends ReadReceipt { * Resets the thread notifications for this room */ public resetThreadUnreadNotificationCount(): void { - this.roomThreadsNotificationType = null; this.threadNotifications.clear(); }