@@ -33,6 +33,7 @@ import {
3333 WidgetKind ,
3434} from "matrix-widget-api" ;
3535import { EventEmitter } from "events" ;
36+ import { MatrixClient } from "matrix-js-sdk/src/client" ;
3637import { MatrixEvent , MatrixEventEvent } from "matrix-js-sdk/src/models/event" ;
3738import { logger } from "matrix-js-sdk/src/logger" ;
3839import { ClientEvent } from "matrix-js-sdk/src/client" ;
@@ -148,6 +149,7 @@ export class ElementWidget extends Widget {
148149}
149150
150151export class StopGapWidget extends EventEmitter {
152+ private client : MatrixClient ;
151153 private messaging : ClientWidgetApi ;
152154 private mockWidget : ElementWidget ;
153155 private scalarToken : string ;
@@ -157,12 +159,13 @@ export class StopGapWidget extends EventEmitter {
157159
158160 constructor ( private appTileProps : IAppTileProps ) {
159161 super ( ) ;
160- let app = appTileProps . app ;
162+ this . client = MatrixClientPeg . get ( ) ;
161163
164+ let app = appTileProps . app ;
162165 // Backwards compatibility: not all old widgets have a creatorUserId
163166 if ( ! app . creatorUserId ) {
164167 app = objectShallowClone ( app ) ; // clone to prevent accidental mutation
165- app . creatorUserId = MatrixClientPeg . get ( ) . getUserId ( ) ;
168+ app . creatorUserId = this . client . getUserId ( ) ;
166169 }
167170
168171 this . mockWidget = new ElementWidget ( app ) ;
@@ -203,7 +206,7 @@ export class StopGapWidget extends EventEmitter {
203206 const fromCustomisation = WidgetVariableCustomisations ?. provideVariables ?.( ) ?? { } ;
204207 const defaults : ITemplateParams = {
205208 widgetRoomId : this . roomId ,
206- currentUserId : MatrixClientPeg . get ( ) . getUserId ( ) ,
209+ currentUserId : this . client . getUserId ( ) ,
207210 userDisplayName : OwnProfileStore . instance . displayName ,
208211 userHttpAvatarUrl : OwnProfileStore . instance . getHttpAvatarUrl ( ) ,
209212 clientId : ELEMENT_CLIENT_ID ,
@@ -260,8 +263,10 @@ export class StopGapWidget extends EventEmitter {
260263 */
261264 public startMessaging ( iframe : HTMLIFrameElement ) : any {
262265 if ( this . started ) return ;
266+
263267 const allowedCapabilities = this . appTileProps . whitelistCapabilities || [ ] ;
264268 const driver = new StopGapWidgetDriver ( allowedCapabilities , this . mockWidget , this . kind , this . roomId ) ;
269+
265270 this . messaging = new ClientWidgetApi ( this . mockWidget , iframe , driver ) ;
266271 this . messaging . on ( "preparing" , ( ) => this . emit ( "preparing" ) ) ;
267272 this . messaging . on ( "ready" , ( ) => this . emit ( "ready" ) ) ;
@@ -302,7 +307,7 @@ export class StopGapWidget extends EventEmitter {
302307 // Populate the map of "read up to" events for this widget with the current event in every room.
303308 // This is a bit inefficient, but should be okay. We do this for all rooms in case the widget
304309 // requests timeline capabilities in other rooms down the road. It's just easier to manage here.
305- for ( const room of MatrixClientPeg . get ( ) . getRooms ( ) ) {
310+ for ( const room of this . client . getRooms ( ) ) {
306311 // Timelines are most recent last
307312 const events = room . getLiveTimeline ( ) ?. getEvents ( ) || [ ] ;
308313 const roomEvent = events [ events . length - 1 ] ;
@@ -311,8 +316,9 @@ export class StopGapWidget extends EventEmitter {
311316 }
312317
313318 // Attach listeners for feeding events - the underlying widget classes handle permissions for us
314- MatrixClientPeg . get ( ) . on ( ClientEvent . Event , this . onEvent ) ;
315- MatrixClientPeg . get ( ) . on ( MatrixEventEvent . Decrypted , this . onEventDecrypted ) ;
319+ this . client . on ( ClientEvent . Event , this . onEvent ) ;
320+ this . client . on ( MatrixEventEvent . Decrypted , this . onEventDecrypted ) ;
321+ this . client . on ( ClientEvent . ToDeviceEvent , this . onToDeviceEvent ) ;
316322
317323 this . messaging . on ( `action:${ WidgetApiFromWidgetAction . UpdateAlwaysOnScreen } ` ,
318324 ( ev : CustomEvent < IStickyActionRequest > ) => {
@@ -363,7 +369,7 @@ export class StopGapWidget extends EventEmitter {
363369
364370 // noinspection JSIgnoredPromiseFromCall
365371 IntegrationManagers . sharedInstance ( ) . getPrimaryManager ( ) . open (
366- MatrixClientPeg . get ( ) . getRoom ( RoomViewStore . instance . getRoomId ( ) ) ,
372+ this . client . getRoom ( RoomViewStore . instance . getRoomId ( ) ) ,
367373 `type_${ integType } ` ,
368374 integId ,
369375 ) ;
@@ -428,14 +434,13 @@ export class StopGapWidget extends EventEmitter {
428434 WidgetMessagingStore . instance . stopMessaging ( this . mockWidget , this . roomId ) ;
429435 this . messaging = null ;
430436
431- if ( MatrixClientPeg . get ( ) ) {
432- MatrixClientPeg . get ( ) . off ( ClientEvent . Event , this . onEvent ) ;
433- MatrixClientPeg . get ( ) . off ( MatrixEventEvent . Decrypted , this . onEventDecrypted ) ;
434- }
437+ this . client . off ( ClientEvent . Event , this . onEvent ) ;
438+ this . client . off ( MatrixEventEvent . Decrypted , this . onEventDecrypted ) ;
439+ this . client . off ( ClientEvent . ToDeviceEvent , this . onToDeviceEvent ) ;
435440 }
436441
437442 private onEvent = ( ev : MatrixEvent ) => {
438- MatrixClientPeg . get ( ) . decryptEventIfNeeded ( ev ) ;
443+ this . client . decryptEventIfNeeded ( ev ) ;
439444 if ( ev . isBeingDecrypted ( ) || ev . isDecryptionFailure ( ) ) return ;
440445 this . feedEvent ( ev ) ;
441446 } ;
@@ -445,6 +450,12 @@ export class StopGapWidget extends EventEmitter {
445450 this . feedEvent ( ev ) ;
446451 } ;
447452
453+ private onToDeviceEvent = async ( ev : MatrixEvent ) => {
454+ await this . client . decryptEventIfNeeded ( ev ) ;
455+ if ( ev . isDecryptionFailure ( ) ) return ;
456+ await this . messaging . feedToDevice ( ev . getEffectiveEvent ( ) , ev . isEncrypted ( ) ) ;
457+ } ;
458+
448459 private feedEvent ( ev : MatrixEvent ) {
449460 if ( ! this . messaging ) return ;
450461
@@ -465,7 +476,7 @@ export class StopGapWidget extends EventEmitter {
465476
466477 // Timelines are most recent last, so reverse the order and limit ourselves to 100 events
467478 // to avoid overusing the CPU.
468- const timeline = MatrixClientPeg . get ( ) . getRoom ( ev . getRoomId ( ) ) . getLiveTimeline ( ) ;
479+ const timeline = this . client . getRoom ( ev . getRoomId ( ) ) . getLiveTimeline ( ) ;
469480 const events = arrayFastClone ( timeline . getEvents ( ) ) . reverse ( ) . slice ( 0 , 100 ) ;
470481
471482 for ( const timelineEvent of events ) {
0 commit comments