@matrix-widget-toolkit/api 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1043,12 +1043,28 @@ var WidgetApiImpl = /** @class */ (function () {
1043
1043
  /** {@inheritDoc WidgetApi.widgetParameters} */
1044
1044
  widgetParameters, _a) {
1045
1045
  var _b = _a === void 0 ? {} : _a, _c = _b.capabilities, capabilities = _c === void 0 ? [] : _c, _d = _b.supportStandalone, supportStandalone = _d === void 0 ? false : _d;
1046
+ var _this = this;
1046
1047
  this.matrixWidgetApi = matrixWidgetApi$1;
1047
1048
  this.widgetId = widgetId;
1048
1049
  this.widgetParameters = widgetParameters;
1049
- var eventName = "action:".concat(matrixWidgetApi.WidgetApiToWidgetAction.SendEvent);
1050
- this.events$ = rxjs.fromEvent(this.matrixWidgetApi, eventName, function (event) {
1050
+ this.events$ = rxjs.fromEvent(this.matrixWidgetApi, "action:".concat(matrixWidgetApi.WidgetApiToWidgetAction.SendEvent), function (event) {
1051
1051
  event.preventDefault();
1052
+ try {
1053
+ _this.matrixWidgetApi.transport.reply(event.detail, {});
1054
+ }
1055
+ catch (_) {
1056
+ // Ignore errors while replying
1057
+ }
1058
+ return event;
1059
+ }).pipe(rxjs.share());
1060
+ this.toDeviceMessages$ = rxjs.fromEvent(this.matrixWidgetApi, 'action:send_to_device', function (event) {
1061
+ event.preventDefault();
1062
+ try {
1063
+ matrixWidgetApi$1.transport.reply(event.detail, {});
1064
+ }
1065
+ catch (_) {
1066
+ // Ignore errors while replying
1067
+ }
1052
1068
  return event;
1053
1069
  }).pipe(rxjs.share());
1054
1070
  this.initialCapabilities = __spreadArray(__spreadArray([], capabilities, true), (supportStandalone ? [] : [matrixWidgetApi.MatrixCapabilities.RequiresClient]), true);
@@ -1294,7 +1310,6 @@ var WidgetApiImpl = /** @class */ (function () {
1294
1310
  };
1295
1311
  /** {@inheritDoc WidgetApi.observeStateEvents} */
1296
1312
  WidgetApiImpl.prototype.observeStateEvents = function (eventType, _a) {
1297
- var _this = this;
1298
1313
  var _b = _a === void 0 ? {} : _a, stateKey = _b.stateKey, roomIds = _b.roomIds;
1299
1314
  var currentRoomId = this.widgetParameters.roomId;
1300
1315
  if (!currentRoomId) {
@@ -1307,7 +1322,6 @@ var WidgetApiImpl = /** @class */ (function () {
1307
1322
  matrixEvent.state_key !== undefined &&
1308
1323
  (stateKey === undefined || matrixEvent.state_key === stateKey) &&
1309
1324
  isInRoom(matrixEvent, currentRoomId, roomIds)) {
1310
- _this.matrixWidgetApi.transport.reply(event.detail, {});
1311
1325
  return event.detail.data;
1312
1326
  }
1313
1327
  return undefined;
@@ -1318,45 +1332,31 @@ var WidgetApiImpl = /** @class */ (function () {
1318
1332
  WidgetApiImpl.prototype.sendStateEvent = function (eventType, content, _a) {
1319
1333
  var _b = _a === void 0 ? {} : _a, roomId = _b.roomId, _c = _b.stateKey, stateKey = _c === void 0 ? '' : _c;
1320
1334
  return __awaiter(this, void 0, void 0, function () {
1321
- var firstEvent$;
1322
- var _this = this;
1323
- return __generator(this, function (_d) {
1324
- firstEvent$ = this.events$.pipe(rxjs.map(function (event) {
1325
- var matrixEvent = event.detail.data;
1326
- if (matrixEvent.sender === _this.widgetParameters.userId &&
1327
- matrixEvent.state_key !== undefined &&
1328
- matrixEvent.type === eventType &&
1329
- (!roomId || matrixEvent.room_id === roomId)) {
1330
- _this.matrixWidgetApi.transport.reply(event.detail, {});
1331
- return event.detail.data;
1332
- }
1333
- return undefined;
1334
- }), rxjs.filter(isDefined), rxjs.first());
1335
- return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
1336
- var subscription, err_2;
1337
- return __generator(this, function (_a) {
1338
- switch (_a.label) {
1339
- case 0:
1340
- subscription = firstEvent$.subscribe({
1341
- next: function (event) { return resolve(event); },
1342
- error: function (err) { return reject(err); },
1343
- });
1344
- _a.label = 1;
1345
- case 1:
1346
- _a.trys.push([1, 3, , 4]);
1347
- return [4 /*yield*/, this.matrixWidgetApi.sendStateEvent(eventType, stateKey, content, roomId)];
1348
- case 2:
1349
- _a.sent();
1350
- return [3 /*break*/, 4];
1351
- case 3:
1352
- err_2 = _a.sent();
1353
- subscription.unsubscribe();
1354
- reject(err_2);
1355
- return [3 /*break*/, 4];
1356
- case 4: return [2 /*return*/];
1357
- }
1358
- });
1359
- }); })];
1335
+ var subject, subscription, _d, event_id_1, room_id_1, event_1;
1336
+ return __generator(this, function (_e) {
1337
+ switch (_e.label) {
1338
+ case 0:
1339
+ subject = new rxjs.ReplaySubject();
1340
+ subscription = this.events$.subscribe(function (e) { return subject.next(e); });
1341
+ _e.label = 1;
1342
+ case 1:
1343
+ _e.trys.push([1, , 4, 5]);
1344
+ return [4 /*yield*/, this.matrixWidgetApi.sendStateEvent(eventType, stateKey, content, roomId)];
1345
+ case 2:
1346
+ _d = _e.sent(), event_id_1 = _d.event_id, room_id_1 = _d.room_id;
1347
+ return [4 /*yield*/, rxjs.firstValueFrom(subject.pipe(rxjs.filter(function (event) {
1348
+ var matrixEvent = event.detail.data;
1349
+ return (matrixEvent.event_id === event_id_1 &&
1350
+ matrixEvent.room_id === room_id_1);
1351
+ }), rxjs.map(function (event) { return event.detail.data; })))];
1352
+ case 3:
1353
+ event_1 = _e.sent();
1354
+ return [2 /*return*/, event_1];
1355
+ case 4:
1356
+ subscription.unsubscribe();
1357
+ return [7 /*endfinally*/];
1358
+ case 5: return [2 /*return*/];
1359
+ }
1360
1360
  });
1361
1361
  });
1362
1362
  };
@@ -1374,7 +1374,6 @@ var WidgetApiImpl = /** @class */ (function () {
1374
1374
  };
1375
1375
  /** {@inheritDoc WidgetApi.observeRoomEvents} */
1376
1376
  WidgetApiImpl.prototype.observeRoomEvents = function (eventType, _a) {
1377
- var _this = this;
1378
1377
  var _b = _a === void 0 ? {} : _a, messageType = _b.messageType, roomIds = _b.roomIds;
1379
1378
  var currentRoomId = this.widgetParameters.roomId;
1380
1379
  if (!currentRoomId) {
@@ -1387,7 +1386,6 @@ var WidgetApiImpl = /** @class */ (function () {
1387
1386
  matrixEvent.state_key === undefined &&
1388
1387
  (!messageType || matrixEvent.content.msgtype === messageType) &&
1389
1388
  isInRoom(matrixEvent, currentRoomId, roomIds)) {
1390
- _this.matrixWidgetApi.transport.reply(event.detail, {});
1391
1389
  return event.detail.data;
1392
1390
  }
1393
1391
  return undefined;
@@ -1398,45 +1396,31 @@ var WidgetApiImpl = /** @class */ (function () {
1398
1396
  WidgetApiImpl.prototype.sendRoomEvent = function (eventType, content, _a) {
1399
1397
  var _b = _a === void 0 ? {} : _a, roomId = _b.roomId;
1400
1398
  return __awaiter(this, void 0, void 0, function () {
1401
- var firstEvents$;
1402
- var _this = this;
1403
- return __generator(this, function (_c) {
1404
- firstEvents$ = this.events$.pipe(rxjs.map(function (event) {
1405
- var matrixEvent = event.detail.data;
1406
- if (matrixEvent.sender === _this.widgetParameters.userId &&
1407
- matrixEvent.state_key === undefined &&
1408
- matrixEvent.type === eventType &&
1409
- (!roomId || matrixEvent.room_id === roomId)) {
1410
- _this.matrixWidgetApi.transport.reply(event.detail, {});
1411
- return event.detail.data;
1412
- }
1413
- return undefined;
1414
- }), rxjs.filter(isDefined), rxjs.first());
1415
- return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
1416
- var subscription, err_3;
1417
- return __generator(this, function (_a) {
1418
- switch (_a.label) {
1419
- case 0:
1420
- subscription = firstEvents$.subscribe({
1421
- next: function (event) { return resolve(event); },
1422
- error: function (err) { return reject(err); },
1423
- });
1424
- _a.label = 1;
1425
- case 1:
1426
- _a.trys.push([1, 3, , 4]);
1427
- return [4 /*yield*/, this.matrixWidgetApi.sendRoomEvent(eventType, content, roomId)];
1428
- case 2:
1429
- _a.sent();
1430
- return [3 /*break*/, 4];
1431
- case 3:
1432
- err_3 = _a.sent();
1433
- subscription.unsubscribe();
1434
- reject(err_3);
1435
- return [3 /*break*/, 4];
1436
- case 4: return [2 /*return*/];
1437
- }
1438
- });
1439
- }); })];
1399
+ var subject, subscription, _c, event_id_2, room_id_2, event_2;
1400
+ return __generator(this, function (_d) {
1401
+ switch (_d.label) {
1402
+ case 0:
1403
+ subject = new rxjs.ReplaySubject();
1404
+ subscription = this.events$.subscribe(function (e) { return subject.next(e); });
1405
+ _d.label = 1;
1406
+ case 1:
1407
+ _d.trys.push([1, , 4, 5]);
1408
+ return [4 /*yield*/, this.matrixWidgetApi.sendRoomEvent(eventType, content, roomId)];
1409
+ case 2:
1410
+ _c = _d.sent(), event_id_2 = _c.event_id, room_id_2 = _c.room_id;
1411
+ return [4 /*yield*/, rxjs.firstValueFrom(subject.pipe(rxjs.filter(function (event) {
1412
+ var matrixEvent = event.detail.data;
1413
+ return (matrixEvent.event_id === event_id_2 &&
1414
+ matrixEvent.room_id === room_id_2);
1415
+ }), rxjs.map(function (event) { return event.detail.data; })))];
1416
+ case 3:
1417
+ event_2 = _d.sent();
1418
+ return [2 /*return*/, event_2];
1419
+ case 4:
1420
+ subscription.unsubscribe();
1421
+ return [7 /*endfinally*/];
1422
+ case 5: return [2 /*return*/];
1423
+ }
1440
1424
  });
1441
1425
  });
1442
1426
  };
@@ -1457,6 +1441,23 @@ var WidgetApiImpl = /** @class */ (function () {
1457
1441
  });
1458
1442
  });
1459
1443
  };
1444
+ /** {@inheritDoc WidgetApi.sendToDeviceMessage} */
1445
+ WidgetApiImpl.prototype.sendToDeviceMessage = function (eventType, encrypted, content) {
1446
+ return __awaiter(this, void 0, void 0, function () {
1447
+ return __generator(this, function (_a) {
1448
+ switch (_a.label) {
1449
+ case 0: return [4 /*yield*/, this.matrixWidgetApi.sendToDevice(eventType, encrypted, content)];
1450
+ case 1:
1451
+ _a.sent();
1452
+ return [2 /*return*/];
1453
+ }
1454
+ });
1455
+ });
1456
+ };
1457
+ /** {@inheritDoc WidgetApi.observeToDeviceMessages} */
1458
+ WidgetApiImpl.prototype.observeToDeviceMessages = function (eventType) {
1459
+ return this.toDeviceMessages$.pipe(rxjs.map(function (e) { return e.detail.data; }), rxjs.filter(function (e) { return e.type === eventType; }));
1460
+ };
1460
1461
  /** {@inheritDoc WidgetApi.openModal} */
1461
1462
  WidgetApiImpl.prototype.openModal = function (pathName, name, options) {
1462
1463
  return __awaiter(this, void 0, void 0, function () {
@@ -1588,7 +1589,7 @@ var WidgetApiImpl = /** @class */ (function () {
1588
1589
  WidgetApiImpl.prototype.requestOpenIDConnectTokenInternal = function () {
1589
1590
  var _a;
1590
1591
  return __awaiter(this, void 0, void 0, function () {
1591
- var leywayMilliseconds, openIdToken, err_4;
1592
+ var leywayMilliseconds, openIdToken, err_2;
1592
1593
  return __generator(this, function (_b) {
1593
1594
  switch (_b.label) {
1594
1595
  case 0:
@@ -1609,14 +1610,28 @@ var WidgetApiImpl = /** @class */ (function () {
1609
1610
  };
1610
1611
  return [2 /*return*/, openIdToken];
1611
1612
  case 3:
1612
- err_4 = _b.sent();
1613
+ err_2 = _b.sent();
1613
1614
  this.cachedOpenIdToken = undefined;
1614
- throw err_4;
1615
+ throw err_2;
1615
1616
  case 4: return [2 /*return*/];
1616
1617
  }
1617
1618
  });
1618
1619
  });
1619
1620
  };
1621
+ /** {@inheritdoc WidgetApi.observeTurnServers} */
1622
+ WidgetApiImpl.prototype.observeTurnServers = function () {
1623
+ return rxjs.from(this.matrixWidgetApi.getTurnServers()).pipe(
1624
+ // For some reason a different naming was chosen for the API, but
1625
+ // we already convert them to the right type for WebRTC consumers.
1626
+ rxjs.map(function (_a) {
1627
+ var uris = _a.uris, username = _a.username, password = _a.password;
1628
+ return ({
1629
+ urls: uris,
1630
+ username: username,
1631
+ credential: password,
1632
+ });
1633
+ }));
1634
+ };
1620
1635
  return WidgetApiImpl;
1621
1636
  }());
1622
1637
 
@@ -1,6 +1,6 @@
1
1
  import { Symbols, WidgetEventCapability, EventDirection, WidgetApi, WidgetApiToWidgetAction, MatrixCapabilities } from 'matrix-widget-api';
2
2
  import { stringify, parse } from 'qs';
3
- import { filter, fromEvent, firstValueFrom, map, first, throwError, from, mergeAll, concat, share } from 'rxjs';
3
+ import { filter, fromEvent, firstValueFrom, map, first, throwError, from, mergeAll, concat, ReplaySubject, share } from 'rxjs';
4
4
 
5
5
  /*
6
6
  * Copyright 2022 Nordeck IT + Consulting GmbH
@@ -1041,12 +1041,28 @@ var WidgetApiImpl = /** @class */ (function () {
1041
1041
  /** {@inheritDoc WidgetApi.widgetParameters} */
1042
1042
  widgetParameters, _a) {
1043
1043
  var _b = _a === void 0 ? {} : _a, _c = _b.capabilities, capabilities = _c === void 0 ? [] : _c, _d = _b.supportStandalone, supportStandalone = _d === void 0 ? false : _d;
1044
+ var _this = this;
1044
1045
  this.matrixWidgetApi = matrixWidgetApi;
1045
1046
  this.widgetId = widgetId;
1046
1047
  this.widgetParameters = widgetParameters;
1047
- var eventName = "action:".concat(WidgetApiToWidgetAction.SendEvent);
1048
- this.events$ = fromEvent(this.matrixWidgetApi, eventName, function (event) {
1048
+ this.events$ = fromEvent(this.matrixWidgetApi, "action:".concat(WidgetApiToWidgetAction.SendEvent), function (event) {
1049
1049
  event.preventDefault();
1050
+ try {
1051
+ _this.matrixWidgetApi.transport.reply(event.detail, {});
1052
+ }
1053
+ catch (_) {
1054
+ // Ignore errors while replying
1055
+ }
1056
+ return event;
1057
+ }).pipe(share());
1058
+ this.toDeviceMessages$ = fromEvent(this.matrixWidgetApi, 'action:send_to_device', function (event) {
1059
+ event.preventDefault();
1060
+ try {
1061
+ matrixWidgetApi.transport.reply(event.detail, {});
1062
+ }
1063
+ catch (_) {
1064
+ // Ignore errors while replying
1065
+ }
1050
1066
  return event;
1051
1067
  }).pipe(share());
1052
1068
  this.initialCapabilities = __spreadArray(__spreadArray([], capabilities, true), (supportStandalone ? [] : [MatrixCapabilities.RequiresClient]), true);
@@ -1292,7 +1308,6 @@ var WidgetApiImpl = /** @class */ (function () {
1292
1308
  };
1293
1309
  /** {@inheritDoc WidgetApi.observeStateEvents} */
1294
1310
  WidgetApiImpl.prototype.observeStateEvents = function (eventType, _a) {
1295
- var _this = this;
1296
1311
  var _b = _a === void 0 ? {} : _a, stateKey = _b.stateKey, roomIds = _b.roomIds;
1297
1312
  var currentRoomId = this.widgetParameters.roomId;
1298
1313
  if (!currentRoomId) {
@@ -1305,7 +1320,6 @@ var WidgetApiImpl = /** @class */ (function () {
1305
1320
  matrixEvent.state_key !== undefined &&
1306
1321
  (stateKey === undefined || matrixEvent.state_key === stateKey) &&
1307
1322
  isInRoom(matrixEvent, currentRoomId, roomIds)) {
1308
- _this.matrixWidgetApi.transport.reply(event.detail, {});
1309
1323
  return event.detail.data;
1310
1324
  }
1311
1325
  return undefined;
@@ -1316,45 +1330,31 @@ var WidgetApiImpl = /** @class */ (function () {
1316
1330
  WidgetApiImpl.prototype.sendStateEvent = function (eventType, content, _a) {
1317
1331
  var _b = _a === void 0 ? {} : _a, roomId = _b.roomId, _c = _b.stateKey, stateKey = _c === void 0 ? '' : _c;
1318
1332
  return __awaiter(this, void 0, void 0, function () {
1319
- var firstEvent$;
1320
- var _this = this;
1321
- return __generator(this, function (_d) {
1322
- firstEvent$ = this.events$.pipe(map(function (event) {
1323
- var matrixEvent = event.detail.data;
1324
- if (matrixEvent.sender === _this.widgetParameters.userId &&
1325
- matrixEvent.state_key !== undefined &&
1326
- matrixEvent.type === eventType &&
1327
- (!roomId || matrixEvent.room_id === roomId)) {
1328
- _this.matrixWidgetApi.transport.reply(event.detail, {});
1329
- return event.detail.data;
1330
- }
1331
- return undefined;
1332
- }), filter(isDefined), first());
1333
- return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
1334
- var subscription, err_2;
1335
- return __generator(this, function (_a) {
1336
- switch (_a.label) {
1337
- case 0:
1338
- subscription = firstEvent$.subscribe({
1339
- next: function (event) { return resolve(event); },
1340
- error: function (err) { return reject(err); },
1341
- });
1342
- _a.label = 1;
1343
- case 1:
1344
- _a.trys.push([1, 3, , 4]);
1345
- return [4 /*yield*/, this.matrixWidgetApi.sendStateEvent(eventType, stateKey, content, roomId)];
1346
- case 2:
1347
- _a.sent();
1348
- return [3 /*break*/, 4];
1349
- case 3:
1350
- err_2 = _a.sent();
1351
- subscription.unsubscribe();
1352
- reject(err_2);
1353
- return [3 /*break*/, 4];
1354
- case 4: return [2 /*return*/];
1355
- }
1356
- });
1357
- }); })];
1333
+ var subject, subscription, _d, event_id_1, room_id_1, event_1;
1334
+ return __generator(this, function (_e) {
1335
+ switch (_e.label) {
1336
+ case 0:
1337
+ subject = new ReplaySubject();
1338
+ subscription = this.events$.subscribe(function (e) { return subject.next(e); });
1339
+ _e.label = 1;
1340
+ case 1:
1341
+ _e.trys.push([1, , 4, 5]);
1342
+ return [4 /*yield*/, this.matrixWidgetApi.sendStateEvent(eventType, stateKey, content, roomId)];
1343
+ case 2:
1344
+ _d = _e.sent(), event_id_1 = _d.event_id, room_id_1 = _d.room_id;
1345
+ return [4 /*yield*/, firstValueFrom(subject.pipe(filter(function (event) {
1346
+ var matrixEvent = event.detail.data;
1347
+ return (matrixEvent.event_id === event_id_1 &&
1348
+ matrixEvent.room_id === room_id_1);
1349
+ }), map(function (event) { return event.detail.data; })))];
1350
+ case 3:
1351
+ event_1 = _e.sent();
1352
+ return [2 /*return*/, event_1];
1353
+ case 4:
1354
+ subscription.unsubscribe();
1355
+ return [7 /*endfinally*/];
1356
+ case 5: return [2 /*return*/];
1357
+ }
1358
1358
  });
1359
1359
  });
1360
1360
  };
@@ -1372,7 +1372,6 @@ var WidgetApiImpl = /** @class */ (function () {
1372
1372
  };
1373
1373
  /** {@inheritDoc WidgetApi.observeRoomEvents} */
1374
1374
  WidgetApiImpl.prototype.observeRoomEvents = function (eventType, _a) {
1375
- var _this = this;
1376
1375
  var _b = _a === void 0 ? {} : _a, messageType = _b.messageType, roomIds = _b.roomIds;
1377
1376
  var currentRoomId = this.widgetParameters.roomId;
1378
1377
  if (!currentRoomId) {
@@ -1385,7 +1384,6 @@ var WidgetApiImpl = /** @class */ (function () {
1385
1384
  matrixEvent.state_key === undefined &&
1386
1385
  (!messageType || matrixEvent.content.msgtype === messageType) &&
1387
1386
  isInRoom(matrixEvent, currentRoomId, roomIds)) {
1388
- _this.matrixWidgetApi.transport.reply(event.detail, {});
1389
1387
  return event.detail.data;
1390
1388
  }
1391
1389
  return undefined;
@@ -1396,45 +1394,31 @@ var WidgetApiImpl = /** @class */ (function () {
1396
1394
  WidgetApiImpl.prototype.sendRoomEvent = function (eventType, content, _a) {
1397
1395
  var _b = _a === void 0 ? {} : _a, roomId = _b.roomId;
1398
1396
  return __awaiter(this, void 0, void 0, function () {
1399
- var firstEvents$;
1400
- var _this = this;
1401
- return __generator(this, function (_c) {
1402
- firstEvents$ = this.events$.pipe(map(function (event) {
1403
- var matrixEvent = event.detail.data;
1404
- if (matrixEvent.sender === _this.widgetParameters.userId &&
1405
- matrixEvent.state_key === undefined &&
1406
- matrixEvent.type === eventType &&
1407
- (!roomId || matrixEvent.room_id === roomId)) {
1408
- _this.matrixWidgetApi.transport.reply(event.detail, {});
1409
- return event.detail.data;
1410
- }
1411
- return undefined;
1412
- }), filter(isDefined), first());
1413
- return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
1414
- var subscription, err_3;
1415
- return __generator(this, function (_a) {
1416
- switch (_a.label) {
1417
- case 0:
1418
- subscription = firstEvents$.subscribe({
1419
- next: function (event) { return resolve(event); },
1420
- error: function (err) { return reject(err); },
1421
- });
1422
- _a.label = 1;
1423
- case 1:
1424
- _a.trys.push([1, 3, , 4]);
1425
- return [4 /*yield*/, this.matrixWidgetApi.sendRoomEvent(eventType, content, roomId)];
1426
- case 2:
1427
- _a.sent();
1428
- return [3 /*break*/, 4];
1429
- case 3:
1430
- err_3 = _a.sent();
1431
- subscription.unsubscribe();
1432
- reject(err_3);
1433
- return [3 /*break*/, 4];
1434
- case 4: return [2 /*return*/];
1435
- }
1436
- });
1437
- }); })];
1397
+ var subject, subscription, _c, event_id_2, room_id_2, event_2;
1398
+ return __generator(this, function (_d) {
1399
+ switch (_d.label) {
1400
+ case 0:
1401
+ subject = new ReplaySubject();
1402
+ subscription = this.events$.subscribe(function (e) { return subject.next(e); });
1403
+ _d.label = 1;
1404
+ case 1:
1405
+ _d.trys.push([1, , 4, 5]);
1406
+ return [4 /*yield*/, this.matrixWidgetApi.sendRoomEvent(eventType, content, roomId)];
1407
+ case 2:
1408
+ _c = _d.sent(), event_id_2 = _c.event_id, room_id_2 = _c.room_id;
1409
+ return [4 /*yield*/, firstValueFrom(subject.pipe(filter(function (event) {
1410
+ var matrixEvent = event.detail.data;
1411
+ return (matrixEvent.event_id === event_id_2 &&
1412
+ matrixEvent.room_id === room_id_2);
1413
+ }), map(function (event) { return event.detail.data; })))];
1414
+ case 3:
1415
+ event_2 = _d.sent();
1416
+ return [2 /*return*/, event_2];
1417
+ case 4:
1418
+ subscription.unsubscribe();
1419
+ return [7 /*endfinally*/];
1420
+ case 5: return [2 /*return*/];
1421
+ }
1438
1422
  });
1439
1423
  });
1440
1424
  };
@@ -1455,6 +1439,23 @@ var WidgetApiImpl = /** @class */ (function () {
1455
1439
  });
1456
1440
  });
1457
1441
  };
1442
+ /** {@inheritDoc WidgetApi.sendToDeviceMessage} */
1443
+ WidgetApiImpl.prototype.sendToDeviceMessage = function (eventType, encrypted, content) {
1444
+ return __awaiter(this, void 0, void 0, function () {
1445
+ return __generator(this, function (_a) {
1446
+ switch (_a.label) {
1447
+ case 0: return [4 /*yield*/, this.matrixWidgetApi.sendToDevice(eventType, encrypted, content)];
1448
+ case 1:
1449
+ _a.sent();
1450
+ return [2 /*return*/];
1451
+ }
1452
+ });
1453
+ });
1454
+ };
1455
+ /** {@inheritDoc WidgetApi.observeToDeviceMessages} */
1456
+ WidgetApiImpl.prototype.observeToDeviceMessages = function (eventType) {
1457
+ return this.toDeviceMessages$.pipe(map(function (e) { return e.detail.data; }), filter(function (e) { return e.type === eventType; }));
1458
+ };
1458
1459
  /** {@inheritDoc WidgetApi.openModal} */
1459
1460
  WidgetApiImpl.prototype.openModal = function (pathName, name, options) {
1460
1461
  return __awaiter(this, void 0, void 0, function () {
@@ -1586,7 +1587,7 @@ var WidgetApiImpl = /** @class */ (function () {
1586
1587
  WidgetApiImpl.prototype.requestOpenIDConnectTokenInternal = function () {
1587
1588
  var _a;
1588
1589
  return __awaiter(this, void 0, void 0, function () {
1589
- var leywayMilliseconds, openIdToken, err_4;
1590
+ var leywayMilliseconds, openIdToken, err_2;
1590
1591
  return __generator(this, function (_b) {
1591
1592
  switch (_b.label) {
1592
1593
  case 0:
@@ -1607,14 +1608,28 @@ var WidgetApiImpl = /** @class */ (function () {
1607
1608
  };
1608
1609
  return [2 /*return*/, openIdToken];
1609
1610
  case 3:
1610
- err_4 = _b.sent();
1611
+ err_2 = _b.sent();
1611
1612
  this.cachedOpenIdToken = undefined;
1612
- throw err_4;
1613
+ throw err_2;
1613
1614
  case 4: return [2 /*return*/];
1614
1615
  }
1615
1616
  });
1616
1617
  });
1617
1618
  };
1619
+ /** {@inheritdoc WidgetApi.observeTurnServers} */
1620
+ WidgetApiImpl.prototype.observeTurnServers = function () {
1621
+ return from(this.matrixWidgetApi.getTurnServers()).pipe(
1622
+ // For some reason a different naming was chosen for the API, but
1623
+ // we already convert them to the right type for WebRTC consumers.
1624
+ map(function (_a) {
1625
+ var uris = _a.uris, username = _a.username, password = _a.password;
1626
+ return ({
1627
+ urls: uris,
1628
+ username: username,
1629
+ credential: password,
1630
+ });
1631
+ }));
1632
+ };
1618
1633
  return WidgetApiImpl;
1619
1634
  }());
1620
1635
 
package/build/index.d.ts CHANGED
@@ -395,6 +395,29 @@ export declare type StateEvent<T = unknown> = Omit<IRoomEvent, 'content' | 'unsi
395
395
  content: T;
396
396
  };
397
397
 
398
+ /**
399
+ * Generic type for to device message events.
400
+ */
401
+ export declare type ToDeviceMessageEvent<T = unknown> = {
402
+ type: string;
403
+ sender: string;
404
+ encrypted: boolean;
405
+ content: T;
406
+ };
407
+
408
+ /**
409
+ * WebRTC Ice server credentials like turn servers, type is compatible to
410
+ * `RTCIceServer` from WebRTC.
411
+ */
412
+ export declare type TurnServer = {
413
+ /** One or more URLs for this turn server. */
414
+ urls: string[];
415
+ /** Username for this turn server. */
416
+ username: string;
417
+ /** Credentials for this turn server. */
418
+ credential: string;
419
+ };
420
+
398
421
  /**
399
422
  * The capability that needs to be requested in order to navigate to another room.
400
423
  */
@@ -420,13 +443,13 @@ export declare type WidgetApi = {
420
443
  */
421
444
  getWidgetConfig<T extends IWidgetApiRequestData>(): Readonly<WidgetConfig<T> | undefined>;
422
445
  /**
423
- * Rerequests the initialy in the constructor passed capabilities.
446
+ * Rerequests capabilities initially passed in the constructor.
424
447
  *
425
448
  * This is useful in case the user denied one or all of them.
426
449
  */
427
450
  rerequestInitialCapabilities(): Promise<void>;
428
451
  /**
429
- * True, if the initial capabilties passed via the constructor were granted.
452
+ * True, if the initial capabilities passed via the constructor were granted.
430
453
  */
431
454
  hasInitialCapabilities(): boolean;
432
455
  /**
@@ -526,7 +549,7 @@ export declare type WidgetApi = {
526
549
  *
527
550
  * @param eventType - The type of the event to receive.
528
551
  * @param options - Options for receiving the room event.
529
- * Use `messageType` to receive events with a specifc
552
+ * Use `messageType` to receive events with a specific
530
553
  * message type.
531
554
  * Use `roomIds` to receive the state events from other
532
555
  * rooms.
@@ -548,7 +571,7 @@ export declare type WidgetApi = {
548
571
  *
549
572
  * @param eventType - The type of the event to receive.
550
573
  * @param options - Options for receiving the room event.
551
- * Use `messageType` to receive events with a specifc
574
+ * Use `messageType` to receive events with a specific
552
575
  * message type.
553
576
  * Use `roomIds` to receive the state events from other
554
577
  * rooms.
@@ -605,6 +628,27 @@ export declare type WidgetApi = {
605
628
  chunk: Array<RoomEvent | StateEvent>;
606
629
  nextToken?: string;
607
630
  }>;
631
+ /**
632
+ * Send a message to a device of a user (or multiple users / devices).
633
+ *
634
+ * @param eventType - The type of the event.
635
+ * @param encrypted - Whether the event should be encrypted.
636
+ * @param content - The content to send. This is a map of user ids, to device
637
+ * ids, to the content that should be send. It is possible to
638
+ * specify a `'*'` device, to send the content to all devices
639
+ * of a user.
640
+ */
641
+ sendToDeviceMessage<T>(eventType: string, encrypted: boolean, content: {
642
+ [userId: string]: {
643
+ [deviceId: string | '*']: T;
644
+ };
645
+ }): Promise<void>;
646
+ /**
647
+ * Observes all to device messages send to the current device.
648
+ *
649
+ * @param eventType - The type of the event.
650
+ */
651
+ observeToDeviceMessages<T>(eventType: string): Observable<ToDeviceMessageEvent<T>>;
608
652
  /**
609
653
  * Open a new modal, wait until the modal closes, and return the result.
610
654
  *
@@ -678,6 +722,11 @@ export declare type WidgetApi = {
678
722
  * old one expired.
679
723
  */
680
724
  requestOpenIDConnectToken(): Promise<IOpenIDCredentials>;
725
+ /**
726
+ * Returns an observable containing WebRTC Ice server credentials, like turn
727
+ * servers, if available.
728
+ */
729
+ observeTurnServers(): Observable<TurnServer>;
681
730
  };
682
731
 
683
732
  /**
@@ -717,6 +766,7 @@ export declare class WidgetApiImpl implements WidgetApi {
717
766
  private outstandingOpenIDConnectTokenRequest;
718
767
  private cachedOpenIdToken;
719
768
  private readonly events$;
769
+ private readonly toDeviceMessages$;
720
770
  private readonly initialCapabilities;
721
771
  constructor(
722
772
  /**
@@ -797,6 +847,14 @@ export declare class WidgetApiImpl implements WidgetApi {
797
847
  chunk: Array<RoomEvent | StateEvent>;
798
848
  nextToken?: string;
799
849
  }>;
850
+ /** {@inheritDoc WidgetApi.sendToDeviceMessage} */
851
+ sendToDeviceMessage<T>(eventType: string, encrypted: boolean, content: {
852
+ [userId: string]: {
853
+ [deviceId: string | '*']: T;
854
+ };
855
+ }): Promise<void>;
856
+ /** {@inheritDoc WidgetApi.observeToDeviceMessages} */
857
+ observeToDeviceMessages<T>(eventType: string): Observable<ToDeviceMessageEvent<T>>;
800
858
  /** {@inheritDoc WidgetApi.openModal} */
801
859
  openModal<T extends Record<string, unknown> = Record<string, unknown>, U extends IModalWidgetCreateData = IModalWidgetCreateData>(pathName: string, name: string, options?: {
802
860
  buttons?: IModalWidgetOpenRequestDataButton[];
@@ -813,6 +871,8 @@ export declare class WidgetApiImpl implements WidgetApi {
813
871
  /** {@inheritdoc WidgetApi.requestOpenIDConnectToken} */
814
872
  requestOpenIDConnectToken(): Promise<IOpenIDCredentials>;
815
873
  private requestOpenIDConnectTokenInternal;
874
+ /** {@inheritdoc WidgetApi.observeTurnServers} */
875
+ observeTurnServers(): Observable<TurnServer>;
816
876
  }
817
877
 
818
878
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matrix-widget-toolkit/api",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "A simplified layer on top of matrix-widget-api to use build widgets.",
5
5
  "author": "Nordeck IT + Consulting GmbH",
6
6
  "license": "Apache-2.0",