2020-11-27 17:05:20 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 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 {createEnum} from "./utils/enum.js";
|
2020-12-04 13:59:52 +01:00
|
|
|
import {orderedUnique} from "./utils/unique.js";
|
2020-11-27 17:05:20 +01:00
|
|
|
|
|
|
|
const ROOMALIAS_PATTERN = /^#([^:]*):(.+)$/;
|
|
|
|
const ROOMID_PATTERN = /^!([^:]*):(.+)$/;
|
|
|
|
const USERID_PATTERN = /^@([^:]+):(.+)$/;
|
2020-12-04 16:38:10 +01:00
|
|
|
const EVENTID_PATTERN = /^$([^:]+):(.+)$/;
|
2020-11-27 17:05:20 +01:00
|
|
|
const GROUPID_PATTERN = /^\+([^:]+):(.+)$/;
|
|
|
|
|
2020-12-02 11:06:35 +01:00
|
|
|
export const IdentifierKind = createEnum(
|
2020-11-27 17:05:20 +01:00
|
|
|
"RoomId",
|
|
|
|
"RoomAlias",
|
|
|
|
"UserId",
|
|
|
|
"GroupId",
|
|
|
|
);
|
|
|
|
|
|
|
|
function asPrefix(identifierKind) {
|
|
|
|
switch (identifierKind) {
|
|
|
|
case IdentifierKind.RoomId: return "!";
|
|
|
|
case IdentifierKind.RoomAlias: return "#";
|
|
|
|
case IdentifierKind.GroupId: return "+";
|
|
|
|
case IdentifierKind.UserId: return "@";
|
|
|
|
default: throw new Error("invalid id kind " + identifierKind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:29:12 +01:00
|
|
|
export function getLabelForLinkKind(kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case LinkKind.User: return "Start chat";
|
|
|
|
case LinkKind.Room: return "View room";
|
|
|
|
case LinkKind.Group: return "View community";
|
|
|
|
case LinkKind.Event: return "View message";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:05:20 +01:00
|
|
|
export const LinkKind = createEnum(
|
|
|
|
"Room",
|
|
|
|
"User",
|
|
|
|
"Group",
|
|
|
|
"Event"
|
|
|
|
)
|
|
|
|
|
|
|
|
export class Link {
|
2020-12-04 15:31:03 +01:00
|
|
|
static validateIdentifier(identifier) {
|
|
|
|
return !!(
|
|
|
|
USERID_PATTERN.exec(identifier) ||
|
|
|
|
ROOMALIAS_PATTERN.exec(identifier) ||
|
|
|
|
ROOMID_PATTERN.exec(identifier) ||
|
|
|
|
GROUPID_PATTERN.exec(identifier)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-02 16:06:48 +01:00
|
|
|
static parse(fragment) {
|
2020-11-27 21:28:54 +01:00
|
|
|
if (!fragment) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-04 16:38:10 +01:00
|
|
|
let [linkStr, queryParams] = fragment.split("?");
|
2020-11-27 17:05:20 +01:00
|
|
|
|
|
|
|
let viaServers = [];
|
|
|
|
if (queryParams) {
|
|
|
|
viaServers = queryParams.split("&")
|
|
|
|
.map(pair => pair.split("="))
|
|
|
|
.filter(([key, value]) => key === "via")
|
|
|
|
.map(([,value]) => value);
|
|
|
|
}
|
|
|
|
|
2020-12-04 16:38:10 +01:00
|
|
|
if (linkStr.startsWith("#/")) {
|
|
|
|
linkStr = linkStr.substr(2);
|
2020-11-27 17:05:20 +01:00
|
|
|
}
|
|
|
|
|
2020-12-04 16:38:10 +01:00
|
|
|
const [identifier, eventId] = linkStr.split("/");
|
|
|
|
|
2020-11-27 17:05:20 +01:00
|
|
|
let matches;
|
|
|
|
matches = USERID_PATTERN.exec(identifier);
|
|
|
|
if (matches) {
|
|
|
|
const server = matches[2];
|
|
|
|
const localPart = matches[1];
|
|
|
|
return new Link(viaServers, IdentifierKind.UserId, localPart, server);
|
|
|
|
}
|
|
|
|
matches = ROOMALIAS_PATTERN.exec(identifier);
|
|
|
|
if (matches) {
|
|
|
|
const server = matches[2];
|
|
|
|
const localPart = matches[1];
|
2020-12-04 16:38:10 +01:00
|
|
|
return new Link(viaServers, IdentifierKind.RoomAlias, localPart, server, eventId);
|
2020-11-27 17:05:20 +01:00
|
|
|
}
|
|
|
|
matches = ROOMID_PATTERN.exec(identifier);
|
|
|
|
if (matches) {
|
|
|
|
const server = matches[2];
|
|
|
|
const localPart = matches[1];
|
2020-12-04 16:38:10 +01:00
|
|
|
return new Link(viaServers, IdentifierKind.RoomId, localPart, server, eventId);
|
2020-11-27 17:05:20 +01:00
|
|
|
}
|
|
|
|
matches = GROUPID_PATTERN.exec(identifier);
|
|
|
|
if (matches) {
|
|
|
|
const server = matches[2];
|
|
|
|
const localPart = matches[1];
|
|
|
|
return new Link(viaServers, IdentifierKind.GroupId, localPart, server);
|
|
|
|
}
|
2020-11-27 21:28:54 +01:00
|
|
|
return null;
|
2020-11-27 17:05:20 +01:00
|
|
|
}
|
|
|
|
|
2020-12-04 16:38:10 +01:00
|
|
|
constructor(viaServers, identifierKind, localPart, server, eventId) {
|
2020-11-27 17:05:20 +01:00
|
|
|
const servers = [server];
|
|
|
|
servers.push(...viaServers);
|
|
|
|
this.servers = orderedUnique(servers);
|
|
|
|
this.identifierKind = identifierKind;
|
|
|
|
this.identifier = `${asPrefix(identifierKind)}${localPart}:${server}`;
|
2020-12-04 16:38:10 +01:00
|
|
|
this.eventId = eventId;
|
2020-11-27 17:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get kind() {
|
|
|
|
if (this.eventId) {
|
|
|
|
return LinkKind.Event;
|
|
|
|
}
|
|
|
|
switch (this.identifierKind) {
|
|
|
|
case IdentifierKind.RoomId:
|
|
|
|
case IdentifierKind.RoomAlias:
|
|
|
|
return LinkKind.Room;
|
|
|
|
case IdentifierKind.UserId:
|
|
|
|
return LinkKind.User;
|
|
|
|
case IdentifierKind.GroupId:
|
|
|
|
return LinkKind.Group;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 11:24:08 +01:00
|
|
|
|
|
|
|
equals(link) {
|
|
|
|
return link &&
|
|
|
|
link.identifier === this.identifier &&
|
|
|
|
this.servers.length === link.servers.length &&
|
|
|
|
this.servers.every((s, i) => link.servers[i] === s);
|
|
|
|
}
|
2020-12-02 15:39:33 +01:00
|
|
|
|
|
|
|
toFragment() {
|
|
|
|
if (this.eventId) {
|
|
|
|
return `/${this.identifier}/${this.eventId}`;
|
|
|
|
} else {
|
|
|
|
return `/${this.identifier}`;
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 17:05:20 +01:00
|
|
|
}
|