From e29fe2ee9b0ee10c679322c0b4c0c0a654ec3ab0 Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Fri, 27 Jan 2017 12:15:10 -0800 Subject: [PATCH] [local-sync] When fetching /new/ messages, make sure we have fetchedmax Summary: In `FetchNewMessagesInFolder`, sometimes we haven't synced anything in the folder we are trying to fetch new messages in. Previously this would just throw an error, now we properly check if we have a fetchedmax, and if not just run a normal fetch. Also, when the target folder box was already open, we were not fetching the /latest/ box status to check the latest uidnext value, so we would skip fetching new messages when in fact there were new messages to fetch (This can happen for example when moving a sent message to the Sent folder before we've started syncing the Sent folder) Test Plan: manual Reviewers: halla, mark, evan Reviewed By: evan Differential Revision: https://phab.nylas.com/D3802 --- .../fetch-new-messages-in-folder.imap.es6 | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/local-sync/src/local-sync-worker/sync-tasks/fetch-new-messages-in-folder.imap.es6 b/packages/local-sync/src/local-sync-worker/sync-tasks/fetch-new-messages-in-folder.imap.es6 index 6e1110251..21d58bbaa 100644 --- a/packages/local-sync/src/local-sync-worker/sync-tasks/fetch-new-messages-in-folder.imap.es6 +++ b/packages/local-sync/src/local-sync-worker/sync-tasks/fetch-new-messages-in-folder.imap.es6 @@ -17,15 +17,23 @@ class FetchNewMessagesInFolderIMAP extends FetchMessagesInFolderIMAP { console.log(`🔜 📂 🆕 Looking for new messages in ${this._folder.name}`) this._db = db; this._imap = imap; + const {syncState: {fetchedmax}} = this._folder - this._box = await this._imap.openBox(this._folder.name) + if (!fetchedmax) { + // Without a fetchedmax, can't tell what's new! + // If we haven't fetched anything on this folder, let's run a normal fetch + // operation + yield super.runTask(db, imap) + return + } - if (this._shouldFetchMessages(this._box)) { + const latestBoxStatus = yield this._imap.getLatestBoxStatus(this._folder.name) + if (latestBoxStatus.uidnext > fetchedmax) { + this._box = await this._imap.openBox(this._folder.name) const boxUidnext = this._box.uidnext - const {syncState: {fetchedmax}} = this._folder yield this._fetchAndProcessMessages({min: fetchedmax, max: boxUidnext}); } else { - console.log(`🔚 📂 ${this._folder.name} has no new messages - skipping fetch messages`) + console.log(`🔚 📂 🆕$ {this._folder.name} has no new messages - skipping fetch messages`) } console.log(`🔚 📂 ${this._folder.name} done`) }