Mailspring/packages/local-sync/src/local-sync-worker/syncback_tasks/save-sent-message.imap.js
Juan Tejada fda7fe18cc [local-sync] Ensure order correct order when running syncback requests
When running syncback requests, if a task is meant to change the UID of
a message (e.g. move it to a new folder), that task should be run after
other tasks that don't affect the UID. Otherwise, when trying to run the
other tasks, they would reference a UID that is no longer valid.

This commit will make sure that we run any tasks that will change message uids last,
/and/ make sure that we don't run more than 1 task that will affect the uids of
the same messages in a row (i.e. without running a sync loop in between)
2016-12-06 15:45:34 -08:00

37 lines
1.1 KiB
JavaScript

const SyncbackTask = require('./syncback-task')
class SaveSentMessageIMAP extends SyncbackTask {
description() {
return `SaveSentMessage`;
}
affectsImapMessageUIDs() {
return false
}
async run(db, imap) {
const {rawMime, messageId} = this.syncbackRequestObject().props;
// Non-gmail
const sentFolder = await db.Folder.find({where: {role: 'sent'}});
if (sentFolder) {
const box = await imap.openBox(sentFolder.name);
return box.append(rawMime);
}
// Gmail, we need to add the message to all mail and add the sent label
const sentLabel = await db.Label.find({where: {role: 'sent'}});
const allMail = await db.Folder.find({where: {role: 'all'}});
if (sentLabel && allMail) {
let box = await imap.openBox(allMail.name);
await box.append(rawMime, {flags: 'SEEN'})
const uids = await box.search([['HEADER', 'Message-ID', messageId]])
// There should only be one uid in the array
return box.setLabels(uids[0], '\\Sent');
}
throw new Error('Could not save message to sent folder.')
}
}
module.exports = SaveSentMessageIMAP;