[local-sync] Fix setlabels task

Make sure we /remove/ the labels when we get an empty set of label ids
This commit is contained in:
Juan Tejada 2016-12-01 13:39:46 -08:00
parent aba77ca637
commit ce27b0db2a
2 changed files with 24 additions and 9 deletions

View file

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

View file

@ -36,7 +36,14 @@ module.exports = (sequelize, Sequelize) => {
}, },
}, },
instanceMethods: { instanceMethods: {
toJSON: function toJSON() { imapLabelIdentifier() {
if (this.role) {
return `\\${this.role[0].toUpperCase()}${this.role.slice(1)}`
}
return this.name;
},
toJSON() {
return { return {
id: `${this.id}`, id: `${this.id}`,
account_id: this.accountId, account_id: this.accountId,