Fix to use Imap BOX and simplify move helper

This commit is contained in:
Evan Morikawa 2016-06-29 16:25:51 -07:00
parent c7fa9a782f
commit 6cb67e417c
3 changed files with 28 additions and 33 deletions

View file

@ -102,6 +102,22 @@ class IMAPBox {
f.once('end', () => resolve(attributesByUID));
});
}
moveFromBox(range, categoryName) {
if (!this._imap) {
throw new Error(`IMAPConnection::move - You need to call connect() first.`)
}
return this._imap.moveAsync(range, categoryName)
}
closeBox({expunge = true} = {}) {
if (!this._imap) {
throw new Error(`IMAPConnection::closeBox - You need to call connect() first.`)
}
return this._imap.closeBoxAsync(expunge)
}
}
@ -228,13 +244,6 @@ class IMAPConnection extends EventEmitter {
)
}
closeBox({expunge = true} = {}) {
if (!this._imap) {
throw new Error(`IMAPConnection::closeBox - You need to call connect() first.`)
}
return this._imap.closeBoxAsync(expunge)
}
getBoxes() {
if (!this._imap) {
throw new Error(`IMAPConnection::getBoxes - You need to call connect() first.`)
@ -249,13 +258,6 @@ class IMAPConnection extends EventEmitter {
return this._imap.addFlagsAsync(messageSrc, flags)
}
move(messageSrc, categoryName) {
if (!this._imap) {
throw new Error(`IMAPConnection::move - You need to call connect() first.`)
}
return this._imap.moveAsync(messageSrc, categoryName)
}
runOperation(operation) {
if (!this._imap) {
throw new Error(`IMAPConnection::runOperation - You need to call connect() first.`)

View file

@ -10,9 +10,9 @@ class MoveToFolderIMAP extends SyncbackTask {
const threadId = this.syncbackRequestObject().props.threadId
const toFolderId = this.syncbackRequestObject().props.folderId
const eachMsg = ({message}) => {
const eachMsg = ({message, box}) => {
return db.Category.findById(toFolderId).then((category) => {
return imap.move(message.categoryUID, category.name)
return box.moveFromBox(message.categoryUID, category.name)
})
}

View file

@ -1,31 +1,24 @@
const _ = require('underscore')
const TaskHelpers = {
messagesForThreadByCategory: function messagesForThreadByCategory(db, threadId) {
const msgsInCategories = {};
return db.Thread.findById(threadId).then((thread) =>
thread.getMessages().each((message) => {
if (!msgsInCategories[message.categoryId]) {
msgsInCategories[message.categoryId] = [message];
} else {
msgsInCategories.push(message);
}
})
).then(() => msgsInCategories)
return db.Thread.findById(threadId).then((thread) => {
return thread.getMessages()
}).then((messages) => {
return _.groupBy(messages, "categoryId")
})
},
forEachMessageInThread: function forEachMessageInThread({threadId, db, imap, callback}) {
console.log("FOR EACH MESSAGE IN THREAD")
return TaskHelpers.messagesForThreadByCategory(db, threadId)
.then((msgsInCategories) => {
const cids = Object.keys(msgsInCategories);
console.log(`Messages in categories: ${cids}`)
return db.Category.findAll({where: {id: cids}})
.each((category) =>
imap.openBox(category.name, {readOnly: false}).then(() => {
console.log(`Category Box open: ${category.id} | ${category.name}`);
imap.openBox(category.name, {readOnly: false}).then((box) => {
return Promise.all(msgsInCategories[category.id].map((message) =>
callback({message, category})
)).then(() => imap.closeBox())
callback({message, category, box})
)).then(() => box.closeBox())
})
)
})