Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show resolved homeserver configuration on the mobile guide #9726

Merged
merged 2 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could move validateServerConfig to getconfig.js?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd cost us 4mb of JS because it pulls in most of the react-sdk and most of the js-sdk

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to expand on that: the problem is that we use multiple webpack layers, so the 4mb consists of things like bluebird, browser-request, etc. If we had a single webpack for the 3 projects, we'd be able to dedupe/eliminate a lot of that cruft

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I was just suggesting taking validateServerConfig stuff you have here and putting it into a separate file so both things can use it (the js-sdk bit would have to stay duplicated) but if there's a reason that's not an option then lgtm.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, that's what I tried first and it's where the 4mb metric came from :(

The logic is slightly different in that here the goal is to resolve a homeserver as fast as possible with minimal safety whereas the validateServerConfig function tries to find a homeserver that's actually safe to use. It's not directly transferable without avoiding the js-sdk's implementation of stuff, which renders those classes nearly pointless.


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