1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Make events pagination responses parse threads (#2011)

This commit is contained in:
Germain
2021-11-04 11:29:14 +00:00
committed by GitHub
parent 50332c4999
commit 195498e9db
2 changed files with 64 additions and 46 deletions

View File

@@ -4578,7 +4578,12 @@ export class MatrixClient extends EventEmitter {
const stateEvents = res.state.map(this.getEventMapper()); const stateEvents = res.state.map(this.getEventMapper());
room.currentState.setUnknownStateEvents(stateEvents); room.currentState.setUnknownStateEvents(stateEvents);
} }
room.addEventsToTimeline(matrixEvents, true, room.getLiveTimeline());
const [timelineEvents, threadedEvents] = this.partitionThreadedEvents(matrixEvents);
room.addEventsToTimeline(timelineEvents, true, room.getLiveTimeline());
this.processThreadEvents(room, threadedEvents);
room.oldState.paginationToken = res.end; room.oldState.paginationToken = res.end;
if (res.chunk.length === 0) { if (res.chunk.length === 0) {
room.oldState.paginationToken = null; room.oldState.paginationToken = null;
@@ -4684,7 +4689,11 @@ export class MatrixClient extends EventEmitter {
const stateEvents = res.state.map(this.getEventMapper()); const stateEvents = res.state.map(this.getEventMapper());
timeline.getState(EventTimeline.BACKWARDS).setUnknownStateEvents(stateEvents); timeline.getState(EventTimeline.BACKWARDS).setUnknownStateEvents(stateEvents);
} }
timelineSet.addEventsToTimeline(matrixEvents, true, timeline, res.start);
const [timelineEvents, threadedEvents] = this.partitionThreadedEvents(matrixEvents);
timelineSet.addEventsToTimeline(timelineEvents, true, timeline, res.start);
this.processThreadEvents(timelineSet.room, threadedEvents);
// there is no guarantee that the event ended up in "timeline" (we // there is no guarantee that the event ended up in "timeline" (we
// might have switched to a neighbouring timeline) - so check the // might have switched to a neighbouring timeline) - so check the
@@ -4817,8 +4826,11 @@ export class MatrixClient extends EventEmitter {
matrixEvents[i] = event; matrixEvents[i] = event;
} }
eventTimeline.getTimelineSet() const [timelineEvents, threadedEvents] = this.partitionThreadedEvents(matrixEvents);
.addEventsToTimeline(matrixEvents, backwards, eventTimeline, token);
const timelineSet = eventTimeline.getTimelineSet();
timelineSet.addEventsToTimeline(timelineEvents, backwards, eventTimeline, token);
this.processThreadEvents(timelineSet.room, threadedEvents);
// if we've hit the end of the timeline, we need to stop trying to // if we've hit the end of the timeline, we need to stop trying to
// paginate. We need to keep the 'forwards' token though, to make sure // paginate. We need to keep the 'forwards' token though, to make sure
@@ -4851,8 +4863,12 @@ export class MatrixClient extends EventEmitter {
} }
const token = res.end; const token = res.end;
const matrixEvents = res.chunk.map(this.getEventMapper()); const matrixEvents = res.chunk.map(this.getEventMapper());
const [timelineEvents, threadedEvents] = this.partitionThreadedEvents(matrixEvents);
eventTimeline.getTimelineSet() eventTimeline.getTimelineSet()
.addEventsToTimeline(matrixEvents, backwards, eventTimeline, token); .addEventsToTimeline(timelineEvents, backwards, eventTimeline, token);
this.processThreadEvents(room, threadedEvents);
// if we've hit the end of the timeline, we need to stop trying to // if we've hit the end of the timeline, we need to stop trying to
// paginate. We need to keep the 'forwards' token though, to make sure // paginate. We need to keep the 'forwards' token though, to make sure
@@ -8551,6 +8567,45 @@ export class MatrixClient extends EventEmitter {
prefix: "/_matrix/client/unstable/im.nheko.summary", prefix: "/_matrix/client/unstable/im.nheko.summary",
}); });
} }
public partitionThreadedEvents(events: MatrixEvent[]): [MatrixEvent[], MatrixEvent[]] {
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) {
const parentEventId = event.parentEventId;
const parentEvent = room?.findEventById(parentEventId) || events.find((mxEv: MatrixEvent) => {
return mxEv.getId() === parentEventId;
});
shouldLiveInThreadTimeline = parentEvent?.isThreadRelation;
}
memo[shouldLiveInThreadTimeline ? 1 : 0].push(event);
return memo;
}, [[], []]);
} else {
// When `experimentalThreadSupport` is disabled
// treat all events as timelineEvents
return [
events,
[],
];
}
}
/**
* @experimental
*/
public processThreadEvents(room: Room, threadedEvents: MatrixEvent[]): void {
threadedEvents
.sort((a, b) => a.getTs() - b.getTs())
.forEach(event => {
room.addThreadedEvent(event);
});
}
} }
/** /**

View File

@@ -285,7 +285,7 @@ export class SyncApi {
} }
leaveObj.timeline = leaveObj.timeline || {}; leaveObj.timeline = leaveObj.timeline || {};
const events = this.mapSyncEventsFormat(leaveObj.timeline, room); const events = this.mapSyncEventsFormat(leaveObj.timeline, room);
const [timelineEvents, threadedEvents] = this.partitionThreadedEvents(events); const [timelineEvents, threadedEvents] = this.client.partitionThreadedEvents(events);
const stateEvents = this.mapSyncEventsFormat(leaveObj.state, room); const stateEvents = this.mapSyncEventsFormat(leaveObj.state, room);
@@ -307,39 +307,6 @@ export class SyncApi {
}); });
} }
/**
* Split events between the ones that will end up in the main
* room timeline versus the one that need to be processed in a thread
* @experimental
*/
public partitionThreadedEvents(events: MatrixEvent[]): [MatrixEvent[], MatrixEvent[]] {
if (this.opts.experimentalThreadSupport) {
return events.reduce((memo, event: MatrixEvent) => {
const room = this.client.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) {
const parentEventId = event.parentEventId;
const parentEvent = room?.findEventById(parentEventId) || events.find((mxEv: MatrixEvent) => {
return mxEv.getId() === parentEventId;
});
shouldLiveInThreadTimeline = parentEvent?.isThreadRelation;
}
memo[shouldLiveInThreadTimeline ? 1 : 0].push(event);
return memo;
}, [[], []]);
} else {
// When `experimentalThreadSupport` is disabled
// treat all events as timelineEvents
return [
events,
[],
];
}
}
/** /**
* Peek into a room. This will result in the room in question being synced so it * Peek into a room. This will result in the room in question being synced so it
* is accessible via getRooms(). Live updates for the room will be provided. * is accessible via getRooms(). Live updates for the room will be provided.
@@ -1320,7 +1287,7 @@ export class SyncApi {
} }
} }
const [timelineEvents, threadedEvents] = this.partitionThreadedEvents(events); const [timelineEvents, threadedEvents] = this.client.partitionThreadedEvents(events);
this.processRoomEvents(room, stateEvents, timelineEvents, syncEventData.fromCache); this.processRoomEvents(room, stateEvents, timelineEvents, syncEventData.fromCache);
this.processThreadEvents(room, threadedEvents); this.processThreadEvents(room, threadedEvents);
@@ -1388,7 +1355,7 @@ export class SyncApi {
const events = this.mapSyncEventsFormat(leaveObj.timeline, room); const events = this.mapSyncEventsFormat(leaveObj.timeline, room);
const accountDataEvents = this.mapSyncEventsFormat(leaveObj.account_data); const accountDataEvents = this.mapSyncEventsFormat(leaveObj.account_data);
const [timelineEvents, threadedEvents] = this.partitionThreadedEvents(events); const [timelineEvents, threadedEvents] = this.client.partitionThreadedEvents(events);
this.processRoomEvents(room, stateEvents, timelineEvents); this.processRoomEvents(room, stateEvents, timelineEvents);
this.processThreadEvents(room, threadedEvents); this.processThreadEvents(room, threadedEvents);
@@ -1734,11 +1701,7 @@ export class SyncApi {
* @experimental * @experimental
*/ */
private processThreadEvents(room: Room, threadedEvents: MatrixEvent[]): void { private processThreadEvents(room: Room, threadedEvents: MatrixEvent[]): void {
threadedEvents return this.client.processThreadEvents(room, threadedEvents);
.sort((a, b) => a.getTs() - b.getTs())
.forEach(event => {
room.addThreadedEvent(event);
});
} }
// extractRelatedEvents(event: MatrixEvent, events: MatrixEvent[], relatedEvents: MatrixEvent[] = []): MatrixEvent[] { // extractRelatedEvents(event: MatrixEvent, events: MatrixEvent[], relatedEvents: MatrixEvent[] = []): MatrixEvent[] {