2021-10-15 16:56:22 +02:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
import { getVectorConfig } from "../getconfig";
|
2021-12-09 23:57:46 +01:00
|
|
|
|
2021-07-06 21:33:01 +02:00
|
|
|
function onBackToElementClick(): void {
|
2019-04-04 02:01:09 +02:00
|
|
|
// Cookie should expire in 4 hours
|
2022-12-09 13:28:29 +01:00
|
|
|
document.cookie = "element_mobile_redirect_to_guide=false;path=/;max-age=14400";
|
|
|
|
window.location.href = "../";
|
2018-09-03 21:38:30 +02:00
|
|
|
}
|
|
|
|
|
2019-05-15 00:59:02 +02:00
|
|
|
// NEVER pass user-controlled content to this function! Hardcoded strings only please.
|
2021-07-06 21:33:01 +02:00
|
|
|
function renderConfigError(message: string): void {
|
2022-12-09 13:28:29 +01:00
|
|
|
const contactMsg =
|
|
|
|
"If this is unexpected, please contact your system administrator " + "or technical support representative.";
|
2020-07-13 18:32:17 +02:00
|
|
|
message = `<h2>Error loading Element</h2><p>${message}</p><p>${contactMsg}</p>`;
|
2019-05-15 00:59:02 +02:00
|
|
|
|
|
|
|
const toHide = document.getElementsByClassName("mx_HomePage_container");
|
2021-07-06 21:33:01 +02:00
|
|
|
const errorContainers = document.getElementsByClassName(
|
|
|
|
"mx_HomePage_errorContainer",
|
2022-05-16 17:47:12 +02:00
|
|
|
) as HTMLCollectionOf<HTMLDivElement>;
|
2019-05-15 00:59:02 +02:00
|
|
|
|
|
|
|
for (const e of toHide) {
|
|
|
|
// We have to clear the content because .style.display='none'; doesn't work
|
|
|
|
// due to an !important in the CSS.
|
2022-12-09 13:28:29 +01:00
|
|
|
e.innerHTML = "";
|
2019-05-15 00:59:02 +02:00
|
|
|
}
|
|
|
|
for (const e of errorContainers) {
|
2022-12-09 13:28:29 +01:00
|
|
|
e.style.display = "block";
|
2019-05-15 00:59:02 +02:00
|
|
|
e.innerHTML = message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 21:33:01 +02:00
|
|
|
async function initPage(): Promise<void> {
|
2022-12-09 13:28:29 +01:00
|
|
|
document.getElementById("back_to_element_button").onclick = onBackToElementClick;
|
2018-09-03 21:38:30 +02:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
const config = await getVectorConfig("..");
|
2019-05-15 00:59:02 +02:00
|
|
|
|
|
|
|
// We manually parse the config similar to how validateServerConfig works because
|
|
|
|
// calling that function pulls in roughly 4mb of JS we don't use.
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
const wkConfig = config["default_server_config"]; // overwritten later under some conditions
|
|
|
|
const serverName = config["default_server_name"];
|
|
|
|
const defaultHsUrl = config["default_hs_url"];
|
|
|
|
const defaultIsUrl = config["default_is_url"];
|
2019-05-15 00:59:02 +02:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
const incompatibleOptions = [wkConfig, serverName, defaultHsUrl].filter((i) => !!i);
|
2019-05-15 00:59:02 +02:00
|
|
|
if (incompatibleOptions.length > 1) {
|
|
|
|
return renderConfigError(
|
|
|
|
"Invalid configuration: can only specify one of default_server_config, default_server_name, " +
|
2022-12-09 13:28:29 +01:00
|
|
|
"or default_hs_url.",
|
2019-05-15 00:59:02 +02:00
|
|
|
);
|
2018-09-03 21:38:30 +02:00
|
|
|
}
|
2019-05-15 00:59:02 +02:00
|
|
|
if (incompatibleOptions.length < 1) {
|
|
|
|
return renderConfigError("Invalid configuration: no default server specified.");
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
let hsUrl = "";
|
|
|
|
let isUrl = "";
|
2019-05-15 00:59:02 +02:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
if (wkConfig && wkConfig["m.homeserver"]) {
|
|
|
|
hsUrl = wkConfig["m.homeserver"]["base_url"];
|
2019-05-15 00:59:02 +02:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
if (wkConfig["m.identity_server"]) {
|
|
|
|
isUrl = wkConfig["m.identity_server"]["base_url"];
|
2019-05-15 00:59:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serverName) {
|
2019-05-15 01:57:04 +02:00
|
|
|
// We also do our own minimal .well-known validation to avoid pulling in the js-sdk
|
|
|
|
try {
|
|
|
|
const result = await fetch(`https://${serverName}/.well-known/matrix/client`);
|
|
|
|
const wkConfig = await result.json();
|
2022-12-09 13:28:29 +01:00
|
|
|
if (wkConfig && wkConfig["m.homeserver"]) {
|
|
|
|
hsUrl = wkConfig["m.homeserver"]["base_url"];
|
2019-05-15 01:57:04 +02:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
if (wkConfig["m.identity_server"]) {
|
|
|
|
isUrl = wkConfig["m.identity_server"]["base_url"];
|
2019-05-15 01:57:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error(e);
|
2019-05-15 01:57:04 +02:00
|
|
|
return renderConfigError("Unable to fetch homeserver configuration");
|
|
|
|
}
|
2019-05-15 00:59:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (defaultHsUrl) {
|
|
|
|
hsUrl = defaultHsUrl;
|
|
|
|
isUrl = defaultIsUrl;
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:57:04 +02:00
|
|
|
if (!hsUrl) {
|
|
|
|
return renderConfigError("Unable to locate homeserver");
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
if (hsUrl && !hsUrl.endsWith("/")) hsUrl += "/";
|
|
|
|
if (isUrl && !isUrl.endsWith("/")) isUrl += "/";
|
2019-05-15 00:59:02 +02:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
if (hsUrl !== "https://matrix.org/") {
|
|
|
|
(document.getElementById("configure_element_button") as HTMLAnchorElement).href =
|
|
|
|
"https://mobile.element.io?hs_url=" + encodeURIComponent(hsUrl) + "&is_url=" + encodeURIComponent(isUrl);
|
|
|
|
document.getElementById("step1_heading").innerHTML = "1: Install the app";
|
|
|
|
document.getElementById("step2_container").style.display = "block";
|
|
|
|
document.getElementById("hs_url").innerText = hsUrl;
|
2019-05-15 00:59:02 +02:00
|
|
|
|
2019-11-28 02:26:18 +01:00
|
|
|
if (isUrl) {
|
2022-12-09 13:28:29 +01:00
|
|
|
document.getElementById("custom_is").style.display = "block";
|
|
|
|
document.getElementById("is_url").style.display = "block";
|
|
|
|
document.getElementById("is_url").innerText = isUrl;
|
2019-05-15 00:59:02 +02:00
|
|
|
}
|
2018-09-04 11:08:52 +02:00
|
|
|
}
|
2018-09-03 21:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initPage();
|