@@ -24,6 +24,7 @@ import {
2424 ISendToDeviceToWidgetActionRequest ,
2525} from "matrix-widget-api" ;
2626
27+ import type { IEvent , IContent } from "./models/event" ;
2728import { ISendEventResponse } from "./@types/requests" ;
2829import { EventType } from "./@types/event" ;
2930import { logger } from "./logger" ;
@@ -69,27 +70,30 @@ export class RoomWidgetClient extends MatrixClient {
6970 super ( opts ) ;
7071
7172 // Request capabilities for the functionality this client needs to support
72- this . capabilities . sendState ?. forEach ( ( { eventType, stateKey } ) =>
73- this . widgetApi . requestCapabilityToSendState ( eventType , stateKey ) ,
73+ if ( capabilities . sendState ?. length || capabilities . receiveState ?. length ) {
74+ widgetApi . requestCapabilityForRoomTimeline ( roomId ) ;
75+ }
76+ capabilities . sendState ?. forEach ( ( { eventType, stateKey } ) =>
77+ widgetApi . requestCapabilityToSendState ( eventType , stateKey ) ,
7478 ) ;
75- this . capabilities . receiveState ?. forEach ( ( { eventType, stateKey } ) =>
76- this . widgetApi . requestCapabilityToReceiveState ( eventType , stateKey ) ,
79+ capabilities . receiveState ?. forEach ( ( { eventType, stateKey } ) =>
80+ widgetApi . requestCapabilityToReceiveState ( eventType , stateKey ) ,
7781 ) ;
78- this . capabilities . sendToDevice ?. forEach ( eventType =>
79- this . widgetApi . requestCapabilityToSendToDevice ( eventType ) ,
82+ capabilities . sendToDevice ?. forEach ( eventType =>
83+ widgetApi . requestCapabilityToSendToDevice ( eventType ) ,
8084 ) ;
81- this . capabilities . receiveToDevice ?. forEach ( eventType =>
82- this . widgetApi . requestCapabilityToReceiveToDevice ( eventType ) ,
85+ capabilities . receiveToDevice ?. forEach ( eventType =>
86+ widgetApi . requestCapabilityToReceiveToDevice ( eventType ) ,
8387 ) ;
84- if ( this . capabilities . turnServers ) {
85- this . widgetApi . requestCapability ( MatrixCapabilities . MSC3846TurnServers ) ;
88+ if ( capabilities . turnServers ) {
89+ widgetApi . requestCapability ( MatrixCapabilities . MSC3846TurnServers ) ;
8690 }
8791
88- this . widgetApi . on ( `action:${ WidgetApiToWidgetAction . SendEvent } ` , this . onEvent ) ;
89- this . widgetApi . on ( `action:${ WidgetApiToWidgetAction . SendToDevice } ` , this . onToDevice ) ;
92+ widgetApi . on ( `action:${ WidgetApiToWidgetAction . SendEvent } ` , this . onEvent ) ;
93+ widgetApi . on ( `action:${ WidgetApiToWidgetAction . SendToDevice } ` , this . onToDevice ) ;
9094
9195 // Open communication with the host
92- this . widgetApi . start ( ) ;
96+ widgetApi . start ( ) ;
9397 }
9498
9599 public async startClient ( opts : IStartClientOpts = { } ) : Promise < void > {
@@ -121,8 +125,8 @@ export class RoomWidgetClient extends MatrixClient {
121125 // so it doesn't really matter what order we inject them in
122126 await Promise . all (
123127 this . capabilities . receiveState ?. map ( async ( { eventType, stateKey } ) => {
124- const rawEvents = await this . widgetApi . readStateEvents ( eventType , undefined , stateKey ) ;
125- const events = rawEvents . map ( rawEvent => new MatrixEvent ( rawEvent ) ) ;
128+ const rawEvents = await this . widgetApi . readStateEvents ( eventType , undefined , stateKey , [ this . roomId ] ) ;
129+ const events = rawEvents . map ( rawEvent => new MatrixEvent ( rawEvent as Partial < IEvent > ) ) ;
126130
127131 await this . syncApi . injectRoomEvents ( this . room , [ ] , events ) ;
128132 events . forEach ( event => {
@@ -157,8 +161,7 @@ export class RoomWidgetClient extends MatrixClient {
157161 content : any ,
158162 stateKey = "" ,
159163 ) : Promise < ISendEventResponse > {
160- if ( roomId !== this . roomId ) throw new Error ( `Can't send events to ${ roomId } ` ) ;
161- return await this . widgetApi . sendStateEvent ( eventType , stateKey , content ) ;
164+ return await this . widgetApi . sendStateEvent ( eventType , stateKey , content , roomId ) ;
162165 }
163166
164167 public async sendToDevice (
@@ -215,11 +218,20 @@ export class RoomWidgetClient extends MatrixClient {
215218
216219 private onEvent = async ( ev : CustomEvent < ISendEventToWidgetActionRequest > ) => {
217220 ev . preventDefault ( ) ;
218- const event = new MatrixEvent ( ev . detail . data ) ;
219- await this . syncApi . injectRoomEvents ( this . room , [ ] , [ event ] ) ;
220- this . emit ( ClientEvent . Event , event ) ;
221- this . setSyncState ( SyncState . Syncing ) ;
222- logger . info ( `Received event ${ event . getId ( ) } ${ event . getType ( ) } ${ event . getStateKey ( ) } ` ) ;
221+
222+ // Verify the room ID matches, since it's possible for the client to
223+ // send us events from other rooms if this widget is always on screen
224+ if ( ev . detail . data . room_id === this . roomId ) {
225+ const event = new MatrixEvent ( ev . detail . data as Partial < IEvent > ) ;
226+ await this . syncApi . injectRoomEvents ( this . room , [ ] , [ event ] ) ;
227+ this . emit ( ClientEvent . Event , event ) ;
228+ this . setSyncState ( SyncState . Syncing ) ;
229+ logger . info ( `Received event ${ event . getId ( ) } ${ event . getType ( ) } ${ event . getStateKey ( ) } ` ) ;
230+ } else {
231+ const { event_id : eventId , room_id : roomId } = ev . detail . data ;
232+ logger . info ( `Received event ${ eventId } for a different room ${ roomId } ; discarding` ) ;
233+ }
234+
223235 await this . ack ( ev ) ;
224236 } ;
225237
@@ -229,7 +241,7 @@ export class RoomWidgetClient extends MatrixClient {
229241 const event = new MatrixEvent ( {
230242 type : ev . detail . data . type ,
231243 sender : ev . detail . data . sender ,
232- content : ev . detail . data . content ,
244+ content : ev . detail . data . content as IContent ,
233245 } ) ;
234246 // Mark the event as encrypted if it was, using fake contents and keys since those are unknown to us
235247 if ( ev . detail . data . encrypted ) event . makeEncrypted ( EventType . RoomMessageEncrypted , { } , "" , "" ) ;
0 commit comments