From c65a52b9af264c1d1f4a1aad419862a72c997316 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 25 May 2023 10:30:23 +0100 Subject: [PATCH 1/2] Make a clear and explicit filter on which events are considered for fetchEventsWhereNeeded --- src/models/thread.ts | 46 +++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/models/thread.ts b/src/models/thread.ts index 11727d9704e..41424fe1b1d 100644 --- a/src/models/thread.ts +++ b/src/models/thread.ts @@ -519,26 +519,22 @@ export class Thread extends ReadReceipt { const recursionSupport = this.client.canSupport.get(Feature.RelationsRecursion) ?? ServerSupport.Unsupported; if (recursionSupport === ServerSupport.Unsupported) { return Promise.all( - events - .filter((e) => e.isEncrypted()) - .map((event: MatrixEvent) => { - // The only type of relation that gets edits is a thread message. - if (event.getThread() === undefined && event.isRelation()) return; - return this.client - .relations(this.roomId, event.getId()!, RelationType.Replace, event.getType(), { - limit: 1, - }) - .then((relations) => { - if (relations.events.length) { - const editEvent = relations.events[0]; - event.makeReplaced(editEvent); - this.insertEventIntoTimeline(editEvent); - } - }) - .catch((e) => { - logger.error("Failed to load edits for encrypted thread event", e); - }); - }), + events.filter(isAnEncryptedThreadMessage).map((event: MatrixEvent) => { + return this.client + .relations(this.roomId, event.getId()!, RelationType.Replace, event.getType(), { + limit: 1, + }) + .then((relations) => { + if (relations.events.length) { + const editEvent = relations.events[0]; + event.makeReplaced(editEvent); + this.insertEventIntoTimeline(editEvent); + } + }) + .catch((e) => { + logger.error("Failed to load edits for encrypted thread event", e); + }); + }), ); } } @@ -708,6 +704,16 @@ export class Thread extends ReadReceipt { } } +/** + * Decide whether an event deserves to have its potential edits fetched. + * + * @returns true if this event is encrypted and is a message that is part of a + * thread. + */ +function isAnEncryptedThreadMessage(event: MatrixEvent): boolean { + return event.isEncrypted() && event.isRelation(THREAD_RELATION_TYPE.name); +} + export const FILTER_RELATED_BY_SENDERS = new ServerControlledNamespacedValue( "related_by_senders", "io.element.relation_senders", From 09213ac4728db415217cd5b1ffcaf46babf0c77c Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 25 May 2023 10:31:21 +0100 Subject: [PATCH 2/2] Convert the logic in fetchEventsWhereNeeded to an async function --- src/models/thread.ts | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/models/thread.ts b/src/models/thread.ts index 41424fe1b1d..3179860819d 100644 --- a/src/models/thread.ts +++ b/src/models/thread.ts @@ -519,21 +519,25 @@ export class Thread extends ReadReceipt { const recursionSupport = this.client.canSupport.get(Feature.RelationsRecursion) ?? ServerSupport.Unsupported; if (recursionSupport === ServerSupport.Unsupported) { return Promise.all( - events.filter(isAnEncryptedThreadMessage).map((event: MatrixEvent) => { - return this.client - .relations(this.roomId, event.getId()!, RelationType.Replace, event.getType(), { - limit: 1, - }) - .then((relations) => { - if (relations.events.length) { - const editEvent = relations.events[0]; - event.makeReplaced(editEvent); - this.insertEventIntoTimeline(editEvent); - } - }) - .catch((e) => { - logger.error("Failed to load edits for encrypted thread event", e); - }); + events.filter(isAnEncryptedThreadMessage).map(async (event: MatrixEvent) => { + try { + const relations = await this.client.relations( + this.roomId, + event.getId()!, + RelationType.Replace, + event.getType(), + { + limit: 1, + }, + ); + if (relations.events.length) { + const editEvent = relations.events[0]; + event.makeReplaced(editEvent); + this.insertEventIntoTimeline(editEvent); + } + } catch (e) { + logger.error("Failed to load edits for encrypted thread event", e); + } }), ); } @@ -708,10 +712,10 @@ export class Thread extends ReadReceipt { * Decide whether an event deserves to have its potential edits fetched. * * @returns true if this event is encrypted and is a message that is part of a - * thread. + * thread - either inside it, or a root. */ function isAnEncryptedThreadMessage(event: MatrixEvent): boolean { - return event.isEncrypted() && event.isRelation(THREAD_RELATION_TYPE.name); + return event.isEncrypted() && (event.isRelation(THREAD_RELATION_TYPE.name) || event.isThreadRoot); } export const FILTER_RELATED_BY_SENDERS = new ServerControlledNamespacedValue(