"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WidgetParser = void 0; var _Widget = require("./Widget"); var _url = require("./validation/url"); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* * 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 WidgetParser = /*#__PURE__*/function () { function WidgetParser() { _classCallCheck(this, WidgetParser); } // private constructor because this is a util class /** * 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; //# sourceMappingURL=WidgetParser.js.map