[local-sync] Fix “Add INBOX label” issue

This commit is contained in:
Ben Gotow 2016-11-30 16:25:24 -08:00
parent d8703b90c4
commit 94722f27ab

View file

@ -6,37 +6,28 @@ class SetThreadLabelsIMAP extends SyncbackTask {
return `SetThreadLabels`;
}
run(db, imap) {
async run(db, imap) {
const threadId = this.syncbackRequestObject().props.threadId
const labelIds = this.syncbackRequestObject().props.labelIds
if (!labelIds || labelIds.length === 0) {
return TaskHelpers.forEachMessageInThread({
db,
imap,
threadId,
callback: ({message, box}) => {
return message.getLabels().then((labels) => {
const labelNames = labels.map(({name}) => name)
return box.removeLabels(message.folderImapUID, labelNames)
})
},
})
}
const labels = await db.Label.findAll({where: {id: labelIds}});
const gmailLabelIdentifiers = labels.map((label) => {
if (label.role) {
return `\\${label.role[0].toUpperCase()}${label.role.slice(1)}`
}
return label.name;
});
// Ben TODO this is super inefficient because it makes IMAP requests
// one UID at a time, rather than gathering all the UIDs and making
// a single removeLabels call.
return TaskHelpers.forEachMessageInThread({
db,
imap,
threadId,
callback: ({message, box}) => {
return db.Label.findAll({
where: {
id: {'in': labelIds},
},
})
.then((labels) => {
const labelNames = labels.map(({name}) => name)
return box.setLabels(message.folderImapUID, labelNames)
})
return box.setLabels(message.folderImapUID, gmailLabelIdentifiers)
},
})
}