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 highlight notifications increasing when total notification is zero #2937

Merged
merged 2 commits into from
Dec 2, 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
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 @@ -9378,15 +9378,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 @@ -81,6 +81,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 @@ -181,7 +182,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 @@ -228,6 +229,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
roomState: this.roomState,
},
);
this.timeline = this.events;
}
}

Expand Down Expand Up @@ -287,6 +289,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 @@ -374,8 +377,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 @@ -423,10 +426,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