@@ -44,6 +44,7 @@ import { CHAT_EFFECTS } from "../../effects";
4444import { containsEmoji } from "../../effects/utils" ;
4545import dis from "../../dispatcher/dispatcher" ;
4646import { tryTransformPermalinkToLocalHref } from "../../utils/permalinks/Permalinks" ;
47+ import { MatrixEvent } from "matrix-js-sdk/src/models/event" ;
4748
4849// TODO: Purge this from the universe
4950
@@ -144,6 +145,52 @@ export class StopGapWidgetDriver extends WidgetDriver {
144145 return { roomId, eventId : r . event_id } ;
145146 }
146147
148+ public async readRoomEvents ( eventType : string , msgtype : string | undefined , limit : number ) : Promise < MatrixEvent [ ] > {
149+ limit = limit > 0 ? Math . min ( limit , 25 ) : 25 ; // arbitrary choice
150+
151+ const client = MatrixClientPeg . get ( ) ;
152+ const roomId = ActiveRoomObserver . activeRoomId ;
153+ const room = client . getRoom ( roomId ) ;
154+ if ( ! client || ! roomId || ! room ) throw new Error ( "Not in a room or not attached to a client" ) ;
155+
156+ const results : MatrixEvent [ ] = [ ] ;
157+ const events = room . getLiveTimeline ( ) . getEvents ( ) ; // timelines are most recent last
158+ for ( let i = events . length - 1 ; i > 0 ; i -- ) {
159+ if ( results . length >= limit ) break ;
160+
161+ const ev = events [ i ] ;
162+ if ( ev . getType ( ) !== eventType ) continue ;
163+ if ( eventType === EventType . RoomMessage && msgtype && msgtype !== ev . getContent ( ) [ 'msgtype' ] ) continue ;
164+ results . push ( ev ) ;
165+ }
166+
167+ return results . map ( e => e . event ) ;
168+ }
169+
170+ public async readStateEvents (
171+ eventType : string , stateKey : string | undefined , limit : number ,
172+ ) : Promise < MatrixEvent [ ] > {
173+ limit = limit > 0 ? Math . min ( limit , 100 ) : 100 ; // arbitrary choice
174+
175+ const client = MatrixClientPeg . get ( ) ;
176+ const roomId = ActiveRoomObserver . activeRoomId ;
177+ const room = client . getRoom ( roomId ) ;
178+ if ( ! client || ! roomId || ! room ) throw new Error ( "Not in a room or not attached to a client" ) ;
179+
180+ const results : MatrixEvent [ ] = [ ] ;
181+ const state = room . currentState . events . get ( eventType ) ;
182+ if ( state ) {
183+ if ( stateKey === "" || ! ! stateKey ) {
184+ const forKey = state . get ( stateKey ) ;
185+ if ( forKey ) results . push ( forKey ) ;
186+ } else {
187+ results . push ( ...Array . from ( state . values ( ) ) ) ;
188+ }
189+ }
190+
191+ return results . slice ( 0 , limit ) . map ( e => e . event ) ;
192+ }
193+
147194 public async askOpenID ( observer : SimpleObservable < IOpenIDUpdate > ) {
148195 const oidcState = WidgetPermissionStore . instance . getOIDCState (
149196 this . forWidget , this . forWidgetKind , this . inRoomId ,
0 commit comments