@@ -14,18 +14,13 @@ import {
1414 RoomCreateOptions ,
1515 RoomCreator ,
1616 RoomEventRedacter ,
17- RoomEventRelationsGetter ,
1817 RoomJoiner ,
1918 RoomKicker ,
2019 RoomStateEventSender ,
21- PaginationError ,
2220 Value ,
2321 isError ,
2422 RoomEvent ,
25- doPagination ,
2623 EventDecoder ,
27- RoomEventRelationsOptions ,
28- StandardChunkPage ,
2924 RoomMessageSender ,
3025 MessageContent ,
3126 ClientRooms ,
@@ -35,9 +30,19 @@ import {
3530 MultipleErrors ,
3631 ClientCapabilitiesNegotiation ,
3732 ClientCapabilitiesResponse ,
33+ RoomMessages ,
34+ PaginationIterator ,
35+ RoomMessagesOptions ,
36+ RoomMessagesPaginator ,
37+ PaginationChunk ,
38+ RoomMessagesResponse ,
39+ StandardPaginationIterator ,
40+ RoomEventRelations ,
41+ RoomEventRelationsPaginator ,
42+ RoomEventRelationsOptions ,
43+ RoomEventRelationsResponse ,
3844} from 'matrix-protection-suite' ;
3945import { MatrixSendClient } from '../MatrixEmitter' ;
40- import { getRelationsForEvent } from './PaginationAPIs' ;
4146import {
4247 MatrixRoomID ,
4348 StringRoomID ,
@@ -155,9 +160,10 @@ export class BotSDKBaseClient
155160 RoomCreator ,
156161 RoomEventGetter ,
157162 RoomEventRedacter ,
158- RoomEventRelationsGetter ,
163+ RoomEventRelations ,
159164 RoomJoiner ,
160165 RoomKicker ,
166+ RoomMessages ,
161167 RoomMessageSender ,
162168 RoomReactionSender ,
163169 RoomStateEventSender ,
@@ -342,24 +348,72 @@ export class BotSDKBaseClient
342348 resultifyBotSDKRequestError
343349 ) ;
344350 }
345- public async forEachRelation < ChunkItem = RoomEvent > (
351+
352+ public toRoomEventRelationsPaginator < TEvent extends RoomEvent = RoomEvent > (
353+ roomID : StringRoomID ,
354+ eventID : StringEventID
355+ ) : RoomEventRelationsPaginator < TEvent > {
356+ return Object . freeze ( {
357+ client : this . client ,
358+ eventDecoder : this . eventDecoder ,
359+ async fetchPage (
360+ ergonomicOptions : RoomEventRelationsOptions
361+ ) : Promise < ActionResult < PaginationChunk < TEvent > > > {
362+ const options = {
363+ ...ergonomicOptions ,
364+ dir : ergonomicOptions . direction === 'forwards' ? 'f' : 'b' ,
365+ } ;
366+ return this . client
367+ . doRequest (
368+ 'GET' ,
369+ `/_matrix/client/v1/rooms/${ encodeURIComponent (
370+ roomID
371+ ) } /relations/${ encodeURIComponent ( eventID ) } `,
372+ options
373+ )
374+ . then ( ( response ) => {
375+ const firstSchemaPass = Value . Decode (
376+ RoomEventRelationsResponse ,
377+ response
378+ ) ;
379+ if ( isError ( firstSchemaPass ) ) {
380+ return firstSchemaPass . elaborate (
381+ 'The response for /relations is severly malformed'
382+ ) ;
383+ }
384+ const parsedEvents : TEvent [ ] = [ ] ;
385+ for ( const event of firstSchemaPass . ok . chunk ) {
386+ const decodedEvent = this . eventDecoder . decodeEvent ( event ) ;
387+ if ( isError ( decodedEvent ) ) {
388+ return decodedEvent . elaborate (
389+ 'Failed to decode an event in the /relations response'
390+ ) ;
391+ }
392+ parsedEvents . push ( decodedEvent . ok as TEvent ) ;
393+ }
394+ return Ok ( {
395+ previousToken : firstSchemaPass . ok . prev_batch ,
396+ nextToken : firstSchemaPass . ok . next_batch ,
397+ chunk : parsedEvents ,
398+ hasNext : firstSchemaPass . ok . next_batch !== undefined ,
399+ hasPrevious : true ,
400+ } satisfies PaginationChunk < TEvent > ) ;
401+ } ) ;
402+ } ,
403+ } ) ;
404+ }
405+
406+ toRoomEventRelationsIterator < TEvent extends RoomEvent = RoomEvent > (
346407 roomID : StringRoomID ,
347408 eventID : StringEventID ,
348- options : RoomEventRelationsOptions < ChunkItem >
349- ) : Promise < ActionResult < void , PaginationError > > {
350- const startingPage = StandardChunkPage . createFirstPage < ChunkItem > (
351- async ( ) =>
352- await getRelationsForEvent < ChunkItem > (
353- this . client ,
354- this . eventDecoder ,
355- roomID ,
356- eventID ,
357- options
358- ) ,
359- options
409+ options : RoomEventRelationsOptions
410+ ) : PaginationIterator < TEvent > {
411+ return new StandardPaginationIterator (
412+ options ,
413+ this . toRoomEventRelationsPaginator ( roomID , eventID )
360414 ) ;
361- return await doPagination ( startingPage , options ) ;
362415 }
416+
363417 public async getAllState < T extends StateEvent > (
364418 room : MatrixRoomID | StringRoomID
365419 ) : Promise < ActionResult < T [ ] > > {
@@ -415,4 +469,67 @@ export class BotSDKBaseClient
415469 resultifyBotSDKRequestError
416470 ) ;
417471 }
472+
473+ toRoomMessagesPaginator < TEvent extends RoomEvent = RoomEvent > (
474+ roomID : StringRoomID
475+ ) : RoomMessagesPaginator < TEvent > {
476+ return Object . freeze ( {
477+ client : this . client ,
478+ eventDecoder : this . eventDecoder ,
479+ async fetchPage (
480+ ergonomicOptions : RoomMessagesOptions
481+ ) : Promise < ActionResult < PaginationChunk < TEvent > > > {
482+ const options = {
483+ ...ergonomicOptions ,
484+ filter : ergonomicOptions . filter
485+ ? JSON . stringify ( ergonomicOptions . filter )
486+ : undefined ,
487+ dir : ergonomicOptions . direction === 'forwards' ? 'f' : 'b' ,
488+ } ;
489+ return this . client
490+ . doRequest (
491+ 'GET' ,
492+ `/_matrix/client/v3/rooms/${ encodeURIComponent ( roomID ) } /messages` ,
493+ options
494+ )
495+ . then ( ( response ) => {
496+ const firstSchemaPass = Value . Decode (
497+ RoomMessagesResponse ,
498+ response
499+ ) ;
500+ if ( isError ( firstSchemaPass ) ) {
501+ return firstSchemaPass . elaborate (
502+ 'The response for /messages is severly malformed'
503+ ) ;
504+ }
505+ const parsedEvents : TEvent [ ] = [ ] ;
506+ for ( const event of firstSchemaPass . ok . chunk ) {
507+ const decodedEvent = this . eventDecoder . decodeEvent ( event ) ;
508+ if ( isError ( decodedEvent ) ) {
509+ return decodedEvent . elaborate (
510+ 'Failed to decode an event in the /messages response'
511+ ) ;
512+ }
513+ parsedEvents . push ( decodedEvent . ok as TEvent ) ;
514+ }
515+ return Ok ( {
516+ previousToken : firstSchemaPass . ok . start ,
517+ nextToken : firstSchemaPass . ok . end ,
518+ chunk : parsedEvents ,
519+ hasNext : firstSchemaPass . ok . end !== undefined ,
520+ hasPrevious : true ,
521+ } satisfies PaginationChunk < TEvent > ) ;
522+ } ) ;
523+ } ,
524+ } ) ;
525+ }
526+ toRoomMessagesIterator < TEvent extends RoomEvent = RoomEvent > (
527+ roomID : StringRoomID ,
528+ options : RoomMessagesOptions
529+ ) : PaginationIterator < TEvent > {
530+ return new StandardPaginationIterator (
531+ options ,
532+ this . toRoomMessagesPaginator ( roomID )
533+ ) ;
534+ }
418535}
0 commit comments