@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,4 +1,5 @@
1
1
  import { Symbols, WidgetEventCapability, EventDirection, WidgetApi, WidgetApiToWidgetAction, MatrixCapabilities } from 'matrix-widget-api';
2
+ import Joi from 'joi';
2
3
  import { stringify, parse } from 'qs';
3
4
  import { filter, fromEvent, firstValueFrom, map, first, throwError, from, mergeAll, concat, ReplaySubject, share } from 'rxjs';
4
5
 
@@ -106,6 +107,17 @@ function getRoomMemberDisplayName(member, allRoomMembers) {
106
107
  * See the License for the specific language governing permissions and
107
108
  * limitations under the License.
108
109
  */
110
+ var __assign$2 = (undefined && undefined.__assign) || function () {
111
+ __assign$2 = Object.assign || function(t) {
112
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
113
+ s = arguments[i];
114
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
115
+ t[p] = s[p];
116
+ }
117
+ return t;
118
+ };
119
+ return __assign$2.apply(this, arguments);
120
+ };
109
121
  /**
110
122
  * Check if the given event is a {@link StateEvent}.
111
123
  *
@@ -124,6 +136,58 @@ function isStateEvent(event) {
124
136
  function isRoomEvent(event) {
125
137
  return !('state_key' in event);
126
138
  }
139
+ /**
140
+ * Check if the given value is a valid {@link RoomEvent}.
141
+ *
142
+ * @param event - The value to check
143
+ * @returns true if value is a valid room event, else false.
144
+ */
145
+ // Allow any here, so that the validation works for every event
146
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
147
+ function isValidRoomEvent(event) {
148
+ return roomEventSchema.validate(event).error === undefined;
149
+ }
150
+ /**
151
+ * Check if the given value is a valid {@link StateEvent}.
152
+ *
153
+ * @param event - The value to check
154
+ * @returns true if value is a valid state event, else false.
155
+ */
156
+ // Allow any here, so that the validation works for every event
157
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
158
+ function isValidStateEvent(event) {
159
+ return stateEventSchema.validate(event).error === undefined;
160
+ }
161
+ /**
162
+ * Check if the given value is a valid {@link ToDeviceMessageEvent}.
163
+ *
164
+ * @param event - The value to check
165
+ * @returns true if value is a valid to device message, else false.
166
+ */
167
+ function isValidToDeviceMessageEvent(event) {
168
+ return toDeviceMessageSchema.validate(event).error === undefined;
169
+ }
170
+ /**
171
+ * Base properties to validate for all events.
172
+ */
173
+ var eventSchemaProps = {
174
+ type: Joi.string().required(),
175
+ // Do roughly check against the format
176
+ // https://spec.matrix.org/v1.13/appendices/#common-identifier-format
177
+ sender: Joi.string().pattern(new RegExp('^@[^\\s:]*:\\S*$')).required(),
178
+ event_id: Joi.string().pattern(new RegExp('^\\$.*')).required(),
179
+ room_id: Joi.string().pattern(new RegExp('^![^:]*:\\S*')).required(),
180
+ origin_server_ts: Joi.date().timestamp('javascript').required(),
181
+ content: Joi.object().required(),
182
+ };
183
+ var roomEventSchema = Joi.object(__assign$2({}, eventSchemaProps)).unknown();
184
+ var stateEventSchema = Joi.object(__assign$2(__assign$2({}, eventSchemaProps), { state_key: Joi.string().allow('').required() })).unknown();
185
+ var toDeviceMessageSchema = Joi.object({
186
+ type: Joi.string().required(),
187
+ sender: Joi.string().required(),
188
+ encrypted: Joi.boolean().required(),
189
+ content: Joi.object().required(),
190
+ }).unknown();
127
191
 
128
192
  /*
129
193
  * Copyright 2022 Nordeck IT + Consulting GmbH
@@ -140,7 +204,7 @@ function isRoomEvent(event) {
140
204
  * See the License for the specific language governing permissions and
141
205
  * limitations under the License.
142
206
  */
143
- var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
207
+ var __awaiter$4 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
144
208
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
145
209
  return new (P || (P = Promise))(function (resolve, reject) {
146
210
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -149,7 +213,7 @@ var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _argu
149
213
  step((generator = generator.apply(thisArg, _arguments || [])).next());
150
214
  });
151
215
  };
