66 * Please see LICENSE files in the repository root for full details. 
77 */ 
88
9- import  {  Room ,  MatrixEvent ,  MatrixEventEvent ,  MatrixClient ,  ClientEvent  }  from  "matrix-js-sdk/src/matrix" ; 
9+ import  { 
10+     Room , 
11+     MatrixEvent , 
12+     MatrixEventEvent , 
13+     MatrixClient , 
14+     ClientEvent , 
15+     RoomStateEvent , 
16+ }  from  "matrix-js-sdk/src/matrix" ; 
1017import  {  KnownMembership  }  from  "matrix-js-sdk/src/types" ; 
1118import  { 
1219    ClientWidgetApi , 
@@ -26,7 +33,6 @@ import {
2633    WidgetApiFromWidgetAction , 
2734    WidgetKind , 
2835}  from  "matrix-widget-api" ; 
29- import  {  Optional  }  from  "matrix-events-sdk" ; 
3036import  {  EventEmitter  }  from  "events" ; 
3137import  {  logger  }  from  "matrix-js-sdk/src/logger" ; 
3238
@@ -56,6 +62,7 @@ import { ViewRoomPayload } from "../../dispatcher/payloads/ViewRoomPayload";
5662import  Modal  from  "../../Modal" ; 
5763import  ErrorDialog  from  "../../components/views/dialogs/ErrorDialog" ; 
5864import  {  SdkContextClass  }  from  "../../contexts/SDKContext" ; 
65+ import  {  UPDATE_EVENT  }  from  "../AsyncStore" ; 
5966
6067// TODO: Destroy all of this code 
6168
@@ -151,6 +158,7 @@ export class StopGapWidget extends EventEmitter {
151158    private  mockWidget : ElementWidget ; 
152159    private  scalarToken ?: string ; 
153160    private  roomId ?: string ; 
161+     private  viewedRoomId : string  |  null  =  null ; 
154162    private  kind : WidgetKind ; 
155163    private  readonly  virtual : boolean ; 
156164    private  readUpToMap : {  [ roomId : string ] : string  }  =  { } ;  // room ID to event ID 
@@ -177,17 +185,6 @@ export class StopGapWidget extends EventEmitter {
177185        this . stickyPromise  =  appTileProps . stickyPromise ; 
178186    } 
179187
180-     private  get  eventListenerRoomId ( ) : Optional < string >  { 
181-         // When widgets are listening to events, we need to make sure they're only 
182-         // receiving events for the right room. In particular, room widgets get locked 
183-         // to the room they were added in while account widgets listen to the currently 
184-         // active room. 
185- 
186-         if  ( this . roomId )  return  this . roomId ; 
187- 
188-         return  SdkContextClass . instance . roomViewStore . getRoomId ( ) ; 
189-     } 
190- 
191188    public  get  widgetApi ( ) : ClientWidgetApi  |  null  { 
192189        return  this . messaging ; 
193190    } 
@@ -259,6 +256,15 @@ export class StopGapWidget extends EventEmitter {
259256            } ) ; 
260257        } 
261258    } ; 
259+ 
260+     private  onRoomViewStoreUpdate  =  ( ) : void   =>  { 
261+         const  roomId  =  SdkContextClass . instance . roomViewStore . getRoomId ( )  ??  null ; 
262+         if  ( roomId  !==  this . viewedRoomId )  { 
263+             this . messaging ! . setViewedRoomId ( roomId ) ; 
264+             this . viewedRoomId  =  roomId ; 
265+         } 
266+     } ; 
267+ 
262268    /** 
263269     * This starts the messaging for the widget if it is not in the state `started` yet. 
264270     * @param  iframe the iframe the widget should use 
@@ -285,6 +291,17 @@ export class StopGapWidget extends EventEmitter {
285291        this . messaging . on ( "capabilitiesNotified" ,  ( )  =>  this . emit ( "capabilitiesNotified" ) ) ; 
286292        this . messaging . on ( `action:${ WidgetApiFromWidgetAction . OpenModalWidget }  ` ,  this . onOpenModal ) ; 
287293
294+         // When widgets are listening to events, we need to make sure they're only 
295+         // receiving events for the right room 
296+         if  ( this . roomId  ===  undefined )  { 
297+             // Account widgets listen to the currently active room 
298+             this . messaging . setViewedRoomId ( SdkContextClass . instance . roomViewStore . getRoomId ( )  ??  null ) ; 
299+             SdkContextClass . instance . roomViewStore . on ( UPDATE_EVENT ,  this . onRoomViewStoreUpdate ) ; 
300+         }  else  { 
301+             // Room widgets get looked to the room they were added in 
302+             this . messaging . setViewedRoomId ( this . roomId ) ; 
303+         } 
304+ 
288305        // Always attach a handler for ViewRoom, but permission check it internally 
289306        this . messaging . on ( `action:${ ElementWidgetActions . ViewRoom }  ` ,  ( ev : CustomEvent < IViewRoomApiRequest > )  =>  { 
290307            ev . preventDefault ( ) ;  // stop the widget API from auto-rejecting this 
@@ -329,6 +346,7 @@ export class StopGapWidget extends EventEmitter {
329346        // Attach listeners for feeding events - the underlying widget classes handle permissions for us 
330347        this . client . on ( ClientEvent . Event ,  this . onEvent ) ; 
331348        this . client . on ( MatrixEventEvent . Decrypted ,  this . onEventDecrypted ) ; 
349+         this . client . on ( RoomStateEvent . Events ,  this . onStateUpdate ) ; 
332350        this . client . on ( ClientEvent . ToDeviceEvent ,  this . onToDeviceEvent ) ; 
333351
334352        this . messaging . on ( 
@@ -457,8 +475,11 @@ export class StopGapWidget extends EventEmitter {
457475        WidgetMessagingStore . instance . stopMessaging ( this . mockWidget ,  this . roomId ) ; 
458476        this . messaging  =  null ; 
459477
478+         SdkContextClass . instance . roomViewStore . off ( UPDATE_EVENT ,  this . onRoomViewStoreUpdate ) ; 
479+ 
460480        this . client . off ( ClientEvent . Event ,  this . onEvent ) ; 
461481        this . client . off ( MatrixEventEvent . Decrypted ,  this . onEventDecrypted ) ; 
482+         this . client . off ( RoomStateEvent . Events ,  this . onStateUpdate ) ; 
462483        this . client . off ( ClientEvent . ToDeviceEvent ,  this . onToDeviceEvent ) ; 
463484    } 
464485
@@ -471,6 +492,14 @@ export class StopGapWidget extends EventEmitter {
471492        this . feedEvent ( ev ) ; 
472493    } ; 
473494
495+     private  onStateUpdate  =  ( ev : MatrixEvent ) : void   =>  { 
496+         if  ( this . messaging  ===  null )  return ; 
497+         const  raw  =  ev . getEffectiveEvent ( ) ; 
498+         this . messaging . feedStateUpdate ( raw  as  IRoomEvent ) . catch ( ( e )  =>  { 
499+             logger . error ( "Error sending state update to widget: " ,  e ) ; 
500+         } ) ; 
501+     } ; 
502+ 
474503    private  onToDeviceEvent  =  async  ( ev : MatrixEvent ) : Promise < void >  =>  { 
475504        await  this . client . decryptEventIfNeeded ( ev ) ; 
476505        if  ( ev . isDecryptionFailure ( ) )  return ; 
@@ -570,7 +599,7 @@ export class StopGapWidget extends EventEmitter {
570599                this . eventsToFeed . add ( ev ) ; 
571600            }  else  { 
572601                const  raw  =  ev . getEffectiveEvent ( ) ; 
573-                 this . messaging . feedEvent ( raw  as  IRoomEvent ,   this . eventListenerRoomId ! ) . catch ( ( e )  =>  { 
602+                 this . messaging . feedEvent ( raw  as  IRoomEvent ) . catch ( ( e )  =>  { 
574603                    logger . error ( "Error sending event to widget: " ,  e ) ; 
575604                } ) ; 
576605            } 
0 commit comments