@@ -19,7 +19,6 @@ import {
19
19
MatrixCapabilities ,
20
20
OpenIDRequestState ,
21
21
SimpleObservable ,
22
- Symbols ,
23
22
Widget ,
24
23
WidgetDriver ,
25
24
WidgetEventCapability ,
@@ -36,7 +35,6 @@ import {
36
35
IContent ,
37
36
MatrixError ,
38
37
MatrixEvent ,
39
- Room ,
40
38
Direction ,
41
39
THREAD_RELATION_TYPE ,
42
40
SendDelayedEventResponse ,
@@ -469,70 +467,69 @@ export class StopGapWidgetDriver extends WidgetDriver {
469
467
}
470
468
}
471
469
472
- private pickRooms ( roomIds ?: ( string | Symbols . AnyRoom ) [ ] ) : Room [ ] {
473
- const client = MatrixClientPeg . get ( ) ;
474
- if ( ! client ) throw new Error ( "Not attached to a client" ) ;
475
-
476
- const targetRooms = roomIds
477
- ? roomIds . includes ( Symbols . AnyRoom )
478
- ? client . getVisibleRooms ( SettingsStore . getValue ( "feature_dynamic_room_predecessors" ) )
479
- : roomIds . map ( ( r ) => client . getRoom ( r ) )
480
- : [ client . getRoom ( SdkContextClass . instance . roomViewStore . getRoomId ( ) ! ) ] ;
481
- return targetRooms . filter ( ( r ) => ! ! r ) as Room [ ] ;
482
- }
483
-
484
- public async readRoomEvents (
470
+ /**
471
+ * Reads all events of the given type, and optionally `msgtype` (if applicable/defined),
472
+ * the user has access to. The widget API will have already verified that the widget is
473
+ * capable of receiving the events. Less events than the limit are allowed to be returned,
474
+ * but not more.
475
+ * @param roomId The ID of the room to look within.
476
+ * @param eventType The event type to be read.
477
+ * @param msgtype The msgtype of the events to be read, if applicable/defined.
478
+ * @param stateKey The state key of the events to be read, if applicable/defined.
479
+ * @param limit The maximum number of events to retrieve. Will be zero to denote "as many as
480
+ * possible".
481
+ * @param since When null, retrieves the number of events specified by the "limit" parameter.
482
+ * Otherwise, the event ID at which only subsequent events will be returned, as many as specified
483
+ * in "limit".
484
+ * @returns {Promise<IRoomEvent[]> } Resolves to the room events, or an empty array.
485
+ */
486
+ public async readRoomTimeline (
487
+ roomId : string ,
485
488
eventType : string ,
486
489
msgtype : string | undefined ,
487
- limitPerRoom : number ,
488
- roomIds ?: ( string | Symbols . AnyRoom ) [ ] ,
490
+ stateKey : string | undefined ,
491
+ limit : number ,
492
+ since : string | undefined ,
489
493
) : Promise < IRoomEvent [ ] > {
490
- limitPerRoom = limitPerRoom > 0 ? Math . min ( limitPerRoom , Number . MAX_SAFE_INTEGER ) : Number . MAX_SAFE_INTEGER ; // relatively arbitrary
491
-
492
- const rooms = this . pickRooms ( roomIds ) ;
493
- const allResults : IRoomEvent [ ] = [ ] ;
494
- for ( const room of rooms ) {
495
- const results : MatrixEvent [ ] = [ ] ;
496
- const events = room . getLiveTimeline ( ) . getEvents ( ) ; // timelines are most recent last
497
- for ( let i = events . length - 1 ; i > 0 ; i -- ) {
498
- if ( results . length >= limitPerRoom ) break ;
499
-
500
- const ev = events [ i ] ;
501
- if ( ev . getType ( ) !== eventType || ev . isState ( ) ) continue ;
502
- if ( eventType === EventType . RoomMessage && msgtype && msgtype !== ev . getContent ( ) [ "msgtype" ] ) continue ;
503
- results . push ( ev ) ;
504
- }
505
-
506
- results . forEach ( ( e ) => allResults . push ( e . getEffectiveEvent ( ) as IRoomEvent ) ) ;
494
+ limit = limit > 0 ? Math . min ( limit , Number . MAX_SAFE_INTEGER ) : Number . MAX_SAFE_INTEGER ; // relatively arbitrary
495
+
496
+ const room = MatrixClientPeg . safeGet ( ) . getRoom ( roomId ) ;
497
+ if ( room === null ) return [ ] ;
498
+ const results : MatrixEvent [ ] = [ ] ;
499
+ const events = room . getLiveTimeline ( ) . getEvents ( ) ; // timelines are most recent last
500
+ for ( let i = events . length - 1 ; i >= 0 ; i -- ) {
501
+ const ev = events [ i ] ;
502
+ if ( results . length >= limit ) break ;
503
+ if ( since !== undefined && ev . getId ( ) === since ) break ;
504
+
505
+ if ( ev . getType ( ) !== eventType || ev . isState ( ) ) continue ;
506
+ if ( eventType === EventType . RoomMessage && msgtype && msgtype !== ev . getContent ( ) [ "msgtype" ] ) continue ;
507
+ if ( ev . getStateKey ( ) !== undefined && stateKey !== undefined && ev . getStateKey ( ) !== stateKey ) continue ;
508
+ results . push ( ev ) ;
507
509
}
508
- return allResults ;
509
- }
510
510
511
- public async readStateEvents (
512
- eventType : string ,
513
- stateKey : string | undefined ,
514
- limitPerRoom : number ,
515
- roomIds ?: ( string | Symbols . AnyRoom ) [ ] ,
516
- ) : Promise < IRoomEvent [ ] > {
517
- limitPerRoom = limitPerRoom > 0 ? Math . min ( limitPerRoom , Number . MAX_SAFE_INTEGER ) : Number . MAX_SAFE_INTEGER ; // relatively arbitrary
518
-
519
- const rooms = this . pickRooms ( roomIds ) ;
520
- const allResults : IRoomEvent [ ] = [ ] ;
521
- for ( const room of rooms ) {
522
- const results : MatrixEvent [ ] = [ ] ;
523
- const state = room . currentState . events . get ( eventType ) ;
524
- if ( state ) {
525
- if ( stateKey === "" || ! ! stateKey ) {
526
- const forKey = state . get ( stateKey ) ;
527
- if ( forKey ) results . push ( forKey ) ;
528
- } else {
529
- results . push ( ...Array . from ( state . values ( ) ) ) ;
530
- }
531
- }
511
+ return results . map ( ( e ) => e . getEffectiveEvent ( ) as IRoomEvent ) ;
512
+ }
532
513
533
- results . slice ( 0 , limitPerRoom ) . forEach ( ( e ) => allResults . push ( e . getEffectiveEvent ( ) as IRoomEvent ) ) ;
534
- }
535
- return allResults ;
514
+ /**
515
+ * Reads the current values of all matching room state entries.
516
+ * @param roomId The ID of the room.
517
+ * @param eventType The event type of the entries to be read.
518
+ * @param stateKey The state key of the entry to be read. If undefined,
519
+ * all room state entries with a matching event type should be returned.
520
+ * @returns {Promise<IRoomEvent[]> } Resolves to the events representing the
521
+ * current values of the room state entries.
522
+ */
523
+ public async readRoomState ( roomId : string , eventType : string , stateKey : string | undefined ) : Promise < IRoomEvent [ ] > {
524
+ const room = MatrixClientPeg . safeGet ( ) . getRoom ( roomId ) ;
525
+ if ( room === null ) return [ ] ;
526
+ const state = room . getLiveTimeline ( ) . getState ( Direction . Forward ) ;
527
+ if ( state === undefined ) return [ ] ;
528
+
529
+ if ( stateKey === undefined )
530
+ return state . getStateEvents ( eventType ) . map ( ( e ) => e . getEffectiveEvent ( ) as IRoomEvent ) ;
531
+ const event = state . getStateEvents ( eventType , stateKey ) ;
532
+ return event === null ? [ ] : [ event . getEffectiveEvent ( ) as IRoomEvent ] ;
536
533
}
537
534
538
535
public async askOpenID ( observer : SimpleObservable < IOpenIDUpdate > ) : Promise < void > {
@@ -693,6 +690,17 @@ export class StopGapWidgetDriver extends WidgetDriver {
693
690
return { file : blob } ;
694
691
}
695
692
693
+ /**
694
+ * Gets the IDs of all joined or invited rooms currently known to the
695
+ * client.
696
+ * @returns The room IDs.
697
+ */
698
+ public getKnownRooms ( ) : string [ ] {
699
+ return MatrixClientPeg . safeGet ( )
700
+ . getVisibleRooms ( SettingsStore . getValue ( "feature_dynamic_room_predecessors" ) )
701
+ . map ( ( r ) => r . roomId ) ;
702
+ }
703
+
696
704
/**
697
705
* Expresses a {@link MatrixError} as a JSON payload
698
706
* for use by Widget API error responses.
0 commit comments