diff --git a/src/commands/interface-manager/CommandException.ts b/src/commands/interface-manager/CommandException.ts deleted file mode 100644 index 88f00b33..00000000 --- a/src/commands/interface-manager/CommandException.ts +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright (C) 2023 Gnuxie - * All rights reserved. - */ - -import { randomUUID } from "crypto"; -import { CommandError, CommandResult } from "./Validation"; -import { LogService } from "matrix-bot-sdk"; - -export enum CommandExceptionKind { - /** - * This class is for exceptions that need to be reported to the user, - * but are mostly irrelevant to the developers because the behaviour is well - * understood and expected. These exceptions will never be logged to the error - * level. - */ - Known = 'Known', - /** - * This class is to be used for reporting unexpected or unknown exceptions - * that the developers need to know about. - */ - Unknown = 'Unknown', -} - -// FIXME: I wonder if we could allow message to be JSX? -// Then room references could be put into the DM and actually mean something. -export class CommandException extends CommandError { - public readonly uuid = randomUUID(); - - constructor( - public readonly exceptionKind: CommandExceptionKind, - public readonly exception: Error|unknown, - message: string) { - super(message) - this.log(); - } - - public static Result(message: string, options: { exception: Error, exceptionKind: CommandExceptionKind }): CommandResult { - return CommandResult.Err(new CommandException(options.exceptionKind, options.exception, message)); - } - - protected log(): void { - const logArguments: Parameters = ["CommandException", this.exceptionKind, this.uuid, this.message, this.exception]; - if (this.exceptionKind === CommandExceptionKind.Known) { - LogService.info(...logArguments); - } else { - LogService.error(...logArguments); - } - } -} diff --git a/src/commands/interface-manager/Permalinks.ts b/src/commands/interface-manager/Permalinks.ts deleted file mode 100644 index 152f4d14..00000000 --- a/src/commands/interface-manager/Permalinks.ts +++ /dev/null @@ -1,133 +0,0 @@ -/** - * Copyright (C) 2023 Gnuxie - * All rights reserved. - * - * This file is only really here while we wait for - * https://github.com/turt2live/matrix-bot-sdk/pull/300 - * to be merged and be used by matrix-appservice-bridge too. - * - * This file incorperates work from matrix-bot-sdk - * https://github.com/turt2live/matrix-bot-sdk - * which included the following license notice: -MIT License - -Copyright (c) 2018 - 2022 Travis Ralston - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - */ - -/** - * The parts of a permalink. - * @see Permalinks - * @category Utilities - */ -export interface PermalinkParts { - /** - * The room ID or alias the permalink references. May be undefined. - */ - roomIdOrAlias?: string; - - /** - * The user ID the permalink references. May be undefined. - */ - userId?: string; - - /** - * The event ID the permalink references. May be undefined. - */ - eventId?: string; - - /** - * The servers the permalink is routed through. - */ - viaServers: string[]; -} - -/** - * Functions for handling permalinks - * @category Utilities - */ -export class Permalinks { - private constructor() { - } - - private static encodeViaArgs(servers: string[]): string { - if (!servers || !servers.length) return ""; - - return `?via=${servers.join("&via=")}`; - } - - /** - * Creates a room permalink. - * @param {string} roomIdOrAlias The room ID or alias to create a permalink for. - * @param {string[]} viaServers The servers to route the permalink through. - * @returns {string} A room permalink. - */ - public static forRoom(roomIdOrAlias: string, viaServers: string[] = []): string { - return `https://matrix.to/#/${encodeURIComponent(roomIdOrAlias)}${Permalinks.encodeViaArgs(viaServers)}`; - } - - /** - * Creates a user permalink. - * @param {string} userId The user ID to create a permalink for. - * @returns {string} A user permalink. - */ - public static forUser(userId: string): string { - return `https://matrix.to/#/${encodeURIComponent(userId)}`; - } - - /** - * Creates an event permalink. - * @param {string} roomIdOrAlias The room ID or alias to create a permalink in. - * @param {string} eventId The event ID to reference in the permalink. - * @param {string[]} viaServers The servers to route the permalink through. - * @returns {string} An event permalink. - */ - public static forEvent(roomIdOrAlias: string, eventId: string, viaServers: string[] = []): string { - return `https://matrix.to/#/${encodeURIComponent(roomIdOrAlias)}/${encodeURIComponent(eventId)}${Permalinks.encodeViaArgs(viaServers)}`; - } - - /** - * Parses a permalink URL into usable parts. - * @param {string} matrixTo The matrix.to URL to parse. - * @returns {PermalinkParts} The parts of the permalink. - */ - public static parseUrl(matrixTo: string): PermalinkParts { - const matrixToRegexp = /^https:\/\/matrix\.to\/#\/(?[^/?]+)\/?(?[^?]+)?(?\?[^]*)?$/; - - const url = matrixToRegexp.exec(matrixTo)?.groups; - if (!url) { - throw new Error("Not a valid matrix.to URL"); - } - - const entity = decodeURIComponent(url.entity); - if (entity[0] === '@') { - return { userId: entity, roomIdOrAlias: undefined, eventId: undefined, viaServers: [] }; - } else if (entity[0] === '#' || entity[0] === '!') { - return { - userId: undefined, - roomIdOrAlias: entity, - eventId: url.eventId && decodeURIComponent(url.eventId), - viaServers: new URLSearchParams(url.query).getAll('via'), - }; - } else { - throw new Error("Unexpected entity"); - } - } -} diff --git a/src/commands/interface-manager/Validation.ts b/src/commands/interface-manager/Validation.ts deleted file mode 100644 index 3d17bd33..00000000 --- a/src/commands/interface-manager/Validation.ts +++ /dev/null @@ -1,104 +0,0 @@ -/** - * Copyright (C) 2022 Gnuxie - * All rights reserved. - * - * This file is modified and is NOT licensed under the Apache License. - * This modified file incorperates work from mjolnir - * https://github.com/matrix-org/mjolnir - * which included the following license notice: - -Copyright 2022 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. - * - * However, this file is modified and the modifications in this file - * are NOT distributed, contributed, committed, or licensed under the Apache License. - */ - -type ValidationMatchExpression = { ok?: (ok: Ok) => any, err?: (err: Err) => any}; - -const noValue = Symbol('noValue'); - -/** - * This is a utility specifically for validating user input, and reporting - * what was wrong back to the end user in a way that makes sense. - * We are trying to tell the user they did something wrong and what that is. - * This is something completely different to a normal exception, - * where we are saying to ourselves that our assumptions in our code about - * the thing we're doing are completely wrong. The user never - * should see exceptions as there is nothing they can do about it. - * - * TO be clear this is only used when the user has done something wrong - * and we need to communicate that. It is not for any other situation. - */ -export class CommandResult { - private constructor( - private readonly okValue: Ok|typeof noValue, - private readonly errValue: Err|typeof noValue, - ) { - - } - public static Ok(value: Ok): CommandResult { - return new CommandResult(value, noValue); - } - - public static Err(value: Err): CommandResult { - return new CommandResult(noValue, value); - } - - public async match(expression: ValidationMatchExpression) { - return this.okValue ? await expression.ok!(this.ok) : await expression.err!(this.err); - } - - public isOk(): boolean { - return this.okValue !== noValue; - } - - public isErr(): boolean { - return this.errValue !== noValue; - } - - public get ok(): Ok { - if (this.isOk()) { - return this.okValue as Ok; - } else { - throw new TypeError("You did not check isOk before accessing ok"); - } - } - - public get err(): Err { - if (this.isErr()) { - return this.errValue as Err; - } else { - throw new TypeError("You did not check isErr before accessing err"); - } - } -} - -export class CommandError { - public constructor( - public readonly message: string, - ) { - // nothing to do. - } - - /** - * Utility to wrap the error into a Result. - * @param message The message for the CommandError. - * @param _options This exists so that the method is extensible by subclasses. Otherwise they wouldn't be able to pass other constructor arguments through this method. - * @returns A CommandResult with a CommandError nested within. - */ - public static Result(message: string, _options = {}): CommandResult { - return CommandResult.Err(new CommandError(message)); - } -}