@matrix-widget-toolkit/api 3.4.2 → 4.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.
Files changed (64) hide show
  1. package/build/cjs/api/WidgetApiImpl.d.cts +179 -0
  2. package/build/cjs/api/WidgetApiImpl.test.d.cts +1 -0
  3. package/build/cjs/api/extras/capabilities.d.cts +12 -0
  4. package/build/cjs/api/extras/capabilities.test.d.cts +1 -0
  5. package/build/cjs/api/extras/displayName.d.cts +12 -0
  6. package/build/cjs/api/extras/displayName.test.d.cts +1 -0
  7. package/build/cjs/api/extras/events.d.cts +40 -0
  8. package/build/cjs/api/extras/events.test.d.cts +1 -0
  9. package/build/cjs/api/extras/index.d.cts +14 -0
  10. package/build/cjs/api/extras/navigateTo.d.cts +24 -0
  11. package/build/cjs/api/extras/navigateTo.test.d.cts +1 -0
  12. package/build/cjs/api/extras/originServerTs.d.cts +10 -0
  13. package/build/cjs/api/extras/originServerTs.test.d.cts +1 -0
  14. package/build/cjs/api/extras/powerLevel.d.cts +105 -0
  15. package/build/cjs/api/extras/powerLevel.test.d.cts +1 -0
  16. package/build/cjs/api/extras/redactions.d.cts +42 -0
  17. package/build/cjs/api/extras/redactions.test.d.cts +1 -0
  18. package/build/cjs/api/extras/relatesTo.d.cts +60 -0
  19. package/build/cjs/api/extras/relatesTo.test.d.cts +1 -0
  20. package/build/cjs/api/extras/roomMember.d.cts +35 -0
  21. package/build/cjs/api/extras/roomMember.test.d.cts +1 -0
  22. package/build/cjs/api/index.d.cts +9 -0
  23. package/build/cjs/api/parameters.d.cts +58 -0
  24. package/build/cjs/api/parameters.test.d.cts +1 -0
  25. package/build/cjs/api/registration.d.cts +33 -0
  26. package/build/cjs/api/registration.test.d.cts +1 -0
  27. package/build/cjs/api/types.d.cts +490 -0
  28. package/build/cjs/api/utils.d.cts +20 -0
  29. package/build/cjs/api/utils.test.d.cts +1 -0
  30. package/build/cjs/{index.js → index.cjs} +226 -61
  31. package/build/cjs/index.d.cts +5 -0
  32. package/build/esm/api/WidgetApiImpl.d.ts +179 -0
  33. package/build/esm/api/WidgetApiImpl.test.d.ts +1 -0
  34. package/build/esm/api/extras/capabilities.d.ts +12 -0
  35. package/build/esm/api/extras/capabilities.test.d.ts +1 -0
  36. package/build/esm/api/extras/displayName.d.ts +12 -0
  37. package/build/esm/api/extras/displayName.test.d.ts +1 -0
  38. package/build/esm/api/extras/events.d.ts +40 -0
  39. package/build/esm/api/extras/events.test.d.ts +1 -0
  40. package/build/esm/api/extras/index.d.ts +14 -0
  41. package/build/esm/api/extras/navigateTo.d.ts +24 -0
  42. package/build/esm/api/extras/navigateTo.test.d.ts +1 -0
  43. package/build/esm/api/extras/originServerTs.d.ts +10 -0
  44. package/build/esm/api/extras/originServerTs.test.d.ts +1 -0
  45. package/build/esm/api/extras/powerLevel.d.ts +105 -0
  46. package/build/esm/api/extras/powerLevel.test.d.ts +1 -0
  47. package/build/esm/api/extras/redactions.d.ts +42 -0
  48. package/build/esm/api/extras/redactions.test.d.ts +1 -0
  49. package/build/esm/api/extras/relatesTo.d.ts +60 -0
  50. package/build/esm/api/extras/relatesTo.test.d.ts +1 -0
  51. package/build/esm/api/extras/roomMember.d.ts +35 -0
  52. package/build/esm/api/extras/roomMember.test.d.ts +1 -0
  53. package/build/esm/api/index.d.ts +9 -0
  54. package/build/esm/api/parameters.d.ts +58 -0
  55. package/build/esm/api/parameters.test.d.ts +1 -0
  56. package/build/esm/api/registration.d.ts +33 -0
  57. package/build/esm/api/registration.test.d.ts +1 -0
  58. package/build/esm/api/types.d.ts +490 -0
  59. package/build/esm/api/utils.d.ts +20 -0
  60. package/build/esm/api/utils.test.d.ts +1 -0
  61. package/build/esm/index.d.ts +5 -0
  62. package/build/esm/index.js +217 -61
  63. package/build/index.d.ts +74 -10
  64. package/package.json +30 -17
