Skip to content

Commit

Permalink
Fix notification type when resetting threads notifications (#2757)
Browse files Browse the repository at this point in the history
  • Loading branch information
Germain committed Oct 17, 2022
1 parent 6245661 commit be11fa6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
41 changes: 36 additions & 5 deletions spec/unit/room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2624,23 +2624,23 @@ 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);
});

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);
});
});

Expand Down Expand Up @@ -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 = {
Expand Down
30 changes: 10 additions & 20 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
private txnToEvent: Record<string, MatrixEvent> = {}; // Pending in-flight requests { string: MatrixEvent }
private notificationCounts: NotificationCount = {};
private readonly threadNotifications = new Map<string, NotificationCount>();
private roomThreadsNotificationType: NotificationCountType | null = null;
private readonly timelineSets: EventTimelineSet[];
public readonly threadsTimelineSets: EventTimelineSet[] = [];
// any filtered timeline sets we're maintaining for this room
Expand Down Expand Up @@ -1251,22 +1250,6 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {

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,
Expand All @@ -1278,16 +1261,23 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
* @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;
}

/**
* @experimental
* Resets the thread notifications for this room
*/
public resetThreadUnreadNotificationCount(): void {
this.roomThreadsNotificationType = null;
this.threadNotifications.clear();
}

Expand Down

0 comments on commit be11fa6

Please sign in to comment.