Rework message pilling to ignore event permalinks
This removes pills from event permalinks since they hide the text associated with the link, which can cause nonsensical messages since words have been removed. In addition, this moves away from fragile regexs to more straightforward code to parse links and adds a test for this case. Regressed by https://github.com/matrix-org/matrix-react-sdk/pull/5188 Fixes https://github.com/vector-im/element-web/issues/15159
This commit is contained in:
parent
71921ad705
commit
fa3ace8473
6 changed files with 72 additions and 26 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||
Copyright 2019, 2021 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.
|
||||
|
@ -76,6 +76,10 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
|
|||
}
|
||||
|
||||
const parts = fullUrl.substring(`${this._elementUrl}/#/`.length).split("/");
|
||||
return ElementPermalinkConstructor.parseLinkParts(parts);
|
||||
}
|
||||
|
||||
static parseLinkParts(parts: string[]): PermalinkParts {
|
||||
if (parts.length < 2) { // we're expecting an entity and an ID of some kind at least
|
||||
throw new Error("URL is missing parts");
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||
Copyright 2019, 2021 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.
|
||||
|
@ -80,4 +80,12 @@ export class PermalinkParts {
|
|||
static forEvent(roomId: string, eventId: string, viaServers: string[]): PermalinkParts {
|
||||
return new PermalinkParts(roomId, eventId, null, null, viaServers || []);
|
||||
}
|
||||
|
||||
get primaryEntityId(): string {
|
||||
return this.roomIdOrAlias || this.userId || this.groupId;
|
||||
}
|
||||
|
||||
get sigil(): string {
|
||||
return this.primaryEntityId[0];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright 2019 New Vector Ltd
|
||||
Copyright 2019, 2021 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.
|
||||
|
@ -405,6 +405,23 @@ 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("#/", "").split("/");
|
||||
return ElementPermalinkConstructor.parseLinkParts(segments);
|
||||
} catch (e) {
|
||||
// Ignore failures
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getServerName(userId) {
|
||||
return userId.split(":").splice(1).join(":");
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
||||
Copyright 2019, 2020, 2021 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.
|
||||
|
@ -19,7 +19,8 @@ import ReactDOM from 'react-dom';
|
|||
import {MatrixClientPeg} from '../MatrixClientPeg';
|
||||
import SettingsStore from "../settings/SettingsStore";
|
||||
import {PushProcessor} from 'matrix-js-sdk/src/pushprocessor';
|
||||
import * as sdk from '../index';
|
||||
import Pill from "../components/views/elements/Pill";
|
||||
import { parseAppLocalLink } from "./permalinks/Permalinks";
|
||||
|
||||
/**
|
||||
* Recurses depth-first through a DOM tree, converting matrix.to links
|
||||
|
@ -43,10 +44,10 @@ export function pillifyLinks(nodes, mxEvent, pills) {
|
|||
|
||||
if (node.tagName === "A" && node.getAttribute("href")) {
|
||||
const href = node.getAttribute("href");
|
||||
|
||||
const parts = parseAppLocalLink(href);
|
||||
// If the link is a (localised) matrix.to link, replace it with a pill
|
||||
const Pill = sdk.getComponent('elements.Pill');
|
||||
if (Pill.isMessagePillUrl(href)) {
|
||||
// We don't want to pill event permalinks, so those are ignored.
|
||||
if (parts && !parts.eventId) {
|
||||
const pillContainer = document.createElement('span');
|
||||
|
||||
const pill = <Pill
|
||||
|
@ -72,8 +73,6 @@ export function pillifyLinks(nodes, mxEvent, pills) {
|
|||
// to clear the pills from the last run of pillifyLinks
|
||||
!node.parentElement.classList.contains("mx_AtRoomPill")
|
||||
) {
|
||||
const Pill = sdk.getComponent('elements.Pill');
|
||||
|
||||
let currentTextNode = node;
|
||||
const roomNotifTextNodes = [];
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue