Skip to content

Commit c8caf67

Browse files
committed
Move bulk of discovery processing to react-sdk
1 parent 51941ea commit c8caf67

2 files changed

Lines changed: 9 additions & 59 deletions

File tree

‎src/i18n/strings/en_EN.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"Unexpected error preparing the app. See console for details.": "Unexpected error preparing the app. See console for details.",
33
"Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.": "Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.",
4-
"Unexpected error resolving homeserver configuration": "Unexpected error resolving homeserver configuration",
54
"Riot Desktop on %(platformName)s": "Riot Desktop on %(platformName)s",
65
"Unknown device": "Unknown device",
76
"%(appName)s via %(browserName)s on %(osName)s": "%(appName)s via %(browserName)s on %(osName)s",

‎src/vector/index.js‎

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ import VectorConferenceHandler from 'matrix-react-sdk/lib/VectorConferenceHandle
4545
import Promise from 'bluebird';
4646
import request from 'browser-request';
4747
import * as languageHandler from 'matrix-react-sdk/lib/languageHandler';
48-
import {_t, _td} from 'matrix-react-sdk/lib/languageHandler';
48+
import {_t, _td, newTranslatableError} from 'matrix-react-sdk/lib/languageHandler';
49+
import AutoDiscoveryUtils from 'matrix-react-sdk/lib/utils/AutoDiscoveryUtils';
4950
import {AutoDiscovery} from "matrix-js-sdk/lib/autodiscovery";
5051

5152
import url from 'url';
@@ -492,61 +493,19 @@ async function verifyServerConfig() {
492493
result = await AutoDiscovery.findClientConfig(serverName);
493494
}
494495

495-
if (!result || !result["m.homeserver"]) {
496-
// This shouldn't happen without major misconfiguration, so we'll log a bit of information
497-
// in the log so we can find this bit of codee but otherwise tell teh user "it broke".
498-
console.error("Ended up in a state of not knowing which homeserver to connect to.");
499-
throw newTranslatableError(_td("Unexpected error resolving homeserver configuration"));
500-
}
501-
502-
const hsResult = result['m.homeserver'];
503-
if (hsResult.state !== AutoDiscovery.SUCCESS) {
504-
if (AutoDiscovery.ALL_ERRORS.indexOf(hsResult.error) !== -1) {
505-
throw newTranslatableError(hsResult.error);
506-
}
507-
throw newTranslatableError(_td("Unexpected error resolving homeserver configuration"));
508-
}
509-
510-
const isResult = result['m.identity_server'];
511-
let preferredIdentityUrl = "https://vector.im";
512-
if (isResult && isResult.state === AutoDiscovery.SUCCESS) {
513-
preferredIdentityUrl = isResult["base_url"];
514-
} else if (isResult && isResult.state !== AutoDiscovery.PROMPT) {
515-
console.error("Error determining preferred identity server URL:", isResult);
516-
throw newTranslatableError(_td("Unexpected error resolving homeserver configuration"));
517-
}
518-
519-
const preferredHomeserverUrl = hsResult["base_url"];
520-
let preferredHomeserverName = serverName ? serverName : hsResult["server_name"];
521-
522-
const url = new URL(preferredHomeserverUrl);
523-
if (!preferredHomeserverName) preferredHomeserverName = url.hostname;
524-
525-
// It should have been set by now, so check it
526-
if (!preferredHomeserverName) {
527-
console.error("Failed to parse homeserver name from homeserver URL");
528-
throw newTranslatableError(_td("Unexpected error resolving homeserver configuration"));
529-
}
530-
531-
const isServerNameDifferentFromUrl = url.hostname !== preferredHomeserverName;
532-
533-
console.log("Using homeserver config:", {
534-
isServerNameDifferentFromUrl,
535-
preferredHomeserverName,
536-
preferredHomeserverUrl,
537-
preferredIdentityUrl,
538-
});
496+
const validatedConfig = AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, result);
497+
console.log("Using homeserver config:", validatedConfig);
539498

540499
// Build our own discovery result for distribution within the app
541500
const configResult = {
542501
"m.homeserver": {
543-
"base_url": preferredHomeserverUrl,
544-
"server_name": preferredHomeserverName,
545-
"server_name_different": isServerNameDifferentFromUrl,
502+
"base_url": validatedConfig.hsUrl,
503+
"server_name": validatedConfig.hsName,
504+
"server_name_different": validatedConfig.hsNameIsDifferent,
546505
},
547506
"m.identity_server": {
548-
"base_url": preferredIdentityUrl,
549-
"enabled": !SdkConfig.get()['disable_identity_server'],
507+
"base_url": validatedConfig.isUrl,
508+
"enabled": validatedConfig.identityEnabled,
550509
},
551510
};
552511

@@ -563,12 +522,4 @@ async function verifyServerConfig() {
563522
return SdkConfig.get();
564523
}
565524

566-
// Helper function to provide English errors in logs, but present translated
567-
// errors to users.
568-
function newTranslatableError(message) {
569-
const error = new Error(message);
570-
error.translatedMessage = _t(message);
571-
return error;
572-
}
573-
574525
loadApp();

0 commit comments

Comments
 (0)