Rebrand Riot -> Element in the permalink classes

pull/21833/head
Travis Ralston 2020-11-02 21:46:48 -07:00
parent e67db1063e
commit dacc7bc111
2 changed files with 24 additions and 24 deletions

View File

@ -19,32 +19,32 @@ import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor";
/** /**
* Generates permalinks that self-reference the running webapp * Generates permalinks that self-reference the running webapp
*/ */
export default class RiotPermalinkConstructor extends PermalinkConstructor { export default class ElementPermalinkConstructor extends PermalinkConstructor {
_riotUrl: string; _elementUrl: string;
constructor(riotUrl: string) { constructor(elementUrl: string) {
super(); super();
this._riotUrl = riotUrl; this._elementUrl = elementUrl;
if (!this._riotUrl.startsWith("http:") && !this._riotUrl.startsWith("https:")) { if (!this._elementUrl.startsWith("http:") && !this._elementUrl.startsWith("https:")) {
throw new Error("Riot prefix URL does not appear to be an HTTP(S) URL"); throw new Error("Element prefix URL does not appear to be an HTTP(S) URL");
} }
} }
forEvent(roomId: string, eventId: string, serverCandidates: string[]): string { forEvent(roomId: string, eventId: string, serverCandidates: string[]): string {
return `${this._riotUrl}/#/room/${roomId}/${eventId}${this.encodeServerCandidates(serverCandidates)}`; return `${this._elementUrl}/#/room/${roomId}/${eventId}${this.encodeServerCandidates(serverCandidates)}`;
} }
forRoom(roomIdOrAlias: string, serverCandidates: string[]): string { forRoom(roomIdOrAlias: string, serverCandidates: string[]): string {
return `${this._riotUrl}/#/room/${roomIdOrAlias}${this.encodeServerCandidates(serverCandidates)}`; return `${this._elementUrl}/#/room/${roomIdOrAlias}${this.encodeServerCandidates(serverCandidates)}`;
} }
forUser(userId: string): string { forUser(userId: string): string {
return `${this._riotUrl}/#/user/${userId}`; return `${this._elementUrl}/#/user/${userId}`;
} }
forGroup(groupId: string): string { forGroup(groupId: string): string {
return `${this._riotUrl}/#/group/${groupId}`; return `${this._elementUrl}/#/group/${groupId}`;
} }
forEntity(entityId: string): string { forEntity(entityId: string): string {
@ -58,7 +58,7 @@ export default class RiotPermalinkConstructor extends PermalinkConstructor {
} }
isPermalinkHost(testHost: string): boolean { isPermalinkHost(testHost: string): boolean {
const parsedUrl = new URL(this._riotUrl); const parsedUrl = new URL(this._elementUrl);
return testHost === (parsedUrl.host || parsedUrl.hostname); // one of the hosts should match return testHost === (parsedUrl.host || parsedUrl.hostname); // one of the hosts should match
} }
@ -69,13 +69,13 @@ export default class RiotPermalinkConstructor extends PermalinkConstructor {
// Heavily inspired by/borrowed from the matrix-bot-sdk (with permission): // 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 // https://github.com/turt2live/matrix-js-bot-sdk/blob/7c4665c9a25c2c8e0fe4e509f2616505b5b66a1c/src/Permalinks.ts#L33-L61
// Adapted for Riot's URL format // Adapted for Element's URL format
parsePermalink(fullUrl: string): PermalinkParts { parsePermalink(fullUrl: string): PermalinkParts {
if (!fullUrl || !fullUrl.startsWith(this._riotUrl)) { if (!fullUrl || !fullUrl.startsWith(this._elementUrl)) {
throw new Error("Does not appear to be a permalink"); throw new Error("Does not appear to be a permalink");
} }
const parts = fullUrl.substring(`${this._riotUrl}/#/`.length).split("/"); const parts = fullUrl.substring(`${this._elementUrl}/#/`.length).split("/");
if (parts.length < 2) { // we're expecting an entity and an ID of some kind at least if (parts.length < 2) { // we're expecting an entity and an ID of some kind at least
throw new Error("URL is missing parts"); throw new Error("URL is missing parts");
} }
@ -100,7 +100,7 @@ export default class RiotPermalinkConstructor extends PermalinkConstructor {
const eventId = secondaryParts[0]; const eventId = secondaryParts[0];
const query = secondaryParts.length > 1 ? secondaryParts[1] : ""; const query = secondaryParts.length > 1 ? secondaryParts[1] : "";
// TODO: Verify Riot works with via args // TODO: Verify Element works with via args
const via = query.split("via=").filter(p => !!p); const via = query.split("via=").filter(p => !!p);
return PermalinkParts.forEvent(entity, eventId, via); return PermalinkParts.forEvent(entity, eventId, via);

View File

@ -19,7 +19,7 @@ import isIp from "is-ip";
import * as utils from 'matrix-js-sdk/src/utils'; import * as utils from 'matrix-js-sdk/src/utils';
import SpecPermalinkConstructor, {baseUrl as matrixtoBaseUrl} from "./SpecPermalinkConstructor"; import SpecPermalinkConstructor, {baseUrl as matrixtoBaseUrl} from "./SpecPermalinkConstructor";
import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor"; import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor";
import RiotPermalinkConstructor from "./RiotPermalinkConstructor"; import ElementPermalinkConstructor from "./ElementPermalinkConstructor";
import matrixLinkify from "../../linkify-matrix"; import matrixLinkify from "../../linkify-matrix";
import SdkConfig from "../../SdkConfig"; import SdkConfig from "../../SdkConfig";
@ -325,7 +325,7 @@ export function tryTransformPermalinkToLocalHref(permalink: string): string {
return m[1]; return m[1];
} }
// A bit of a hack to convert permalinks of unknown origin to Riot links // A bit of a hack to convert permalinks of unknown origin to Element links
try { try {
const permalinkParts = parsePermalink(permalink); const permalinkParts = parsePermalink(permalink);
if (permalinkParts) { if (permalinkParts) {
@ -357,7 +357,7 @@ export function getPrimaryPermalinkEntity(permalink: string): string {
const m = permalink.match(matrixLinkify.VECTOR_URL_PATTERN); const m = permalink.match(matrixLinkify.VECTOR_URL_PATTERN);
if (m) { if (m) {
// A bit of a hack, but it gets the job done // A bit of a hack, but it gets the job done
const handler = new RiotPermalinkConstructor("http://localhost"); const handler = new ElementPermalinkConstructor("http://localhost");
const entityInfo = m[1].split('#').slice(1).join('#'); const entityInfo = m[1].split('#').slice(1).join('#');
permalinkParts = handler.parsePermalink(`http://localhost/#${entityInfo}`); permalinkParts = handler.parsePermalink(`http://localhost/#${entityInfo}`);
} }
@ -375,20 +375,20 @@ export function getPrimaryPermalinkEntity(permalink: string): string {
} }
function getPermalinkConstructor(): PermalinkConstructor { function getPermalinkConstructor(): PermalinkConstructor {
const riotPrefix = SdkConfig.get()['permalinkPrefix']; const elementPrefix = SdkConfig.get()['permalinkPrefix'];
if (riotPrefix && riotPrefix !== matrixtoBaseUrl) { if (elementPrefix && elementPrefix !== matrixtoBaseUrl) {
return new RiotPermalinkConstructor(riotPrefix); return new ElementPermalinkConstructor(elementPrefix);
} }
return new SpecPermalinkConstructor(); return new SpecPermalinkConstructor();
} }
export function parsePermalink(fullUrl: string): PermalinkParts { export function parsePermalink(fullUrl: string): PermalinkParts {
const riotPrefix = SdkConfig.get()['permalinkPrefix']; const elementPrefix = SdkConfig.get()['permalinkPrefix'];
if (fullUrl.startsWith(matrixtoBaseUrl)) { if (fullUrl.startsWith(matrixtoBaseUrl)) {
return new SpecPermalinkConstructor().parsePermalink(fullUrl); return new SpecPermalinkConstructor().parsePermalink(fullUrl);
} else if (riotPrefix && fullUrl.startsWith(riotPrefix)) { } else if (elementPrefix && fullUrl.startsWith(elementPrefix)) {
return new RiotPermalinkConstructor(riotPrefix).parsePermalink(fullUrl); return new ElementPermalinkConstructor(elementPrefix).parsePermalink(fullUrl);
} }
return null; // not a permalink we can handle return null; // not a permalink we can handle