2021-09-03 08:04:37 -04:00
|
|
|
type LinkType =
|
|
|
|
| { type: "profile"; id: string }
|
2021-09-07 09:26:12 -04:00
|
|
|
| { type: "navigate"; path: string; navigation_type?: null }
|
2021-09-09 17:39:17 -04:00
|
|
|
| {
|
|
|
|
type: "navigate";
|
|
|
|
path: string;
|
|
|
|
navigation_type: "channel";
|
|
|
|
channel_id: string;
|
|
|
|
}
|
2021-09-03 08:04:37 -04:00
|
|
|
| { type: "external"; href: string; url: URL }
|
|
|
|
| { type: "none" };
|
|
|
|
|
|
|
|
const ALLOWED_ORIGINS = [
|
|
|
|
location.hostname,
|
|
|
|
"app.revolt.chat",
|
|
|
|
"nightly.revolt.chat",
|
|
|
|
"local.revolt.chat",
|
|
|
|
];
|
|
|
|
|
2021-09-09 17:39:17 -04:00
|
|
|
const CHANNEL_PATH_RE =
|
|
|
|
/^\/server\/[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}\/channel\/[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/;
|
2021-09-07 09:26:12 -04:00
|
|
|
|
2021-09-03 08:04:37 -04:00
|
|
|
export function determineLink(href?: string): LinkType {
|
|
|
|
let internal,
|
|
|
|
url: URL | null = null;
|
|
|
|
|
|
|
|
if (href) {
|
|
|
|
try {
|
|
|
|
url = new URL(href, location.href);
|
|
|
|
|
|
|
|
if (ALLOWED_ORIGINS.includes(url.hostname)) {
|
|
|
|
const path = url.pathname;
|
|
|
|
if (path.startsWith("/@")) {
|
|
|
|
const id = path.substr(2);
|
|
|
|
if (/[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}/.test(id)) {
|
|
|
|
return { type: "profile", id };
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-09 17:39:17 -04:00
|
|
|
if (CHANNEL_PATH_RE.test(path)) {
|
|
|
|
return {
|
|
|
|
type: "navigate",
|
|
|
|
path,
|
|
|
|
navigation_type: "channel",
|
|
|
|
channel_id: path.slice(43),
|
|
|
|
};
|
2021-09-07 09:26:12 -04:00
|
|
|
}
|
2021-09-03 08:04:37 -04:00
|
|
|
return { type: "navigate", path };
|
|
|
|
}
|
|
|
|
|
|
|
|
internal = true;
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
|
|
|
|
|
|
|
if (!internal && url) {
|
2021-09-25 05:54:32 -04:00
|
|
|
if (url.protocol !== "javascript") {
|
|
|
|
return { type: "external", href, url };
|
|
|
|
}
|
2021-09-03 08:04:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { type: "none" };
|
|
|
|
}
|