Remove federtaed argument
This commit is contained in:
parent
14e22d41dc
commit
5b2c36833c
3 changed files with 205 additions and 227 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
/* eslint-disable no-fallthrough */
|
||||||
|
|
||||||
import {
|
import {
|
||||||
parseHash,
|
parseHash,
|
||||||
parsePermalink,
|
parsePermalink,
|
||||||
|
@ -7,59 +9,43 @@ import {
|
||||||
toURL,
|
toURL,
|
||||||
} from "./parser";
|
} from "./parser";
|
||||||
|
|
||||||
import { LinkDiscriminator } from "./types";
|
import { LinkKind } from "./types";
|
||||||
|
|
||||||
function identifierType(id: string) {
|
const identifierType = (id: string): LinkKind =>
|
||||||
return identifyTypeFromRegex(id, verifiers, LinkDiscriminator.ParseFailed);
|
identifyTypeFromRegex(id, verifiers, LinkKind.ParseFailed);
|
||||||
}
|
|
||||||
|
|
||||||
it("types identifiers correctly", () => {
|
it("types identifiers correctly", () => {
|
||||||
expect(identifierType("@user:matrix.org")).toEqual(LinkDiscriminator.UserId);
|
expect(identifierType("@user:matrix.org")).toEqual(LinkKind.UserId);
|
||||||
expect(identifierType("!room:matrix.org")).toEqual(LinkDiscriminator.RoomId);
|
expect(identifierType("!room:matrix.org")).toEqual(LinkKind.RoomId);
|
||||||
expect(identifierType("!somewhere:example.org/$event:example.org")).toEqual(
|
expect(identifierType("!somewhere:example.org/$event:example.org")).toEqual(
|
||||||
LinkDiscriminator.Permalink
|
LinkKind.Permalink
|
||||||
);
|
);
|
||||||
expect(identifierType("+group:matrix.org")).toEqual(
|
expect(identifierType("+group:matrix.org")).toEqual(LinkKind.GroupId);
|
||||||
LinkDiscriminator.GroupId
|
expect(identifierType("#alias:matrix.org")).toEqual(LinkKind.Alias);
|
||||||
);
|
|
||||||
expect(identifierType("#alias:matrix.org")).toEqual(LinkDiscriminator.Alias);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("types garbage as such", () => {
|
it("types garbage as such", () => {
|
||||||
expect(identifierType("sdfa;fdlkja")).toEqual(LinkDiscriminator.ParseFailed);
|
expect(identifierType("sdfa;fdlkja")).toEqual(LinkKind.ParseFailed);
|
||||||
expect(identifierType("$event$matrix.org")).toEqual(
|
expect(identifierType("$event$matrix.org")).toEqual(LinkKind.ParseFailed);
|
||||||
LinkDiscriminator.ParseFailed
|
expect(identifierType("/user:matrix.org")).toEqual(LinkKind.ParseFailed);
|
||||||
);
|
|
||||||
expect(identifierType("/user:matrix.org")).toEqual(
|
|
||||||
LinkDiscriminator.ParseFailed
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("parses vias", () => {
|
it("parses args correctly", () => {
|
||||||
expect(
|
expect(
|
||||||
parseArgs("via=example.org&via=alt.example.org")
|
parseArgs("via=example.org&via=alt.example.org")
|
||||||
).toHaveProperty("vias", ["example.org", "alt.example.org"]);
|
).toHaveProperty("vias", ["example.org", "alt.example.org"]);
|
||||||
});
|
|
||||||
|
|
||||||
it("parses sharer", () => {
|
|
||||||
expect(parseArgs("sharer=blah")).toHaveProperty("sharer", "blah");
|
expect(parseArgs("sharer=blah")).toHaveProperty("sharer", "blah");
|
||||||
});
|
|
||||||
|
|
||||||
it("parses client", () => {
|
|
||||||
expect(parseArgs("client=blah.com")).toHaveProperty("client", "blah.com");
|
expect(parseArgs("client=blah.com")).toHaveProperty("client", "blah.com");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("parses federated", () => {
|
|
||||||
expect(parseArgs("federated=true")).toHaveProperty("federated", true);
|
|
||||||
expect(parseArgs("federated=false")).toHaveProperty("federated", false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("parses permalinks", () => {
|
it("parses permalinks", () => {
|
||||||
expect(parsePermalink("!somewhere:example.org/$event:example.org")).toEqual({
|
expect(parsePermalink("!somewhere:example.org/$event:example.org")).toEqual(
|
||||||
roomKind: LinkDiscriminator.RoomId,
|
{
|
||||||
|
roomKind: LinkKind.RoomId,
|
||||||
roomLink: "!somewhere:example.org",
|
roomLink: "!somewhere:example.org",
|
||||||
eventId: "$event:example.org",
|
eventId: "$event:example.org",
|
||||||
});
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("formats links correctly", () => {
|
it("formats links correctly", () => {
|
||||||
|
@ -70,7 +56,7 @@ it("formats links correctly", () => {
|
||||||
const parse = parseHash(bigLink);
|
const parse = parseHash(bigLink);
|
||||||
|
|
||||||
switch (parse.kind) {
|
switch (parse.kind) {
|
||||||
case LinkDiscriminator.ParseFailed:
|
case LinkKind.ParseFailed:
|
||||||
fail("Parse failed");
|
fail("Parse failed");
|
||||||
default:
|
default:
|
||||||
expect(toURL(origin, parse).toString()).toEqual(prefix + bigLink);
|
expect(toURL(origin, parse).toString()).toEqual(prefix + bigLink);
|
||||||
|
|
|
@ -1,13 +1,98 @@
|
||||||
import forEach from "lodash/forEach";
|
import forEach from "lodash/forEach";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
LinkDiscriminator,
|
LinkKind,
|
||||||
SafeLink,
|
SafeLink,
|
||||||
Link,
|
Link,
|
||||||
LinkContent,
|
LinkContent,
|
||||||
Arguments,
|
Arguments,
|
||||||
|
Permalink,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Verifiers are regexes which will match valid
|
||||||
|
* identifiers to their type. (This is a lie, they
|
||||||
|
* can return anything)
|
||||||
|
*/
|
||||||
|
type Verifier<A> = [RegExp, A];
|
||||||
|
export const roomVerifiers: Verifier<LinkKind.Alias | LinkKind.RoomId>[] = [
|
||||||
|
[/^#([^/:]+?):(.+)$/, LinkKind.Alias],
|
||||||
|
[/^!([^/:]+?):(.+)$/, LinkKind.RoomId],
|
||||||
|
];
|
||||||
|
export const verifiers: Verifier<LinkKind>[] = [
|
||||||
|
[/^[!#]([^/:]+?):(.+?)\/\$([^/:]+?):(.+?)$/, LinkKind.Permalink],
|
||||||
|
[/^@([^/:]+?):(.+)$/, LinkKind.UserId],
|
||||||
|
[/^\+([^/:]+?):(.+)$/, LinkKind.GroupId],
|
||||||
|
...roomVerifiers,
|
||||||
|
];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* identifyTypeFromRegex applies the verifiers to the identifier and
|
||||||
|
* returns the identifier's type
|
||||||
|
*/
|
||||||
|
export function identifyTypeFromRegex<T, F>(
|
||||||
|
identifier: string,
|
||||||
|
verifiers: Verifier<T>[],
|
||||||
|
fail: F
|
||||||
|
): T | F {
|
||||||
|
if (identifier !== encodeURI(identifier)) {
|
||||||
|
return fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
return verifiers.reduce<T | F>((kind, verifier) => {
|
||||||
|
if (kind !== fail) {
|
||||||
|
return kind;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (identifier.match(verifier[0])) {
|
||||||
|
return verifier[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return kind;
|
||||||
|
}, fail);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Parses a permalink.
|
||||||
|
* Assumes the permalink is correct.
|
||||||
|
*/
|
||||||
|
export function parsePermalink(identifier: string): Permalink {
|
||||||
|
const [roomLink, eventId] = identifier.split("/");
|
||||||
|
const roomKind = identifyTypeFromRegex(
|
||||||
|
roomLink,
|
||||||
|
roomVerifiers,
|
||||||
|
// This is hacky but we're assuming identifier is a valid permalink
|
||||||
|
LinkKind.Alias
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
roomKind,
|
||||||
|
roomLink,
|
||||||
|
eventId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Repalces null with undefined
|
||||||
|
*/
|
||||||
|
function bottomExchange<T>(nullable: T | null): T | undefined {
|
||||||
|
if (nullable === null) return undefined;
|
||||||
|
return nullable;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* parseArgs parses the <extra args> part of matrix.to links
|
||||||
|
*/
|
||||||
|
export function parseArgs(args: string): Arguments {
|
||||||
|
const params = new URLSearchParams(args);
|
||||||
|
|
||||||
|
return {
|
||||||
|
vias: params.getAll("via"),
|
||||||
|
client: bottomExchange(params.get("client")),
|
||||||
|
sharer: bottomExchange(params.get("sharer")),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* parseLink takes a striped matrix.to hash link (without the '#/' prefix)
|
* parseLink takes a striped matrix.to hash link (without the '#/' prefix)
|
||||||
* and parses into a Link. If the parse failed the result will
|
* and parses into a Link. If the parse failed the result will
|
||||||
|
@ -19,16 +104,16 @@ export function parseHash(hash: string): Link {
|
||||||
const kind = identifyTypeFromRegex(
|
const kind = identifyTypeFromRegex(
|
||||||
identifier,
|
identifier,
|
||||||
verifiers,
|
verifiers,
|
||||||
LinkDiscriminator.ParseFailed
|
LinkKind.ParseFailed
|
||||||
);
|
);
|
||||||
|
|
||||||
let parsedLink: LinkContent = {
|
const parsedLink: LinkContent = {
|
||||||
identifier,
|
identifier,
|
||||||
arguments: parseArgs(args),
|
arguments: parseArgs(args),
|
||||||
originalLink: hash,
|
originalLink: hash,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (kind === LinkDiscriminator.Permalink) {
|
if (kind === LinkKind.Permalink) {
|
||||||
const { roomKind, roomLink, eventId } = parsePermalink(identifier);
|
const { roomKind, roomLink, eventId } = parsePermalink(identifier);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -46,68 +131,25 @@ export function parseHash(hash: string): Link {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Parses a permalink.
|
|
||||||
* Assumes the permalink is correct.
|
|
||||||
*/
|
|
||||||
export function parsePermalink(identifier: string) {
|
|
||||||
const [roomLink, eventId] = identifier.split("/");
|
|
||||||
const roomKind = identifyTypeFromRegex(
|
|
||||||
roomLink,
|
|
||||||
roomVerifiers,
|
|
||||||
// This is hacky but we're assuming identifier is a valid permalink
|
|
||||||
LinkDiscriminator.Alias
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
roomKind,
|
|
||||||
roomLink,
|
|
||||||
eventId,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* parseArgs parses the <extra args> part of matrix.to links
|
|
||||||
*/
|
|
||||||
export function parseArgs(args: string): Arguments {
|
|
||||||
const params = new URLSearchParams(args);
|
|
||||||
const _federated = params.get("federated");
|
|
||||||
const federated = _federated !== null ? _federated === "true" : null;
|
|
||||||
|
|
||||||
return {
|
|
||||||
vias: params.getAll("via"),
|
|
||||||
federated: bottomExchange(federated),
|
|
||||||
client: bottomExchange(params.get("client")),
|
|
||||||
sharer: bottomExchange(params.get("sharer")),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Repalces null with undefined
|
|
||||||
*/
|
|
||||||
function bottomExchange<T>(nullable: T | null): T | undefined {
|
|
||||||
if (nullable === null) return undefined;
|
|
||||||
return nullable;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* toURI converts a Link to a url. It's recommended
|
* toURI converts a Link to a url. It's recommended
|
||||||
* to use the original link instead of toURI if it existed.
|
* to use the original link instead of toURI if it existed.
|
||||||
* This is handy function in case the Link was constructed.
|
* This is handy function in case the Link was constructed.
|
||||||
*/
|
*/
|
||||||
export function toURL(origin: string, link: SafeLink): URL {
|
export function toURL(origin: string, link: SafeLink): URL {
|
||||||
switch (link.kind) {
|
|
||||||
case LinkDiscriminator.GroupId:
|
|
||||||
case LinkDiscriminator.UserId:
|
|
||||||
case LinkDiscriminator.RoomId:
|
|
||||||
case LinkDiscriminator.Alias:
|
|
||||||
case LinkDiscriminator.Permalink:
|
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
const url = new URL(origin);
|
||||||
|
switch (link.kind) {
|
||||||
|
case LinkKind.GroupId:
|
||||||
|
case LinkKind.UserId:
|
||||||
|
case LinkKind.RoomId:
|
||||||
|
case LinkKind.Alias:
|
||||||
|
case LinkKind.Permalink:
|
||||||
forEach(link.arguments, (value, key) => {
|
forEach(link.arguments, (value, key) => {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
// do nothing
|
// do nothing
|
||||||
} else if (key === "vias") {
|
} else if (key === "vias") {
|
||||||
(<string[]>(<unknown>value)).forEach((via) =>
|
(value as string[]).forEach((via) =>
|
||||||
params.append("via", via)
|
params.append("via", via)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -115,53 +157,7 @@ export function toURL(origin: string, link: SafeLink): URL {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const url = new URL(origin);
|
|
||||||
url.hash = `/${link.identifier}?${params.toString()}`;
|
url.hash = `/${link.identifier}?${params.toString()}`;
|
||||||
|
}
|
||||||
return url;
|
return url;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Verifiers are regexes which will match valid
|
|
||||||
* identifiers to their type. (This is a lie, they
|
|
||||||
* can return anything)
|
|
||||||
*/
|
|
||||||
type Verifier<A> = [RegExp, A];
|
|
||||||
export const roomVerifiers: Verifier<
|
|
||||||
LinkDiscriminator.Alias | LinkDiscriminator.RoomId
|
|
||||||
>[] = [
|
|
||||||
[/^#([^\/:]+?):(.+)$/, LinkDiscriminator.Alias],
|
|
||||||
[/^!([^\/:]+?):(.+)$/, LinkDiscriminator.RoomId],
|
|
||||||
];
|
|
||||||
export const verifiers: Verifier<LinkDiscriminator>[] = [
|
|
||||||
[/^[\!#]([^\/:]+?):(.+?)\/\$([^\/:]+?):(.+?)$/, LinkDiscriminator.Permalink],
|
|
||||||
[/^@([^\/:]+?):(.+)$/, LinkDiscriminator.UserId],
|
|
||||||
[/^\+([^\/:]+?):(.+)$/, LinkDiscriminator.GroupId],
|
|
||||||
...roomVerifiers,
|
|
||||||
];
|
|
||||||
|
|
||||||
/*
|
|
||||||
* identifyTypeFromRegex applies the verifiers to the identifier and
|
|
||||||
* returns the identifier's type
|
|
||||||
*/
|
|
||||||
export function identifyTypeFromRegex<T, F>(
|
|
||||||
identifier: string,
|
|
||||||
verifiers: Verifier<T>[],
|
|
||||||
fail: F
|
|
||||||
): T | F {
|
|
||||||
if (identifier !== encodeURI(identifier)) {
|
|
||||||
return fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
return verifiers.reduce<T | F>((discriminator, verifier) => {
|
|
||||||
if (discriminator !== fail) {
|
|
||||||
return discriminator;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (identifier.match(verifier[0])) {
|
|
||||||
return verifier[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
return discriminator;
|
|
||||||
}, fail);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,6 @@ export interface Arguments {
|
||||||
vias: string[];
|
vias: string[];
|
||||||
// Schemeless http identifier
|
// Schemeless http identifier
|
||||||
client?: string;
|
client?: string;
|
||||||
// Indicates whether a room exists on a federating server (assumed to be the
|
|
||||||
// default), or if the client must connect via the server identified by the
|
|
||||||
// room ID or event ID
|
|
||||||
federated?: boolean;
|
|
||||||
// MXID
|
// MXID
|
||||||
sharer?: string;
|
sharer?: string;
|
||||||
}
|
}
|
||||||
|
@ -16,7 +12,7 @@ export interface LinkContent {
|
||||||
originalLink: string;
|
originalLink: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum LinkDiscriminator {
|
export enum LinkKind {
|
||||||
Alias = "ALIAS",
|
Alias = "ALIAS",
|
||||||
RoomId = "ROOM_ID",
|
RoomId = "ROOM_ID",
|
||||||
UserId = "USER_ID",
|
UserId = "USER_ID",
|
||||||
|
@ -26,30 +22,30 @@ export enum LinkDiscriminator {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Alias extends LinkContent {
|
export interface Alias extends LinkContent {
|
||||||
kind: LinkDiscriminator.Alias;
|
kind: LinkKind.Alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RoomId extends LinkContent {
|
export interface RoomId extends LinkContent {
|
||||||
kind: LinkDiscriminator.RoomId;
|
kind: LinkKind.RoomId;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UserId extends LinkContent {
|
export interface UserId extends LinkContent {
|
||||||
kind: LinkDiscriminator.UserId;
|
kind: LinkKind.UserId;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GroupId extends LinkContent {
|
export interface GroupId extends LinkContent {
|
||||||
kind: LinkDiscriminator.GroupId;
|
kind: LinkKind.GroupId;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Permalink extends LinkContent {
|
export interface Permalink extends LinkContent {
|
||||||
kind: LinkDiscriminator.Permalink;
|
kind: LinkKind.Permalink;
|
||||||
roomKind: LinkDiscriminator.RoomId | LinkDiscriminator.Alias;
|
roomKind: LinkKind.RoomId | LinkKind.Alias;
|
||||||
roomLink: string;
|
roomLink: string;
|
||||||
eventId: string;
|
eventId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ParseFailed {
|
export interface ParseFailed {
|
||||||
kind: LinkDiscriminator.ParseFailed;
|
kind: LinkKind.ParseFailed;
|
||||||
originalLink: string;
|
originalLink: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue