[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 labelIds = this.syncbackRequestObject().props.labelIds
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;
});
if (!labelIds || labelIds.length === 0) {
return TaskHelpers.forEachMessageInThread({
db,
imap,
threadId,
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
// one UID at a time, rather than gathering all the UIDs and making
@ -27,7 +35,7 @@ class SetThreadLabelsIMAP extends SyncbackTask {
imap,
threadId,
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: {
toJSON: function toJSON() {
imapLabelIdentifier() {
if (this.role) {
return `\\${this.role[0].toUpperCase()}${this.role.slice(1)}`
}
return this.name;
},
toJSON() {
return {
id: `${this.id}`,
account_id: this.accountId,