diff --git a/src/models/thread.ts b/src/models/thread.ts index 11727d9704e..3179860819d 100644 --- a/src/models/thread.ts +++ b/src/models/thread.ts @@ -519,26 +519,26 @@ 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(), { + events.filter(isAnEncryptedThreadMessage).map(async (event: MatrixEvent) => { + try { + const relations = await 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); - }); - }), + }, + ); + 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 +708,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 - either inside it, or a root. + */ +function isAnEncryptedThreadMessage(event: MatrixEvent): boolean { + return event.isEncrypted() && (event.isRelation(THREAD_RELATION_TYPE.name) || event.isThreadRoot); +} + export const FILTER_RELATED_BY_SENDERS = new ServerControlledNamespacedValue( "related_by_senders", "io.element.relation_senders",