Pillify http and non-prefixed matrix.to links (#10277)
This commit is contained in:
parent
9b74b0f057
commit
303b878b17
5 changed files with 98 additions and 28 deletions
|
@ -18,6 +18,7 @@ import PermalinkConstructor, { PermalinkParts } from "./PermalinkConstructor";
|
|||
|
||||
export const host = "matrix.to";
|
||||
export const baseUrl = `https://${host}`;
|
||||
export const baseUrlPattern = `^(?:https?://)?${host.replace(".", "\\.")}/#/(.*)`;
|
||||
|
||||
/**
|
||||
* Generates matrix.to permalinks
|
||||
|
@ -55,11 +56,17 @@ export default class MatrixToPermalinkConstructor extends PermalinkConstructor {
|
|||
// Heavily inspired by/borrowed from the matrix-bot-sdk (with permission):
|
||||
// https://github.com/turt2live/matrix-js-bot-sdk/blob/7c4665c9a25c2c8e0fe4e509f2616505b5b66a1c/src/Permalinks.ts#L33-L61
|
||||
public parsePermalink(fullUrl: string): PermalinkParts {
|
||||
if (!fullUrl || !fullUrl.startsWith(baseUrl)) {
|
||||
if (!fullUrl) {
|
||||
throw new Error("Does not appear to be a permalink");
|
||||
}
|
||||
|
||||
const parts = fullUrl.substring(`${baseUrl}/#/`.length).split("/");
|
||||
const matches = [...fullUrl.matchAll(new RegExp(baseUrlPattern, "gi"))][0];
|
||||
|
||||
if (!matches || matches.length < 2) {
|
||||
throw new Error("Does not appear to be a permalink");
|
||||
}
|
||||
|
||||
const parts = matches[1].split("/");
|
||||
|
||||
const entity = parts[0];
|
||||
if (entity[0] === "@") {
|
||||
|
|
|
@ -48,10 +48,10 @@ export default class PermalinkConstructor {
|
|||
// https://github.com/turt2live/matrix-js-bot-sdk/blob/7c4665c9a25c2c8e0fe4e509f2616505b5b66a1c/src/Permalinks.ts#L1-L6
|
||||
export class PermalinkParts {
|
||||
public constructor(
|
||||
public readonly roomIdOrAlias: string,
|
||||
public readonly eventId: string,
|
||||
public readonly userId: string,
|
||||
public readonly viaServers: string[],
|
||||
public readonly roomIdOrAlias: string | null,
|
||||
public readonly eventId: string | null,
|
||||
public readonly userId: string | null,
|
||||
public readonly viaServers: string[] | null,
|
||||
) {}
|
||||
|
||||
public static forUser(userId: string): PermalinkParts {
|
||||
|
@ -66,11 +66,11 @@ export class PermalinkParts {
|
|||
return new PermalinkParts(roomId, eventId, null, viaServers);
|
||||
}
|
||||
|
||||
public get primaryEntityId(): string {
|
||||
public get primaryEntityId(): string | null {
|
||||
return this.roomIdOrAlias || this.userId;
|
||||
}
|
||||
|
||||
public get sigil(): string {
|
||||
return this.primaryEntityId[0];
|
||||
return this.primaryEntityId?.[0] || "?";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,10 @@ import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
|||
import { EventType } from "matrix-js-sdk/src/@types/event";
|
||||
|
||||
import { MatrixClientPeg } from "../../MatrixClientPeg";
|
||||
import MatrixToPermalinkConstructor, { baseUrl as matrixtoBaseUrl } from "./MatrixToPermalinkConstructor";
|
||||
import MatrixToPermalinkConstructor, {
|
||||
baseUrl as matrixtoBaseUrl,
|
||||
baseUrlPattern as matrixToBaseUrlPattern,
|
||||
} from "./MatrixToPermalinkConstructor";
|
||||
import PermalinkConstructor, { PermalinkParts } from "./PermalinkConstructor";
|
||||
import ElementPermalinkConstructor from "./ElementPermalinkConstructor";
|
||||
import SdkConfig from "../../SdkConfig";
|
||||
|
@ -420,8 +423,9 @@ function getPermalinkConstructor(): PermalinkConstructor {
|
|||
export function parsePermalink(fullUrl: string): PermalinkParts | null {
|
||||
try {
|
||||
const elementPrefix = SdkConfig.get("permalink_prefix");
|
||||
if (decodeURIComponent(fullUrl).startsWith(matrixtoBaseUrl)) {
|
||||
return new MatrixToPermalinkConstructor().parsePermalink(decodeURIComponent(fullUrl));
|
||||
const decodedUrl = decodeURIComponent(fullUrl);
|
||||
if (new RegExp(matrixToBaseUrlPattern, "i").test(decodedUrl)) {
|
||||
return new MatrixToPermalinkConstructor().parsePermalink(decodedUrl);
|
||||
} else if (fullUrl.startsWith("matrix:")) {
|
||||
return new MatrixSchemePermalinkConstructor().parsePermalink(fullUrl);
|
||||
} else if (elementPrefix && fullUrl.startsWith(elementPrefix)) {
|
||||
|
|
44
test/utils/permalinks/MatrixToPermalinkConstructor-test.ts
Normal file
44
test/utils/permalinks/MatrixToPermalinkConstructor-test.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
Copyright 2023 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 MatrixToPermalinkConstructor from "../../../src/utils/permalinks/MatrixToPermalinkConstructor";
|
||||
import { PermalinkParts } from "../../../src/utils/permalinks/PermalinkConstructor";
|
||||
|
||||
describe("MatrixToPermalinkConstructor", () => {
|
||||
const peramlinkConstructor = new MatrixToPermalinkConstructor();
|
||||
|
||||
describe("parsePermalink", () => {
|
||||
it.each([
|
||||
["empty URL", ""],
|
||||
["something that is not an URL", "hello"],
|
||||
["should raise an error for a non-matrix.to URL", "https://example.com/#/@user:example.com"],
|
||||
])("should raise an error for %s", (name: string, url: string) => {
|
||||
expect(() => peramlinkConstructor.parsePermalink(url)).toThrow(
|
||||
new Error("Does not appear to be a permalink"),
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["(https)", "https://matrix.to/#/@user:example.com"],
|
||||
["(http)", "http://matrix.to/#/@user:example.com"],
|
||||
["without protocol", "matrix.to/#/@user:example.com"],
|
||||
])("should parse an MXID %s", (name: string, url: string) => {
|
||||
expect(peramlinkConstructor.parsePermalink(url)).toEqual(
|
||||
new PermalinkParts(null, null, "@user:example.com", null),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -15,6 +15,7 @@ limitations under the License.
|
|||
import { Room, RoomMember, EventType, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
||||
import { PermalinkParts } from "../../../src/utils/permalinks/PermalinkConstructor";
|
||||
import {
|
||||
makeRoomPermalink,
|
||||
makeUserPermalink,
|
||||
|
@ -367,6 +368,7 @@ describe("Permalinks", function () {
|
|||
expect(result).toBe("https://matrix.to/#/@someone:example.org");
|
||||
});
|
||||
|
||||
describe("parsePermalink", () => {
|
||||
it("should correctly parse room permalinks with a via argument", () => {
|
||||
const result = parsePermalink("https://matrix.to/#/!room_id:server?via=some.org");
|
||||
expect(result?.roomIdOrAlias).toBe("!room_id:server");
|
||||
|
@ -387,4 +389,17 @@ describe("Permalinks", function () {
|
|||
expect(result?.roomIdOrAlias).toBe("!room_id:server");
|
||||
expect(result?.viaServers).toEqual(["m1.org", "m2.org"]);
|
||||
});
|
||||
|
||||
it("should correctly parse permalinks with http protocol", () => {
|
||||
expect(parsePermalink("http://matrix.to/#/@user:example.com")).toEqual(
|
||||
new PermalinkParts(null, null, "@user:example.com", null),
|
||||
);
|
||||
});
|
||||
|
||||
it("should correctly parse permalinks without protocol", () => {
|
||||
expect(parsePermalink("matrix.to/#/@user:example.com")).toEqual(
|
||||
new PermalinkParts(null, null, "@user:example.com", null),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue