From 9abc6a2e444b278946c39c7b8a471ad2aef4c33f Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Fri, 6 Sep 2019 18:29:40 +0100 Subject: [PATCH] CLI: Fixes #1779: Make sure setting side-effects are applied even when running in command line mode --- CliClient/app/app.js | 2 + ReactNativeClient/lib/BaseApplication.js | 112 ++++++++++++----------- 2 files changed, 62 insertions(+), 52 deletions(-) diff --git a/CliClient/app/app.js b/CliClient/app/app.js index 8883029b30a..82a86990f18 100644 --- a/CliClient/app/app.js +++ b/CliClient/app/app.js @@ -384,6 +384,8 @@ class Application extends BaseApplication { this.currentFolder_ = await Folder.load(Setting.value('activeFolderId')); + await this.applySettingsSideEffects(); + try { await this.execCommand(argv); } catch (error) { diff --git a/ReactNativeClient/lib/BaseApplication.js b/ReactNativeClient/lib/BaseApplication.js index 1c3731c73d2..6f84e6fb807 100644 --- a/ReactNativeClient/lib/BaseApplication.js +++ b/ReactNativeClient/lib/BaseApplication.js @@ -325,6 +325,60 @@ class BaseApplication { return middleware; } + async applySettingsSideEffects(action = null) { + const sideEffects = { + 'dateFormat': async () => { + time.setLocale(Setting.value('locale')); + time.setDateFormat(Setting.value('dateFormat')); + time.setTimeFormat(Setting.value('timeFormat')); + }, + 'net.ignoreTlsErrors': async () => { + process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = Setting.value('net.ignoreTlsErrors') ? '0' : '1'; + }, + 'net.customCertificates': async () => { + const caPaths = Setting.value('net.customCertificates').split(','); + for (let i = 0; i < caPaths.length; i++) { + const f = caPaths[i].trim(); + if (!f) continue; + syswidecas.addCAs(f); + } + }, + 'encryption.enabled': async () => { + if (this.hasGui()) { + await EncryptionService.instance().loadMasterKeysFromSettings(); + DecryptionWorker.instance().scheduleStart(); + const loadedMasterKeyIds = EncryptionService.instance().loadedMasterKeyIds(); + + this.dispatch({ + type: 'MASTERKEY_REMOVE_NOT_LOADED', + ids: loadedMasterKeyIds, + }); + + // Schedule a sync operation so that items that need to be encrypted + // are sent to sync target. + reg.scheduleSync(); + } + }, + 'sync.interval': async () => { + if (this.hasGui()) reg.setupRecurrentSync(); + }, + }; + + sideEffects['timeFormat'] = sideEffects['dateFormat']; + sideEffects['locale'] = sideEffects['dateFormat']; + sideEffects['encryption.activeMasterKeyId'] = sideEffects['encryption.enabled'] + sideEffects['encryption.passwordCache'] = sideEffects['encryption.enabled'] + + if (action) { + const effect = sideEffects[action.key]; + if (effect) await effect(); + } else { + for (const key in sideEffects) { + await sideEffects[key](); + } + } + } + async generalMiddleware(store, next, action) { // this.logger().debug('Reducer action', this.reducerActionToString(action)); @@ -376,58 +430,10 @@ class BaseApplication { refreshNotes = true; } - // if (action.type == 'NOTE_DELETE') { - // refreshTags = true; - // } - if (refreshNotes) { await this.refreshNotes(newState, refreshNotesUseSelectedNoteId); } - // if (refreshTags) { - // this.dispatch({ - // type: 'TAG_UPDATE_ALL', - // items: await Tag.allWithNotes(), - // }); - // } - - if ((action.type == 'SETTING_UPDATE_ONE' && (action.key == 'dateFormat' || action.key == 'timeFormat' || action.key == 'locale')) || action.type == 'SETTING_UPDATE_ALL') { - time.setLocale(Setting.value('locale')); - time.setDateFormat(Setting.value('dateFormat')); - time.setTimeFormat(Setting.value('timeFormat')); - } - - if ((action.type == 'SETTING_UPDATE_ONE' && action.key == 'net.ignoreTlsErrors') || action.type == 'SETTING_UPDATE_ALL') { - // https://stackoverflow.com/questions/20082893/unable-to-verify-leaf-signature - process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = Setting.value('net.ignoreTlsErrors') ? '0' : '1'; - } - - if ((action.type == 'SETTING_UPDATE_ONE' && action.key == 'net.customCertificates') || action.type == 'SETTING_UPDATE_ALL') { - const caPaths = Setting.value('net.customCertificates').split(','); - for (let i = 0; i < caPaths.length; i++) { - const f = caPaths[i].trim(); - if (!f) continue; - syswidecas.addCAs(f); - } - } - - if ((action.type == 'SETTING_UPDATE_ONE' && action.key.indexOf('encryption.') === 0) || action.type == 'SETTING_UPDATE_ALL') { - if (this.hasGui()) { - await EncryptionService.instance().loadMasterKeysFromSettings(); - DecryptionWorker.instance().scheduleStart(); - const loadedMasterKeyIds = EncryptionService.instance().loadedMasterKeyIds(); - - this.dispatch({ - type: 'MASTERKEY_REMOVE_NOT_LOADED', - ids: loadedMasterKeyIds, - }); - - // Schedule a sync operation so that items that need to be encrypted - // are sent to sync target. - reg.scheduleSync(); - } - } - if (action.type === 'NOTE_UPDATE_ONE') { refreshFolders = true; } @@ -436,10 +442,6 @@ class BaseApplication { refreshFolders = 'now'; } - if ((this.hasGui() && action.type == 'SETTING_UPDATE_ONE' && action.key == 'sync.interval') || action.type == 'SETTING_UPDATE_ALL') { - reg.setupRecurrentSync(); - } - if (this.hasGui() && action.type === 'SYNC_GOT_ENCRYPTED_ITEM') { DecryptionWorker.instance().scheduleStart(); } @@ -448,6 +450,12 @@ class BaseApplication { ResourceFetcher.instance().autoAddResources(); } + if (action.type == 'SETTING_UPDATE_ONE') { + await this.applySettingsSideEffects(action); + } else if (action.type == 'SETTING_UPDATE_ALL') { + await this.applySettingsSideEffects(); + } + if (refreshFolders) { if (refreshFolders === 'now') { await FoldersScreenUtils.refreshFolders();