Mailspring/packages/nylas-sync/syncback_tasks/task-helpers.js

28 lines
937 B
JavaScript
Raw Normal View History

const _ = require('underscore')
2016-06-30 02:44:30 +08:00
const TaskHelpers = {
messagesForThreadByCategory: function messagesForThreadByCategory(db, threadId) {
return db.Thread.findById(threadId).then((thread) => {
return thread.getMessages()
}).then((messages) => {
return _.groupBy(messages, "categoryId")
})
2016-06-30 02:44:30 +08:00
},
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) => {
2016-06-30 03:25:38 +08:00
return Promise.all(msgsInCategories[category.id].map((message) =>
callback({message, category, box})
)).then(() => box.closeBox())
2016-06-30 02:44:30 +08:00
})
)
})
},
}
module.exports = TaskHelpers