Parse matrix-schemed URIs (#7453)
Co-authored-by: J. Ryan Stinnett <jryans@gmail.com> Co-authored-by: Dariusz Niemczyk <dariuszn@element.io> Co-authored-by: Timo K <toger5@hotmail.de> With this pr all href use matrix matrix.to links. As a consequence right-click copy link will always return get you a sharable matrix.to link.
This commit is contained in:
+4
-3
@@ -175,9 +175,10 @@ const transformTags: IExtendedSanitizeOptions["transformTags"] = { // custom to
|
||||
if (attribs.href) {
|
||||
attribs.target = '_blank'; // by default
|
||||
|
||||
const transformed = tryTransformPermalinkToLocalHref(attribs.href);
|
||||
if (transformed !== attribs.href || attribs.href.match(ELEMENT_URL_PATTERN)) {
|
||||
attribs.href = transformed;
|
||||
const transformed = tryTransformPermalinkToLocalHref(attribs.href); // only used to check if it is a link that can be handled locally
|
||||
if (transformed !== attribs.href || // it could be converted so handle locally symbols e.g. @user:server.tdl, matrix: and matrix.to
|
||||
attribs.href.match(ELEMENT_URL_PATTERN) // for https:vector|riot...
|
||||
) {
|
||||
delete attribs.target;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import * as sdk from '../../../index';
|
||||
import dis from '../../../dispatcher/dispatcher';
|
||||
import { MatrixClientPeg } from '../../../MatrixClientPeg';
|
||||
import FlairStore from "../../../stores/FlairStore";
|
||||
import { getPrimaryPermalinkEntity, parseAppLocalLink } from "../../../utils/permalinks/Permalinks";
|
||||
import { getPrimaryPermalinkEntity, parsePermalink } from "../../../utils/permalinks/Permalinks";
|
||||
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
||||
import { Action } from "../../../dispatcher/actions";
|
||||
import { mediaFromMxc } from "../../../customisations/Media";
|
||||
@@ -85,7 +85,7 @@ class Pill extends React.Component {
|
||||
|
||||
if (nextProps.url) {
|
||||
if (nextProps.inMessage) {
|
||||
const parts = parseAppLocalLink(nextProps.url);
|
||||
const parts = parsePermalink(nextProps.url);
|
||||
resourceId = parts.primaryEntityId; // The room/user ID
|
||||
prefix = parts.sigil; // The first character of prefix
|
||||
} else {
|
||||
|
||||
+41
-29
@@ -15,13 +15,14 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import * as linkifyjs from 'linkifyjs';
|
||||
import linkifyElement from 'linkify-element';
|
||||
import linkifyString from 'linkify-string';
|
||||
import * as linkifyjs from '@matrix-org/linkifyjs';
|
||||
import linkifyElement from '@matrix-org/linkify-element';
|
||||
import linkifyString from '@matrix-org/linkify-string';
|
||||
import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
|
||||
import { registerPlugin } from 'linkifyjs';
|
||||
import { registerCustomProtocol, registerPlugin } from '@matrix-org/linkifyjs';
|
||||
|
||||
import { baseUrl } from "./utils/permalinks/SpecPermalinkConstructor";
|
||||
//linkifyjs/src/core/fsm
|
||||
import { baseUrl } from "./utils/permalinks/MatrixToPermalinkConstructor";
|
||||
import {
|
||||
parsePermalink,
|
||||
tryTransformEntityToPermalink,
|
||||
@@ -31,7 +32,7 @@ import dis from './dispatcher/dispatcher';
|
||||
import { Action } from './dispatcher/actions';
|
||||
import { ViewUserPayload } from './dispatcher/payloads/ViewUserPayload';
|
||||
|
||||
enum Type {
|
||||
export enum Type {
|
||||
URL = "url",
|
||||
UserId = "userid",
|
||||
RoomAlias = "roomalias",
|
||||
@@ -53,41 +54,29 @@ function matrixOpaqueIdLinkifyParser({
|
||||
name: Type;
|
||||
}) {
|
||||
const {
|
||||
DOMAIN,
|
||||
DOT,
|
||||
// A generic catchall text token
|
||||
TEXT,
|
||||
// IPV4 necessity
|
||||
NUM,
|
||||
TLD,
|
||||
COLON,
|
||||
SYM,
|
||||
HYPHEN,
|
||||
UNDERSCORE,
|
||||
// because 'localhost' is tokenised to the localhost token,
|
||||
// usernames @localhost:foo.com are otherwise not matched!
|
||||
LOCALHOST,
|
||||
domain,
|
||||
} = scanner.tokens;
|
||||
|
||||
const S_START = parser.start;
|
||||
const matrixSymbol = utils.createTokenClass(name, { isLink: true });
|
||||
|
||||
const localpartTokens = [
|
||||
DOMAIN,
|
||||
// IPV4 necessity
|
||||
NUM,
|
||||
TLD,
|
||||
|
||||
// because 'localhost' is tokenised to the localhost token,
|
||||
// usernames @localhost:foo.com are otherwise not matched!
|
||||
LOCALHOST,
|
||||
SYM,
|
||||
UNDERSCORE,
|
||||
TEXT,
|
||||
];
|
||||
const domainpartTokens = [DOMAIN, NUM, TLD, LOCALHOST];
|
||||
const localpartTokens = [domain, TLD, LOCALHOST, SYM, UNDERSCORE, HYPHEN];
|
||||
const domainpartTokens = [domain, TLD, LOCALHOST, HYPHEN];
|
||||
|
||||
const INITIAL_STATE = S_START.tt(token);
|
||||
|
||||
const LOCALPART_STATE = INITIAL_STATE.tt(DOMAIN);
|
||||
const LOCALPART_STATE = INITIAL_STATE.tt(domain);
|
||||
for (const token of localpartTokens) {
|
||||
INITIAL_STATE.tt(token, LOCALPART_STATE);
|
||||
LOCALPART_STATE.tt(token, LOCALPART_STATE);
|
||||
@@ -98,7 +87,7 @@ function matrixOpaqueIdLinkifyParser({
|
||||
}
|
||||
|
||||
const DOMAINPART_STATE_DOT = LOCALPART_STATE.tt(COLON);
|
||||
const DOMAINPART_STATE = DOMAINPART_STATE_DOT.tt(DOMAIN);
|
||||
const DOMAINPART_STATE = DOMAINPART_STATE_DOT.tt(domain);
|
||||
DOMAINPART_STATE.tt(DOT, DOMAINPART_STATE_DOT);
|
||||
for (const token of domainpartTokens) {
|
||||
DOMAINPART_STATE.tt(token, DOMAINPART_STATE);
|
||||
@@ -117,6 +106,7 @@ function matrixOpaqueIdLinkifyParser({
|
||||
}
|
||||
|
||||
function onUserClick(event: MouseEvent, userId: string) {
|
||||
event.preventDefault();
|
||||
const member = new RoomMember(null, userId);
|
||||
if (!member) { return; }
|
||||
dis.dispatch<ViewUserPayload>({
|
||||
@@ -124,6 +114,7 @@ function onUserClick(event: MouseEvent, userId: string) {
|
||||
member: member,
|
||||
});
|
||||
}
|
||||
|
||||
function onAliasClick(event: MouseEvent, roomAlias: string) {
|
||||
event.preventDefault();
|
||||
dis.dispatch({
|
||||
@@ -131,6 +122,7 @@ function onAliasClick(event: MouseEvent, roomAlias: string) {
|
||||
room_alias: roomAlias,
|
||||
});
|
||||
}
|
||||
|
||||
function onGroupClick(event: MouseEvent, groupId: string) {
|
||||
event.preventDefault();
|
||||
dis.dispatch({ action: 'view_group', group_id: groupId });
|
||||
@@ -168,6 +160,19 @@ export const options = {
|
||||
onUserClick(e, permalink.userId);
|
||||
},
|
||||
};
|
||||
} else {
|
||||
// for events, rooms etc. (anything other then users)
|
||||
const localHref = tryTransformPermalinkToLocalHref(href);
|
||||
if (localHref !== href) {
|
||||
// it could be converted to a localHref -> therefore handle locally
|
||||
return {
|
||||
// @ts-ignore see https://linkify.js.org/docs/options.html
|
||||
click: function(e) {
|
||||
e.preventDefault();
|
||||
window.location.hash = localHref;
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// OK fine, it's not actually a permalink
|
||||
@@ -178,21 +183,24 @@ export const options = {
|
||||
return {
|
||||
// @ts-ignore see https://linkify.js.org/docs/options.html
|
||||
click: function(e) {
|
||||
onUserClick(e, href);
|
||||
const userId = parsePermalink(href).userId;
|
||||
onUserClick(e, userId);
|
||||
},
|
||||
};
|
||||
case Type.RoomAlias:
|
||||
return {
|
||||
// @ts-ignore see https://linkify.js.org/docs/options.html
|
||||
click: function(e) {
|
||||
onAliasClick(e, href);
|
||||
const alias = parsePermalink(href).roomIdOrAlias;
|
||||
onAliasClick(e, alias);
|
||||
},
|
||||
};
|
||||
case Type.GroupId:
|
||||
return {
|
||||
// @ts-ignore see https://linkify.js.org/docs/options.html
|
||||
click: function(e) {
|
||||
onGroupClick(e, href);
|
||||
const groupId = parsePermalink(href).groupId;
|
||||
onGroupClick(e, groupId);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -219,7 +227,9 @@ export const options = {
|
||||
if (type === Type.URL) {
|
||||
try {
|
||||
const transformed = tryTransformPermalinkToLocalHref(href);
|
||||
if (transformed !== href || decodeURIComponent(href).match(ELEMENT_URL_PATTERN)) {
|
||||
if (transformed !== href || // if it could be converted to handle locally for matrix symbols e.g. @user:server.tdl and matrix.to
|
||||
decodeURIComponent(href).match(ELEMENT_URL_PATTERN) // for https:vector|riot...
|
||||
) {
|
||||
return null;
|
||||
} else {
|
||||
return '_blank';
|
||||
@@ -266,6 +276,8 @@ registerPlugin(Type.UserId, ({ scanner, parser, utils }) => {
|
||||
});
|
||||
});
|
||||
|
||||
registerCustomProtocol("matrix", true);
|
||||
|
||||
export const linkify = linkifyjs;
|
||||
export const _linkifyElement = linkifyElement;
|
||||
export const _linkifyString = linkifyString;
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
import PermalinkConstructor, { PermalinkParts } from "./PermalinkConstructor";
|
||||
|
||||
/**
|
||||
* Generates matrix: scheme permalinks
|
||||
*/
|
||||
export default class MatrixSchemePermalinkConstructor extends PermalinkConstructor {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
private encodeEntity(entity: string): string {
|
||||
if (entity[0] === "!") {
|
||||
return `roomid/${entity.slice(1)}`;
|
||||
} else if (entity[0] === "#") {
|
||||
return `r/${entity.slice(1)}`;
|
||||
} else if (entity[0] === "@") {
|
||||
return `u/${entity.slice(1)}`;
|
||||
} else if (entity[0] === "$") {
|
||||
return `e/${entity.slice(1)}`;
|
||||
}
|
||||
|
||||
throw new Error("Cannot encode entity: " + entity);
|
||||
}
|
||||
|
||||
forEvent(roomId: string, eventId: string, serverCandidates: string[]): string {
|
||||
return `matrix:${this.encodeEntity(roomId)}` +
|
||||
`/${this.encodeEntity(eventId)}${this.encodeServerCandidates(serverCandidates)}`;
|
||||
}
|
||||
|
||||
forRoom(roomIdOrAlias: string, serverCandidates: string[]): string {
|
||||
return `matrix:${this.encodeEntity(roomIdOrAlias)}${this.encodeServerCandidates(serverCandidates)}`;
|
||||
}
|
||||
|
||||
forUser(userId: string): string {
|
||||
return `matrix:${this.encodeEntity(userId)}`;
|
||||
}
|
||||
|
||||
forGroup(groupId: string): string {
|
||||
throw new Error("Deliberately not implemented");
|
||||
}
|
||||
|
||||
forEntity(entityId: string): string {
|
||||
return `matrix:${this.encodeEntity(entityId)}`;
|
||||
}
|
||||
|
||||
isPermalinkHost(testHost: string): boolean {
|
||||
// TODO: Change API signature to accept the URL for checking
|
||||
return testHost === "";
|
||||
}
|
||||
|
||||
encodeServerCandidates(candidates: string[]) {
|
||||
if (!candidates || candidates.length === 0) return '';
|
||||
return `?via=${candidates.map(c => encodeURIComponent(c)).join("&via=")}`;
|
||||
}
|
||||
|
||||
parsePermalink(fullUrl: string): PermalinkParts {
|
||||
if (!fullUrl || !fullUrl.startsWith("matrix:")) {
|
||||
throw new Error("Does not appear to be a permalink");
|
||||
}
|
||||
|
||||
const parts = fullUrl.substring("matrix:".length).split('/');
|
||||
|
||||
const identifier = parts[0];
|
||||
const entityNoSigil = parts[1];
|
||||
if (identifier === 'u') {
|
||||
// Probably a user, no further parsing needed.
|
||||
return PermalinkParts.forUser(`@${entityNoSigil}`);
|
||||
} else if (identifier === 'r' || identifier === 'roomid') {
|
||||
const sigil = identifier === 'r' ? '#' : '!';
|
||||
|
||||
if (parts.length === 2) { // room without event permalink
|
||||
const [roomId, query = ""] = entityNoSigil.split("?");
|
||||
const via = query.split(/&?via=/g).filter(p => !!p);
|
||||
return PermalinkParts.forRoom(`${sigil}${roomId}`, via);
|
||||
}
|
||||
|
||||
if (parts[2] === 'e') { // event permalink
|
||||
const eventIdAndQuery = parts.length > 3 ? parts.slice(3).join('/') : "";
|
||||
const [eventId, query = ""] = eventIdAndQuery.split("?");
|
||||
const via = query.split(/&?via=/g).filter(p => !!p);
|
||||
return PermalinkParts.forEvent(`${sigil}${entityNoSigil}`, `$${eventId}`, via);
|
||||
}
|
||||
|
||||
throw new Error("Faulty room permalink");
|
||||
} else {
|
||||
throw new Error("Unknown entity type in permalink");
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -22,7 +22,7 @@ export const baseUrl = `https://${host}`;
|
||||
/**
|
||||
* Generates matrix.to permalinks
|
||||
*/
|
||||
export default class SpecPermalinkConstructor extends PermalinkConstructor {
|
||||
export default class MatrixToPermalinkConstructor extends PermalinkConstructor {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
@@ -23,11 +23,12 @@ import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
||||
import { MatrixClientPeg } from "../../MatrixClientPeg";
|
||||
import SpecPermalinkConstructor, { baseUrl as matrixtoBaseUrl } from "./SpecPermalinkConstructor";
|
||||
import MatrixToPermalinkConstructor, { baseUrl as matrixtoBaseUrl } from "./MatrixToPermalinkConstructor";
|
||||
import PermalinkConstructor, { PermalinkParts } from "./PermalinkConstructor";
|
||||
import ElementPermalinkConstructor from "./ElementPermalinkConstructor";
|
||||
import SdkConfig from "../../SdkConfig";
|
||||
import { ELEMENT_URL_PATTERN } from "../../linkify-matrix";
|
||||
import MatrixSchemePermalinkConstructor from "./MatrixSchemePermalinkConstructor";
|
||||
|
||||
// The maximum number of servers to pick when working out which servers
|
||||
// to add to permalinks. The servers are appended as ?via=example.org
|
||||
@@ -312,14 +313,14 @@ export function makeGroupPermalink(groupId: string): string {
|
||||
export function isPermalinkHost(host: string): boolean {
|
||||
// Always check if the permalink is a spec permalink (callers are likely to call
|
||||
// parsePermalink after this function).
|
||||
if (new SpecPermalinkConstructor().isPermalinkHost(host)) return true;
|
||||
if (new MatrixToPermalinkConstructor().isPermalinkHost(host)) return true;
|
||||
return getPermalinkConstructor().isPermalinkHost(host);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an entity (permalink, room alias, user ID, etc) into a local URL
|
||||
* if possible. If the given entity is not found to be valid enough to be converted
|
||||
* then a null value will be returned.
|
||||
* if possible. If it is already a permalink (matrix.to) it gets returned
|
||||
* unchanged.
|
||||
* @param {string} entity The entity to transform.
|
||||
* @returns {string|null} The transformed permalink or null if unable.
|
||||
*/
|
||||
@@ -327,12 +328,31 @@ export function tryTransformEntityToPermalink(entity: string): string {
|
||||
if (!entity) return null;
|
||||
|
||||
// Check to see if it is a bare entity for starters
|
||||
if (entity[0] === '#' || entity[0] === '!') return makeRoomPermalink(entity);
|
||||
{if (entity[0] === '#' || entity[0] === '!') return makeRoomPermalink(entity);}
|
||||
if (entity[0] === '@') return makeUserPermalink(entity);
|
||||
if (entity[0] === '+') return makeGroupPermalink(entity);
|
||||
|
||||
// Then try and merge it into a permalink
|
||||
return tryTransformPermalinkToLocalHref(entity);
|
||||
if (entity.slice(0, 7) === "matrix:") {
|
||||
try {
|
||||
const permalinkParts = parsePermalink(entity);
|
||||
if (permalinkParts) {
|
||||
if (permalinkParts.roomIdOrAlias) {
|
||||
const eventIdPart = permalinkParts.eventId ? `/${permalinkParts.eventId}` : '';
|
||||
let pl = matrixtoBaseUrl+`/#/${permalinkParts.roomIdOrAlias}${eventIdPart}`;
|
||||
if (permalinkParts.viaServers.length > 0) {
|
||||
pl += new MatrixToPermalinkConstructor().encodeServerCandidates(permalinkParts.viaServers);
|
||||
}
|
||||
return pl;
|
||||
} else if (permalinkParts.groupId) {
|
||||
return matrixtoBaseUrl + `/#/${permalinkParts.groupId}`;
|
||||
} else if (permalinkParts.userId) {
|
||||
return matrixtoBaseUrl + `/#/${permalinkParts.userId}`;
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -342,7 +362,7 @@ export function tryTransformEntityToPermalink(entity: string): string {
|
||||
* @returns {string} The transformed permalink or original URL if unable.
|
||||
*/
|
||||
export function tryTransformPermalinkToLocalHref(permalink: string): string {
|
||||
if (!permalink.startsWith("http:") && !permalink.startsWith("https:")) {
|
||||
if (!permalink.startsWith("http:") && !permalink.startsWith("https:") && !permalink.startsWith("matrix:")) {
|
||||
return permalink;
|
||||
}
|
||||
|
||||
@@ -364,7 +384,7 @@ export function tryTransformPermalinkToLocalHref(permalink: string): string {
|
||||
const eventIdPart = permalinkParts.eventId ? `/${permalinkParts.eventId}` : '';
|
||||
permalink = `#/room/${permalinkParts.roomIdOrAlias}${eventIdPart}`;
|
||||
if (permalinkParts.viaServers.length > 0) {
|
||||
permalink += new SpecPermalinkConstructor().encodeServerCandidates(permalinkParts.viaServers);
|
||||
permalink += new MatrixToPermalinkConstructor().encodeServerCandidates(permalinkParts.viaServers);
|
||||
}
|
||||
} else if (permalinkParts.groupId) {
|
||||
permalink = `#/group/${permalinkParts.groupId}`;
|
||||
@@ -411,13 +431,15 @@ function getPermalinkConstructor(): PermalinkConstructor {
|
||||
return new ElementPermalinkConstructor(elementPrefix);
|
||||
}
|
||||
|
||||
return new SpecPermalinkConstructor();
|
||||
return new MatrixToPermalinkConstructor();
|
||||
}
|
||||
|
||||
export function parsePermalink(fullUrl: string): PermalinkParts {
|
||||
const elementPrefix = SdkConfig.get()['permalinkPrefix'];
|
||||
if (decodeURIComponent(fullUrl).startsWith(matrixtoBaseUrl)) {
|
||||
return new SpecPermalinkConstructor().parsePermalink(decodeURIComponent(fullUrl));
|
||||
return new MatrixToPermalinkConstructor().parsePermalink(decodeURIComponent(fullUrl));
|
||||
} else if (fullUrl.startsWith("matrix:")) {
|
||||
return new MatrixSchemePermalinkConstructor().parsePermalink(fullUrl);
|
||||
} else if (elementPrefix && fullUrl.startsWith(elementPrefix)) {
|
||||
return new ElementPermalinkConstructor(elementPrefix).parsePermalink(fullUrl);
|
||||
}
|
||||
@@ -425,23 +447,6 @@ export function parsePermalink(fullUrl: string): PermalinkParts {
|
||||
return null; // not a permalink we can handle
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an app local link (`#/(user|room|group)/identifer`) to a Matrix entity
|
||||
* (room, user, group). Such links are produced by `HtmlUtils` when encountering
|
||||
* links, which calls `tryTransformPermalinkToLocalHref` in this module.
|
||||
* @param {string} localLink The app local link
|
||||
* @returns {PermalinkParts}
|
||||
*/
|
||||
export function parseAppLocalLink(localLink: string): PermalinkParts {
|
||||
try {
|
||||
const segments = localLink.replace("#/", "");
|
||||
return ElementPermalinkConstructor.parseAppRoute(segments);
|
||||
} catch (e) {
|
||||
// Ignore failures
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getServerName(userId: string): string {
|
||||
return userId.split(":").splice(1).join(":");
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
||||
import { MatrixClientPeg } from '../MatrixClientPeg';
|
||||
import SettingsStore from "../settings/SettingsStore";
|
||||
import Pill from "../components/views/elements/Pill";
|
||||
import { parseAppLocalLink } from "./permalinks/Permalinks";
|
||||
import { parsePermalink } from "./permalinks/Permalinks";
|
||||
|
||||
/**
|
||||
* Recurses depth-first through a DOM tree, converting matrix.to links
|
||||
@@ -46,7 +46,7 @@ export function pillifyLinks(nodes: ArrayLike<Element>, mxEvent: MatrixEvent, pi
|
||||
|
||||
if (node.tagName === "A" && node.getAttribute("href")) {
|
||||
const href = node.getAttribute("href");
|
||||
const parts = parseAppLocalLink(href);
|
||||
const parts = parsePermalink(href);
|
||||
// If the link is a (localised) matrix.to link, replace it with a pill
|
||||
// We don't want to pill event permalinks, so those are ignored.
|
||||
if (parts && !parts.eventId) {
|
||||
|
||||
Reference in New Issue
Block a user