@@ -1,9 +1,14 @@
1
1
  'use strict';
2
2
 
3
3
  var matrixWidgetApi = require('matrix-widget-api');
4
+ var Joi = require('joi');
4
5
  var qs = require('qs');
5
6
  var rxjs = require('rxjs');
6
7
 
8
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
+
10
+ var Joi__default = /*#__PURE__*/_interopDefault(Joi);
11
+
7
12
  /*
8
13
  * Copyright 2022 Nordeck IT + Consulting GmbH
9
14
  *
@@ -108,6 +113,17 @@ function getRoomMemberDisplayName(member, allRoomMembers) {
108
113
  * See the License for the specific language governing permissions and
109
114
  * limitations under the License.
110
115
  */
116
+ var __assign$2 = (undefined && undefined.__assign) || function () {
117
+ __assign$2 = Object.assign || function(t) {
118
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
119
+ s = arguments[i];
120
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
121
+ t[p] = s[p];
122
+ }
123
+ return t;
124
+ };
125
+ return __assign$2.apply(this, arguments);
126
+ };
111
127
  /**
112
128
  * Check if the given event is a {@link StateEvent}.
113
129
  *
@@ -126,6 +142,58 @@ function isStateEvent(event) {
126
142
  function isRoomEvent(event) {
127
143
  return !('state_key' in event);
128
144
  }
145
+ /**
146
+ * Check if the given value is a valid {@link RoomEvent}.
147
+ *
148
+ * @param event - The value to check
149
+ * @returns true if value is a valid room event, else false.
150
+ */
151
+ // Allow any here, so that the validation works for every event
152
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
153
+ function isValidRoomEvent(event) {
154
+ return roomEventSchema.validate(event).error === undefined;
155
+ }
156
+ /**
157
+ * Check if the given value is a valid {@link StateEvent}.
158
+ *
159
+ * @param event - The value to check
160
+ * @returns true if value is a valid state event, else false.
161
+ */
162
+ // Allow any here, so that the validation works for every event
163
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
164
+ function isValidStateEvent(event) {
165
+ return stateEventSchema.validate(event).error === undefined;
166
+ }
167
+ /**
168
+ * Check if the given value is a valid {@link ToDeviceMessageEvent}.
169
+ *
170
+ * @param event - The value to check
171
+ * @returns true if value is a valid to device message, else false.
172
+ */
173
+ function isValidToDeviceMessageEvent(event) {
174
+ return toDeviceMessageSchema.validate(event).error === undefined;
175
+ }
176
+ /**
177
+ * Base properties to validate for all events.
178
+ */
179
+ var eventSchemaProps = {
180
+ type: Joi__default.default.string().required(),
181
+ // Do roughly check against the format
182
+ // https://spec.matrix.org/v1.13/appendices/#common-identifier-format
183
+ sender: Joi__default.default.string().pattern(new RegExp('^@[^\\s:]*:\\S*$')).required(),
184
+ event_id: Joi__default.default.string().pattern(new RegExp('^\\$.*')).required(),
185
+ room_id: Joi__default.default.string().pattern(new RegExp('^![^:]*:\\S*')).required(),
186
+ origin_server_ts: Joi__default.default.date().timestamp('javascript').required(),
187
+ content: Joi__default.default.object().required(),
188
+ };
189
+ var roomEventSchema = Joi__default.default.object(__assign$2({}, eventSchemaProps)).unknown();
190
+ var stateEventSchema = Joi__default.default.object(__assign$2(__assign$2({}, eventSchemaProps), { state_key: Joi__default.default.string().allow('').required() })).unknown();
191
+ var toDeviceMessageSchema = Joi__default.default.object({
192
+ type: Joi__default.default.string().required(),
193
+ sender: Joi__default.default.string().required(),
194
+ encrypted: Joi__default.default.boolean().required(),
195
+ content: Joi__default.default.object().required(),
196
+ }).unknown();
129
197
 
