diff --git a/packages/isomorphic-core/src/imap-connection.js b/packages/isomorphic-core/src/imap-connection.js index a93af5e80..c812434e9 100644 --- a/packages/isomorphic-core/src/imap-connection.js +++ b/packages/isomorphic-core/src/imap-connection.js @@ -192,14 +192,14 @@ class IMAPConnection extends EventEmitter { /** * @return {Promise} that resolves to instance of IMAPBox */ - openBox(folderName, {readOnly = false} = {}) { + openBox(folderName, {readOnly = false, forceOpen = false} = {}) { if (!folderName) { throw new Error('IMAPConnection::openBox - You must provide a folder name') } if (!this._imap) { throw new IMAPConnectionNotReadyError(`IMAPConnection::openBox`) } - if (folderName === this.getOpenBoxName()) { + if (!forceOpen && folderName === this.getOpenBoxName()) { return Promise.resolve(new IMAPBox(this, this._imap._box)); } this._isOpeningBox = true @@ -213,13 +213,14 @@ class IMAPConnection extends EventEmitter { }) } - getBoxStatus(folderName) { + getLatestBoxStatus(folderName) { if (!folderName) { - throw new Error('IMAPConnection::getBoxStatus - You must provide a folder name') + throw new Error('IMAPConnection::getLatestBoxStatus - You must provide a folder name') } - const openBoxName = this.getOpenBoxName() - if (openBoxName === folderName) { - return this._imap._box + if (folderName === this.getOpenBoxName()) { + // If the box is already open, we need to re-issue a SELECT in order to + // get the latest stats from the box (e.g. latest uidnext, etc) + return this.openBox(folderName, {forceOpen: true}) } return this.createConnectionPromise((resolve, reject) => { return this._imap.statusAsync(folderName) diff --git a/packages/local-sync/src/local-sync-worker/sync-tasks/fetch-messages-in-folder.imap.es6 b/packages/local-sync/src/local-sync-worker/sync-tasks/fetch-messages-in-folder.imap.es6 index 42250c085..e31fbd4ec 100644 --- a/packages/local-sync/src/local-sync-worker/sync-tasks/fetch-messages-in-folder.imap.es6 +++ b/packages/local-sync/src/local-sync-worker/sync-tasks/fetch-messages-in-folder.imap.es6 @@ -283,7 +283,7 @@ class FetchMessagesInFolderIMAP extends SyncTask { * `Interruptible` */ async * _openMailboxAndEnsureValidity() { - const box = yield this._imap.openBox(this._folder.name); + const box = yield this._imap.openBox(this._folder.name, {forceOpen: true}); if (box.persistentUIDs === false) { throw new Error("Mailbox does not support persistentUIDs."); @@ -488,7 +488,7 @@ class FetchMessagesInFolderIMAP extends SyncTask { return true } - const boxStatus = yield this._imap.getBoxStatus(this._folder.name) + const boxStatus = yield this._imap.getLatestBoxStatus(this._folder.name) const {syncState} = this._folder const hasNewMessages = boxStatus.uidnext > syncState.fetchedmax