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

Adjust fetchEditsWhereNeeded to use a clearer filter and async function #3411

Merged
merged 2 commits into from
May 26, 2023
Merged
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
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