2017-12-10 15:50:41 +03:00
|
|
|
/*
|
2021-01-27 14:46:20 +03:00
|
|
|
Copyright 2019, 2021 The Matrix.org Foundation C.I.C.
|
2017-12-10 15:50:41 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-12-06 04:00:09 +03:00
|
|
|
import isIp from "is-ip";
|
2021-03-01 22:05:50 +03:00
|
|
|
import * as utils from "matrix-js-sdk/src/utils";
|
|
|
|
import {Room} from "matrix-js-sdk/src/models/room";
|
|
|
|
|
|
|
|
import {MatrixClientPeg} from "../../MatrixClientPeg";
|
2019-09-30 22:04:20 +03:00
|
|
|
import SpecPermalinkConstructor, {baseUrl as matrixtoBaseUrl} from "./SpecPermalinkConstructor";
|
2019-10-01 01:04:10 +03:00
|
|
|
import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor";
|
2020-11-03 07:46:48 +03:00
|
|
|
import ElementPermalinkConstructor from "./ElementPermalinkConstructor";
|
2019-11-06 12:41:14 +03:00
|
|
|
import matrixLinkify from "../../linkify-matrix";
|
2019-12-21 00:41:07 +03:00
|
|
|
import SdkConfig from "../../SdkConfig";
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2018-10-26 19:25:23 +03:00
|
|
|
// The maximum number of servers to pick when working out which servers
|
|
|
|
// to add to permalinks. The servers are appended as ?via=example.org
|
|
|
|
const MAX_SERVER_CANDIDATES = 3;
|
|
|
|
|
2018-10-27 04:47:53 +03:00
|
|
|
|
2019-02-21 17:36:37 +03:00
|
|
|
// Permalinks can have servers appended to them so that the user
|
|
|
|
// receiving them can have a fighting chance at joining the room.
|
|
|
|
// These servers are called "candidates" at this point because
|
|
|
|
// it is unclear whether they are going to be useful to actually
|
|
|
|
// join in the future.
|
|
|
|
//
|
|
|
|
// We pick 3 servers based on the following criteria:
|
|
|
|
//
|
|
|
|
// Server 1: The highest power level user in the room, provided
|
|
|
|
// they are at least PL 50. We don't calculate "what is a moderator"
|
|
|
|
// here because it is less relevant for the vast majority of rooms.
|
|
|
|
// We also want to ensure that we get an admin or high-ranking mod
|
|
|
|
// as they are less likely to leave the room. If no user happens
|
|
|
|
// to meet this criteria, we'll pick the most popular server in the
|
|
|
|
// room.
|
|
|
|
//
|
|
|
|
// Server 2: The next most popular server in the room (in user
|
|
|
|
// distribution). This cannot be the same as Server 1. If no other
|
|
|
|
// servers are available then we'll only return Server 1.
|
|
|
|
//
|
|
|
|
// Server 3: The next most popular server by user distribution. This
|
|
|
|
// has the same rules as Server 2, with the added exception that it
|
|
|
|
// must be unique from Server 1 and 2.
|
|
|
|
|
|
|
|
// Rationale for popular servers: It's hard to get rid of people when
|
|
|
|
// they keep flocking in from a particular server. Sure, the server could
|
|
|
|
// be ACL'd in the future or for some reason be evicted from the room
|
|
|
|
// however an event like that is unlikely the larger the room gets. If
|
|
|
|
// the server is ACL'd at the time of generating the link however, we
|
|
|
|
// shouldn't pick them. We also don't pick IP addresses.
|
|
|
|
|
|
|
|
// Note: we don't pick the server the room was created on because the
|
|
|
|
// homeserver should already be using that server as a last ditch attempt
|
|
|
|
// and there's less of a guarantee that the server is a resident server.
|
|
|
|
// Instead, we actively figure out which servers are likely to be residents
|
|
|
|
// in the future and try to use those.
|
|
|
|
|
|
|
|
// Note: Users receiving permalinks that happen to have all 3 potential
|
|
|
|
// servers fail them (in terms of joining) are somewhat expected to hunt
|
|
|
|
// down the person who gave them the link to ask for a participating server.
|
|
|
|
// The receiving user can then manually append the known-good server to
|
|
|
|
// the list and magically have the link work.
|
|
|
|
|
2019-02-22 13:45:43 +03:00
|
|
|
export class RoomPermalinkCreator {
|
2021-04-06 14:26:50 +03:00
|
|
|
private _room: Room;
|
|
|
|
private _roomId: string;
|
|
|
|
private _highestPlUserId: string;
|
|
|
|
private _populationMap: { [serverName: string]: number };
|
|
|
|
private _bannedHostsRegexps: RegExp[];
|
|
|
|
private _allowedHostsRegexps: RegExp[];
|
|
|
|
private _serverCandidates: string[];
|
|
|
|
private _started: boolean;
|
|
|
|
|
2019-05-13 23:12:58 +03:00
|
|
|
// We support being given a roomId as a fallback in the event the `room` object
|
|
|
|
// doesn't exist or is not healthy for us to rely on. For example, loading a
|
|
|
|
// permalink to a room which the MatrixClient doesn't know about.
|
2021-04-06 14:26:50 +03:00
|
|
|
constructor(room: Room, roomId: string = null) {
|
2019-02-21 17:36:37 +03:00
|
|
|
this._room = room;
|
2019-05-13 23:12:58 +03:00
|
|
|
this._roomId = room ? room.roomId : roomId;
|
2019-02-21 17:36:37 +03:00
|
|
|
this._highestPlUserId = null;
|
|
|
|
this._populationMap = null;
|
|
|
|
this._bannedHostsRegexps = null;
|
|
|
|
this._allowedHostsRegexps = null;
|
|
|
|
this._serverCandidates = null;
|
2019-04-09 21:01:09 +03:00
|
|
|
this._started = false;
|
2019-02-21 17:36:37 +03:00
|
|
|
|
2019-05-13 23:12:58 +03:00
|
|
|
if (!this._roomId) {
|
|
|
|
throw new Error("Failed to resolve a roomId for the permalink creator to use");
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:36:37 +03:00
|
|
|
this.onMembership = this.onMembership.bind(this);
|
|
|
|
this.onRoomState = this.onRoomState.bind(this);
|
|
|
|
}
|
2018-10-27 05:34:06 +03:00
|
|
|
|
2019-02-21 17:36:37 +03:00
|
|
|
load() {
|
2019-04-06 00:25:59 +03:00
|
|
|
if (!this._room || !this._room.currentState) {
|
|
|
|
// Under rare and unknown circumstances it is possible to have a room with no
|
|
|
|
// currentState, at least potentially at the early stages of joining a room.
|
|
|
|
// To avoid breaking everything, we'll just warn rather than throw as well as
|
|
|
|
// not bother updating the various aspects of the share link.
|
|
|
|
console.warn("Tried to load a permalink creator with no room state");
|
|
|
|
return;
|
|
|
|
}
|
2019-02-21 17:36:37 +03:00
|
|
|
this._updateAllowedServers();
|
2019-02-21 19:52:02 +03:00
|
|
|
this._updateHighestPlUser();
|
2019-02-21 17:36:37 +03:00
|
|
|
this._updatePopulationMap();
|
|
|
|
this._updateServerCandidates();
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
this.load();
|
|
|
|
this._room.on("RoomMember.membership", this.onMembership);
|
|
|
|
this._room.on("RoomState.events", this.onRoomState);
|
2019-04-09 21:01:09 +03:00
|
|
|
this._started = true;
|
2019-02-21 17:36:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2019-02-21 17:44:20 +03:00
|
|
|
this._room.removeListener("RoomMember.membership", this.onMembership);
|
|
|
|
this._room.removeListener("RoomState.events", this.onRoomState);
|
2019-04-09 21:01:09 +03:00
|
|
|
this._started = false;
|
|
|
|
}
|
|
|
|
|
2021-03-01 22:05:50 +03:00
|
|
|
get serverCandidates() {
|
|
|
|
return this._serverCandidates;
|
|
|
|
}
|
|
|
|
|
2019-04-09 21:01:09 +03:00
|
|
|
isStarted() {
|
|
|
|
return this._started;
|
2019-02-21 17:36:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
forEvent(eventId) {
|
2019-09-30 21:51:17 +03:00
|
|
|
return getPermalinkConstructor().forEvent(this._roomId, eventId, this._serverCandidates);
|
2019-02-21 17:36:37 +03:00
|
|
|
}
|
|
|
|
|
2020-12-06 13:29:11 +03:00
|
|
|
forShareableRoom() {
|
2020-12-02 02:34:43 +03:00
|
|
|
if (this._room) {
|
2020-11-28 11:50:51 +03:00
|
|
|
// Prefer to use canonical alias for permalink if possible
|
|
|
|
const alias = this._room.getCanonicalAlias();
|
2020-12-02 02:34:43 +03:00
|
|
|
if (alias) {
|
|
|
|
return getPermalinkConstructor().forRoom(alias, this._serverCandidates);
|
|
|
|
}
|
2020-11-28 11:50:51 +03:00
|
|
|
}
|
2020-12-02 02:34:43 +03:00
|
|
|
return getPermalinkConstructor().forRoom(this._roomId, this._serverCandidates);
|
2020-12-06 13:29:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
forRoom() {
|
|
|
|
return getPermalinkConstructor().forRoom(this._roomId, this._serverCandidates);
|
2019-02-21 17:36:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onRoomState(event) {
|
2019-02-21 19:51:20 +03:00
|
|
|
switch (event.getType()) {
|
|
|
|
case "m.room.server_acl":
|
|
|
|
this._updateAllowedServers();
|
2019-02-21 19:51:46 +03:00
|
|
|
this._updateHighestPlUser();
|
2019-02-21 19:51:20 +03:00
|
|
|
this._updatePopulationMap();
|
|
|
|
this._updateServerCandidates();
|
|
|
|
return;
|
|
|
|
case "m.room.power_levels":
|
|
|
|
this._updateHighestPlUser();
|
|
|
|
this._updateServerCandidates();
|
|
|
|
return;
|
2019-02-21 17:36:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onMembership(evt, member, oldMembership) {
|
|
|
|
const userId = member.userId;
|
|
|
|
const membership = member.membership;
|
|
|
|
const serverName = getServerName(userId);
|
|
|
|
const hasJoined = oldMembership !== "join" && membership === "join";
|
|
|
|
const hasLeft = oldMembership === "join" && membership !== "join";
|
|
|
|
|
|
|
|
if (hasLeft) {
|
|
|
|
this._populationMap[serverName]--;
|
|
|
|
} else if (hasJoined) {
|
|
|
|
this._populationMap[serverName]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updateHighestPlUser();
|
|
|
|
this._updateServerCandidates();
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateHighestPlUser() {
|
|
|
|
const plEvent = this._room.currentState.getStateEvents("m.room.power_levels", "");
|
2019-02-25 18:21:23 +03:00
|
|
|
if (plEvent) {
|
|
|
|
const content = plEvent.getContent();
|
|
|
|
if (content) {
|
|
|
|
const users = content.users;
|
|
|
|
if (users) {
|
|
|
|
const entries = Object.entries(users);
|
|
|
|
const allowedEntries = entries.filter(([userId]) => {
|
|
|
|
const member = this._room.getMember(userId);
|
|
|
|
if (!member || member.membership !== "join") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const serverName = getServerName(userId);
|
|
|
|
return !isHostnameIpAddress(serverName) &&
|
2019-09-30 21:51:17 +03:00
|
|
|
!isHostInRegex(serverName, this._bannedHostsRegexps) &&
|
|
|
|
isHostInRegex(serverName, this._allowedHostsRegexps);
|
2019-02-25 18:21:23 +03:00
|
|
|
});
|
|
|
|
const maxEntry = allowedEntries.reduce((max, entry) => {
|
|
|
|
return (entry[1] > max[1]) ? entry : max;
|
|
|
|
}, [null, 0]);
|
|
|
|
const [userId, powerLevel] = maxEntry;
|
|
|
|
// object wasn't empty, and max entry wasn't a demotion from the default
|
|
|
|
if (userId !== null && powerLevel >= 50) {
|
|
|
|
this._highestPlUserId = userId;
|
|
|
|
return;
|
2019-02-21 17:36:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._highestPlUserId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateAllowedServers() {
|
|
|
|
const bannedHostsRegexps = [];
|
|
|
|
let allowedHostsRegexps = [new RegExp(".*")]; // default allow everyone
|
|
|
|
if (this._room.currentState) {
|
|
|
|
const aclEvent = this._room.currentState.getStateEvents("m.room.server_acl", "");
|
|
|
|
if (aclEvent && aclEvent.getContent()) {
|
|
|
|
const getRegex = (hostname) => new RegExp("^" + utils.globToRegexp(hostname, false) + "$");
|
|
|
|
|
|
|
|
const denied = aclEvent.getContent().deny || [];
|
|
|
|
denied.forEach(h => bannedHostsRegexps.push(getRegex(h)));
|
|
|
|
|
|
|
|
const allowed = aclEvent.getContent().allow || [];
|
|
|
|
allowedHostsRegexps = []; // we don't want to use the default rule here
|
|
|
|
allowed.forEach(h => allowedHostsRegexps.push(getRegex(h)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._bannedHostsRegexps = bannedHostsRegexps;
|
|
|
|
this._allowedHostsRegexps = allowedHostsRegexps;
|
|
|
|
}
|
|
|
|
|
|
|
|
_updatePopulationMap() {
|
2019-09-30 21:51:17 +03:00
|
|
|
const populationMap: { [server: string]: number } = {};
|
2019-02-21 17:36:37 +03:00
|
|
|
for (const member of this._room.getJoinedMembers()) {
|
|
|
|
const serverName = getServerName(member.userId);
|
|
|
|
if (!populationMap[serverName]) {
|
|
|
|
populationMap[serverName] = 0;
|
|
|
|
}
|
|
|
|
populationMap[serverName]++;
|
|
|
|
}
|
|
|
|
this._populationMap = populationMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateServerCandidates() {
|
|
|
|
let candidates = [];
|
|
|
|
if (this._highestPlUserId) {
|
|
|
|
candidates.push(getServerName(this._highestPlUserId));
|
|
|
|
}
|
|
|
|
|
|
|
|
const serversByPopulation = Object.keys(this._populationMap)
|
|
|
|
.sort((a, b) => this._populationMap[b] - this._populationMap[a])
|
2019-02-21 19:52:18 +03:00
|
|
|
.filter(a => {
|
|
|
|
return !candidates.includes(a) &&
|
2019-09-30 21:51:17 +03:00
|
|
|
!isHostnameIpAddress(a) &&
|
|
|
|
!isHostInRegex(a, this._bannedHostsRegexps) &&
|
|
|
|
isHostInRegex(a, this._allowedHostsRegexps);
|
2019-02-21 19:52:18 +03:00
|
|
|
});
|
2019-02-21 17:36:37 +03:00
|
|
|
|
|
|
|
const remainingServers = serversByPopulation.slice(0, MAX_SERVER_CANDIDATES - candidates.length);
|
|
|
|
candidates = candidates.concat(remainingServers);
|
|
|
|
|
|
|
|
this._serverCandidates = candidates;
|
|
|
|
}
|
2017-12-10 15:50:41 +03:00
|
|
|
}
|
|
|
|
|
2019-10-01 05:37:24 +03:00
|
|
|
export function makeGenericPermalink(entityId: string): string {
|
|
|
|
return getPermalinkConstructor().forEntity(entityId);
|
|
|
|
}
|
|
|
|
|
2017-12-10 15:50:41 +03:00
|
|
|
export function makeUserPermalink(userId) {
|
2019-09-30 21:51:17 +03:00
|
|
|
return getPermalinkConstructor().forUser(userId);
|
2017-12-10 15:50:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function makeRoomPermalink(roomId) {
|
2019-04-04 18:53:17 +03:00
|
|
|
if (!roomId) {
|
|
|
|
throw new Error("can't permalink a falsey roomId");
|
|
|
|
}
|
|
|
|
|
2018-10-27 04:47:53 +03:00
|
|
|
// If the roomId isn't actually a room ID, don't try to list the servers.
|
|
|
|
// Aliases are already routable, and don't need extra information.
|
2019-09-30 21:51:17 +03:00
|
|
|
if (roomId[0] !== '!') return getPermalinkConstructor().forRoom(roomId, []);
|
2018-10-27 04:47:53 +03:00
|
|
|
|
2019-02-21 17:38:17 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const room = client.getRoom(roomId);
|
2019-02-22 14:00:00 +03:00
|
|
|
if (!room) {
|
2019-09-30 21:51:17 +03:00
|
|
|
return getPermalinkConstructor().forRoom(roomId, []);
|
2019-02-22 14:00:00 +03:00
|
|
|
}
|
2019-02-22 13:45:43 +03:00
|
|
|
const permalinkCreator = new RoomPermalinkCreator(room);
|
|
|
|
permalinkCreator.load();
|
|
|
|
return permalinkCreator.forRoom();
|
2017-12-10 15:50:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function makeGroupPermalink(groupId) {
|
2019-09-30 21:51:17 +03:00
|
|
|
return getPermalinkConstructor().forGroup(groupId);
|
2017-12-10 15:50:41 +03:00
|
|
|
}
|
2018-10-25 03:01:08 +03:00
|
|
|
|
2019-09-30 21:53:45 +03:00
|
|
|
export function isPermalinkHost(host: string): boolean {
|
2019-10-01 01:04:49 +03:00
|
|
|
// 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;
|
2019-09-30 21:53:45 +03:00
|
|
|
return getPermalinkConstructor().isPermalinkHost(host);
|
|
|
|
}
|
|
|
|
|
2020-03-04 23:56:58 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param {string} entity The entity to transform.
|
|
|
|
* @returns {string|null} The transformed permalink or null if unable.
|
|
|
|
*/
|
|
|
|
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] === '@') return makeUserPermalink(entity);
|
|
|
|
if (entity[0] === '+') return makeGroupPermalink(entity);
|
|
|
|
|
|
|
|
// Then try and merge it into a permalink
|
|
|
|
return tryTransformPermalinkToLocalHref(entity);
|
|
|
|
}
|
|
|
|
|
2019-10-01 05:17:54 +03:00
|
|
|
/**
|
|
|
|
* Transforms a permalink (or possible permalink) into a local URL if possible. If
|
|
|
|
* the given permalink is found to not be a permalink, it'll be returned unaltered.
|
2019-10-01 06:03:16 +03:00
|
|
|
* @param {string} permalink The permalink to try and transform.
|
|
|
|
* @returns {string} The transformed permalink or original URL if unable.
|
2019-10-01 05:17:54 +03:00
|
|
|
*/
|
|
|
|
export function tryTransformPermalinkToLocalHref(permalink: string): string {
|
|
|
|
if (!permalink.startsWith("http:") && !permalink.startsWith("https:")) {
|
|
|
|
return permalink;
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:29 +03:00
|
|
|
const m = permalink.match(matrixLinkify.ELEMENT_URL_PATTERN);
|
2019-10-01 05:17:54 +03:00
|
|
|
if (m) {
|
|
|
|
return m[1];
|
|
|
|
}
|
|
|
|
|
2020-11-03 07:46:48 +03:00
|
|
|
// A bit of a hack to convert permalinks of unknown origin to Element links
|
2019-10-01 05:17:54 +03:00
|
|
|
try {
|
|
|
|
const permalinkParts = parsePermalink(permalink);
|
|
|
|
if (permalinkParts) {
|
|
|
|
if (permalinkParts.roomIdOrAlias) {
|
|
|
|
const eventIdPart = permalinkParts.eventId ? `/${permalinkParts.eventId}` : '';
|
|
|
|
permalink = `#/room/${permalinkParts.roomIdOrAlias}${eventIdPart}`;
|
2020-09-08 12:26:37 +03:00
|
|
|
if (permalinkParts.viaServers.length > 0) {
|
2020-09-08 17:09:45 +03:00
|
|
|
permalink += new SpecPermalinkConstructor().encodeServerCandidates(permalinkParts.viaServers);
|
2020-09-08 12:26:37 +03:00
|
|
|
}
|
2019-10-01 05:17:54 +03:00
|
|
|
} else if (permalinkParts.groupId) {
|
|
|
|
permalink = `#/group/${permalinkParts.groupId}`;
|
|
|
|
} else if (permalinkParts.userId) {
|
|
|
|
permalink = `#/user/${permalinkParts.userId}`;
|
|
|
|
} // else not a valid permalink for our purposes - do not handle
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Not an href we need to care about
|
|
|
|
}
|
|
|
|
|
|
|
|
return permalink;
|
|
|
|
}
|
|
|
|
|
2019-10-01 05:27:51 +03:00
|
|
|
export function getPrimaryPermalinkEntity(permalink: string): string {
|
|
|
|
try {
|
|
|
|
let permalinkParts = parsePermalink(permalink);
|
|
|
|
|
|
|
|
// If not a permalink, try the vector patterns.
|
|
|
|
if (!permalinkParts) {
|
2020-12-21 15:46:29 +03:00
|
|
|
const m = permalink.match(matrixLinkify.ELEMENT_URL_PATTERN);
|
2019-10-01 05:27:51 +03:00
|
|
|
if (m) {
|
|
|
|
// A bit of a hack, but it gets the job done
|
2020-11-03 07:46:48 +03:00
|
|
|
const handler = new ElementPermalinkConstructor("http://localhost");
|
2019-10-01 05:27:51 +03:00
|
|
|
const entityInfo = m[1].split('#').slice(1).join('#');
|
|
|
|
permalinkParts = handler.parsePermalink(`http://localhost/#${entityInfo}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!permalinkParts) return null; // not processable
|
|
|
|
if (permalinkParts.userId) return permalinkParts.userId;
|
|
|
|
if (permalinkParts.groupId) return permalinkParts.groupId;
|
|
|
|
if (permalinkParts.roomIdOrAlias) return permalinkParts.roomIdOrAlias;
|
|
|
|
} catch (e) {
|
|
|
|
// no entity - not a permalink
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-30 21:51:17 +03:00
|
|
|
function getPermalinkConstructor(): PermalinkConstructor {
|
2020-11-03 07:46:48 +03:00
|
|
|
const elementPrefix = SdkConfig.get()['permalinkPrefix'];
|
|
|
|
if (elementPrefix && elementPrefix !== matrixtoBaseUrl) {
|
|
|
|
return new ElementPermalinkConstructor(elementPrefix);
|
2019-09-30 21:51:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new SpecPermalinkConstructor();
|
2018-10-25 03:01:08 +03:00
|
|
|
}
|
|
|
|
|
2019-10-01 01:04:10 +03:00
|
|
|
export function parsePermalink(fullUrl: string): PermalinkParts {
|
2020-11-03 07:46:48 +03:00
|
|
|
const elementPrefix = SdkConfig.get()['permalinkPrefix'];
|
2019-10-01 01:04:10 +03:00
|
|
|
if (fullUrl.startsWith(matrixtoBaseUrl)) {
|
|
|
|
return new SpecPermalinkConstructor().parsePermalink(fullUrl);
|
2020-11-03 07:46:48 +03:00
|
|
|
} else if (elementPrefix && fullUrl.startsWith(elementPrefix)) {
|
|
|
|
return new ElementPermalinkConstructor(elementPrefix).parsePermalink(fullUrl);
|
2019-10-01 01:04:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return null; // not a permalink we can handle
|
|
|
|
}
|
|
|
|
|
2021-01-27 14:46:20 +03:00
|
|
|
/**
|
|
|
|
* 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 {
|
2021-02-03 17:05:22 +03:00
|
|
|
const segments = localLink.replace("#/", "");
|
|
|
|
return ElementPermalinkConstructor.parseAppRoute(segments);
|
2021-01-27 14:46:20 +03:00
|
|
|
} catch (e) {
|
|
|
|
// Ignore failures
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:36:37 +03:00
|
|
|
function getServerName(userId) {
|
|
|
|
return userId.split(":").splice(1).join(":");
|
2018-10-26 19:22:18 +03:00
|
|
|
}
|
2018-12-06 04:00:09 +03:00
|
|
|
|
|
|
|
function getHostnameFromMatrixDomain(domain) {
|
|
|
|
if (!domain) return null;
|
2019-02-21 17:37:52 +03:00
|
|
|
return new URL(`https://${domain}`).hostname;
|
2018-12-06 04:00:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function isHostInRegex(hostname, regexps) {
|
|
|
|
hostname = getHostnameFromMatrixDomain(hostname);
|
|
|
|
if (!hostname) return true; // assumed
|
|
|
|
if (regexps.length > 0 && !regexps[0].test) throw new Error(regexps[0]);
|
|
|
|
|
|
|
|
return regexps.filter(h => h.test(hostname)).length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isHostnameIpAddress(hostname) {
|
|
|
|
hostname = getHostnameFromMatrixDomain(hostname);
|
|
|
|
if (!hostname) return false;
|
|
|
|
|
|
|
|
// is-ip doesn't want IPv6 addresses surrounded by brackets, so
|
|
|
|
// take them off.
|
|
|
|
if (hostname.startsWith("[") && hostname.endsWith("]")) {
|
|
|
|
hostname = hostname.substring(1, hostname.length - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return isIp(hostname);
|
2018-12-06 04:14:22 +03:00
|
|
|
}
|
2021-03-01 22:05:50 +03:00
|
|
|
|
|
|
|
export const calculateRoomVia = (room: Room) => {
|
|
|
|
const permalinkCreator = new RoomPermalinkCreator(room);
|
|
|
|
permalinkCreator.load();
|
|
|
|
return permalinkCreator.serverCandidates;
|
|
|
|
};
|