@@ -369,8 +369,43 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
369369 * timeline which would otherwise be unable to paginate forwards without this token).
370370 * Removing just the old live timeline whilst preserving previous ones is not supported.
371371 */
372- public resetLiveTimeline ( backPaginationToken ?: string | null , forwardPaginationToken ?: string | null ) : void {
372+ public async resetLiveTimeline ( backPaginationToken ?: string | null , forwardPaginationToken ?: string | null ) : Promise < void > {
373+ const oldLive = this . liveTimeline ;
373374 this . timelineSet . resetLiveTimeline ( backPaginationToken ?? undefined , forwardPaginationToken ?? undefined ) ;
375+ const newLive = this . liveTimeline ;
376+
377+ // FIXME: Remove the following as soon as https://github.com/matrix-org/synapse/issues/14830 is resolved.
378+ //
379+ // The pagination API for thread timelines currently can't handle the type of pagination tokens returned by sync
380+ //
381+ // To make this work anyway, we'll have to transform them into one of the types that the API can handle.
382+ // One option is passing the tokens to /messages, which can handle sync tokens, and returns the right format.
383+ // /messages does not return new tokens on requests with a limit of 0.
384+ // This means our timelines might overlap a slight bit, but that's not an issue, as we deduplicate messages
385+ // anyway.
386+
387+ let newBackward : string | undefined ;
388+ let oldForward : string | undefined ;
389+ if ( backPaginationToken ) {
390+ const res = await this . client . createMessagesRequest (
391+ this . roomId , backPaginationToken , 1 , Direction . Forward ,
392+ ) ;
393+ newBackward = res . end ;
394+ }
395+ if ( forwardPaginationToken ) {
396+ const res = await this . client . createMessagesRequest (
397+ this . roomId , forwardPaginationToken , 1 , Direction . Backward ,
398+ ) ;
399+ oldForward = res . start ;
400+ }
401+ // Only replace the token if we don't have paginated away from this position already. This situation doesn't
402+ // occur today, but if the above issue is resolved, we'd have to go down this path.
403+ if ( forwardPaginationToken && oldLive . getPaginationToken ( Direction . Forward ) === forwardPaginationToken ) {
404+ oldLive . setPaginationToken ( oldForward ?? null , Direction . Forward ) ;
405+ }
406+ if ( backPaginationToken && newLive . getPaginationToken ( Direction . Backward ) === backPaginationToken ) {
407+ newLive . setPaginationToken ( newBackward ?? null , Direction . Backward ) ;
408+ }
374409 }
375410
376411 private async updateThreadMetadata ( ) : Promise < void > {
0 commit comments