Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix notification type when resetting threads notifications #2757

Merged
merged 4 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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