mirror of https://github.com/vector-im/riot-web
				
				
				
			Merge pull request #5386 from matrix-org/travis/element-permalink
Rebrand Riot -> Element in the permalink classespull/21833/head
						commit
						971ade57bc
					
				| 
						 | 
				
			
			@ -19,32 +19,32 @@ import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor";
 | 
			
		|||
/**
 | 
			
		||||
 * Generates permalinks that self-reference the running webapp
 | 
			
		||||
 */
 | 
			
		||||
export default class RiotPermalinkConstructor extends PermalinkConstructor {
 | 
			
		||||
    _riotUrl: string;
 | 
			
		||||
export default class ElementPermalinkConstructor extends PermalinkConstructor {
 | 
			
		||||
    _elementUrl: string;
 | 
			
		||||
 | 
			
		||||
    constructor(riotUrl: string) {
 | 
			
		||||
    constructor(elementUrl: string) {
 | 
			
		||||
        super();
 | 
			
		||||
        this._riotUrl = riotUrl;
 | 
			
		||||
        this._elementUrl = elementUrl;
 | 
			
		||||
 | 
			
		||||
        if (!this._riotUrl.startsWith("http:") && !this._riotUrl.startsWith("https:")) {
 | 
			
		||||
            throw new Error("Riot prefix URL does not appear to be an HTTP(S) URL");
 | 
			
		||||
        if (!this._elementUrl.startsWith("http:") && !this._elementUrl.startsWith("https:")) {
 | 
			
		||||
            throw new Error("Element prefix URL does not appear to be an HTTP(S) URL");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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 {
 | 
			
		||||
        return `${this._riotUrl}/#/room/${roomIdOrAlias}${this.encodeServerCandidates(serverCandidates)}`;
 | 
			
		||||
        return `${this._elementUrl}/#/room/${roomIdOrAlias}${this.encodeServerCandidates(serverCandidates)}`;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    forUser(userId: string): string {
 | 
			
		||||
        return `${this._riotUrl}/#/user/${userId}`;
 | 
			
		||||
        return `${this._elementUrl}/#/user/${userId}`;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    forGroup(groupId: string): string {
 | 
			
		||||
        return `${this._riotUrl}/#/group/${groupId}`;
 | 
			
		||||
        return `${this._elementUrl}/#/group/${groupId}`;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    forEntity(entityId: string): string {
 | 
			
		||||
| 
						 | 
				
			
			@ -58,7 +58,7 @@ export default class RiotPermalinkConstructor extends PermalinkConstructor {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -69,13 +69,13 @@ export default class RiotPermalinkConstructor 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
 | 
			
		||||
    // Adapted for Riot's URL format
 | 
			
		||||
    // Adapted for Element's URL format
 | 
			
		||||
    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");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
            throw new Error("URL is missing parts");
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -100,7 +100,7 @@ export default class RiotPermalinkConstructor extends PermalinkConstructor {
 | 
			
		|||
            const eventId = secondaryParts[0];
 | 
			
		||||
            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);
 | 
			
		||||
 | 
			
		||||
            return PermalinkParts.forEvent(entity, eventId, via);
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +19,7 @@ import isIp from "is-ip";
 | 
			
		|||
import * as utils from 'matrix-js-sdk/src/utils';
 | 
			
		||||
import SpecPermalinkConstructor, {baseUrl as matrixtoBaseUrl} from "./SpecPermalinkConstructor";
 | 
			
		||||
import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor";
 | 
			
		||||
import RiotPermalinkConstructor from "./RiotPermalinkConstructor";
 | 
			
		||||
import ElementPermalinkConstructor from "./ElementPermalinkConstructor";
 | 
			
		||||
import matrixLinkify from "../../linkify-matrix";
 | 
			
		||||
import SdkConfig from "../../SdkConfig";
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -325,7 +325,7 @@ export function tryTransformPermalinkToLocalHref(permalink: string): string {
 | 
			
		|||
        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 {
 | 
			
		||||
        const permalinkParts = parsePermalink(permalink);
 | 
			
		||||
        if (permalinkParts) {
 | 
			
		||||
| 
						 | 
				
			
			@ -357,7 +357,7 @@ export function getPrimaryPermalinkEntity(permalink: string): string {
 | 
			
		|||
            const m = permalink.match(matrixLinkify.VECTOR_URL_PATTERN);
 | 
			
		||||
            if (m) {
 | 
			
		||||
                // 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('#');
 | 
			
		||||
                permalinkParts = handler.parsePermalink(`http://localhost/#${entityInfo}`);
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			@ -375,20 +375,20 @@ export function getPrimaryPermalinkEntity(permalink: string): string {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
function getPermalinkConstructor(): PermalinkConstructor {
 | 
			
		||||
    const riotPrefix = SdkConfig.get()['permalinkPrefix'];
 | 
			
		||||
    if (riotPrefix && riotPrefix !== matrixtoBaseUrl) {
 | 
			
		||||
        return new RiotPermalinkConstructor(riotPrefix);
 | 
			
		||||
    const elementPrefix = SdkConfig.get()['permalinkPrefix'];
 | 
			
		||||
    if (elementPrefix && elementPrefix !== matrixtoBaseUrl) {
 | 
			
		||||
        return new ElementPermalinkConstructor(elementPrefix);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return new SpecPermalinkConstructor();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function parsePermalink(fullUrl: string): PermalinkParts {
 | 
			
		||||
    const riotPrefix = SdkConfig.get()['permalinkPrefix'];
 | 
			
		||||
    const elementPrefix = SdkConfig.get()['permalinkPrefix'];
 | 
			
		||||
    if (fullUrl.startsWith(matrixtoBaseUrl)) {
 | 
			
		||||
        return new SpecPermalinkConstructor().parsePermalink(fullUrl);
 | 
			
		||||
    } else if (riotPrefix && fullUrl.startsWith(riotPrefix)) {
 | 
			
		||||
        return new RiotPermalinkConstructor(riotPrefix).parsePermalink(fullUrl);
 | 
			
		||||
    } else if (elementPrefix && fullUrl.startsWith(elementPrefix)) {
 | 
			
		||||
        return new ElementPermalinkConstructor(elementPrefix).parsePermalink(fullUrl);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return null; // not a permalink we can handle
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue