-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathWidgetApi.ts
805 lines (744 loc) · 34.6 KB
/
WidgetApi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
* Copyright 2020 - 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EventEmitter } from "events";
import { Capability } from "./interfaces/Capabilities";
import { IWidgetApiRequest, IWidgetApiRequestEmptyData } from "./interfaces/IWidgetApiRequest";
import { IWidgetApiAcknowledgeResponseData } from "./interfaces/IWidgetApiResponse";
import { WidgetApiDirection } from "./interfaces/WidgetApiDirection";
import {
ISupportedVersionsActionRequest,
ISupportedVersionsActionResponseData,
} from "./interfaces/SupportedVersionsAction";
import { ApiVersion, CurrentApiVersions, UnstableApiVersion } from "./interfaces/ApiVersion";
import {
ICapabilitiesActionRequest,
ICapabilitiesActionResponseData,
INotifyCapabilitiesActionRequest,
IRenegotiateCapabilitiesRequestData,
} from "./interfaces/CapabilitiesAction";
import { ITransport } from "./transport/ITransport";
import { PostmessageTransport } from "./transport/PostmessageTransport";
import { WidgetApiFromWidgetAction, WidgetApiToWidgetAction } from "./interfaces/WidgetApiAction";
import { IWidgetApiErrorResponseData } from "./interfaces/IWidgetApiErrorResponse";
import { IStickerActionRequestData } from "./interfaces/StickerAction";
import { IStickyActionRequestData, IStickyActionResponseData } from "./interfaces/StickyAction";
import {
IGetOpenIDActionRequestData,
IGetOpenIDActionResponse,
IOpenIDCredentials,
OpenIDRequestState,
} from "./interfaces/GetOpenIDAction";
import { IOpenIDCredentialsActionRequest } from "./interfaces/OpenIDCredentialsAction";
import { MatrixWidgetType, WidgetType } from "./interfaces/WidgetType";
import {
BuiltInModalButtonID,
IModalWidgetCreateData,
IModalWidgetOpenRequestData,
IModalWidgetOpenRequestDataButton,
IModalWidgetReturnData,
ModalButtonID,
} from "./interfaces/ModalWidgetActions";
import { ISetModalButtonEnabledActionRequestData } from "./interfaces/SetModalButtonEnabledAction";
import { ISendEventFromWidgetRequestData, ISendEventFromWidgetResponseData } from "./interfaces/SendEventAction";
import {
ISendToDeviceFromWidgetRequestData,
ISendToDeviceFromWidgetResponseData,
} from "./interfaces/SendToDeviceAction";
import { EventDirection, WidgetEventCapability } from "./models/WidgetEventCapability";
import { INavigateActionRequestData } from "./interfaces/NavigateAction";
import { IReadEventFromWidgetRequestData, IReadEventFromWidgetResponseData } from "./interfaces/ReadEventAction";
import {
IReadRoomAccountDataFromWidgetRequestData,
IReadRoomAccountDataFromWidgetResponseData,
} from "./interfaces/ReadRoomAccountDataAction";
import { IRoomEvent } from "./interfaces/IRoomEvent";
import {IRoomAccountData} from "./interfaces/IRoomAccountData";
import { ITurnServer, IUpdateTurnServersRequest } from "./interfaces/TurnServerActions";
import { Symbols } from "./Symbols";
import {
IReadRelationsFromWidgetRequestData,
IReadRelationsFromWidgetResponseData,
} from "./interfaces/ReadRelationsAction";
import {
IUserDirectorySearchFromWidgetRequestData,
IUserDirectorySearchFromWidgetResponseData,
} from "./interfaces/UserDirectorySearchAction";
import {
IGetMediaConfigActionFromWidgetRequestData,
IGetMediaConfigActionFromWidgetResponseData,
} from "./interfaces/GetMediaConfigAction";
import {
IUploadFileActionFromWidgetRequestData,
IUploadFileActionFromWidgetResponseData,
} from "./interfaces/UploadFileAction";
/**
* API handler for widgets. This raises events for each action
* received as `action:${action}` (eg: "action:screenshot").
* Default handling can be prevented by using preventDefault()
* on the raised event. The default handling varies for each
* action: ones which the SDK can handle safely are acknowledged
* appropriately and ones which are unhandled (custom or require
* the widget to do something) are rejected with an error.
*
* Events which are preventDefault()ed must reply using the
* transport. The events raised will have a detail of an
* IWidgetApiRequest interface.
*
* When the WidgetApi is ready to start sending requests, it will
* raise a "ready" CustomEvent. After the ready event fires, actions
* can be sent and the transport will be ready.
*/
export class WidgetApi extends EventEmitter {
public readonly transport: ITransport;
private capabilitiesFinished = false;
private supportsMSC2974Renegotiate = false;
private requestedCapabilities: Capability[] = [];
private approvedCapabilities?: Capability[];
private cachedClientVersions?: ApiVersion[];
private turnServerWatchers = 0;
/**
* Creates a new API handler for the given widget.
* @param {string} widgetId The widget ID to listen for. If not supplied then
* the API will use the widget ID from the first valid request it receives.
* @param {string} clientOrigin The origin of the client, or null if not known.
*/
public constructor(widgetId: string | null = null, private clientOrigin: string | null = null) {
super();
if (!window.parent) {
throw new Error("No parent window. This widget doesn't appear to be embedded properly.");
}
this.transport = new PostmessageTransport(
WidgetApiDirection.FromWidget,
widgetId,
window.parent,
window,
);
this.transport.targetOrigin = clientOrigin;
this.transport.on("message", this.handleMessage.bind(this));
}
/**
* Determines if the widget was granted a particular capability. Note that on
* clients where the capabilities are not fed back to the widget this function
* will rely on requested capabilities instead.
* @param {Capability} capability The capability to check for approval of.
* @returns {boolean} True if the widget has approval for the given capability.
*/
public hasCapability(capability: Capability): boolean {
if (Array.isArray(this.approvedCapabilities)) {
return this.approvedCapabilities.includes(capability);
}
return this.requestedCapabilities.includes(capability);
}
/**
* Request a capability from the client. It is not guaranteed to be allowed,
* but will be asked for.
* @param {Capability} capability The capability to request.
* @throws Throws if the capabilities negotiation has already started and the
* widget is unable to request additional capabilities.
*/
public requestCapability(capability: Capability) {
if (this.capabilitiesFinished && !this.supportsMSC2974Renegotiate) {
throw new Error("Capabilities have already been negotiated");
}
this.requestedCapabilities.push(capability);
}
/**
* Request capabilities from the client. They are not guaranteed to be allowed,
* but will be asked for if the negotiation has not already happened.
* @param {Capability[]} capabilities The capabilities to request.
* @throws Throws if the capabilities negotiation has already started.
*/
public requestCapabilities(capabilities: Capability[]) {
capabilities.forEach(cap => this.requestCapability(cap));
}
/**
* Requests the capability to interact with rooms other than the user's currently
* viewed room. Applies to event receiving and sending.
* @param {string | Symbols.AnyRoom} roomId The room ID, or `Symbols.AnyRoom` to
* denote all known rooms.
*/
public requestCapabilityForRoomTimeline(roomId: string | Symbols.AnyRoom) {
this.requestCapability(`org.matrix.msc2762.timeline:${roomId}`);
}
/**
* Requests the capability to send a given state event with optional explicit
* state key. It is not guaranteed to be allowed, but will be asked for if the
* negotiation has not already happened.
* @param {string} eventType The state event type to ask for.
* @param {string} stateKey If specified, the specific state key to request.
* Otherwise all state keys will be requested.
*/
public requestCapabilityToSendState(eventType: string, stateKey?: string) {
this.requestCapability(WidgetEventCapability.forStateEvent(EventDirection.Send, eventType, stateKey).raw);
}
/**
* Requests the capability to receive a given state event with optional explicit
* state key. It is not guaranteed to be allowed, but will be asked for if the
* negotiation has not already happened.
* @param {string} eventType The state event type to ask for.
* @param {string} stateKey If specified, the specific state key to request.
* Otherwise all state keys will be requested.
*/
public requestCapabilityToReceiveState(eventType: string, stateKey?: string) {
this.requestCapability(WidgetEventCapability.forStateEvent(EventDirection.Receive, eventType, stateKey).raw);
}
/**
* Requests the capability to send a given to-device event. It is not
* guaranteed to be allowed, but will be asked for if the negotiation has
* not already happened.
* @param {string} eventType The room event type to ask for.
*/
public requestCapabilityToSendToDevice(eventType: string) {
this.requestCapability(WidgetEventCapability.forToDeviceEvent(EventDirection.Send, eventType).raw);
}
/**
* Requests the capability to receive a given to-device event. It is not
* guaranteed to be allowed, but will be asked for if the negotiation has
* not already happened.
* @param {string} eventType The room event type to ask for.
*/
public requestCapabilityToReceiveToDevice(eventType: string) {
this.requestCapability(WidgetEventCapability.forToDeviceEvent(EventDirection.Receive, eventType).raw);
}
/**
* Requests the capability to send a given room event. It is not guaranteed to be
* allowed, but will be asked for if the negotiation has not already happened.
* @param {string} eventType The room event type to ask for.
*/
public requestCapabilityToSendEvent(eventType: string) {
this.requestCapability(WidgetEventCapability.forRoomEvent(EventDirection.Send, eventType).raw);
}
/**
* Requests the capability to receive a given room event. It is not guaranteed to be
* allowed, but will be asked for if the negotiation has not already happened.
* @param {string} eventType The room event type to ask for.
*/
public requestCapabilityToReceiveEvent(eventType: string) {
this.requestCapability(WidgetEventCapability.forRoomEvent(EventDirection.Receive, eventType).raw);
}
/**
* Requests the capability to send a given message event with optional explicit
* `msgtype`. It is not guaranteed to be allowed, but will be asked for if the
* negotiation has not already happened.
* @param {string} msgtype If specified, the specific msgtype to request.
* Otherwise all message types will be requested.
*/
public requestCapabilityToSendMessage(msgtype?: string) {
this.requestCapability(WidgetEventCapability.forRoomMessageEvent(EventDirection.Send, msgtype).raw);
}
/**
* Requests the capability to receive a given message event with optional explicit
* `msgtype`. It is not guaranteed to be allowed, but will be asked for if the
* negotiation has not already happened.
* @param {string} msgtype If specified, the specific msgtype to request.
* Otherwise all message types will be requested.
*/
public requestCapabilityToReceiveMessage(msgtype?: string) {
this.requestCapability(WidgetEventCapability.forRoomMessageEvent(EventDirection.Receive, msgtype).raw);
}
/**
* Requests the capability to receive a given item in room account data. It is not guaranteed to be
* allowed, but will be asked for if the negotiation has not already happened.
* @param {string} eventType The state event type to ask for.
*/
public requestCapabilityToReceiveRoomAccountData(eventType: string) {
this.requestCapability(WidgetEventCapability.forRoomAccountData(EventDirection.Receive, eventType).raw);
}
/**
* Requests an OpenID Connect token from the client for the currently logged in
* user. This token can be validated server-side with the federation API. Note
* that the widget is responsible for validating the token and caching any results
* it needs.
* @returns {Promise<IOpenIDCredentials>} Resolves to a token for verification.
* @throws Throws if the user rejected the request or the request failed.
*/
public requestOpenIDConnectToken(): Promise<IOpenIDCredentials> {
return new Promise<IOpenIDCredentials>((resolve, reject) => {
this.transport.sendComplete<IGetOpenIDActionRequestData, IGetOpenIDActionResponse>(
WidgetApiFromWidgetAction.GetOpenIDCredentials, {},
).then(response => {
const rdata = response.response;
if (rdata.state === OpenIDRequestState.Allowed) {
resolve(rdata);
} else if (rdata.state === OpenIDRequestState.Blocked) {
reject(new Error("User declined to verify their identity"));
} else if (rdata.state === OpenIDRequestState.PendingUserConfirmation) {
const handlerFn = (ev: CustomEvent<IOpenIDCredentialsActionRequest>) => {
ev.preventDefault();
const request = ev.detail;
if (request.data.original_request_id !== response.requestId) return;
if (request.data.state === OpenIDRequestState.Allowed) {
resolve(request.data);
this.transport.reply(request, <IWidgetApiRequestEmptyData>{}); // ack
} else if (request.data.state === OpenIDRequestState.Blocked) {
reject(new Error("User declined to verify their identity"));
this.transport.reply(request, <IWidgetApiRequestEmptyData>{}); // ack
} else {
reject(new Error("Invalid state on reply: " + rdata.state));
this.transport.reply(request, <IWidgetApiErrorResponseData>{
error: {
message: "Invalid state",
},
});
}
this.off(`action:${WidgetApiToWidgetAction.OpenIDCredentials}`, handlerFn);
};
this.on(`action:${WidgetApiToWidgetAction.OpenIDCredentials}`, handlerFn);
} else {
reject(new Error("Invalid state: " + rdata.state));
}
}).catch(reject);
});
}
/**
* Asks the client for additional capabilities. Capabilities can be queued for this
* request with the requestCapability() functions.
* @returns {Promise<void>} Resolves when complete. Note that the promise resolves when
* the capabilities request has gone through, not when the capabilities are approved/denied.
* Use the WidgetApiToWidgetAction.NotifyCapabilities action to detect changes.
*/
public updateRequestedCapabilities(): Promise<void> {
return this.transport.send(WidgetApiFromWidgetAction.MSC2974RenegotiateCapabilities,
<IRenegotiateCapabilitiesRequestData>{
capabilities: this.requestedCapabilities,
}).then();
}
/**
* Tell the client that the content has been loaded.
* @returns {Promise} Resolves when the client acknowledges the request.
*/
public sendContentLoaded(): Promise<void> {
return this.transport.send(WidgetApiFromWidgetAction.ContentLoaded, <IWidgetApiRequestEmptyData>{}).then();
}
/**
* Sends a sticker to the client.
* @param {IStickerActionRequestData} sticker The sticker to send.
* @returns {Promise} Resolves when the client acknowledges the request.
*/
public sendSticker(sticker: IStickerActionRequestData): Promise<void> {
return this.transport.send(WidgetApiFromWidgetAction.SendSticker, sticker).then();
}
/**
* Asks the client to set the always-on-screen status for this widget.
* @param {boolean} value The new state to request.
* @returns {Promise<boolean>} Resolve with true if the client was able to fulfill
* the request, resolves to false otherwise. Rejects if an error occurred.
*/
public setAlwaysOnScreen(value: boolean): Promise<boolean> {
return this.transport.send<IStickyActionRequestData, IStickyActionResponseData>(
WidgetApiFromWidgetAction.UpdateAlwaysOnScreen, {value},
).then(res => res.success);
}
/**
* Opens a modal widget.
* @param {string} url The URL to the modal widget.
* @param {string} name The name of the widget.
* @param {IModalWidgetOpenRequestDataButton[]} buttons The buttons to have on the widget.
* @param {IModalWidgetCreateData} data Data to supply to the modal widget.
* @param {WidgetType} type The type of modal widget.
* @returns {Promise<void>} Resolves when the modal widget has been opened.
*/
public openModalWidget(
url: string,
name: string,
buttons: IModalWidgetOpenRequestDataButton[] = [],
data: IModalWidgetCreateData = {},
type: WidgetType = MatrixWidgetType.Custom,
): Promise<void> {
return this.transport.send<IModalWidgetOpenRequestData>(
WidgetApiFromWidgetAction.OpenModalWidget, { type, url, name, buttons, data },
).then();
}
/**
* Closes the modal widget. The widget's session will be terminated shortly after.
* @param {IModalWidgetReturnData} data Optional data to close the modal widget with.
* @returns {Promise<void>} Resolves when complete.
*/
public closeModalWidget(data: IModalWidgetReturnData = {}): Promise<void> {
return this.transport.send<IModalWidgetReturnData>(WidgetApiFromWidgetAction.CloseModalWidget, data).then();
}
public sendRoomEvent(
eventType: string,
content: unknown,
roomId?: string,
): Promise<ISendEventFromWidgetResponseData> {
return this.transport.send<ISendEventFromWidgetRequestData, ISendEventFromWidgetResponseData>(
WidgetApiFromWidgetAction.SendEvent,
{type: eventType, content, room_id: roomId},
);
}
public sendStateEvent(
eventType: string,
stateKey: string,
content: unknown,
roomId?: string,
): Promise<ISendEventFromWidgetResponseData> {
return this.transport.send<ISendEventFromWidgetRequestData, ISendEventFromWidgetResponseData>(
WidgetApiFromWidgetAction.SendEvent,
{type: eventType, content, state_key: stateKey, room_id: roomId},
);
}
/**
* Sends a to-device event.
* @param {string} eventType The type of events being sent.
* @param {boolean} encrypted Whether to encrypt the message contents.
* @param {Object} contentMap A map from user IDs to device IDs to message contents.
* @returns {Promise<ISendToDeviceFromWidgetResponseData>} Resolves when complete.
*/
public sendToDevice(
eventType: string,
encrypted: boolean,
contentMap: { [userId: string]: { [deviceId: string]: object } },
): Promise<ISendToDeviceFromWidgetResponseData> {
return this.transport.send<ISendToDeviceFromWidgetRequestData, ISendToDeviceFromWidgetResponseData>(
WidgetApiFromWidgetAction.SendToDevice,
{type: eventType, encrypted, messages: contentMap},
);
}
public readRoomAccountData(
eventType: string,
roomIds?: (string | Symbols.AnyRoom)[],
): Promise<IRoomAccountData[]> {
const data: IReadEventFromWidgetRequestData = {type: eventType};
if (roomIds) {
if (roomIds.includes(Symbols.AnyRoom)) {
data.room_ids = Symbols.AnyRoom;
} else {
data.room_ids = roomIds;
}
}
return this.transport.send<
IReadRoomAccountDataFromWidgetRequestData,
IReadRoomAccountDataFromWidgetResponseData
>(
WidgetApiFromWidgetAction.BeeperReadRoomAccountData,
data,
).then(r => r.events);
}
public readRoomEvents(
eventType: string,
limit?: number,
msgtype?: string,
roomIds?: (string | Symbols.AnyRoom)[],
since?: string | undefined,
): Promise<IRoomEvent[]> {
const data: IReadEventFromWidgetRequestData = {type: eventType, msgtype: msgtype};
if (limit !== undefined) {
data.limit = limit;
}
if (roomIds) {
if (roomIds.includes(Symbols.AnyRoom)) {
data.room_ids = Symbols.AnyRoom;
} else {
data.room_ids = roomIds;
}
}
if (since) {
data.since = since;
}
return this.transport.send<IReadEventFromWidgetRequestData, IReadEventFromWidgetResponseData>(
WidgetApiFromWidgetAction.MSC2876ReadEvents,
data,
).then(r => r.events);
}
/**
* Reads all related events given a known eventId.
* @param eventId The id of the parent event to be read.
* @param roomId The room to look within. When undefined, the user's currently
* viewed room.
* @param relationType The relationship type of child events to search for.
* When undefined, all relations are returned.
* @param eventType The event type of child events to search for. When undefined,
* all related events are returned.
* @param limit The maximum number of events to retrieve per room. If not
* supplied, the server will apply a default limit.
* @param from The pagination token to start returning results from, as
* received from a previous call. If not supplied, results start at the most
* recent topological event known to the server.
* @param to The pagination token to stop returning results at. If not
* supplied, results continue up to limit or until there are no more events.
* @param direction The direction to search for according to MSC3715.
* @returns Resolves to the room relations.
*/
public async readEventRelations(
eventId: string,
roomId?: string,
relationType?: string,
eventType?: string,
limit?: number,
from?: string,
to?: string,
direction?: 'f' | 'b',
): Promise<IReadRelationsFromWidgetResponseData> {
const versions = await this.getClientVersions();
if (!versions.includes(UnstableApiVersion.MSC3869)) {
throw new Error("The read_relations action is not supported by the client.")
}
const data: IReadRelationsFromWidgetRequestData = {
event_id: eventId,
rel_type: relationType,
event_type: eventType,
room_id: roomId,
to,
from,
limit,
direction,
};
return this.transport.send<IReadRelationsFromWidgetRequestData, IReadRelationsFromWidgetResponseData>(
WidgetApiFromWidgetAction.MSC3869ReadRelations,
data,
)
}
public readStateEvents(
eventType: string,
limit?: number,
stateKey?: string,
roomIds?: (string | Symbols.AnyRoom)[],
): Promise<IRoomEvent[]> {
const data: IReadEventFromWidgetRequestData = {
type: eventType,
state_key: stateKey === undefined ? true : stateKey,
};
if (limit !== undefined) {
data.limit = limit;
}
if (roomIds) {
if (roomIds.includes(Symbols.AnyRoom)) {
data.room_ids = Symbols.AnyRoom;
} else {
data.room_ids = roomIds;
}
}
return this.transport.send<IReadEventFromWidgetRequestData, IReadEventFromWidgetResponseData>(
WidgetApiFromWidgetAction.MSC2876ReadEvents,
data,
).then(r => r.events);
}
/**
* Sets a button as disabled or enabled on the modal widget. Buttons are enabled by default.
* @param {ModalButtonID} buttonId The button ID to enable/disable.
* @param {boolean} isEnabled Whether or not the button is enabled.
* @returns {Promise<void>} Resolves when complete.
* @throws Throws if the button cannot be disabled, or the client refuses to disable the button.
*/
public setModalButtonEnabled(buttonId: ModalButtonID, isEnabled: boolean): Promise<void> {
if (buttonId === BuiltInModalButtonID.Close) {
throw new Error("The close button cannot be disabled");
}
return this.transport.send<ISetModalButtonEnabledActionRequestData>(
WidgetApiFromWidgetAction.SetModalButtonEnabled, {button: buttonId, enabled: isEnabled},
).then();
}
/**
* Attempts to navigate the client to the given URI. This can only be called with Matrix URIs
* (currently only matrix.to, but in future a Matrix URI scheme will be defined).
* @param {string} uri The URI to navigate to.
* @returns {Promise<void>} Resolves when complete.
* @throws Throws if the URI is invalid or cannot be processed.
* @deprecated This currently relies on an unstable MSC (MSC2931).
*/
public navigateTo(uri: string): Promise<void> {
if (!uri || !uri.startsWith("https://matrix.to/#")) {
throw new Error("Invalid matrix.to URI");
}
return this.transport.send<INavigateActionRequestData>(
WidgetApiFromWidgetAction.MSC2931Navigate, {uri},
).then();
}
/**
* Starts watching for TURN servers, yielding an initial set of credentials as soon as possible,
* and thereafter yielding new credentials whenever the previous ones expire.
* @yields {ITurnServer} The TURN server URIs and credentials currently available to the widget.
*/
public async* getTurnServers(): AsyncGenerator<ITurnServer> {
let setTurnServer: (server: ITurnServer) => void;
const onUpdateTurnServers = async (ev: CustomEvent<IUpdateTurnServersRequest>) => {
ev.preventDefault();
setTurnServer(ev.detail.data);
await this.transport.reply<IWidgetApiAcknowledgeResponseData>(ev.detail, {});
};
// Start listening for updates before we even start watching, to catch
// TURN data that is sent immediately
this.on(`action:${WidgetApiToWidgetAction.UpdateTurnServers}`, onUpdateTurnServers);
// Only send the 'watch' action if we aren't already watching
if (this.turnServerWatchers === 0) {
try {
await this.transport.send<IWidgetApiRequestEmptyData>(WidgetApiFromWidgetAction.WatchTurnServers, {});
} catch (e) {
this.off(`action:${WidgetApiToWidgetAction.UpdateTurnServers}`, onUpdateTurnServers);
throw e;
}
}
this.turnServerWatchers++;
try {
// Watch for new data indefinitely (until this generator's return method is called)
while (true) {
yield await new Promise<ITurnServer>(resolve => setTurnServer = resolve);
}
} finally {
// The loop was broken by the caller - clean up
this.off(`action:${WidgetApiToWidgetAction.UpdateTurnServers}`, onUpdateTurnServers);
// Since sending the 'unwatch' action will end updates for all other
// consumers, only send it if we're the only consumer remaining
this.turnServerWatchers--;
if (this.turnServerWatchers === 0) {
await this.transport.send<IWidgetApiRequestEmptyData>(WidgetApiFromWidgetAction.UnwatchTurnServers, {});
}
}
}
/**
* Search for users in the user directory.
* @param searchTerm The term to search for.
* @param limit The maximum number of results to return. If not supplied, the
* @returns Resolves to the search results.
*/
public async searchUserDirectory(
searchTerm: string,
limit?: number,
): Promise<IUserDirectorySearchFromWidgetResponseData> {
const versions = await this.getClientVersions();
if (!versions.includes(UnstableApiVersion.MSC3973)) {
throw new Error("The user_directory_search action is not supported by the client.")
}
const data: IUserDirectorySearchFromWidgetRequestData = {
search_term: searchTerm,
limit,
};
return this.transport.send<
IUserDirectorySearchFromWidgetRequestData,
IUserDirectorySearchFromWidgetResponseData
>(WidgetApiFromWidgetAction.MSC3973UserDirectorySearch, data);
}
/**
* Get the config for the media repository.
* @returns Promise which resolves with an object containing the config.
*/
public async getMediaConfig(): Promise<IGetMediaConfigActionFromWidgetResponseData> {
const versions = await this.getClientVersions();
if (!versions.includes(UnstableApiVersion.MSC4039)) {
throw new Error("The get_media_config action is not supported by the client.")
}
const data: IGetMediaConfigActionFromWidgetRequestData = {};
return this.transport.send<
IGetMediaConfigActionFromWidgetRequestData,
IGetMediaConfigActionFromWidgetResponseData
>(WidgetApiFromWidgetAction.MSC4039GetMediaConfigAction, data);
}
/**
* Upload a file to the media repository on the homeserver.
* @param file - The object to upload. Something that can be sent to
* XMLHttpRequest.send (typically a File).
* @returns Resolves to the location of the uploaded file.
*/
public async uploadFile(file: XMLHttpRequestBodyInit): Promise<IUploadFileActionFromWidgetResponseData> {
const versions = await this.getClientVersions();
if (!versions.includes(UnstableApiVersion.MSC4039)) {
throw new Error("The upload_file action is not supported by the client.")
}
const data: IUploadFileActionFromWidgetRequestData = {
file,
};
return this.transport.send<
IUploadFileActionFromWidgetRequestData,
IUploadFileActionFromWidgetResponseData
>(WidgetApiFromWidgetAction.MSC4039UploadFileAction, data);
}
/**
* Starts the communication channel. This should be done early to ensure
* that messages are not missed. Communication can only be stopped by the client.
*/
public start() {
this.transport.start();
this.getClientVersions().then(v => {
if (v.includes(UnstableApiVersion.MSC2974)) {
this.supportsMSC2974Renegotiate = true;
}
});
}
private handleMessage(ev: CustomEvent<IWidgetApiRequest>) {
const actionEv = new CustomEvent(`action:${ev.detail.action}`, {
detail: ev.detail,
cancelable: true,
});
this.emit(`action:${ev.detail.action}`, actionEv);
if (!actionEv.defaultPrevented) {
switch (ev.detail.action) {
case WidgetApiToWidgetAction.SupportedApiVersions:
return this.replyVersions(<ISupportedVersionsActionRequest>ev.detail);
case WidgetApiToWidgetAction.Capabilities:
return this.handleCapabilities(<ICapabilitiesActionRequest>ev.detail);
case WidgetApiToWidgetAction.UpdateVisibility:
return this.transport.reply(ev.detail, <IWidgetApiRequestEmptyData>{}); // ack to avoid error spam
case WidgetApiToWidgetAction.NotifyCapabilities:
return this.transport.reply(ev.detail, <IWidgetApiRequestEmptyData>{}); // ack to avoid error spam
default:
return this.transport.reply(ev.detail, <IWidgetApiErrorResponseData>{
error: {
message: "Unknown or unsupported action: " + ev.detail.action,
},
});
}
}
}
private replyVersions(request: ISupportedVersionsActionRequest) {
this.transport.reply<ISupportedVersionsActionResponseData>(request, {
supported_versions: CurrentApiVersions,
});
}
public getClientVersions(): Promise<ApiVersion[]> {
if (Array.isArray(this.cachedClientVersions)) {
return Promise.resolve(this.cachedClientVersions);
}
return this.transport.send<IWidgetApiRequestEmptyData, ISupportedVersionsActionResponseData>(
WidgetApiFromWidgetAction.SupportedApiVersions, {},
).then(r => {
this.cachedClientVersions = r.supported_versions;
return r.supported_versions;
}).catch(e => {
console.warn("non-fatal error getting supported client versions: ", e);
return [];
});
}
private handleCapabilities(request: ICapabilitiesActionRequest) {
if (this.capabilitiesFinished) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: {
message: "Capability negotiation already completed",
},
});
}
// See if we can expect a capabilities notification or not
return this.getClientVersions().then(v => {
if (v.includes(UnstableApiVersion.MSC2871)) {
this.once(
`action:${WidgetApiToWidgetAction.NotifyCapabilities}`,
(ev: CustomEvent<INotifyCapabilitiesActionRequest>) => {
this.approvedCapabilities = ev.detail.data.approved;
this.emit("ready");
},
);
} else {
// if we can't expect notification, we're as done as we can be
this.emit("ready");
}
// in either case, reply to that capabilities request
this.capabilitiesFinished = true;
return this.transport.reply<ICapabilitiesActionResponseData>(request, {
capabilities: this.requestedCapabilities,
});
});
}
}