From efb3a04bb419d1f248adb819d1e1e44f81cc392d Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Mon, 26 Feb 2018 11:41:32 -0800 Subject: [PATCH] Reset the local cache for an account when removing it #724 --- app/src/flux/mailsync-bridge.es6 | 35 +++++++++++++++++---------- app/src/flux/stores/account-store.es6 | 8 +++++- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/src/flux/mailsync-bridge.es6 b/app/src/flux/mailsync-bridge.es6 index e2e0552dd..c32e2cac6 100644 --- a/app/src/flux/mailsync-bridge.es6 +++ b/app/src/flux/mailsync-bridge.es6 @@ -200,7 +200,7 @@ export default class MailsyncBridge { this._clients[accountId].sendMessage(json); } - async resetCacheForAccount(account) { + async resetCacheForAccount(account, { silent } = {}) { // grab the existing client, if there is one const syncingClient = this._clients[account.id]; @@ -208,6 +208,11 @@ export default class MailsyncBridge { const resetClient = new MailsyncProcess(this._getClientConfiguration()); resetClient.account = (await KeyManager.insertAccountSecrets(account)).toJSON(); resetClient.identity = IdentityStore.identity(); + + // no-op - do not allow us to kill this client - we may be reseting the cache of an + // account which does not exist anymore, but we don't want to interrupt this process + resetClient.kill = () => {}; + this._clients[account.id] = resetClient; // kill the old client, ensureClients will be a no-op because the @@ -216,24 +221,28 @@ export default class MailsyncBridge { syncingClient.kill(); } - AppEnv.showErrorDialog({ - title: `Cleanup Started`, - message: `Mailspring is clearing it's cache for ${ - account.emailAddress - }. Depending on the size of the mailbox, this may take a few seconds or a few minutes. An alert will appear when cleanup is complete.`, - }); + if (!silent) { + AppEnv.showErrorDialog({ + title: `Cleanup Started`, + message: `Mailspring is clearing it's cache for ${ + account.emailAddress + }. Depending on the size of the mailbox, this may take a few seconds or a few minutes. An alert will appear when cleanup is complete.`, + }); + } try { const start = Date.now(); await resetClient.resetCache(); - AppEnv.showErrorDialog({ - title: `Cleanup Complete`, - message: `Mailspring reset the local cache for ${account.emailAddress} in ${Math.ceil( - (Date.now() - start) / 1000 - )} seconds. Your mailbox will now begin to sync again.`, - }); + if (!silent) { + AppEnv.showErrorDialog({ + title: `Cleanup Complete`, + message: `Mailspring reset the local cache for ${account.emailAddress} in ${Math.ceil( + (Date.now() - start) / 1000 + )} seconds. Your mailbox will now begin to sync again.`, + }); + } } catch (error) { AppEnv.showErrorDialog({ title: `Cleanup Error`, diff --git a/app/src/flux/stores/account-store.es6 b/app/src/flux/stores/account-store.es6 index 005d32a23..53a951ec4 100644 --- a/app/src/flux/stores/account-store.es6 +++ b/app/src/flux/stores/account-store.es6 @@ -174,7 +174,6 @@ class AccountStore extends MailspringStore { _onRemoveAccount = id => { const account = this._accounts.find(a => a.id === id); if (!account) return; - KeyManager.deleteAccountSecrets(account); this._caches = {}; @@ -193,10 +192,17 @@ class AccountStore extends MailspringStore { this._save(); if (remainingAccounts.length === 0) { + // Clear everything and logout const ipc = require('electron').ipcRenderer; ipc.send('command', 'application:relaunch-to-initial-windows', { resetDatabase: true, }); + } else { + // Clear the cached data for the account and reset secrets once that has completed + AppEnv.mailsyncBridge.resetCacheForAccount(account, { silent: true }).then(() => { + console.log('Account removal complete.'); + KeyManager.deleteAccountSecrets(account); + }); } };