From 2206b80e65aa5aa3e7b50b6dc9b148d37514dff8 Mon Sep 17 00:00:00 2001 From: Germain Date: Tue, 7 Dec 2021 10:58:34 +0000 Subject: [PATCH] Threads notifications after app startup (#2043) --- src/client.ts | 15 +++++++-------- src/models/room.ts | 27 ++++++++++++++++----------- src/models/thread.ts | 6 ++++++ 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/client.ts b/src/client.ts index 79e48193e..cdb0cc516 100644 --- a/src/client.ts +++ b/src/client.ts @@ -871,8 +871,7 @@ export class MatrixClient extends EventEmitter { // state, such as highlights when the user's name is mentioned. this.on("Event.decrypted", (event) => { const oldActions = event.getPushActions(); - const actions = this.pushProcessor.actionsForEvent(event); - event.setPushActions(actions); // Might as well while we're here + const actions = this.getPushActionsForEvent(event, true); const room = this.getRoom(event.getRoomId()); if (!room) return; @@ -882,10 +881,8 @@ export class MatrixClient extends EventEmitter { // 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 && oldActions.tweaks - ? !!oldActions.tweaks.highlight : false; - const newHighlight = actions && actions.tweaks - ? !!actions.tweaks.highlight : false; + const oldHighlight = !!oldActions?.tweaks?.highlight; + const newHighlight = !!actions?.tweaks?.highlight; 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 @@ -4641,10 +4638,12 @@ export class MatrixClient extends EventEmitter { * Obtain a dict of actions which should be performed for this event according * to the push rules for this user. Caches the dict on the event. * @param {MatrixEvent} event The event to get push actions for. + * @param {boolean} forceRecalculate forces to recalculate actions for an event + * Useful when an event just got decrypted * @return {module:pushprocessor~PushAction} A dict of actions to perform. */ - public getPushActionsForEvent(event: MatrixEvent): IActionsObject { - if (!event.getPushActions()) { + public getPushActionsForEvent(event: MatrixEvent, forceRecalculate = false): IActionsObject { + if (!event.getPushActions() || forceRecalculate) { event.setPushActions(this.pushProcessor.actionsForEvent(event)); } return event.getPushActions(); diff --git a/src/models/room.ts b/src/models/room.ts index b655384b6..e66679ee8 100644 --- a/src/models/room.ts +++ b/src/models/room.ts @@ -1852,16 +1852,7 @@ export class Room extends EventEmitter { }); } - /** - * Get the ID of the event that a given user has read up to, or null if we - * have received no read receipts from them. - * @param {String} userId The user ID to get read receipt event ID for - * @param {Boolean} ignoreSynthesized If true, return only receipts that have been - * sent by the server, not implicit ones generated - * by the JS SDK. - * @return {String} ID of the latest event that the given user has read, or null. - */ - public getEventReadUpTo(userId: string, ignoreSynthesized = false): string | null { + public getReadReceiptForUserId(userId: string, ignoreSynthesized = false): IWrappedReceipt | null { let receipts = this.receipts; if (ignoreSynthesized) { receipts = this.realReceipts; @@ -1874,7 +1865,21 @@ export class Room extends EventEmitter { return null; } - return receipts["m.read"][userId].eventId; + return receipts["m.read"][userId]; + } + + /** + * Get the ID of the event that a given user has read up to, or null if we + * have received no read receipts from them. + * @param {String} userId The user ID to get read receipt event ID for + * @param {Boolean} ignoreSynthesized If true, return only receipts that have been + * sent by the server, not implicit ones generated + * by the JS SDK. + * @return {String} ID of the latest event that the given user has read, or null. + */ + public getEventReadUpTo(userId: string, ignoreSynthesized = false): string | null { + const readReceipt = this.getReadReceiptForUserId(userId, ignoreSynthesized); + return readReceipt?.eventId ?? null; } /** diff --git a/src/models/thread.ts b/src/models/thread.ts index 112a99cca..64d90cec1 100644 --- a/src/models/thread.ts +++ b/src/models/thread.ts @@ -25,6 +25,8 @@ export enum ThreadEvent { New = "Thread.new", Ready = "Thread.ready", Update = "Thread.update", + NewReply = "Thread.newReply", + ViewThread = "Thred.viewThread", } /** @@ -109,6 +111,10 @@ export class Thread extends TypedEventEmitter { await this.client.decryptEventIfNeeded(event, {}); this.emit(ThreadEvent.Update, this); + + if (event.isThreadRelation) { + this.emit(ThreadEvent.NewReply, this, event); + } } /**