diff options
Diffstat (limited to 'includes/external/matrix/node_modules/matrix-widget-api/lib/models')
10 files changed, 737 insertions, 0 deletions
diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/Widget.d.ts b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/Widget.d.ts new file mode 100644 index 0000000..d2278e6 --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/Widget.d.ts @@ -0,0 +1,53 @@ +import { IWidget, IWidgetData, WidgetType } from ".."; +import { ITemplateParams } from ".."; +/** + * Represents the barest form of widget. + */ +export declare class Widget { + private definition; + constructor(definition: IWidget); + /** + * The user ID who created the widget. + */ + get creatorUserId(): string; + /** + * The type of widget. + */ + get type(): WidgetType; + /** + * The ID of the widget. + */ + get id(): string; + /** + * The name of the widget, or null if not set. + */ + get name(): string | null; + /** + * The title for the widget, or null if not set. + */ + get title(): string | null; + /** + * The templated URL for the widget. + */ + get templateUrl(): string; + /** + * The origin for this widget. + */ + get origin(): string; + /** + * Whether or not the client should wait for the iframe to load. Defaults + * to true. + */ + get waitForIframeLoad(): boolean; + /** + * The raw data for the widget. This will always be defined, though + * may be empty. + */ + get rawData(): IWidgetData; + /** + * Gets a complete widget URL for the client to render. + * @param {ITemplateParams} params The template parameters. + * @returns {string} A templated URL. + */ + getCompleteUrl(params: ITemplateParams): string; +} diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/Widget.js b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/Widget.js new file mode 100644 index 0000000..4650c54 --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/Widget.js @@ -0,0 +1,134 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Widget = void 0; + +var _utils = require("./validation/utils"); + +var _ = require(".."); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } + +function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } + +/** + * Represents the barest form of widget. + */ +var Widget = /*#__PURE__*/function () { + function Widget(definition) { + _classCallCheck(this, Widget); + + this.definition = definition; + if (!this.definition) throw new Error("Definition is required"); + (0, _utils.assertPresent)(definition, "id"); + (0, _utils.assertPresent)(definition, "creatorUserId"); + (0, _utils.assertPresent)(definition, "type"); + (0, _utils.assertPresent)(definition, "url"); + } + /** + * The user ID who created the widget. + */ + + + _createClass(Widget, [{ + key: "creatorUserId", + get: function get() { + return this.definition.creatorUserId; + } + /** + * The type of widget. + */ + + }, { + key: "type", + get: function get() { + return this.definition.type; + } + /** + * The ID of the widget. + */ + + }, { + key: "id", + get: function get() { + return this.definition.id; + } + /** + * The name of the widget, or null if not set. + */ + + }, { + key: "name", + get: function get() { + return this.definition.name || null; + } + /** + * The title for the widget, or null if not set. + */ + + }, { + key: "title", + get: function get() { + return this.rawData.title || null; + } + /** + * The templated URL for the widget. + */ + + }, { + key: "templateUrl", + get: function get() { + return this.definition.url; + } + /** + * The origin for this widget. + */ + + }, { + key: "origin", + get: function get() { + return new URL(this.templateUrl).origin; + } + /** + * Whether or not the client should wait for the iframe to load. Defaults + * to true. + */ + + }, { + key: "waitForIframeLoad", + get: function get() { + if (this.definition.waitForIframeLoad === false) return false; + if (this.definition.waitForIframeLoad === true) return true; + return true; // default true + } + /** + * The raw data for the widget. This will always be defined, though + * may be empty. + */ + + }, { + key: "rawData", + get: function get() { + return this.definition.data || {}; + } + /** + * Gets a complete widget URL for the client to render. + * @param {ITemplateParams} params The template parameters. + * @returns {string} A templated URL. + */ + + }, { + key: "getCompleteUrl", + value: function getCompleteUrl(params) { + return (0, _.runTemplate)(this.templateUrl, this.definition, params); + } + }]); + + return Widget; +}(); + +exports.Widget = Widget;
\ No newline at end of file diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetEventCapability.d.ts b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetEventCapability.d.ts new file mode 100644 index 0000000..c0fbcdb --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetEventCapability.d.ts @@ -0,0 +1,31 @@ +import { Capability } from ".."; +export declare enum EventKind { + Event = "event", + State = "state_event", + ToDevice = "to_device" +} +export declare enum EventDirection { + Send = "send", + Receive = "receive" +} +export declare class WidgetEventCapability { + readonly direction: EventDirection; + readonly eventType: string; + readonly kind: EventKind; + readonly keyStr: string | null; + readonly raw: string; + private constructor(); + matchesAsStateEvent(direction: EventDirection, eventType: string, stateKey: string | null): boolean; + matchesAsToDeviceEvent(direction: EventDirection, eventType: string): boolean; + matchesAsRoomEvent(direction: EventDirection, eventType: string, msgtype?: string | null): boolean; + static forStateEvent(direction: EventDirection, eventType: string, stateKey?: string): WidgetEventCapability; + static forToDeviceEvent(direction: EventDirection, eventType: string): WidgetEventCapability; + static forRoomEvent(direction: EventDirection, eventType: string): WidgetEventCapability; + static forRoomMessageEvent(direction: EventDirection, msgtype?: string): WidgetEventCapability; + /** + * Parses a capabilities request to find all the event capability requests. + * @param {Iterable<Capability>} capabilities The capabilities requested/to parse. + * @returns {WidgetEventCapability[]} An array of event capability requests. May be empty, but never null. + */ + static findEventCapabilities(capabilities: Iterable<Capability>): WidgetEventCapability[]; +} diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetEventCapability.js b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetEventCapability.js new file mode 100644 index 0000000..337665e --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetEventCapability.js @@ -0,0 +1,253 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.WidgetEventCapability = exports.EventKind = exports.EventDirection = void 0; + +function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } + +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } + +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } + +function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } + +/* + * Copyright 2020 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +var EventKind; +exports.EventKind = EventKind; + +(function (EventKind) { + EventKind["Event"] = "event"; + EventKind["State"] = "state_event"; + EventKind["ToDevice"] = "to_device"; +})(EventKind || (exports.EventKind = EventKind = {})); + +var EventDirection; +exports.EventDirection = EventDirection; + +(function (EventDirection) { + EventDirection["Send"] = "send"; + EventDirection["Receive"] = "receive"; +})(EventDirection || (exports.EventDirection = EventDirection = {})); + +var WidgetEventCapability = /*#__PURE__*/function () { + function WidgetEventCapability(direction, eventType, kind, keyStr, raw) { + _classCallCheck(this, WidgetEventCapability); + + this.direction = direction; + this.eventType = eventType; + this.kind = kind; + this.keyStr = keyStr; + this.raw = raw; + } + + _createClass(WidgetEventCapability, [{ + key: "matchesAsStateEvent", + value: function matchesAsStateEvent(direction, eventType, stateKey) { + if (this.kind !== EventKind.State) return false; // not a state event + + if (this.direction !== direction) return false; // direction mismatch + + if (this.eventType !== eventType) return false; // event type mismatch + + if (this.keyStr === null) return true; // all state keys are allowed + + if (this.keyStr === stateKey) return true; // this state key is allowed + // Default not allowed + + return false; + } + }, { + key: "matchesAsToDeviceEvent", + value: function matchesAsToDeviceEvent(direction, eventType) { + if (this.kind !== EventKind.ToDevice) return false; // not a to-device event + + if (this.direction !== direction) return false; // direction mismatch + + if (this.eventType !== eventType) return false; // event type mismatch + // Checks passed, the event is allowed + + return true; + } + }, { + key: "matchesAsRoomEvent", + value: function matchesAsRoomEvent(direction, eventType) { + var msgtype = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; + if (this.kind !== EventKind.Event) return false; // not a room event + + if (this.direction !== direction) return false; // direction mismatch + + if (this.eventType !== eventType) return false; // event type mismatch + + if (this.eventType === "m.room.message") { + if (this.keyStr === null) return true; // all message types are allowed + + if (this.keyStr === msgtype) return true; // this message type is allowed + } else { + return true; // already passed the check for if the event is allowed + } // Default not allowed + + + return false; + } + }], [{ + key: "forStateEvent", + value: function forStateEvent(direction, eventType, stateKey) { + // TODO: Enable support for m.* namespace once the MSC lands. + // https://github.com/matrix-org/matrix-widget-api/issues/22 + eventType = eventType.replace(/#/g, '\\#'); + stateKey = stateKey !== null && stateKey !== undefined ? "#".concat(stateKey) : ''; + var str = "org.matrix.msc2762.".concat(direction, ".state_event:").concat(eventType).concat(stateKey); // cheat by sending it through the processor + + return WidgetEventCapability.findEventCapabilities([str])[0]; + } + }, { + key: "forToDeviceEvent", + value: function forToDeviceEvent(direction, eventType) { + // TODO: Enable support for m.* namespace once the MSC lands. + // https://github.com/matrix-org/matrix-widget-api/issues/56 + var str = "org.matrix.msc3819.".concat(direction, ".to_device:").concat(eventType); // cheat by sending it through the processor + + return WidgetEventCapability.findEventCapabilities([str])[0]; + } + }, { + key: "forRoomEvent", + value: function forRoomEvent(direction, eventType) { + // TODO: Enable support for m.* namespace once the MSC lands. + // https://github.com/matrix-org/matrix-widget-api/issues/22 + var str = "org.matrix.msc2762.".concat(direction, ".event:").concat(eventType); // cheat by sending it through the processor + + return WidgetEventCapability.findEventCapabilities([str])[0]; + } + }, { + key: "forRoomMessageEvent", + value: function forRoomMessageEvent(direction, msgtype) { + // TODO: Enable support for m.* namespace once the MSC lands. + // https://github.com/matrix-org/matrix-widget-api/issues/22 + msgtype = msgtype === null || msgtype === undefined ? '' : msgtype; + var str = "org.matrix.msc2762.".concat(direction, ".event:m.room.message#").concat(msgtype); // cheat by sending it through the processor + + return WidgetEventCapability.findEventCapabilities([str])[0]; + } + /** + * Parses a capabilities request to find all the event capability requests. + * @param {Iterable<Capability>} capabilities The capabilities requested/to parse. + * @returns {WidgetEventCapability[]} An array of event capability requests. May be empty, but never null. + */ + + }, { + key: "findEventCapabilities", + value: function findEventCapabilities(capabilities) { + var parsed = []; + + var _iterator = _createForOfIteratorHelper(capabilities), + _step; + + try { + for (_iterator.s(); !(_step = _iterator.n()).done;) { + var cap = _step.value; + var _direction = null; + var eventSegment = void 0; + var _kind = null; // TODO: Enable support for m.* namespace once the MSCs land. + // https://github.com/matrix-org/matrix-widget-api/issues/22 + // https://github.com/matrix-org/matrix-widget-api/issues/56 + + if (cap.startsWith("org.matrix.msc2762.send.event:")) { + _direction = EventDirection.Send; + _kind = EventKind.Event; + eventSegment = cap.substring("org.matrix.msc2762.send.event:".length); + } else if (cap.startsWith("org.matrix.msc2762.send.state_event:")) { + _direction = EventDirection.Send; + _kind = EventKind.State; + eventSegment = cap.substring("org.matrix.msc2762.send.state_event:".length); + } else if (cap.startsWith("org.matrix.msc3819.send.to_device:")) { + _direction = EventDirection.Send; + _kind = EventKind.ToDevice; + eventSegment = cap.substring("org.matrix.msc3819.send.to_device:".length); + } else if (cap.startsWith("org.matrix.msc2762.receive.event:")) { + _direction = EventDirection.Receive; + _kind = EventKind.Event; + eventSegment = cap.substring("org.matrix.msc2762.receive.event:".length); + } else if (cap.startsWith("org.matrix.msc2762.receive.state_event:")) { + _direction = EventDirection.Receive; + _kind = EventKind.State; + eventSegment = cap.substring("org.matrix.msc2762.receive.state_event:".length); + } else if (cap.startsWith("org.matrix.msc3819.receive.to_device:")) { + _direction = EventDirection.Receive; + _kind = EventKind.ToDevice; + eventSegment = cap.substring("org.matrix.msc3819.receive.to_device:".length); + } + + if (_direction === null || _kind === null || eventSegment === undefined) continue; // The capability uses `#` as a separator between event type and state key/msgtype, + // so we split on that. However, a # is also valid in either one of those so we + // join accordingly. + // Eg: `m.room.message##m.text` is "m.room.message" event with msgtype "#m.text". + + var expectingKeyStr = eventSegment.startsWith("m.room.message#") || _kind === EventKind.State; + + var _keyStr = null; + + if (eventSegment.includes('#') && expectingKeyStr) { + // Dev note: regex is difficult to write, so instead the rules are manually written + // out. This is probably just as understandable as a boring regex though, so win-win? + // Test cases: + // str eventSegment keyStr + // ------------------------------------------------------------- + // m.room.message# m.room.message <empty string> + // m.room.message#test m.room.message test + // m.room.message\# m.room.message# test + // m.room.message##test m.room.message #test + // m.room.message\##test m.room.message# test + // m.room.message\\##test m.room.message\# test + // m.room.message\\###test m.room.message\# #test + // First step: explode the string + var parts = eventSegment.split('#'); // To form the eventSegment, we'll keep finding parts of the exploded string until + // there's one that doesn't end with the escape character (\). We'll then join those + // segments together with the exploding character. We have to remember to consume the + // escape character as well. + + var idx = parts.findIndex(function (p) { + return !p.endsWith("\\"); + }); + eventSegment = parts.slice(0, idx + 1).map(function (p) { + return p.endsWith('\\') ? p.substring(0, p.length - 1) : p; + }).join('#'); // The keyStr is whatever is left over. + + _keyStr = parts.slice(idx + 1).join('#'); + } + + parsed.push(new WidgetEventCapability(_direction, eventSegment, _kind, _keyStr, cap)); + } + } catch (err) { + _iterator.e(err); + } finally { + _iterator.f(); + } + + return parsed; + } + }]); + + return WidgetEventCapability; +}(); + +exports.WidgetEventCapability = WidgetEventCapability;
\ No newline at end of file diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetParser.d.ts b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetParser.d.ts new file mode 100644 index 0000000..f0cde3b --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetParser.d.ts @@ -0,0 +1,46 @@ +import { Widget } from "./Widget"; +import { IWidget } from ".."; +export interface IStateEvent { + event_id: string; + room_id: string; + type: string; + sender: string; + origin_server_ts: number; + unsigned?: unknown; + content: unknown; + state_key: string; +} +export interface IAccountDataWidgets { + [widgetId: string]: { + type: "m.widget"; + state_key: string; + sender: string; + content: IWidget; + id?: string; + }; +} +export declare class WidgetParser { + private constructor(); + /** + * Parses widgets from the "m.widgets" account data event. This will always + * return an array, though may be empty if no valid widgets were found. + * @param {IAccountDataWidgets} content The content of the "m.widgets" account data. + * @returns {Widget[]} The widgets in account data, or an empty array. + */ + static parseAccountData(content: IAccountDataWidgets): Widget[]; + /** + * Parses all the widgets possible in the given array. This will always return + * an array, though may be empty if no widgets could be parsed. + * @param {IStateEvent[]} currentState The room state to parse. + * @returns {Widget[]} The widgets in the state, or an empty array. + */ + static parseWidgetsFromRoomState(currentState: IStateEvent[]): Widget[]; + /** + * Parses a state event into a widget. If the state event does not represent + * a widget (wrong event type, invalid widget, etc) then null is returned. + * @param {IStateEvent} stateEvent The state event. + * @returns {Widget|null} The widget, or null if invalid + */ + static parseRoomWidget(stateEvent: IStateEvent): Widget | null; + private static processEstimatedWidget; +} diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetParser.js b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetParser.js new file mode 100644 index 0000000..42c61d8 --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/WidgetParser.js @@ -0,0 +1,150 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.WidgetParser = void 0; + +var _Widget = require("./Widget"); + +var _url = require("./validation/url"); + +function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } + +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } + +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } + +function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } + +var WidgetParser = /*#__PURE__*/function () { + function WidgetParser() {// private constructor because this is a util class + + _classCallCheck(this, WidgetParser); + } + /** + * Parses widgets from the "m.widgets" account data event. This will always + * return an array, though may be empty if no valid widgets were found. + * @param {IAccountDataWidgets} content The content of the "m.widgets" account data. + * @returns {Widget[]} The widgets in account data, or an empty array. + */ + + + _createClass(WidgetParser, null, [{ + key: "parseAccountData", + value: function parseAccountData(content) { + if (!content) return []; + var result = []; + + for (var _i = 0, _Object$keys = Object.keys(content); _i < _Object$keys.length; _i++) { + var _widgetId = _Object$keys[_i]; + var roughWidget = content[_widgetId]; + if (!roughWidget) continue; + if (roughWidget.type !== "m.widget" && roughWidget.type !== "im.vector.modular.widgets") continue; + if (!roughWidget.sender) continue; + var probableWidgetId = roughWidget.state_key || roughWidget.id; + if (probableWidgetId !== _widgetId) continue; + var asStateEvent = { + content: roughWidget.content, + sender: roughWidget.sender, + type: "m.widget", + state_key: _widgetId, + event_id: "$example", + room_id: "!example", + origin_server_ts: 1 + }; + var widget = WidgetParser.parseRoomWidget(asStateEvent); + if (widget) result.push(widget); + } + + return result; + } + /** + * Parses all the widgets possible in the given array. This will always return + * an array, though may be empty if no widgets could be parsed. + * @param {IStateEvent[]} currentState The room state to parse. + * @returns {Widget[]} The widgets in the state, or an empty array. + */ + + }, { + key: "parseWidgetsFromRoomState", + value: function parseWidgetsFromRoomState(currentState) { + if (!currentState) return []; + var result = []; + + var _iterator = _createForOfIteratorHelper(currentState), + _step; + + try { + for (_iterator.s(); !(_step = _iterator.n()).done;) { + var state = _step.value; + var widget = WidgetParser.parseRoomWidget(state); + if (widget) result.push(widget); + } + } catch (err) { + _iterator.e(err); + } finally { + _iterator.f(); + } + + return result; + } + /** + * Parses a state event into a widget. If the state event does not represent + * a widget (wrong event type, invalid widget, etc) then null is returned. + * @param {IStateEvent} stateEvent The state event. + * @returns {Widget|null} The widget, or null if invalid + */ + + }, { + key: "parseRoomWidget", + value: function parseRoomWidget(stateEvent) { + if (!stateEvent) return null; // TODO: [Legacy] Remove legacy support + + if (stateEvent.type !== "m.widget" && stateEvent.type !== "im.vector.modular.widgets") { + return null; + } // Dev note: Throughout this function we have null safety to ensure that + // if the caller did not supply something useful that we don't error. This + // is done against the requirements of the interface because not everyone + // will have an interface to validate against. + + + var content = stateEvent.content || {}; // Form our best approximation of a widget with the information we have + + var estimatedWidget = { + id: stateEvent.state_key, + creatorUserId: content['creatorUserId'] || stateEvent.sender, + name: content['name'], + type: content['type'], + url: content['url'], + waitForIframeLoad: content['waitForIframeLoad'], + data: content['data'] + }; // Finally, process that widget + + return WidgetParser.processEstimatedWidget(estimatedWidget); + } + }, { + key: "processEstimatedWidget", + value: function processEstimatedWidget(widget) { + // Validate that the widget has the best chance of passing as a widget + if (!widget.id || !widget.creatorUserId || !widget.type) { + return null; + } + + if (!(0, _url.isValidUrl)(widget.url)) { + return null; + } // TODO: Validate data for known widget types + + + return new _Widget.Widget(widget); + } + }]); + + return WidgetParser; +}(); + +exports.WidgetParser = WidgetParser;
\ No newline at end of file diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/url.d.ts b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/url.d.ts new file mode 100644 index 0000000..0c6625d --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/url.d.ts @@ -0,0 +1 @@ +export declare function isValidUrl(val: string): boolean; diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/url.js b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/url.js new file mode 100644 index 0000000..1e927f9 --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/url.js @@ -0,0 +1,41 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isValidUrl = isValidUrl; + +/* + * Copyright 2020 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +function isValidUrl(val) { + if (!val) return false; // easy: not valid if not present + + try { + var parsed = new URL(val); + + if (parsed.protocol !== "http" && parsed.protocol !== "https") { + return false; + } + + return true; + } catch (e) { + if (e instanceof TypeError) { + return false; + } + + throw e; + } +}
\ No newline at end of file diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/utils.d.ts b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/utils.d.ts new file mode 100644 index 0000000..90252db --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/utils.d.ts @@ -0,0 +1 @@ +export declare function assertPresent<O>(obj: O, key: keyof O): void; diff --git a/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/utils.js b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/utils.js new file mode 100644 index 0000000..961fd14 --- /dev/null +++ b/includes/external/matrix/node_modules/matrix-widget-api/lib/models/validation/utils.js @@ -0,0 +1,27 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.assertPresent = assertPresent; + +/* + * Copyright 2020 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +function assertPresent(obj, key) { + if (!obj[key]) { + throw new Error("".concat(key, " is required")); + } +}
\ No newline at end of file |