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

35 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-06-30 02:44:30 +08:00
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)
},
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) =>
2016-06-30 03:26:45 +08:00
imap.openBox(category.name, {readOnly: false}).then(() => {
2016-06-30 02:44:30 +08:00
console.log(`Category Box open: ${category.id} | ${category.name}`);
2016-06-30 03:25:38 +08:00
return Promise.all(msgsInCategories[category.id].map((message) =>
2016-06-30 02:44:30 +08:00
callback({message, category})
)).then(() => imap.closeBoxAsync())
})
)
})
},
}
module.exports = TaskHelpers