Skip to content

Commit

Permalink
CLI: Fixes #1779: Make sure setting side-effects are applied even whe…
Browse files Browse the repository at this point in the history
…n running in command line mode
  • Loading branch information
laurent22 committed Sep 6, 2019
1 parent 11f23f4 commit 9abc6a2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CliClient/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
112 changes: 60 additions & 52 deletions ReactNativeClient/lib/BaseApplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}
Expand All @@ -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();
Expand Down

0 comments on commit 9abc6a2

Please sign in to comment.