Skip to content

Commit

Permalink
Fix highlight notifications increasing when total notification is zero (
Browse files Browse the repository at this point in the history
  • Loading branch information
Germain authored Dec 2, 2022
1 parent fa2eeac commit 53a45a3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions spec/unit/notifications.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ describe("fixNotificationCountOnDecryption", () => {

fixNotificationCountOnDecryption(mockClient, event);

expect(room.getRoomUnreadNotificationCount(NotificationCountType.Total)).toBe(0);
expect(room.getRoomUnreadNotificationCount(NotificationCountType.Highlight)).toBe(0);
expect(room.getRoomUnreadNotificationCount(NotificationCountType.Total)).toBe(1);
expect(room.getRoomUnreadNotificationCount(NotificationCountType.Highlight)).toBe(1);
});

it("changes the thread count to highlight on decryption", () => {
Expand Down
3 changes: 1 addition & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9379,15 +9379,14 @@ export function fixNotificationCountOnDecryption(cli: MatrixClient, event: Matri

const isThreadEvent = !!event.threadRootId && !event.isThreadRoot;

const totalCount = room.getUnreadCountForEventContext(NotificationCountType.Total, event);
const currentCount = room.getUnreadCountForEventContext(NotificationCountType.Highlight, event);

// Ensure the unread counts are kept up to date if the event is encrypted
// We also want to make sure that the notification count goes up if we already
// have encrypted events to avoid other code from resetting 'highlight' to zero.
const oldHighlight = !!oldActions?.tweaks?.highlight;
const newHighlight = !!actions?.tweaks?.highlight;
if ((oldHighlight !== newHighlight || currentCount > 0) && totalCount > 0) {
if (oldHighlight !== newHighlight || currentCount > 0) {
// TODO: Handle mentions received while the client is offline
// See also https://github.com/vector-im/element-web/issues/9069
const hasReadEvent = isThreadEvent
Expand Down
2 changes: 1 addition & 1 deletion src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
Array.from(this.threads)
.forEach(([, thread]) => {
if (thread.length === 0) return;
const currentUserParticipated = thread.events.some(event => {
const currentUserParticipated = thread.timeline.some(event => {
return event.getSender() === this.client.getUserId();
});
if (filterType !== ThreadFilterType.My || currentUserParticipated) {
Expand Down
13 changes: 6 additions & 7 deletions src/models/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
* A reference to all the events ID at the bottom of the threads
*/
public readonly timelineSet: EventTimelineSet;
public timeline: MatrixEvent[] = [];

private _currentUserParticipated = false;

Expand Down Expand Up @@ -186,7 +187,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
private onRedaction = async (event: MatrixEvent): Promise<void> => {
if (event.threadRootId !== this.id) return; // ignore redactions for other timelines
if (this.replyCount <= 0) {
for (const threadEvent of this.events) {
for (const threadEvent of this.timeline) {
this.clearEventMetadata(threadEvent);
}
this.lastEvent = this.rootEvent;
Expand Down Expand Up @@ -233,6 +234,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
roomState: this.roomState,
},
);
this.timeline = this.events;
}
}

Expand Down Expand Up @@ -292,6 +294,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
this.setEventMetadata(event);
await this.fetchEditsWhereNeeded(event);
}
this.timeline = this.events;
}

private getRootEventBundledRelationship(rootEvent = this.rootEvent): IThreadBundledRelationship | undefined {
Expand Down Expand Up @@ -403,8 +406,8 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
* Return last reply to the thread, if known.
*/
public lastReply(matches: (ev: MatrixEvent) => boolean = (): boolean => true): MatrixEvent | null {
for (let i = this.events.length - 1; i >= 0; i--) {
const event = this.events[i];
for (let i = this.timeline.length - 1; i >= 0; i--) {
const event = this.timeline[i];
if (matches(event)) {
return event;
}
Expand Down Expand Up @@ -452,10 +455,6 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
return this.timelineSet;
}

public get timeline(): MatrixEvent[] {
return this.events;
}

public addReceipt(event: MatrixEvent, synthetic: boolean): void {
throw new Error("Unsupported function on the thread model");
}
Expand Down

0 comments on commit 53a45a3

Please sign in to comment.