summaryrefslogtreecommitdiff
path: root/includes/external/matrix/node_modules/matrix-widget-api/src/models
diff options
context:
space:
mode:
authorRaindropsSys <raindrops@equestria.dev>2023-11-17 23:25:29 +0100
committerRaindropsSys <raindrops@equestria.dev>2023-11-17 23:25:29 +0100
commit953ddd82e48dd206cef5ac94456549aed13b3ad5 (patch)
tree8f003106ee2e7f422e5a22d2ee04d0db302e66c0 /includes/external/matrix/node_modules/matrix-widget-api/src/models
parent62a9199846b0c07c03218703b33e8385764f42d9 (diff)
downloadpluralconnect-953ddd82e48dd206cef5ac94456549aed13b3ad5.tar.gz
pluralconnect-953ddd82e48dd206cef5ac94456549aed13b3ad5.tar.bz2
pluralconnect-953ddd82e48dd206cef5ac94456549aed13b3ad5.zip
Updated 30 files and deleted 2976 files (automated)
Diffstat (limited to 'includes/external/matrix/node_modules/matrix-widget-api/src/models')
-rw-r--r--includes/external/matrix/node_modules/matrix-widget-api/src/models/Widget.ts109
-rw-r--r--includes/external/matrix/node_modules/matrix-widget-api/src/models/WidgetEventCapability.ts204
-rw-r--r--includes/external/matrix/node_modules/matrix-widget-api/src/models/WidgetParser.ts147
-rw-r--r--includes/external/matrix/node_modules/matrix-widget-api/src/models/validation/url.ts32
-rw-r--r--includes/external/matrix/node_modules/matrix-widget-api/src/models/validation/utils.ts21
5 files changed, 0 insertions, 513 deletions
diff --git a/includes/external/matrix/node_modules/matrix-widget-api/src/models/Widget.ts b/includes/external/matrix/node_modules/matrix-widget-api/src/models/Widget.ts
deleted file mode 100644
index 0b66452..0000000
--- a/includes/external/matrix/node_modules/matrix-widget-api/src/models/Widget.ts
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * 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.
- */
-
-import { IWidget, IWidgetData, WidgetType } from "..";
-import { assertPresent } from "./validation/utils";
-import { ITemplateParams, runTemplate } from "..";
-
-/**
- * Represents the barest form of widget.
- */
-export class Widget {
- public constructor(private definition: IWidget) {
- if (!this.definition) throw new Error("Definition is required");
-
- assertPresent(definition, "id");
- assertPresent(definition, "creatorUserId");
- assertPresent(definition, "type");
- assertPresent(definition, "url");
- }
-
- /**
- * The user ID who created the widget.
- */
- public get creatorUserId(): string {
- return this.definition.creatorUserId;
- }
-
- /**
- * The type of widget.
- */
- public get type(): WidgetType {
- return this.definition.type;
- }
-
- /**
- * The ID of the widget.
- */
- public get id(): string {
- return this.definition.id;
- }
-
- /**
- * The name of the widget, or null if not set.
- */
- public get name(): string | null {
- return this.definition.name || null;
- }
-
- /**
- * The title for the widget, or null if not set.
- */
- public get title(): string | null {
- return this.rawData.title || null;
- }
-
- /**
- * The templated URL for the widget.
- */
- public get templateUrl(): string {
- return this.definition.url;
- }
-
- /**
- * The origin for this widget.
- */
- public get origin(): string {
- return new URL(this.templateUrl).origin;
- }
-
- /**
- * Whether or not the client should wait for the iframe to load. Defaults
- * to true.
- */
- public get waitForIframeLoad(): boolean {
- 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.
- */
- public get rawData(): IWidgetData {
- 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.
- */
- public getCompleteUrl(params: ITemplateParams): string {
- return runTemplate(this.templateUrl, this.definition, params);
- }
-}
diff --git a/includes/external/matrix/node_modules/matrix-widget-api/src/models/WidgetEventCapability.ts b/includes/external/matrix/node_modules/matrix-widget-api/src/models/WidgetEventCapability.ts
deleted file mode 100644
index 16d933e..0000000
--- a/includes/external/matrix/node_modules/matrix-widget-api/src/models/WidgetEventCapability.ts
+++ /dev/null
@@ -1,204 +0,0 @@
-/*
- * 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.
- */
-
-import { Capability } from "..";
-
-export enum EventKind {
- Event = "event",
- State = "state_event",
- ToDevice = "to_device",
-}
-
-export enum EventDirection {
- Send = "send",
- Receive = "receive",
-}
-
-export class WidgetEventCapability {
- private constructor(
- public readonly direction: EventDirection,
- public readonly eventType: string,
- public readonly kind: EventKind,
- public readonly keyStr: string | null,
- public readonly raw: string,
- ) {
- }
-
- public matchesAsStateEvent(direction: EventDirection, eventType: string, stateKey: string | null): boolean {
- 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;
- }
-
- public matchesAsToDeviceEvent(direction: EventDirection, eventType: string): boolean {
- 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;
- }
-
- public matchesAsRoomEvent(direction: EventDirection, eventType: string, msgtype: string | null = null): boolean {
- 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;
- }
-
- public static forStateEvent(
- direction: EventDirection,
- eventType: string,
- stateKey?: string,
- ): WidgetEventCapability {
- // 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 ? `#${stateKey}` : '';
- const str = `org.matrix.msc2762.${direction}.state_event:${eventType}${stateKey}`;
-
- // cheat by sending it through the processor
- return WidgetEventCapability.findEventCapabilities([str])[0];
- }
-
- public static forToDeviceEvent(direction: EventDirection, eventType: string): WidgetEventCapability {
- // TODO: Enable support for m.* namespace once the MSC lands.
- // https://github.com/matrix-org/matrix-widget-api/issues/56
- const str = `org.matrix.msc3819.${direction}.to_device:${eventType}`;
-
- // cheat by sending it through the processor
- return WidgetEventCapability.findEventCapabilities([str])[0];
- }
-
- public static forRoomEvent(direction: EventDirection, eventType: string): WidgetEventCapability {
- // TODO: Enable support for m.* namespace once the MSC lands.
- // https://github.com/matrix-org/matrix-widget-api/issues/22
- const str = `org.matrix.msc2762.${direction}.event:${eventType}`;
-
- // cheat by sending it through the processor
- return WidgetEventCapability.findEventCapabilities([str])[0];
- }
-
- public static forRoomMessageEvent(direction: EventDirection, msgtype?: string): WidgetEventCapability {
- // 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;
- const str = `org.matrix.msc2762.${direction}.event:m.room.message#${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.
- */
- public static findEventCapabilities(capabilities: Iterable<Capability>): WidgetEventCapability[] {
- const parsed: WidgetEventCapability[] = [];
- for (const cap of capabilities) {
- let direction: EventDirection | null = null;
- let eventSegment: string | undefined;
- let kind: EventKind | null = 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".
- const expectingKeyStr = eventSegment.startsWith("m.room.message#") || kind === EventKind.State;
- let keyStr: string | null = 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
- const 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.
- const idx = parts.findIndex(p => !p.endsWith("\\"));
- eventSegment = parts.slice(0, idx + 1)
- .map(p => 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));
- }
- return parsed;
- }
-}
diff --git a/includes/external/matrix/node_modules/matrix-widget-api/src/models/WidgetParser.ts b/includes/external/matrix/node_modules/matrix-widget-api/src/models/WidgetParser.ts
deleted file mode 100644
index f93c077..0000000
--- a/includes/external/matrix/node_modules/matrix-widget-api/src/models/WidgetParser.ts
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * 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.
- */
-
-import { Widget } from "./Widget";
-import { IWidget } from "..";
-import { isValidUrl } from "./validation/url";
-
-export interface IStateEvent {
- event_id: string; // eslint-disable-line camelcase
- room_id: string; // eslint-disable-line camelcase
- type: string;
- sender: string;
- origin_server_ts: number; // eslint-disable-line camelcase
- unsigned?: unknown;
- content: unknown;
- state_key: string; // eslint-disable-line camelcase
-}
-
-export interface IAccountDataWidgets {
- [widgetId: string]: {
- type: "m.widget";
- // the state_key is also the widget's ID
- state_key: string; // eslint-disable-line camelcase
- sender: string; // current user's ID
- content: IWidget;
- id?: string; // off-spec, but possible
- };
-}
-
-export class WidgetParser {
- private constructor() {
- // 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.
- */
- public static parseAccountData(content: IAccountDataWidgets): Widget[] {
- if (!content) return [];
-
- const result: Widget[] = [];
- for (const widgetId of Object.keys(content)) {
- const roughWidget = content[widgetId];
- if (!roughWidget) continue;
- if (roughWidget.type !== "m.widget" && roughWidget.type !== "im.vector.modular.widgets") continue;
- if (!roughWidget.sender) continue;
-
- const probableWidgetId = roughWidget.state_key || roughWidget.id;
- if (probableWidgetId !== widgetId) continue;
-
- const asStateEvent: IStateEvent = {
- content: roughWidget.content,
- sender: roughWidget.sender,
- type: "m.widget",
- state_key: widgetId,
- event_id: "$example",
- room_id: "!example",
- origin_server_ts: 1,
- };
-
- const 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.
- */
- public static parseWidgetsFromRoomState(currentState: IStateEvent[]): Widget[] {
- if (!currentState) return [];
- const result: Widget[] = [];
- for (const state of currentState) {
- const widget = WidgetParser.parseRoomWidget(state);
- if (widget) result.push(widget);
- }
- 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
- */
- public static parseRoomWidget(stateEvent: IStateEvent): Widget | null {
- 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.
-
- const content = stateEvent.content as IWidget || {};
-
- // Form our best approximation of a widget with the information we have
- const estimatedWidget: IWidget = {
- 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);
- }
-
- private static processEstimatedWidget(widget: IWidget): Widget | null {
- // Validate that the widget has the best chance of passing as a widget
- if (!widget.id || !widget.creatorUserId || !widget.type) {
- return null;
- }
- if (!isValidUrl(widget.url)) {
- return null;
- }
- // TODO: Validate data for known widget types
- return new Widget(widget);
- }
-}
diff --git a/includes/external/matrix/node_modules/matrix-widget-api/src/models/validation/url.ts b/includes/external/matrix/node_modules/matrix-widget-api/src/models/validation/url.ts
deleted file mode 100644
index c56a9c6..0000000
--- a/includes/external/matrix/node_modules/matrix-widget-api/src/models/validation/url.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * 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.
- */
-
-export function isValidUrl(val: string): boolean {
- if (!val) return false; // easy: not valid if not present
-
- try {
- const 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;
- }
-}
diff --git a/includes/external/matrix/node_modules/matrix-widget-api/src/models/validation/utils.ts b/includes/external/matrix/node_modules/matrix-widget-api/src/models/validation/utils.ts
deleted file mode 100644
index b9c8761..0000000
--- a/includes/external/matrix/node_modules/matrix-widget-api/src/models/validation/utils.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * 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.
- */
-
-export function assertPresent<O>(obj: O, key: keyof O) {
- if (!obj[key]) {
- throw new Error(`${key} is required`);
- }
-}