152
- var __generator$3 = (undefined && undefined.__generator) || function (thisArg, body) {
216
+ var __generator$4 = (undefined && undefined.__generator) || function (thisArg, body) {
153
217
  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);
154
218
  return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
155
219
  function verb(n) { return function (v) { return step([n, v]); }; }
@@ -190,10 +254,10 @@ var WIDGET_CAPABILITY_NAVIGATE = 'org.matrix.msc2931.navigate';
190
254
  * @param opts - {@link NavigateToRoomOptions}
191
255
  */
192
256
  function navigateToRoom(widgetApi_1, roomId_1) {
193
- return __awaiter$3(this, arguments, void 0, function (widgetApi, roomId, opts) {
257
+ return __awaiter$4(this, arguments, void 0, function (widgetApi, roomId, opts) {
194
258
  var _a, via, params, url;
195
259
  if (opts === void 0) { opts = {}; }
196
- return __generator$3(this, function (_b) {
260
+ return __generator$4(this, function (_b) {
197
261
  switch (_b.label) {
198
262
  case 0:
199
263
  _a = opts.via, via = _a === void 0 ? [] : _a;
@@ -437,7 +501,7 @@ function calculateActionPowerLevel(powerLevelStateEvent, action) {
437
501
  * See the License for the specific language governing permissions and
438
502
  * limitations under the License.
439
503
  */
440
- var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
504
+ var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
441
505
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
442
506
  return new (P || (P = Promise))(function (resolve, reject) {
443
507
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -446,7 +510,7 @@ var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _argu
446
510
  step((generator = generator.apply(thisArg, _arguments || [])).next());
447
511
  });
448
512
  };
449
- var __generator$2 = (undefined && undefined.__generator) || function (thisArg, body) {
513
+ var __generator$3 = (undefined && undefined.__generator) || function (thisArg, body) {
450
514
  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);
451
515
  return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
452
516
  function verb(n) { return function (v) { return step([n, v]); }; }
@@ -496,9 +560,9 @@ function isValidRedactionEvent(event) {
496
560
  * @returns The redaction event that was send to the room.
497
561
  */
498
562
  function redactEvent(widgetApi, eventId) {
499
- return __awaiter$2(this, void 0, void 0, function () {
563
+ return __awaiter$3(this, void 0, void 0, function () {
500
564
  var result;
501
- return __generator$2(this, function (_a) {
565
+ return __generator$3(this, function (_a) {
502
566
  switch (_a.label) {
503
567
  case 0: return [4 /*yield*/, widgetApi.sendRoomEvent(ROOM_EVENT_REDACTION, { redacts: eventId })];
504
568
  case 1:
@@ -711,6 +775,7 @@ function extractWidgetParameters() {
711
775
  clientId: params['matrix_client_id'],
712
776
  clientLanguage: params['matrix_client_language'],
713
777
  baseUrl: params['matrix_base_url'],
778
+ deviceId: params['matrix_device_id'],
714
779
  isOpenedByClient: isOpenedByClient,
715
780
  };
716
781
  }
@@ -763,7 +828,7 @@ var __assign = (undefined && undefined.__assign) || function () {
763
828
  };
764
829
  return __assign.apply(this, arguments);
765
830
  };
766
- var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
831
+ var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
767
832
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
768
833
  return new (P || (P = Promise))(function (resolve, reject) {
769
834
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -772,7 +837,7 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _argu
772
837
  step((generator = generator.apply(thisArg, _arguments || [])).next());
773
838
  });
774
839
  };
775
- var __generator$1 = (undefined && undefined.__generator) || function (thisArg, body) {
840
+ var __generator$2 = (undefined && undefined.__generator) || function (thisArg, body) {
776
841
  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);
777
842
  return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
778
843
  function verb(n) { return function (v) { return step([n, v]); }; }
@@ -811,12 +876,12 @@ var __rest = (undefined && undefined.__rest) || function (s, e) {
811
876
  return t;
812
877
  };
813
878
  /**
814
- * Checks whether all widget parameters were provided to the widget.
879
+ * Checks whether the necessary widget parameters were provided to the widget.
815
880
  *
816
881
  * @param widgetApi - The widget api to read the parameters from
817
882
  * @returns True, if all parameters were provided.
818
883
  */
819
- function hasRequiredWidgetParameters(widgetApi) {
884
+ function hasWidgetParameters(widgetApi) {
820
885
  return (typeof widgetApi.widgetParameters.userId === 'string' &&
821
886
  typeof widgetApi.widgetParameters.displayName === 'string' &&
822
887
  typeof widgetApi.widgetParameters.avatarUrl === 'string' &&
@@ -836,13 +901,13 @@ function hasRequiredWidgetParameters(widgetApi) {
836
901
  * @returns The generated URL.
837
902
  */
838
903
  function generateWidgetRegistrationUrl(options) {
839
- var _a, _b, _c, _d, _e, _f, _g, _h;
904
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
840
905
  if (options === void 0) { options = {}; }
841
- var pathName = options.pathName, _j = options.includeParameters, includeParameters = _j === void 0 ? true : _j, widgetParameters = options.widgetParameters;
906
+ var pathName = options.pathName, _k = options.includeParameters, includeParameters = _k === void 0 ? true : _k, widgetParameters = options.widgetParameters;
842
907
  // don't forward widgetId and parentUrl as they will be generated by the client
843
908
  // eslint-disable-next-line
844
- var _k = extractRawWidgetParameters(); _k.widgetId; _k.parentUrl; var rawWidgetParameters = __rest(_k, ["widgetId", "parentUrl"]);
845
- 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' }))
909
+ var _l = extractRawWidgetParameters(); _l.widgetId; _l.parentUrl; var rawWidgetParameters = __rest(_l, ["widgetId", "parentUrl"]);
910
+ 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' }))
846
911
  .map(function (_a) {
847
912
  var k = _a[0], v = _a[1];
848
913
  return "".concat(k, "=").concat(v);
@@ -871,10 +936,10 @@ var STATE_EVENT_WIDGETS = 'im.vector.modular.widgets';
871
936
  * registration, like the display name of the widget.
872
937
  */
873
938
  function repairWidgetRegistration(widgetApi_1) {
874
- return __awaiter$1(this, arguments, void 0, function (widgetApi, registration) {
939
+ return __awaiter$2(this, arguments, void 0, function (widgetApi, registration) {
875
940
  var readResult, url, name, type, data;
876
941
  if (registration === void 0) { registration = {}; }
877
- return __generator$1(this, function (_a) {
942
+ return __generator$2(this, function (_a) {
878
943
  switch (_a.label) {
879
944
  case 0: return [4 /*yield*/, widgetApi.requestCapabilities([
880
945
  WidgetEventCapability.forStateEvent(EventDirection.Send, STATE_EVENT_WIDGETS, widgetApi.widgetId),
@@ -936,6 +1001,73 @@ function repairWidgetRegistration(widgetApi_1) {
936
1001
  * See the License for the specific language governing permissions and
937
1002
  * limitations under the License.
938
1003
  */
1004
+ /**
1005
+ * Enumeration of widget parameters that can be checked if they are available upon registration.
1006
+ */
1007
+ var WidgetParameter;
1008
+ (function (WidgetParameter) {
1009
+ WidgetParameter["UserId"] = "userId";
1010
+ WidgetParameter["DisplayName"] = "displayName";
1011
+ WidgetParameter["AvatarUrl"] = "avatarUrl";
1012
+ WidgetParameter["RoomId"] = "roomId";
1013
+ WidgetParameter["Theme"] = "theme";
1014
+ WidgetParameter["ClientId"] = "clientId";
1015
+ WidgetParameter["ClientLanguage"] = "clientLanguage";
1016
+ WidgetParameter["DeviceId"] = "deviceId";
1017
+ WidgetParameter["BaseUrl"] = "baseUrl";
1018
+ })(WidgetParameter || (WidgetParameter = {}));
1019
+
1020
+ /*
1021
+ * Copyright 2022 Nordeck IT + Consulting GmbH
1022
+ *
1023
+ * Licensed under the Apache License, Version 2.0 (the "License");
1024
+ * you may not use this file except in compliance with the License.
1025
+ * You may obtain a copy of the License at
1026
+ *
1027
+ * http://www.apache.org/licenses/LICENSE-2.0
1028
+ *
1029
+ * Unless required by applicable law or agreed to in writing, software
1030
+ * distributed under the License is distributed on an "AS IS" BASIS,
1031
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1032
+ * See the License for the specific language governing permissions and
1033
+ * limitations under the License.
1034
+ */
1035
+ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
1036
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1037
+ return new (P || (P = Promise))(function (resolve, reject) {
1038
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1039
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1040
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1041
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1042
+ });
1043
+ };
1044
+ var __generator$1 = (undefined && undefined.__generator) || function (thisArg, body) {
1045
+ 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);
1046
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
1047
+ function verb(n) { return function (v) { return step([n, v]); }; }
1048
+ function step(op) {
1049
+ if (f) throw new TypeError("Generator is already executing.");
1050
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
1051
+ 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;
1052
+ if (y = 0, t) op = [op[0] & 2, t.value];
1053
+ switch (op[0]) {
1054
+ case 0: case 1: t = op; break;
1055
+ case 4: _.label++; return { value: op[1], done: false };
1056
+ case 5: _.label++; y = op[1]; op = [0]; continue;
1057
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
1058
+ default:
1059
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
1060
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
1061
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
1062
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
1063
+ if (t[2]) _.ops.pop();
1064
+ _.trys.pop(); continue;
1065
+ }
1066
+ op = body.call(thisArg, _);
1067
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
1068
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
1069
+ }
1070
+ };
939
1071
  function convertToRawCapabilities(rawCapabilities) {
940
1072
  return rawCapabilities.map(function (c) { return (typeof c === 'string' ? c : c.raw); });
941
1073
  }
@@ -962,6 +1094,47 @@ function isInRoom(matrixEvent, currentRoomId, roomIds) {
962
1094
  }
963
1095
  return roomIds.includes(matrixEvent.room_id);
964
1096
  }
1097
+ /**
1098
+ * Create a state event from the arguments.
1099
+ *
1100
+ * @returns A state event with current timestamp origin_server_ts.
1101
+ */
1102
+ function makeEventFromSendStateEventResult(type, stateKey, content, sender, sendResult) {
1103
+ if (sendResult.event_id === undefined) {
1104
+ throw new Error('Send state event did not return an event ID');
1105
+ }
1106
+ return {
1107
+ content: content,
1108
+ event_id: sendResult.event_id,
1109
+ origin_server_ts: Date.now(),
1110
+ room_id: sendResult.room_id,
1111
+ sender: sender,
1112
+ state_key: stateKey,
1113
+ type: type,
1114
+ };
1115
+ }
1116
+ /**
1117
+ * Send a state event and resolve to a "virtual" state event.
1118
+ *
1119
+ * @returns Promise, that resolves to a state event with current timestamp origin_server_ts.
1120
+ */
1121
+ function sendStateEventWithEventResult(widgetApi, type, stateKey, content) {
1122
+ return __awaiter$1(this, void 0, void 0, function () {
1123
+ var response;
1124
+ return __generator$1(this, function (_a) {
1125
+ switch (_a.label) {
1126
+ case 0:
1127
+ if (widgetApi.widgetParameters.userId === undefined) {
1128
+ throw new Error('Own user ID is undefined');
1129
+ }
1130
+ return [4 /*yield*/, widgetApi.sendStateEvent(type, content, { stateKey: stateKey })];
1131
+ case 1:
1132
+ response = _a.sent();
1133
+ return [2 /*return*/, makeEventFromSendStateEventResult(type, stateKey, content, widgetApi.widgetParameters.userId, response)];
1134
+ }
1135
+ });
1136
+ });
1137
+ }
965
1138
 
966
1139
  /*
967
1140
  * Copyright 2022 Nordeck IT + Consulting GmbH
@@ -1067,7 +1240,9 @@ var WidgetApiImpl = /** @class */ (function () {
1067
1240
  // Ignore errors while replying
1068
1241
  }
1069
1242
  return event;
1070
- }).pipe(share());
1243
+ })
1244
+ .pipe(filter(function (event) { return isValidToDeviceMessageEvent(event.detail.data); }))
1245
+ .pipe(share());
1071
1246
  this.initialCapabilities = __spreadArray(__spreadArray([], capabilities, true), (supportStandalone ? [] : [MatrixCapabilities.RequiresClient]), true);
1072
1247
  }
1073
1248
  /**
@@ -1302,11 +1477,15 @@ var WidgetApiImpl = /** @class */ (function () {
1302
1477
  /** {@inheritDoc WidgetApi.receiveStateEvents} */
1303
1478
  WidgetApiImpl.prototype.receiveStateEvents = function (eventType_1) {
1304
1479
  return __awaiter(this, arguments, void 0, function (eventType, _a) {
1480
+ var unvalidatedEvents, validatedEvents;
1305
1481
  var _b = _a === void 0 ? {} : _a, stateKey = _b.stateKey, roomIds = _b.roomIds;
1306
1482
  return __generator(this, function (_c) {
1307
1483
  switch (_c.label) {
1308
1484
  case 0: return [4 /*yield*/, this.matrixWidgetApi.readStateEvents(eventType, Number.MAX_SAFE_INTEGER, stateKey, typeof roomIds === 'string' ? [Symbols.AnyRoom] : roomIds)];
1309
- case 1: return [2 /*return*/, (_c.sent())];
1485
+ case 1:
1486
+ unvalidatedEvents = (_c.sent());
1487
+ validatedEvents = unvalidatedEvents.filter(isValidStateEvent);
1488
+ return [2 /*return*/, validatedEvents];
1310
1489
  }
1311
1490
  });
1312
1491
  });
@@ -1329,48 +1508,25 @@ var WidgetApiImpl = /** @class */ (function () {
1329
1508
  }
1330
1509
  return undefined;
1331
1510
  }), filter(isDefined));
1332
- return concat(historyEvent$, futureEvent$);
1511
+ return concat(historyEvent$, futureEvent$).pipe(filter(isValidStateEvent));
1333
1512
  };
1334
1513
  /** {@inheritDoc WidgetApi.sendStateEvent} */
1335
- WidgetApiImpl.prototype.sendStateEvent = function (eventType_1, content_1) {
1336
- return __awaiter(this, arguments, void 0, function (eventType, content, _a) {
1337
- var subject, subscription, _b, event_id_1, room_id_1, event_1;
1338
- var _c = _a === void 0 ? {} : _a, roomId = _c.roomId, _d = _c.stateKey, stateKey = _d === void 0 ? '' : _d;
1339
- return __generator(this, function (_e) {
1340
- switch (_e.label) {
1341
- case 0:
1342
- subject = new ReplaySubject();
1343
- subscription = this.events$.subscribe(function (e) { return subject.next(e); });
1344
- _e.label = 1;
1345
- case 1:
1346
- _e.trys.push([1, , 4, 5]);
1347
- return [4 /*yield*/, this.matrixWidgetApi.sendStateEvent(eventType, stateKey, content, roomId)];
1348
- case 2:
1349
- _b = _e.sent(), event_id_1 = _b.event_id, room_id_1 = _b.room_id;
1350
- return [4 /*yield*/, firstValueFrom(subject.pipe(filter(function (event) {
1351
- var matrixEvent = event.detail.data;
1352
- return (matrixEvent.event_id === event_id_1 &&
1353
- matrixEvent.room_id === room_id_1);
1354
- }), map(function (event) { return event.detail.data; })))];
1355
- case 3:
1356
- event_1 = _e.sent();
1357
- return [2 /*return*/, event_1];
1358
- case 4:
1359
- subscription.unsubscribe();
1360
- return [7 /*endfinally*/];
1361
- case 5: return [2 /*return*/];
1362
- }
1363
- });
1364
- });
1514
+ WidgetApiImpl.prototype.sendStateEvent = function (eventType, content, _a) {
1515
+ var _b = _a === void 0 ? {} : _a, roomId = _b.roomId, _c = _b.stateKey, stateKey = _c === void 0 ? '' : _c;
1516
+ return this.matrixWidgetApi.sendStateEvent(eventType, stateKey, content, roomId);
1365
1517
  };
1366
1518
  /** {@inheritDoc WidgetApi.receiveRoomEvents} */
1367
1519
  WidgetApiImpl.prototype.receiveRoomEvents = function (eventType_1) {
1368
1520
  return __awaiter(this, arguments, void 0, function (eventType, _a) {
1521
+ var unvalidatedEvents, validatedEvents;
1369
1522
  var _b = _a === void 0 ? {} : _a, messageType = _b.messageType, roomIds = _b.roomIds;
1370
1523
  return __generator(this, function (_c) {
1371
1524
  switch (_c.label) {
1372
1525
  case 0: return [4 /*yield*/, this.matrixWidgetApi.readRoomEvents(eventType, Number.MAX_SAFE_INTEGER, messageType, typeof roomIds === 'string' ? [Symbols.AnyRoom] : roomIds)];
1373
- case 1: return [2 /*return*/, (_c.sent())];
1526
+ case 1:
1527
+ unvalidatedEvents = (_c.sent());
1528
+ validatedEvents = unvalidatedEvents.filter(isValidRoomEvent);
1529
+ return [2 /*return*/, validatedEvents];
1374
1530
  }
1375
1531
  });
1376
1532
  });
@@ -1393,12 +1549,12 @@ var WidgetApiImpl = /** @class */ (function () {
1393
1549
  }
1394
1550
  return undefined;
1395
1551
  }), filter(isDefined));
1396
- return concat(historyEvent$, futureEvent$);
1552
+ return concat(historyEvent$, futureEvent$).pipe(filter(isValidRoomEvent));
1397
1553
  };
1398
1554
  /** {@inheritDoc WidgetApi.sendRoomEvent} */
1399
1555
  WidgetApiImpl.prototype.sendRoomEvent = function (eventType_1, content_1) {
1400
1556
  return __awaiter(this, arguments, void 0, function (eventType, content, _a) {
1401
- var subject, subscription, _b, event_id_2, room_id_2, event_2;
1557
+ var subject, subscription, _b, event_id_1, room_id_1, event_1;
1402
1558
  var _c = _a === void 0 ? {} : _a, roomId = _c.roomId;
1403
1559
  return __generator(this, function (_d) {
1404
1560
  switch (_d.label) {
@@ -1410,15 +1566,15 @@ var WidgetApiImpl = /** @class */ (function () {
1410
1566
  _d.trys.push([1, , 4, 5]);
1411
1567
  return [4 /*yield*/, this.matrixWidgetApi.sendRoomEvent(eventType, content, roomId)];
1412
1568
  case 2:
1413
- _b = _d.sent(), event_id_2 = _b.event_id, room_id_2 = _b.room_id;
1569
+ _b = _d.sent(), event_id_1 = _b.event_id, room_id_1 = _b.room_id;
1414
1570
  return [4 /*yield*/, firstValueFrom(subject.pipe(filter(function (event) {
1415
1571
  var matrixEvent = event.detail.data;
1416
- return (matrixEvent.event_id === event_id_2 &&
1417
- matrixEvent.room_id === room_id_2);
1572
+ return (matrixEvent.event_id === event_id_1 &&
1573
+ matrixEvent.room_id === room_id_1);
1418
1574
  }), map(function (event) { return event.detail.data; })))];
1419
1575
  case 3:
1420
- event_2 = _d.sent();
1421
- return [2 /*return*/, event_2];
1576
+ event_1 = _d.sent();
1577
+ return [2 /*return*/, event_1];
1422
1578
  case 4:
1423
1579
  subscription.unsubscribe();
1424
1580
  return [7 /*endfinally*/];
@@ -1459,7 +1615,7 @@ var WidgetApiImpl = /** @class */ (function () {
1459
1615
  };
1460
1616
  /** {@inheritDoc WidgetApi.observeToDeviceMessages} */
1461
1617
  WidgetApiImpl.prototype.observeToDeviceMessages = function (eventType) {
1462
- return this.toDeviceMessages$.pipe(map(function (e) { return e.detail.data; }), filter(function (e) { return e.type === eventType; }));
1618
+ return this.toDeviceMessages$.pipe(map(function (e) { return e.detail.data; }), filter(isValidToDeviceMessageEvent), filter(function (e) { return e.type === eventType; }));
1463
1619
  };
1464
1620
  /** {@inheritDoc WidgetApi.openModal} */
1465
1621
  WidgetApiImpl.prototype.openModal = function (pathName, name, options) {
@@ -1694,4 +1850,4 @@ var WidgetApiImpl = /** @class */ (function () {
1694
1850
  return WidgetApiImpl;
1695
1851
  }());
1696
1852
 
1697
- export { ROOM_EVENT_REDACTION, STATE_EVENT_POWER_LEVELS, STATE_EVENT_ROOM_MEMBER, WIDGET_CAPABILITY_NAVIGATE, WidgetApiImpl, calculateUserPowerLevel, compareOriginServerTS, extractRawWidgetParameters, extractWidgetApiParameters, extractWidgetParameters, generateRoomTimelineCapabilities, generateWidgetRegistrationUrl, getContent, getOriginalEventId, getRoomMemberDisplayName, hasActionPower, hasRequiredWidgetParameters, hasRoomEventPower, hasStateEventPower, isRoomEvent, isStateEvent, isValidEventWithRelatesTo, isValidPowerLevelStateEvent, isValidRedactionEvent, isValidRoomMemberStateEvent, navigateToRoom, observeRedactionEvents, parseWidgetId, redactEvent, repairWidgetRegistration };
1853
+ export { ROOM_EVENT_REDACTION, STATE_EVENT_POWER_LEVELS, STATE_EVENT_ROOM_MEMBER, WIDGET_CAPABILITY_NAVIGATE, WidgetApiImpl, WidgetParameter, calculateUserPowerLevel, compareOriginServerTS, extractRawWidgetParameters, extractWidgetApiParameters, extractWidgetParameters, generateRoomTimelineCapabilities, generateWidgetRegistrationUrl, getContent, getOriginalEventId, getRoomMemberDisplayName, hasActionPower, hasRoomEventPower, hasStateEventPower, hasWidgetParameters, isRoomEvent, isStateEvent, isValidEventWithRelatesTo, isValidPowerLevelStateEvent, isValidRedactionEvent, isValidRoomEvent, isValidRoomMemberStateEvent, isValidStateEvent as isValidStateEVent, isValidToDeviceMessageEvent, makeEventFromSendStateEventResult, navigateToRoom, observeRedactionEvents, parseWidgetId, redactEvent, repairWidgetRegistration, sendStateEventWithEventResult };
package/build/index.d.ts CHANGED
@@ -11,6 +11,7 @@ import { IModalWidgetOpenRequestDataButton } from 'matrix-widget-api';
11
11
  import { IModalWidgetReturnData } from 'matrix-widget-api';
12
12
  import { IOpenIDCredentials } from 'matrix-widget-api';
13
13
  import { IRoomEvent } from 'matrix-widget-api';
14
+ import { ISendEventFromWidgetResponseData } from 'matrix-widget-api';
14
15
  import { IUploadFileActionFromWidgetResponseData } from 'matrix-widget-api';
15
16
  import { IWidget } from 'matrix-widget-api';
16
17
  import { IWidgetApiRequest } from 'matrix-widget-api';
@@ -139,14 +140,6 @@ export declare function getRoomMemberDisplayName(member: StateEvent<RoomMemberSt
139
140
  */
140
141
  export declare function hasActionPower(powerLevelStateEvent: PowerLevelsStateEvent | undefined, userId: string | undefined, action: PowerLevelsActions): boolean;
141
142
 
142
- /**
143
- * Checks whether all widget parameters were provided to the widget.
144
- *
145
- * @param widgetApi - The widget api to read the parameters from
146
- * @returns True, if all parameters were provided.
147
- */
148
- export declare function hasRequiredWidgetParameters(widgetApi: WidgetApi): boolean;
149
-
150
143
  /**
151
144
  * Check if a user has the power to send a specific room event.
152
145
  *
@@ -167,6 +160,14 @@ export declare function hasRoomEventPower(powerLevelStateEvent: PowerLevelsState
167
160
  */
168
161
  export declare function hasStateEventPower(powerLevelStateEvent: PowerLevelsStateEvent | undefined, userId: string | undefined, eventType: string): boolean;
169
162
 
163
+ /**
164
+ * Checks whether the necessary widget parameters were provided to the widget.
165
+ *
166
+ * @param widgetApi - The widget api to read the parameters from
167
+ * @returns True, if all parameters were provided.
168
+ */
169
+ export declare function hasWidgetParameters(widgetApi: WidgetApi): boolean;
170
+
170
171
  /**
171
172
  * Check if the given event is a {@link RoomEvent}.
172
173
  *
@@ -206,6 +207,14 @@ export declare function isValidPowerLevelStateEvent(event: StateEvent<unknown>):
206
207
  */
207
208
  export declare function isValidRedactionEvent(event: RoomEvent<unknown>): event is RedactionRoomEvent;
208
209
 
210
+ /**
211
+ * Check if the given value is a valid {@link RoomEvent}.
212
+ *
213
+ * @param event - The value to check
214
+ * @returns true if value is a valid room event, else false.
215
+ */
216
+ export declare function isValidRoomEvent(event: unknown): event is RoomEvent<any>;
217
+
209
218
  /**
210
219
  * Validates that `event` is has a valid structure for a
211
220
  * {@link RoomMemberStateEventContent}.
@@ -214,6 +223,29 @@ export declare function isValidRedactionEvent(event: RoomEvent<unknown>): event
214
223
  */
215
224
  export declare function isValidRoomMemberStateEvent(event: StateEvent<unknown>): event is StateEvent<RoomMemberStateEventContent>;
216
225
 
226
+ /**
227
+ * Check if the given value is a valid {@link StateEvent}.
228
+ *
229
+ * @param event - The value to check
230
+ * @returns true if value is a valid state event, else false.
231
+ */
232
+ export declare function isValidStateEVent(event: unknown): event is StateEvent<any>;
233
+
234
+ /**
235
+ * Check if the given value is a valid {@link ToDeviceMessageEvent}.
236
+ *
237
+ * @param event - The value to check
238
+ * @returns true if value is a valid to device message, else false.
239
+ */
240
+ export declare function isValidToDeviceMessageEvent(event: unknown): event is ToDeviceMessageEvent<any>;
241
+
242
+ /**
243
+ * Create a state event from the arguments.
244
+ *
245
+ * @returns A state event with current timestamp origin_server_ts.
246
+ */
247
+ export declare function makeEventFromSendStateEventResult<T>(type: string, stateKey: string, content: T, sender: string, sendResult: ISendEventFromWidgetResponseData): StateEvent<T>;
248
+
217
249
  /**
218
250
  * The membership state of a user.
219
251
  */
@@ -380,6 +412,13 @@ export declare type RoomMemberStateEventContent = {
380
412
  avatar_url?: string | null;
381
413
  };
382
414
 
415
+ /**
416
+ * Send a state event and resolve to a "virtual" state event.
417
+ *
418
+ * @returns Promise, that resolves to a state event with current timestamp origin_server_ts.
419
+ */
420
+ export declare function sendStateEventWithEventResult<T>(widgetApi: WidgetApi, type: string, stateKey: string, content: T): Promise<StateEvent<T>>;
421
+
383
422
  /**
384
423
  * The name of the power levels state event.
385
424
  */
@@ -542,7 +581,7 @@ export declare type WidgetApi = {
542
581
  sendStateEvent<T>(eventType: string, content: T, options?: {
543
582
  roomId?: string;
544
583
  stateKey?: string;
545
- }): Promise<StateEvent<T>>;
584
+ }): Promise<ISendEventFromWidgetResponseData>;
546
585
  /**
547
586
  * Receive all room events of a given type from the current room.
548
587
  *
@@ -858,7 +897,7 @@ export declare class WidgetApiImpl implements WidgetApi {
858
897
  sendStateEvent<T>(eventType: string, content: T, { roomId, stateKey }?: {
859
898
  roomId?: string;
860
899
  stateKey?: string;
861
- }): Promise<StateEvent<T>>;
900
+ }): Promise<ISendEventFromWidgetResponseData>;
862
901
  /** {@inheritDoc WidgetApi.receiveRoomEvents} */
863
902
  receiveRoomEvents<T>(eventType: string, { messageType, roomIds, }?: {
864
903
  messageType?: string;
@@ -989,6 +1028,21 @@ export declare type WidgetId = {
989
1028
  isModal: boolean;
990
1029
  };
991
1030
 
1031
+ /**
1032
+ * Enumeration of widget parameters that can be checked if they are available upon registration.
1033
+ */
1034
+ export declare enum WidgetParameter {
1035
+ UserId = "userId",
1036
+ DisplayName = "displayName",
1037
+ AvatarUrl = "avatarUrl",
1038
+ RoomId = "roomId",
1039
+ Theme = "theme",
1040
+ ClientId = "clientId",
1041
+ ClientLanguage = "clientLanguage",
1042
+ DeviceId = "deviceId",
1043
+ BaseUrl = "baseUrl"
1044
+ }
1045
+
992
1046
  /**
993
1047
  * Parameters passed from the client to the widget during initialization.
994
1048
  */
@@ -1028,6 +1082,10 @@ export declare type WidgetParameters = {
1028
1082
  * The current selected language in the client.
1029
1083
  */
1030
1084
  clientLanguage?: string;
1085
+ /**
1086
+ * The device id of the current client session..
1087
+ */
1088
+ deviceId?: string;
1031
1089
  /**
1032
1090
  * The homeserver base URL.
1033
1091
  */
@@ -1057,6 +1115,12 @@ export declare type WidgetRegistration = {
1057
1115
  * The display name of the widget.
1058
1116
  */
1059
1117
  name?: string;
1118
+ /**
1119
+ * Checks for custom widget-specific registration parameters that might be required.
1120
+ *
1121
+ * Added for backwards compatibility with old widget registrations.
1122
+ */
1123
+ requiredParameters?: WidgetParameter[];
1060
1124
  /**
1061
1125
  * The avatar URL used to display an icon on the widget.
1062
1126
  *