130
198
  /*
131
199
  * Copyright 2022 Nordeck IT + Consulting GmbH
@@ -142,7 +210,7 @@ function isRoomEvent(event) {
142
210
  * See the License for the specific language governing permissions and
143
211
  * limitations under the License.
144
212
  */
145
- var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
213
+ var __awaiter$4 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
146
214
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
147
215
  return new (P || (P = Promise))(function (resolve, reject) {
148
216
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -151,7 +219,7 @@ var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _argu
151
219
  step((generator = generator.apply(thisArg, _arguments || [])).next());
152
220
  });
153
221
  };
154
- var __generator$3 = (undefined && undefined.__generator) || function (thisArg, body) {
222
+ var __generator$4 = (undefined && undefined.__generator) || function (thisArg, body) {
155
223
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
156
224
  return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
157
225
  function verb(n) { return function (v) { return step([n, v]); }; }
@@ -192,10 +260,10 @@ var WIDGET_CAPABILITY_NAVIGATE = 'org.matrix.msc2931.navigate';
192
260
  * @param opts - {@link NavigateToRoomOptions}
193
261
  */
194
262
  function navigateToRoom(widgetApi_1, roomId_1) {
195
- return __awaiter$3(this, arguments, void 0, function (widgetApi, roomId, opts) {
263
+ return __awaiter$4(this, arguments, void 0, function (widgetApi, roomId, opts) {
196
264
  var _a, via, params, url;
197
265
  if (opts === void 0) { opts = {}; }
198
- return __generator$3(this, function (_b) {
266
+ return __generator$4(this, function (_b) {
199
267
  switch (_b.label) {
200
268
  case 0:
201
269
  _a = opts.via, via = _a === void 0 ? [] : _a;
@@ -439,7 +507,7 @@ function calculateActionPowerLevel(powerLevelStateEvent, action) {
439
507
  * See the License for the specific language governing permissions and
440
508
  * limitations under the License.
441
509
  */
442
- var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
510
+ var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
443
511
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
444
512
  return new (P || (P = Promise))(function (resolve, reject) {
445
513
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -448,7 +516,7 @@ var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _argu
448
516
  step((generator = generator.apply(thisArg, _arguments || [])).next());
449
517
  });
450
518
  };
451
- var __generator$2 = (undefined && undefined.__generator) || function (thisArg, body) {
519
+ var __generator$3 = (undefined && undefined.__generator) || function (thisArg, body) {
452
520
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
453
521
  return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
454
522
  function verb(n) { return function (v) { return step([n, v]); }; }
@@ -498,9 +566,9 @@ function isValidRedactionEvent(event) {
498
566
  * @returns The redaction event that was send to the room.
499
567
  */
500
568
  function redactEvent(widgetApi, eventId) {
501
- return __awaiter$2(this, void 0, void 0, function () {
569
+ return __awaiter$3(this, void 0, void 0, function () {
502
570
  var result;
503
- return __generator$2(this, function (_a) {
571
+ return __generator$3(this, function (_a) {
504
572
  switch (_a.label) {
505
573
  case 0: return [4 /*yield*/, widgetApi.sendRoomEvent(ROOM_EVENT_REDACTION, { redacts: eventId })];
506
574
  case 1:
@@ -713,6 +781,7 @@ function extractWidgetParameters() {
713
781
  clientId: params['matrix_client_id'],
714
782
  clientLanguage: params['matrix_client_language'],
715
783
  baseUrl: params['matrix_base_url'],
784
+ deviceId: params['matrix_device_id'],
716
785
  isOpenedByClient: isOpenedByClient,
717
786
  };
718
787
  }
@@ -765,7 +834,7 @@ var __assign = (undefined && undefined.__assign) || function () {
765
834
  };
766
835
  return __assign.apply(this, arguments);
767
836
  };
768
- var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
837
+ var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
769
838
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
770
839
  return new (P || (P = Promise))(function (resolve, reject) {
771
840
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -774,7 +843,7 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _argu
774
843
  step((generator = generator.apply(thisArg, _arguments || [])).next());
775
844
  });
776
845
  };
777
- var __generator$1 = (undefined && undefined.__generator) || function (thisArg, body) {
846
+ var __generator$2 = (undefined && undefined.__generator) || function (thisArg, body) {
778
847
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
779
848
  return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
780
849
  function verb(n) { return function (v) { return step([n, v]); }; }
@@ -813,12 +882,12 @@ var __rest = (undefined && undefined.__rest) || function (s, e) {
813
882
  return t;
814
883
  };
815
884
  /**
816
- * Checks whether all widget parameters were provided to the widget.
885
+ * Checks whether the necessary widget parameters were provided to the widget.
817
886
  *
818
887
  * @param widgetApi - The widget api to read the parameters from
819
888
  * @returns True, if all parameters were provided.
820
889
  */
821
- function hasRequiredWidgetParameters(widgetApi) {
890
+ function hasWidgetParameters(widgetApi) {
822
891
  return (typeof widgetApi.widgetParameters.userId === 'string' &&
823
892
  typeof widgetApi.widgetParameters.displayName === 'string' &&
824
893
  typeof widgetApi.widgetParameters.avatarUrl === 'string' &&
@@ -838,13 +907,13 @@ function hasRequiredWidgetParameters(widgetApi) {
838
907
  * @returns The generated URL.
839
908
  */
840
909
  function generateWidgetRegistrationUrl(options) {
841
- var _a, _b, _c, _d, _e, _f, _g, _h;
910
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
842
911
  if (options === void 0) { options = {}; }
843
- var pathName = options.pathName, _j = options.includeParameters, includeParameters = _j === void 0 ? true : _j, widgetParameters = options.widgetParameters;
912
+ var pathName = options.pathName, _k = options.includeParameters, includeParameters = _k === void 0 ? true : _k, widgetParameters = options.widgetParameters;
844
913
  // don't forward widgetId and parentUrl as they will be generated by the client
845
914
  // eslint-disable-next-line
846
- var _k = extractRawWidgetParameters(); _k.widgetId; _k.parentUrl; var rawWidgetParameters = __rest(_k, ["widgetId", "parentUrl"]);
847
- var parameters = Object.entries(__assign(__assign({}, rawWidgetParameters), { theme: (_a = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.theme) !== null && _a !== void 0 ? _a : '$org.matrix.msc2873.client_theme', matrix_user_id: (_b = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.userId) !== null && _b !== void 0 ? _b : '$matrix_user_id', matrix_display_name: (_c = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.displayName) !== null && _c !== void 0 ? _c : '$matrix_display_name', matrix_avatar_url: (_d = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.avatarUrl) !== null && _d !== void 0 ? _d : '$matrix_avatar_url', matrix_room_id: (_e = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.roomId) !== null && _e !== void 0 ? _e : '$matrix_room_id', matrix_client_id: (_f = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.clientId) !== null && _f !== void 0 ? _f : '$org.matrix.msc2873.client_id', matrix_client_language: (_g = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.clientLanguage) !== null && _g !== void 0 ? _g : '$org.matrix.msc2873.client_language', matrix_base_url: (_h = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.baseUrl) !== null && _h !== void 0 ? _h : '$org.matrix.msc4039.matrix_base_url' }))
915
+ var _l = extractRawWidgetParameters(); _l.widgetId; _l.parentUrl; var rawWidgetParameters = __rest(_l, ["widgetId", "parentUrl"]);
916
+ var parameters = Object.entries(__assign(__assign({}, rawWidgetParameters), { theme: (_a = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.theme) !== null && _a !== void 0 ? _a : '$org.matrix.msc2873.client_theme', matrix_user_id: (_b = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.userId) !== null && _b !== void 0 ? _b : '$matrix_user_id', matrix_display_name: (_c = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.displayName) !== null && _c !== void 0 ? _c : '$matrix_display_name', matrix_avatar_url: (_d = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.avatarUrl) !== null && _d !== void 0 ? _d : '$matrix_avatar_url', matrix_room_id: (_e = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.roomId) !== null && _e !== void 0 ? _e : '$matrix_room_id', matrix_client_id: (_f = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.clientId) !== null && _f !== void 0 ? _f : '$org.matrix.msc2873.client_id', matrix_client_language: (_g = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.clientLanguage) !== null && _g !== void 0 ? _g : '$org.matrix.msc2873.client_language', matrix_device_id: (_h = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.deviceId) !== null && _h !== void 0 ? _h : '$org.matrix.msc3819.matrix_device_id', matrix_base_url: (_j = widgetParameters === null || widgetParameters === void 0 ? void 0 : widgetParameters.baseUrl) !== null && _j !== void 0 ? _j : '$org.matrix.msc4039.matrix_base_url' }))
848
917
  .map(function (_a) {
849
918
  var k = _a[0], v = _a[1];
850
919
  return "".concat(k, "=").concat(v);
@@ -873,10 +942,10 @@ var STATE_EVENT_WIDGETS = 'im.vector.modular.widgets';
873
942
  * registration, like the display name of the widget.
874
943
  */
875
944
  function repairWidgetRegistration(widgetApi_1) {
876
- return __awaiter$1(this, arguments, void 0, function (widgetApi, registration) {
945
+ return __awaiter$2(this, arguments, void 0, function (widgetApi, registration) {
877
946
  var readResult, url, name, type, data;
878
947
  if (registration === void 0) { registration = {}; }
879
- return __generator$1(this, function (_a) {
948
+ return __generator$2(this, function (_a) {
880
949
  switch (_a.label) {
881
950
  case 0: return [4 /*yield*/, widgetApi.requestCapabilities([
882
951
  matrixWidgetApi.WidgetEventCapability.forStateEvent(matrixWidgetApi.EventDirection.Send, STATE_EVENT_WIDGETS, widgetApi.widgetId),
@@ -938,6 +1007,73 @@ function repairWidgetRegistration(widgetApi_1) {
938
1007
  * See the License for the specific language governing permissions and
939
1008
  * limitations under the License.
940
1009
  */
1010
+ /**
1011
+ * Enumeration of widget parameters that can be checked if they are available upon registration.
1012
+ */
1013
+ exports.WidgetParameter = void 0;
1014
+ (function (WidgetParameter) {
1015
+ WidgetParameter["UserId"] = "userId";
1016
+ WidgetParameter["DisplayName"] = "displayName";
1017
+ WidgetParameter["AvatarUrl"] = "avatarUrl";
1018
+ WidgetParameter["RoomId"] = "roomId";
1019
+ WidgetParameter["Theme"] = "theme";
1020
+ WidgetParameter["ClientId"] = "clientId";
1021
+ WidgetParameter["ClientLanguage"] = "clientLanguage";
1022
+ WidgetParameter["DeviceId"] = "deviceId";
1023
+ WidgetParameter["BaseUrl"] = "baseUrl";
1024
+ })(exports.WidgetParameter || (exports.WidgetParameter = {}));
1025
+
1026
+ /*
1027
+ * Copyright 2022 Nordeck IT + Consulting GmbH
1028
+ *
1029
+ * Licensed under the Apache License, Version 2.0 (the "License");
1030
+ * you may not use this file except in compliance with the License.
1031
+ * You may obtain a copy of the License at
1032
+ *
1033
+ * http://www.apache.org/licenses/LICENSE-2.0
1034
+ *
1035
+ * Unless required by applicable law or agreed to in writing, software
1036
+ * distributed under the License is distributed on an "AS IS" BASIS,
1037
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1038
+ * See the License for the specific language governing permissions and
1039
+ * limitations under the License.
1040
+ */
1041
+ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
1042
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1043
+ return new (P || (P = Promise))(function (resolve, reject) {
1044
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1045
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1046
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1047
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1048
+ });
1049
+ };
1050
+ var __generator$1 = (undefined && undefined.__generator) || function (thisArg, body) {
1051
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
1052
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
1053
+ function verb(n) { return function (v) { return step([n, v]); }; }
1054
+ function step(op) {
1055
+ if (f) throw new TypeError("Generator is already executing.");
1056
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
1057
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
1058
+ if (y = 0, t) op = [op[0] & 2, t.value];
1059
+ switch (op[0]) {
1060
+ case 0: case 1: t = op; break;
1061
+ case 4: _.label++; return { value: op[1], done: false };
1062
+ case 5: _.label++; y = op[1]; op = [0]; continue;
1063
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
1064
+ default:
1065
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
1066
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
1067
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
1068
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
1069
+ if (t[2]) _.ops.pop();
1070
+ _.trys.pop(); continue;
1071
+ }
1072
+ op = body.call(thisArg, _);
1073
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
1074
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
1075
+ }
1076
+ };
941
1077
  function convertToRawCapabilities(rawCapabilities) {
942
1078
  return rawCapabilities.map(function (c) { return (typeof c === 'string' ? c : c.raw); });
943
1079
  }
@@ -964,6 +1100,47 @@ function isInRoom(matrixEvent, currentRoomId, roomIds) {
964
1100
  }
965
1101
  return roomIds.includes(matrixEvent.room_id);
966
1102
  }
1103
+ /**
1104
+ * Create a state event from the arguments.
1105
+ *
1106
+ * @returns A state event with current timestamp origin_server_ts.
1107
+ */
1108
+ function makeEventFromSendStateEventResult(type, stateKey, content, sender, sendResult) {
1109
+ if (sendResult.event_id === undefined) {
1110
+ throw new Error('Send state event did not return an event ID');
1111
+ }
1112
+ return {
1113
+ content: content,
1114
+ event_id: sendResult.event_id,
1115
+ origin_server_ts: Date.now(),
1116
+ room_id: sendResult.room_id,
1117
+ sender: sender,
1118
+ state_key: stateKey,
1119
+ type: type,
1120
+ };
1121
+ }
1122
+ /**
1123
+ * Send a state event and resolve to a "virtual" state event.
1124
+ *
1125
+ * @returns Promise, that resolves to a state event with current timestamp origin_server_ts.
1126
+ */
1127
+ function sendStateEventWithEventResult(widgetApi, type, stateKey, content) {
1128
+ return __awaiter$1(this, void 0, void 0, function () {
1129
+ var response;
1130
+ return __generator$1(this, function (_a) {
1131
+ switch (_a.label) {
1132
+ case 0:
1133
+ if (widgetApi.widgetParameters.userId === undefined) {
1134
+ throw new Error('Own user ID is undefined');
1135
+ }
1136
+ return [4 /*yield*/, widgetApi.sendStateEvent(type, content, { stateKey: stateKey })];
1137
+ case 1:
1138
+ response = _a.sent();
1139
+ return [2 /*return*/, makeEventFromSendStateEventResult(type, stateKey, content, widgetApi.widgetParameters.userId, response)];
1140
+ }
1141
+ });
1142
+ });
1143
+ }
967
1144
 
968
1145
  /*
969
1146
  * Copyright 2022 Nordeck IT + Consulting GmbH
@@ -1069,7 +1246,9 @@ var WidgetApiImpl = /** @class */ (function () {
1069
1246
  // Ignore errors while replying
1070
1247
  }
1071
1248
  return event;
1072
- }).pipe(rxjs.share());
1249
+ })
1250
+ .pipe(rxjs.filter(function (event) { return isValidToDeviceMessageEvent(event.detail.data); }))
1251
+ .pipe(rxjs.share());
1073
1252
  this.initialCapabilities = __spreadArray(__spreadArray([], capabilities, true), (supportStandalone ? [] : [matrixWidgetApi.MatrixCapabilities.RequiresClient]), true);
1074
1253
  }
1075
1254
  /**
@@ -1304,11 +1483,15 @@ var WidgetApiImpl = /** @class */ (function () {
1304
1483
  /** {@inheritDoc WidgetApi.receiveStateEvents} */
1305
1484
  WidgetApiImpl.prototype.receiveStateEvents = function (eventType_1) {
1306
1485
  return __awaiter(this, arguments, void 0, function (eventType, _a) {
1486
+ var unvalidatedEvents, validatedEvents;
1307
1487
  var _b = _a === void 0 ? {} : _a, stateKey = _b.stateKey, roomIds = _b.roomIds;
1308
1488
  return __generator(this, function (_c) {
1309
1489
  switch (_c.label) {
1310
1490
  case 0: return [4 /*yield*/, this.matrixWidgetApi.readStateEvents(eventType, Number.MAX_SAFE_INTEGER, stateKey, typeof roomIds === 'string' ? [matrixWidgetApi.Symbols.AnyRoom] : roomIds)];
1311
- case 1: return [2 /*return*/, (_c.sent())];
1491
+ case 1:
1492
+ unvalidatedEvents = (_c.sent());
1493
+ validatedEvents = unvalidatedEvents.filter(isValidStateEvent);
1494
+ return [2 /*return*/, validatedEvents];
1312
1495
  }
1313
1496
  });
1314
1497
  });
@@ -1331,48 +1514,25 @@ var WidgetApiImpl = /** @class */ (function () {
1331
1514
  }
1332
1515
  return undefined;
1333
1516
  }), rxjs.filter(isDefined));
1334
- return rxjs.concat(historyEvent$, futureEvent$);
1517
+ return rxjs.concat(historyEvent$, futureEvent$).pipe(rxjs.filter(isValidStateEvent));
1335
1518
  };
1336
1519
  /** {@inheritDoc WidgetApi.sendStateEvent} */
1337
- WidgetApiImpl.prototype.sendStateEvent = function (eventType_1, content_1) {
1338
- return __awaiter(this, arguments, void 0, function (eventType, content, _a) {
1339
- var subject, subscription, _b, event_id_1, room_id_1, event_1;
1340
- var _c = _a === void 0 ? {} : _a, roomId = _c.roomId, _d = _c.stateKey, stateKey = _d === void 0 ? '' : _d;
1341
- return __generator(this, function (_e) {
1342
- switch (_e.label) {
1343
- case 0:
1344
- subject = new rxjs.ReplaySubject();
1345
- subscription = this.events$.subscribe(function (e) { return subject.next(e); });
1346
- _e.label = 1;
1347
- case 1:
1348
- _e.trys.push([1, , 4, 5]);
1349
- return [4 /*yield*/, this.matrixWidgetApi.sendStateEvent(eventType, stateKey, content, roomId)];
1350
- case 2:
1351
- _b = _e.sent(), event_id_1 = _b.event_id, room_id_1 = _b.room_id;
1352
- return [4 /*yield*/, rxjs.firstValueFrom(subject.pipe(rxjs.filter(function (event) {
1353
- var matrixEvent = event.detail.data;
1354
- return (matrixEvent.event_id === event_id_1 &&
1355
- matrixEvent.room_id === room_id_1);
1356
- }), rxjs.map(function (event) { return event.detail.data; })))];
1357
- case 3:
1358
- event_1 = _e.sent();
1359
- return [2 /*return*/, event_1];
1360
- case 4:
1361
- subscription.unsubscribe();
1362
- return [7 /*endfinally*/];
1363
- case 5: return [2 /*return*/];
1364
- }
1365
- });
1366
- });
1520
+ WidgetApiImpl.prototype.sendStateEvent = function (eventType, content, _a) {
1521
+ var _b = _a === void 0 ? {} : _a, roomId = _b.roomId, _c = _b.stateKey, stateKey = _c === void 0 ? '' : _c;
1522
+ return this.matrixWidgetApi.sendStateEvent(eventType, stateKey, content, roomId);
1367
1523
  };
1368
1524
  /** {@inheritDoc WidgetApi.receiveRoomEvents} */
1369
1525
  WidgetApiImpl.prototype.receiveRoomEvents = function (eventType_1) {
1370
1526
  return __awaiter(this, arguments, void 0, function (eventType, _a) {
1527
+ var unvalidatedEvents, validatedEvents;
1371
1528
  var _b = _a === void 0 ? {} : _a, messageType = _b.messageType, roomIds = _b.roomIds;
1372
1529
  return __generator(this, function (_c) {
1373
1530
  switch (_c.label) {
1374
1531
  case 0: return [4 /*yield*/, this.matrixWidgetApi.readRoomEvents(eventType, Number.MAX_SAFE_INTEGER, messageType, typeof roomIds === 'string' ? [matrixWidgetApi.Symbols.AnyRoom] : roomIds)];
1375
- case 1: return [2 /*return*/, (_c.sent())];
1532
+ case 1:
1533
+ unvalidatedEvents = (_c.sent());
1534
+ validatedEvents = unvalidatedEvents.filter(isValidRoomEvent);
1535
+ return [2 /*return*/, validatedEvents];
1376
1536
  }
1377
1537
  });
1378
1538
  });
@@ -1395,12 +1555,12 @@ var WidgetApiImpl = /** @class */ (function () {
1395
1555
  }
1396
1556
  return undefined;
1397
1557
  }), rxjs.filter(isDefined));
1398
- return rxjs.concat(historyEvent$, futureEvent$);
1558
+ return rxjs.concat(historyEvent$, futureEvent$).pipe(rxjs.filter(isValidRoomEvent));
1399
1559
  };
1400
1560
  /** {@inheritDoc WidgetApi.sendRoomEvent} */
1401
1561
  WidgetApiImpl.prototype.sendRoomEvent = function (eventType_1, content_1) {
1402
1562
  return __awaiter(this, arguments, void 0, function (eventType, content, _a) {
1403
- var subject, subscription, _b, event_id_2, room_id_2, event_2;
1563
+ var subject, subscription, _b, event_id_1, room_id_1, event_1;
1404
1564
  var _c = _a === void 0 ? {} : _a, roomId = _c.roomId;
1405
1565
  return __generator(this, function (_d) {
1406
1566
  switch (_d.label) {
@@ -1412,15 +1572,15 @@ var WidgetApiImpl = /** @class */ (function () {
1412
1572
  _d.trys.push([1, , 4, 5]);
1413
1573
  return [4 /*yield*/, this.matrixWidgetApi.sendRoomEvent(eventType, content, roomId)];
1414
1574
  case 2:
1415
- _b = _d.sent(), event_id_2 = _b.event_id, room_id_2 = _b.room_id;
1575
+ _b = _d.sent(), event_id_1 = _b.event_id, room_id_1 = _b.room_id;
1416
1576
  return [4 /*yield*/, rxjs.firstValueFrom(subject.pipe(rxjs.filter(function (event) {
1417
1577
  var matrixEvent = event.detail.data;
1418
- return (matrixEvent.event_id === event_id_2 &&
1419
- matrixEvent.room_id === room_id_2);
1578
+ return (matrixEvent.event_id === event_id_1 &&
1579
+ matrixEvent.room_id === room_id_1);
1420
1580
  }), rxjs.map(function (event) { return event.detail.data; })))];
1421
1581
  case 3:
1422
- event_2 = _d.sent();
1423
- return [2 /*return*/, event_2];
1582
+ event_1 = _d.sent();
1583
+ return [2 /*return*/, event_1];
1424
1584
  case 4:
1425
1585
  subscription.unsubscribe();
1426
1586
  return [7 /*endfinally*/];
@@ -1461,7 +1621,7 @@ var WidgetApiImpl = /** @class */ (function () {
1461
1621
  };
1462
1622
  /** {@inheritDoc WidgetApi.observeToDeviceMessages} */
1463
1623
  WidgetApiImpl.prototype.observeToDeviceMessages = function (eventType) {
1464
- return this.toDeviceMessages$.pipe(rxjs.map(function (e) { return e.detail.data; }), rxjs.filter(function (e) { return e.type === eventType; }));
1624
+ return this.toDeviceMessages$.pipe(rxjs.map(function (e) { return e.detail.data; }), rxjs.filter(isValidToDeviceMessageEvent), rxjs.filter(function (e) { return e.type === eventType; }));
1465
1625
  };
1466
1626
  /** {@inheritDoc WidgetApi.openModal} */
1467
1627
  WidgetApiImpl.prototype.openModal = function (pathName, name, options) {
@@ -1712,17 +1872,22 @@ exports.getContent = getContent;
1712
1872
  exports.getOriginalEventId = getOriginalEventId;
1713
1873
  exports.getRoomMemberDisplayName = getRoomMemberDisplayName;
1714
1874
  exports.hasActionPower = hasActionPower;
1715
- exports.hasRequiredWidgetParameters = hasRequiredWidgetParameters;
1716
1875
  exports.hasRoomEventPower = hasRoomEventPower;
1717
1876
  exports.hasStateEventPower = hasStateEventPower;
1877
+ exports.hasWidgetParameters = hasWidgetParameters;
1718
1878
  exports.isRoomEvent = isRoomEvent;
1719
1879
  exports.isStateEvent = isStateEvent;
1720
1880
  exports.isValidEventWithRelatesTo = isValidEventWithRelatesTo;
1721
1881
  exports.isValidPowerLevelStateEvent = isValidPowerLevelStateEvent;
1722
1882
  exports.isValidRedactionEvent = isValidRedactionEvent;
1883
+ exports.isValidRoomEvent = isValidRoomEvent;
1723
1884
  exports.isValidRoomMemberStateEvent = isValidRoomMemberStateEvent;
1885
+ exports.isValidStateEVent = isValidStateEvent;
1886
+ exports.isValidToDeviceMessageEvent = isValidToDeviceMessageEvent;
1887
+ exports.makeEventFromSendStateEventResult = makeEventFromSendStateEventResult;
1724
1888
  exports.navigateToRoom = navigateToRoom;
1725
1889
  exports.observeRedactionEvents = observeRedactionEvents;
1726
1890
  exports.parseWidgetId = parseWidgetId;
1727
1891
  exports.redactEvent = redactEvent;
1728
1892
  exports.repairWidgetRegistration = repairWidgetRegistration;
1893
+ exports.sendStateEventWithEventResult = sendStateEventWithEventResult;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @packageDocumentation This is package that wraps `matrix-widget-api` to
3
+ * provide a more convenient API.
4
+ */
5
+ export * from './api';