Skip to content

Commit

Permalink
Adjust fetchEditsWhereNeeded to use a clearer filter and async functi…
Browse files Browse the repository at this point in the history
…on (matrix-org#3411)

* Make a clear and explicit filter on which events are considered for fetchEventsWhereNeeded

* Convert the logic in fetchEventsWhereNeeded to an async function
  • Loading branch information
andybalaam authored May 26, 2023
1 parent 7ade461 commit b29e1e9
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/models/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,26 +519,26 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
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);
}
}),
);
}
}
Expand Down Expand Up @@ -708,6 +708,16 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
}
}

/**
* 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",
Expand Down

0 comments on commit b29e1e9

Please sign in to comment.