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)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue