Files
matrix-quotes-bot/node_modules/matrix-widget-api/lib/models/WidgetEventCapability.js
unfunny dfe4cc537f init
2026-09-13 00:29:11 -04:00

258 lines
14 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.WidgetEventCapability = exports.EventKind = exports.EventDirection = void 0;
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 EventKind = /*#__PURE__*/function (EventKind) {
EventKind["Event"] = "event";
EventKind["State"] = "state_event";
EventKind["ToDevice"] = "to_device";
EventKind["RoomAccount"] = "room_account";
return EventKind;
}({});
exports.EventKind = EventKind;
var EventDirection = /*#__PURE__*/function (EventDirection) {
EventDirection["Send"] = "send";
EventDirection["Receive"] = "receive";
return 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: "matchesAsRoomAccountData",
value: function matchesAsRoomAccountData(direction, eventType) {
if (this.kind !== EventKind.RoomAccount) return false; // not room account data
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: "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];
}
}, {
key: "forRoomAccountData",
value: function forRoomAccountData(direction, eventType) {
var str = "com.beeper.capabilities.".concat(direction, ".room_account_data:").concat(eventType);
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);
} else if (cap.startsWith("com.beeper.capabilities.receive.room_account_data:")) {
_direction = EventDirection.Receive;
_kind = EventKind.RoomAccount;
eventSegment = cap.substring("com.beeper.capabilities.receive.room_account_data:".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;
//# sourceMappingURL=WidgetEventCapability.js.map