Mailspring/packages/nylas-sync/syncback_tasks/task-helpers.js
2016-06-29 16:25:51 -07:00

27 lines
937 B
JavaScript

const _ = require('underscore')
const TaskHelpers = {
messagesForThreadByCategory: function messagesForThreadByCategory(db, threadId) {
return db.Thread.findById(threadId).then((thread) => {
return thread.getMessages()
}).then((messages) => {
return _.groupBy(messages, "categoryId")
})
},
forEachMessageInThread: function forEachMessageInThread({threadId, db, imap, callback}) {
return TaskHelpers.messagesForThreadByCategory(db, threadId)
.then((msgsInCategories) => {
const cids = Object.keys(msgsInCategories);
return db.Category.findAll({where: {id: cids}})
.each((category) =>
imap.openBox(category.name, {readOnly: false}).then((box) => {
return Promise.all(msgsInCategories[category.id].map((message) =>
callback({message, category, box})
)).then(() => box.closeBox())
})
)
})
},
}
module.exports = TaskHelpers