Skip to content

Commit

Permalink
Make events pagination responses parse threads (matrix-org#2011)
Browse files Browse the repository at this point in the history
  • Loading branch information
germain-gg authored and skyka13711 committed Apr 14, 2022
1 parent c3f4949 commit a43b104
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8840,36 +8840,21 @@ export class MatrixClient extends EventEmitter {
}

public partitionThreadedEvents(events: MatrixEvent[]): [MatrixEvent[], MatrixEvent[]] {
// Indices to the events array, for readibility
const ROOM = 0;
const THREAD = 1;
const threadRoots = new Set<string>();
if (this.supportsExperimentalThreads()) {
if (this.supportsExperimentalThreads) {
return events.reduce((memo, event: MatrixEvent) => {
const room = this.getRoom(event.getRoomId());
// An event should live in the thread timeline if
// - It's a reply in thread event
// - It's related to a reply in thread event
let shouldLiveInThreadTimeline = event.isThreadRelation;
if (shouldLiveInThreadTimeline) {
threadRoots.add(event.relationEventId);
} else {
if (!shouldLiveInThreadTimeline) {
const parentEventId = event.parentEventId;
const parentEvent = room?.findEventById(parentEventId) || events.find((mxEv: MatrixEvent) => {
return mxEv.getId() === parentEventId;
});
shouldLiveInThreadTimeline = parentEvent?.isThreadRelation;

// Copy all the reactions and annotations to the root event
// to the thread timeline. They will end up living in both
// timelines at the same time
const targetingThreadRoot = parentEvent?.isThreadRoot || threadRoots.has(event.relationEventId);
if (targetingThreadRoot && !event.isThreadRelation && event.relationEventId) {
memo[THREAD].push(event);
}
}
const targetTimeline = shouldLiveInThreadTimeline ? THREAD : ROOM;
memo[targetTimeline].push(event);
memo[shouldLiveInThreadTimeline ? 1 : 0].push(event);
return memo;
}, [[], []]);
} else {
Expand Down

0 comments on commit a43b104

Please sign in to comment.