From 974797648f3e631a49955e4a6aa5c03afe17d3d2 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 26 Jun 2019 21:08:04 +0100 Subject: [PATCH] Move config-getting to VectorBasePlatform in Electron get config via IPC from main process which has access to the "local" config.json override file and can make people happy :D Remove bunch of duplicated code, and move comments around to put them in the right place Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- electron_app/src/electron-main.js | 5 ++- src/vector/getconfig.js | 4 ++- src/vector/index.js | 42 +++-------------------- src/vector/platform/ElectronPlatform.js | 5 +++ src/vector/platform/VectorBasePlatform.js | 6 ++++ 5 files changed, 22 insertions(+), 40 deletions(-) diff --git a/electron_app/src/electron-main.js b/electron_app/src/electron-main.js index 8b837caec2..4c6f584c1b 100644 --- a/electron_app/src/electron-main.js +++ b/electron_app/src/electron-main.js @@ -1,8 +1,8 @@ /* Copyright 2016 Aviral Dasgupta Copyright 2016 OpenMarket Ltd -Copyright 2017 Michael Telatynski <7t3chguy@gmail.com> Copyright 2018 New Vector Ltd +Copyright 2017, 2019 Michael Telatynski <7t3chguy@gmail.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -173,6 +173,9 @@ ipcMain.on('ipcCall', async function(ev, payload) { await migrateFromOldOrigin(); migratingOrigin = false; break; + case 'getConfig': + ret = vectorConfig; + break; default: mainWindow.webContents.send('ipcReply', { id: payload.id, diff --git a/src/vector/getconfig.js b/src/vector/getconfig.js index 239ba8067a..0febfbd1df 100644 --- a/src/vector/getconfig.js +++ b/src/vector/getconfig.js @@ -17,6 +17,8 @@ limitations under the License. import Promise from 'bluebird'; import request from 'browser-request'; +// Load the config file. First try to load up a domain-specific config of the +// form "config.$domain.json" and if that fails, fall back to config.json. export async function getVectorConfig(relativeLocation) { if (relativeLocation === undefined) relativeLocation = ''; if (relativeLocation !== '' && !relativeLocation.endsWith('/')) relativeLocation += '/'; @@ -32,7 +34,7 @@ export async function getVectorConfig(relativeLocation) { } } -function getConfig(configJsonFilename) { +export function getConfig(configJsonFilename) { return new Promise(function(resolve, reject) { request( { method: "GET", url: configJsonFilename }, diff --git a/src/vector/index.js b/src/vector/index.js index 597b3f374f..f77bf971b6 100644 --- a/src/vector/index.js +++ b/src/vector/index.js @@ -2,6 +2,7 @@ Copyright 2015, 2016 OpenMarket Ltd Copyright 2017 Vector Creations Ltd Copyright 2018, 2019 New Vector Ltd +Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -66,8 +67,6 @@ import Olm from 'olm'; import CallHandler from 'matrix-react-sdk/lib/CallHandler'; -import {getVectorConfig} from './getconfig'; - let lastLocationHashSet = null; // Disable warnings for now: we use deprecated bluebird functions @@ -170,38 +169,6 @@ function makeRegistrationUrl(params) { return url; } -export function getConfig(configJsonFilename) { - return new Promise(function(resolve, reject) { - request( - { method: "GET", url: configJsonFilename }, - (err, response, body) => { - if (err || response.status < 200 || response.status >= 300) { - // Lack of a config isn't an error, we should - // just use the defaults. - // Also treat a blank config as no config, assuming - // the status code is 0, because we don't get 404s - // from file: URIs so this is the only way we can - // not fail if the file doesn't exist when loading - // from a file:// URI. - if (response) { - if (response.status == 404 || (response.status == 0 && body == '')) { - resolve({}); - } - } - reject({err: err, response: response}); - return; - } - - // We parse the JSON ourselves rather than use the JSON - // parameter, since this throws a parse error on empty - // which breaks if there's no config.json and we're - // loading from the filesystem (see above). - resolve(JSON.parse(body)); - }, - ); - }); -} - function onTokenLoginCompleted() { // if we did a token login, we're now left with the token, hs and is // url as query params in the url; a little nasty but let's redirect to @@ -252,12 +219,12 @@ async function loadApp() { PlatformPeg.set(new WebPlatform()); } - // Load the config file. First try to load up a domain-specific config of the - // form "config.$domain.json" and if that fails, fall back to config.json. + const platform = PlatformPeg.get(); + let configJson; let configError; try { - configJson = await getVectorConfig(); + configJson = await platform.getConfig(); } catch (e) { configError = e; } @@ -342,7 +309,6 @@ async function loadApp() { Unable to load config file: please refresh the page to try again. , document.getElementById('matrixchat')); } else if (validBrowser || acceptInvalidBrowser) { - const platform = PlatformPeg.get(); platform.startUpdater(); // Don't bother loading the app until the config is verified diff --git a/src/vector/platform/ElectronPlatform.js b/src/vector/platform/ElectronPlatform.js index 04f472d645..387403a600 100644 --- a/src/vector/platform/ElectronPlatform.js +++ b/src/vector/platform/ElectronPlatform.js @@ -4,6 +4,7 @@ Copyright 2016 Aviral Dasgupta Copyright 2016 OpenMarket Ltd Copyright 2018 New Vector Ltd +Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -101,6 +102,10 @@ export default class ElectronPlatform extends VectorBasePlatform { this.stopUpdateCheck = this.stopUpdateCheck.bind(this); } + async getConfig(): Promise<{}> { + return this._ipcCall('getConfig'); + } + async onUpdateDownloaded(ev, updateInfo) { dis.dispatch({ action: 'new_version', diff --git a/src/vector/platform/VectorBasePlatform.js b/src/vector/platform/VectorBasePlatform.js index c10c0accd3..f24031f732 100644 --- a/src/vector/platform/VectorBasePlatform.js +++ b/src/vector/platform/VectorBasePlatform.js @@ -4,6 +4,7 @@ Copyright 2016 Aviral Dasgupta Copyright 2016 OpenMarket Ltd Copyright 2018 New Vector Ltd +Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -21,6 +22,7 @@ limitations under the License. import BasePlatform from 'matrix-react-sdk/lib/BasePlatform'; import { _t } from 'matrix-react-sdk/lib/languageHandler'; import dis from 'matrix-react-sdk/lib/dispatcher'; +import {getVectorConfig} from "../getconfig"; import Favico from 'favico.js'; @@ -44,6 +46,10 @@ export default class VectorBasePlatform extends BasePlatform { this.stopUpdateCheck = this.stopUpdateCheck.bind(this); } + async getConfig(): Promise<{}> { + return getVectorConfig(); + } + getHumanReadableName(): string { return 'Vector Base Platform'; // no translation required: only used for analytics }