diff --git a/packages/nylas-sync/imap/refresh-mailboxes-operation.js b/packages/nylas-sync/imap/fetch-category-list.js similarity index 95% rename from packages/nylas-sync/imap/refresh-mailboxes-operation.js rename to packages/nylas-sync/imap/fetch-category-list.js index f0d475d5c..9cb46cd7e 100644 --- a/packages/nylas-sync/imap/refresh-mailboxes-operation.js +++ b/packages/nylas-sync/imap/fetch-category-list.js @@ -1,6 +1,6 @@ -class RefreshMailboxesOperation { +class FetchCategoryList { description() { - return `RefreshMailboxesOperation`; + return `FetchCategoryList`; } _roleForMailbox(boxName, box) { @@ -86,4 +86,4 @@ class RefreshMailboxesOperation { } } -module.exports = RefreshMailboxesOperation; +module.exports = FetchCategoryList; diff --git a/packages/nylas-sync/imap/sync-mailbox-operation.js b/packages/nylas-sync/imap/fetch-messages-in-category.js similarity index 83% rename from packages/nylas-sync/imap/sync-mailbox-operation.js rename to packages/nylas-sync/imap/fetch-messages-in-category.js index 56aab99f3..a56dc49b7 100644 --- a/packages/nylas-sync/imap/sync-mailbox-operation.js +++ b/packages/nylas-sync/imap/fetch-messages-in-category.js @@ -5,17 +5,17 @@ const {Capabilities} = IMAPConnection; const MessageFlagAttributes = ['id', 'CategoryUID', 'unread', 'starred'] -class SyncMailboxOperation { +class FetchMessagesInCategory { constructor(category, options) { this._category = category; this._options = options; if (!this._category) { - throw new Error("SyncMailboxOperation requires a category") + throw new Error("FetchMessagesInCategory requires a category") } } description() { - return `SyncMailboxOperation (${this._category.name} - ${this._category.id})\n Options: ${JSON.stringify(this._options)}`; + return `FetchMessagesInCategory (${this._category.name} - ${this._category.id})\n Options: ${JSON.stringify(this._options)}`; } _getLowerBoundUID(count) { @@ -177,36 +177,42 @@ class SyncMailboxOperation { }); } + _shouldRunDeepScan() { + const {timeDeepScan} = this._category.syncState; + return Date.now() - (timeDeepScan || 0) > this._options.deepFolderScan + } + + _runDeepScan(range) { + const {Message} = this.db; + return this._imap.fetchUIDAttributes(range).then((remoteUIDAttributes) => + Message.findAll({ + where: {CategoryId: this._category.id}, + attributes: MessageFlagAttributes, + }).then((localMessageAttributes) => + Promise.props({ + upserts: this._createAndUpdateMessages(remoteUIDAttributes, localMessageAttributes), + deletes: this._removeDeletedMessages(remoteUIDAttributes, localMessageAttributes), + }) + ).then(() => { + return this.updateCategorySyncState({ + highestmodseq: this._box.highestmodseq, + timeDeepScan: Date.now(), + timeShallowScan: Date.now(), + }); + }) + ); + } + _fetchChangesToMessages() { - const {highestmodseq, timeDeepScan} = this._category.syncState; + const {highestmodseq} = this._category.syncState; const nextHighestmodseq = this._box.highestmodseq; - const {Message} = this._db; - const {limit} = this._options; - const range = `${this._getLowerBoundUID(limit)}:*`; + const range = `${this._getLowerBoundUID(this._options.limit)}:*`; console.log(` - fetching changes to messages ${range}`) - const shouldRunDeepScan = Date.now() - (timeDeepScan || 0) > this._options.deepFolderScan - - if (shouldRunDeepScan) { - return this._imap.fetchUIDAttributes(range).then((remoteUIDAttributes) => - Message.findAll({ - where: {CategoryId: this._category.id}, - attributes: MessageFlagAttributes, - }).then((localMessageAttributes) => - Promise.props({ - upserts: this._createAndUpdateMessages(remoteUIDAttributes, localMessageAttributes), - deletes: this._removeDeletedMessages(remoteUIDAttributes, localMessageAttributes), - }) - ).then(() => { - return this.updateCategorySyncState({ - highestmodseq: nextHighestmodseq, - timeDeepScan: Date.now(), - timeShallowScan: Date.now(), - }); - }) - ); + if (this._shouldRunDeepScan()) { + return this._runDeepScan(range) } let shallowFetch = null; @@ -222,7 +228,7 @@ class SyncMailboxOperation { } return shallowFetch.then((remoteUIDAttributes) => - Message.findAll({ + this._db.Message.findAll({ where: {CategoryId: this._category.id}, attributes: MessageFlagAttributes, }).then((localMessageAttributes) => @@ -257,4 +263,4 @@ class SyncMailboxOperation { } } -module.exports = SyncMailboxOperation; +module.exports = FetchMessagesInCategory; diff --git a/packages/nylas-sync/sync-worker.js b/packages/nylas-sync/sync-worker.js index d0c18886c..0e9ab3e54 100644 --- a/packages/nylas-sync/sync-worker.js +++ b/packages/nylas-sync/sync-worker.js @@ -4,8 +4,8 @@ const { DatabaseConnector, } = require('nylas-core'); -const RefreshMailboxesOperation = require('./imap/refresh-mailboxes-operation') -const SyncMailboxOperation = require('./imap/sync-mailbox-operation') +const FetchCategoryList = require('./imap/fetch-category-list') +const FetchMessagesInCategory = require('./imap/fetch-messages-in-category') // // account.syncPolicy = { // afterSync: 'idle', @@ -111,12 +111,12 @@ class SyncWorker { }); } - queueOperationsForUpdates() { + fetchCategoryList() { // todo: syncback operations belong here! - return this._conn.runOperation(new RefreshMailboxesOperation()) + return this._conn.runOperation(new FetchCategoryList()) } - queueOperationsForFolderSyncs() { + fetchMessagesInCategory() { const {Category} = this._db; const {folderSyncOptions} = this._account.syncPolicy; @@ -131,24 +131,21 @@ class SyncWorker { // ) return Promise.all(categoriesToSync.map((cat) => - this._conn.runOperation(new SyncMailboxOperation(cat, folderSyncOptions)) - )).then(() => { - this._lastSyncTime = Date.now(); - }); + this._conn.runOperation(new FetchMessagesInCategory(cat, folderSyncOptions)) + )) }); } syncNow() { clearTimeout(this._syncTimer); - this.ensureConnection().then(() => - this.queueOperationsForUpdates().then(() => - this.queueOperationsForFolderSyncs() - ) - ).catch((err) => { - // Sync has failed for some reason. What do we do?! - console.error(err); - }).finally(() => { + this.ensureConnection() + .then(this.fetchCategoryList.bind(this)) + .then(this.syncbackMessageActions.bind(this)) + .then(this.fetchMessagesInCategory.bind(this)) + .then(() => { this._lastSyncTime = Date.now() }) + .catch(console.error) + .finally(() => { this.onSyncDidComplete(); this.scheduleNextSync(); });