Skip to content

Commit 2111db7

Browse files
authored
Merge pull request element-hq#10181 from vector-im/t3chguy/electron_config_via_plaf
Move config-getting to VectorBasePlatform
2 parents fceb05e + 01a78fe commit 2111db7

5 files changed

Lines changed: 24 additions & 43 deletions

File tree

electron_app/src/electron-main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
Copyright 2016 Aviral Dasgupta
33
Copyright 2016 OpenMarket Ltd
4-
Copyright 2017 Michael Telatynski <7t3chguy@gmail.com>
54
Copyright 2018 New Vector Ltd
5+
Copyright 2017, 2019 Michael Telatynski <7t3chguy@gmail.com>
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -173,6 +173,9 @@ ipcMain.on('ipcCall', async function(ev, payload) {
173173
await migrateFromOldOrigin();
174174
migratingOrigin = false;
175175
break;
176+
case 'getConfig':
177+
ret = vectorConfig;
178+
break;
176179
default:
177180
mainWindow.webContents.send('ipcReply', {
178181
id: payload.id,

src/vector/getconfig.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
import Promise from 'bluebird';
1818
import request from 'browser-request';
1919

20+
// Load the config file. First try to load up a domain-specific config of the
21+
// form "config.$domain.json" and if that fails, fall back to config.json.
2022
export async function getVectorConfig(relativeLocation) {
2123
if (relativeLocation === undefined) relativeLocation = '';
2224
if (relativeLocation !== '' && !relativeLocation.endsWith('/')) relativeLocation += '/';

src/vector/index.js

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Copyright 2015, 2016 OpenMarket Ltd
33
Copyright 2017 Vector Creations Ltd
44
Copyright 2018, 2019 New Vector Ltd
5+
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
56
67
Licensed under the Apache License, Version 2.0 (the "License");
78
you may not use this file except in compliance with the License.
@@ -43,7 +44,6 @@ import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
4344
sdk.loadSkin(require('../component-index'));
4445
import VectorConferenceHandler from 'matrix-react-sdk/lib/VectorConferenceHandler';
4546
import Promise from 'bluebird';
46-
import request from 'browser-request';
4747
import * as languageHandler from 'matrix-react-sdk/lib/languageHandler';
4848
import {_t, _td, newTranslatableError} from 'matrix-react-sdk/lib/languageHandler';
4949
import AutoDiscoveryUtils from 'matrix-react-sdk/lib/utils/AutoDiscoveryUtils';
@@ -66,8 +66,6 @@ import Olm from 'olm';
6666

6767
import CallHandler from 'matrix-react-sdk/lib/CallHandler';
6868

69-
import {getVectorConfig} from './getconfig';
70-
7169
let lastLocationHashSet = null;
7270

7371
// Disable warnings for now: we use deprecated bluebird functions
@@ -119,7 +117,7 @@ function routeUrl(location) {
119117
}
120118

121119
function onHashChange(ev) {
122-
if (decodeURIComponent(window.location.hash) == lastLocationHashSet) {
120+
if (decodeURIComponent(window.location.hash) === lastLocationHashSet) {
123121
// we just set this: no need to route it!
124122
return;
125123
}
@@ -159,7 +157,7 @@ function makeRegistrationUrl(params) {
159157

160158
const keys = Object.keys(params);
161159
for (let i = 0; i < keys.length; ++i) {
162-
if (i == 0) {
160+
if (i === 0) {
163161
url += '?';
164162
} else {
165163
url += '&';
@@ -170,38 +168,6 @@ function makeRegistrationUrl(params) {
170168
return url;
171169
}
172170

173-
export function getConfig(configJsonFilename) {
174-
return new Promise(function(resolve, reject) {
175-
request(
176-
{ method: "GET", url: configJsonFilename },
177-
(err, response, body) => {
178-
if (err || response.status < 200 || response.status >= 300) {
179-
// Lack of a config isn't an error, we should
180-
// just use the defaults.
181-
// Also treat a blank config as no config, assuming
182-
// the status code is 0, because we don't get 404s
183-
// from file: URIs so this is the only way we can
184-
// not fail if the file doesn't exist when loading
185-
// from a file:// URI.
186-
if (response) {
187-
if (response.status == 404 || (response.status == 0 && body == '')) {
188-
resolve({});
189-
}
190-
}
191-
reject({err: err, response: response});
192-
return;
193-
}
194-
195-
// We parse the JSON ourselves rather than use the JSON
196-
// parameter, since this throws a parse error on empty
197-
// which breaks if there's no config.json and we're
198-
// loading from the filesystem (see above).
199-
resolve(JSON.parse(body));
200-
},
201-
);
202-
});
203-
}
204-
205171
function onTokenLoginCompleted() {
206172
// if we did a token login, we're now left with the token, hs and is
207173
// url as query params in the url; a little nasty but let's redirect to
@@ -252,12 +218,12 @@ async function loadApp() {
252218
PlatformPeg.set(new WebPlatform());
253219
}
254220

255-
// Load the config file. First try to load up a domain-specific config of the
256-
// form "config.$domain.json" and if that fails, fall back to config.json.
221+
const platform = PlatformPeg.get();
222+
257223
let configJson;
258224
let configError;
259225
try {
260-
configJson = await getVectorConfig();
226+
configJson = await platform.getConfig();
261227
} catch (e) {
262228
configError = e;
263229
}
@@ -342,7 +308,6 @@ async function loadApp() {
342308
Unable to load config file: please refresh the page to try again.
343309
</div>, document.getElementById('matrixchat'));
344310
} else if (validBrowser || acceptInvalidBrowser) {
345-
const platform = PlatformPeg.get();
346311
platform.startUpdater();
347312

348313
// Don't bother loading the app until the config is verified
@@ -410,7 +375,7 @@ function loadOlm() {
410375
}).then(() => {
411376
console.log("Using WebAssembly Olm");
412377
}).catch((e) => {
413-
console.log("Failed to load Olm: trying legacy version");
378+
console.log("Failed to load Olm: trying legacy version", e);
414379
return new Promise((resolve, reject) => {
415380
const s = document.createElement('script');
416381
s.src = 'olm_legacy.js'; // XXX: This should be cache-busted too

src/vector/platform/ElectronPlatform.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Copyright 2016 Aviral Dasgupta
55
Copyright 2016 OpenMarket Ltd
66
Copyright 2018 New Vector Ltd
7+
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
78
89
Licensed under the Apache License, Version 2.0 (the "License");
910
you may not use this file except in compliance with the License.
@@ -101,6 +102,10 @@ export default class ElectronPlatform extends VectorBasePlatform {
101102
this.stopUpdateCheck = this.stopUpdateCheck.bind(this);
102103
}
103104

105+
async getConfig(): Promise<{}> {
106+
return this._ipcCall('getConfig');
107+
}
108+
104109
async onUpdateDownloaded(ev, updateInfo) {
105110
dis.dispatch({
106111
action: 'new_version',

src/vector/platform/VectorBasePlatform.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Copyright 2016 Aviral Dasgupta
55
Copyright 2016 OpenMarket Ltd
66
Copyright 2018 New Vector Ltd
7+
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
78
89
Licensed under the Apache License, Version 2.0 (the "License");
910
you may not use this file except in compliance with the License.
@@ -21,6 +22,7 @@ limitations under the License.
2122
import BasePlatform from 'matrix-react-sdk/lib/BasePlatform';
2223
import { _t } from 'matrix-react-sdk/lib/languageHandler';
2324
import dis from 'matrix-react-sdk/lib/dispatcher';
25+
import {getVectorConfig} from "../getconfig";
2426

2527
import Favico from 'favico.js';
2628

@@ -44,6 +46,10 @@ export default class VectorBasePlatform extends BasePlatform {
4446
this.stopUpdateCheck = this.stopUpdateCheck.bind(this);
4547
}
4648

49+
async getConfig(): Promise<{}> {
50+
return getVectorConfig();
51+
}
52+
4753
getHumanReadableName(): string {
4854
return 'Vector Base Platform'; // no translation required: only used for analytics
4955
}

0 commit comments

Comments
 (0)