Skip to content

Commit

Permalink
Merge pull request #9726 from vector-im/travis/wk/mobileguide
Browse files Browse the repository at this point in the history
Show resolved homeserver configuration on the mobile guide
  • Loading branch information
turt2live authored May 17, 2019
2 parents 1ac5df3 + 22cc33e commit 7a65dc9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 7 deletions.
21 changes: 20 additions & 1 deletion src/vector/mobile_guide/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

<style type="text/css">

/* By default, hide the custom IS stuff - enabled in JS */
#custom_is, #is_url {
display: none;
}

body {
background: #c5e0f7;
background: -moz-linear-gradient(top, #c5e0f7 0%, #ffffff 100%);
Expand Down Expand Up @@ -109,6 +114,14 @@
margin: 20px;
}

.mx_HomePage_errorContainer {
display: none; /* shown in JS if needed */
margin: 20px;
border: 1px solid red;
background-color: #ffb9b9;
padding: 5px;
}

.mx_HomePage_container h1,
.mx_HomePage_container h2,
.mx_HomePage_container h3,
Expand Down Expand Up @@ -152,6 +165,10 @@

<body>

<div class="mx_HomePage_errorContainer">
<!-- populated by JS if needed -->
</div>

<div class="mx_HomePage_container">
<div class="mx_HomePage_col mx_HomePage_header">
<a href="https://riot.im">
Expand Down Expand Up @@ -365,7 +382,9 @@ <h2>Step 2: Use a custom server</h2>
<p>Launch the app, and enable <strong>Use custom server options (advanced)</strong>.</p>
<p class="mx_Spacer">In the homeserver field, enter:</p>
<p><strong id="hs_url"></strong></p>
<p class="mx_Spacer"><em>Note: You shouldn&apos;t need to modify the identity server field, which by default is set to https://vector.im.</em></p>
<p class="mx_Spacer" id="default_is"><em>Note: You shouldn&apos;t need to modify the identity server field, which by default is set to https://vector.im.</em></p>
<p class="mx_Spacer" id="custom_is">In the identity server field, enter:</p>
<p><strong id="is_url"></strong></p>
</div>
</div>
</div>
Expand Down
95 changes: 89 additions & 6 deletions src/vector/mobile_guide/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,102 @@ function onBackToRiotClick() {
window.location.href = '../';
}

// NEVER pass user-controlled content to this function! Hardcoded strings only please.
function renderConfigError(message) {
const contactMsg = "If this is unexpected, please contact your system administrator " +
"or technical support representative.";
message = `<h2>Error loading Riot</h2><p>${message}</p><p>${contactMsg}</p>`;

const toHide = document.getElementsByClassName("mx_HomePage_container");
const errorContainers = document.getElementsByClassName("mx_HomePage_errorContainer");

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.
e.innerHTML = '';
}
for (const e of errorContainers) {
e.style.display = 'block';
e.innerHTML = message;
}
}

async function initPage() {
document.getElementById('back_to_riot_button').onclick = onBackToRiotClick;

const config = await getVectorConfig('..');
let hsUrl;
if (config && config['default_hs_url']) {
hsUrl = config['default_hs_url'];
let config = await getVectorConfig('..');

// We manually parse the config similar to how validateServerConfig works because
// calling that function pulls in roughly 4mb of JS we don't use.

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'];

const incompatibleOptions = [wkConfig, serverName, defaultHsUrl].filter(i => !!i);
if (incompatibleOptions.length > 1) {
return renderConfigError(
"Invalid configuration: can only specify one of default_server_config, default_server_name, " +
"or default_hs_url.",
);
}
if (incompatibleOptions.length < 1) {
return renderConfigError("Invalid configuration: no default server specified.");
}

let hsUrl = '';
let isUrl = '';

if (wkConfig && wkConfig['m.homeserver']) {
hsUrl = wkConfig['m.homeserver']['base_url'];

if (wkConfig['m.identity_server']) {
isUrl = wkConfig['m.identity_server']['base_url'];
}
}

if (serverName) {
// 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();
if (wkConfig && wkConfig['m.homeserver']) {
hsUrl = wkConfig['m.homeserver']['base_url'];

if (wkConfig['m.identity_server']) {
isUrl = wkConfig['m.identity_server']['base_url'];
}
}
} catch (e) {
console.error(e);
return renderConfigError("Unable to fetch homeserver configuration");
}
}

if (defaultHsUrl) {
hsUrl = defaultHsUrl;
isUrl = defaultIsUrl;
}

if (!hsUrl) {
return renderConfigError("Unable to locate homeserver");
}

if (hsUrl && !hsUrl.endsWith('/')) hsUrl += '/';
if (hsUrl && hsUrl !== 'https://matrix.org/') {
if (isUrl && !isUrl.endsWith('/')) isUrl += '/';

if (hsUrl !== 'https://matrix.org/') {
document.getElementById('step2_container').style.display = 'block';
document.getElementById('hs_url').innerHTML = hsUrl;
document.getElementById('hs_url').innerText = hsUrl;
document.getElementById('step_login_header').innerHTML= 'Step 3: Register or Log in';

if (isUrl && isUrl !== "https://vector.im/") {
document.getElementById('default_is').style.display = 'none';
document.getElementById('custom_is').style.display = 'block';
document.getElementById('is_url').style.display = 'block';
document.getElementById('is_url').innerText = isUrl;
}
}
}

Expand Down

0 comments on commit 7a65dc9

Please sign in to comment.