Merge pull request #148 from whiteout-io/dev/WO-651

[WO-651] Do not error if imap upload fails after send
This commit is contained in:
Tankred Hase 2014-11-04 11:33:24 +01:00
commit 8e737474f5
2 changed files with 142 additions and 38 deletions

View File

@ -261,7 +261,6 @@ EmailDAO.prototype.openFolder = function(options, callback) {
path: options.folder.path
}, callback);
};
/**
* Synchronizes a folder's contents from disk to memory, i.e. if
* a message has disappeared from the disk, this method will remove it from folder.messages, and
@ -1024,22 +1023,13 @@ EmailDAO.prototype.sendEncrypted = function(options, callback) {
return callback(err);
}
// upload the sent message to the sent folder if necessary
var sentFolder = _.findWhere(self._account.folders, {
type: FOLDER_TYPE_SENT
});
if (self.ignoreUploadOnSent || !sentFolder || !rfcText) {
self.done();
return callback();
}
self._imapClient.uploadMessage({
path: sentFolder.path,
// try to upload to sent, but we don't actually care if the upload failed or not
// this should not negatively impact the process of sending
self._uploadToSent({
message: rfcText
}, function(err) {
}, function() {
self.done();
callback(err);
callback();
});
});
};
@ -1074,22 +1064,13 @@ EmailDAO.prototype.sendPlaintext = function(options, callback) {
return callback(err);
}
// upload the sent message to the sent folder if necessary
var sentFolder = _.findWhere(self._account.folders, {
type: FOLDER_TYPE_SENT
});
if (self.ignoreUploadOnSent || !sentFolder || !rfcText) {
self.done();
return callback();
}
self._imapClient.uploadMessage({
path: sentFolder.path,
// try to upload to sent, but we don't actually care if the upload failed or not
// this should not negatively impact the process of sending
self._uploadToSent({
message: rfcText
}, function(err) {
}, function() {
self.done();
callback(err);
callback();
});
});
};
@ -1602,6 +1583,20 @@ EmailDAO.prototype._imapListMessages = function(options, callback) {
self._imapClient.listMessages(options, callback);
};
/**
* Uploads a built message to a folder
*
* @param {Object} options.folder The folder where to find the message
* @param {String} options.message The rfc2822 compatible raw ASCII e-mail source
* @param {Function} callback (error) The callback when the imap client is done uploading
*/
EmailDAO.prototype._imapUploadMessage = function(options, callback) {
this._imapClient.uploadMessage({
path: options.folder.path,
message: options.message
}, callback);
};
/**
* Stream an email messsage's body
* @param {String} options.folder The folder
@ -1688,6 +1683,48 @@ EmailDAO.prototype._localDeleteMessage = function(options, callback) {
};
//
//
// Internal Helper Methods
//
//
/**
* Uploads a message to the sent folder, if necessary.
* Calls back immediately if ignoreUploadOnSent == true or not sent folder was found.
*
* @param {String} options.message The rfc2822 compatible raw ASCII e-mail source
* @param {Function} callback (error) The callback when the imap client is done uploading
*/
EmailDAO.prototype._uploadToSent = function(options, callback) {
var self = this;
self.busy();
// upload the sent message to the sent folder if necessary
var sentFolder = _.findWhere(self._account.folders, {
type: FOLDER_TYPE_SENT
});
// return for wrong usage
if (self.ignoreUploadOnSent || !sentFolder || !options.message) {
self.done();
return callback();
}
// upload
self._imapUploadMessage({
folder: sentFolder,
message: options.message
}, function(err) {
self.done();
callback(err);
});
};
//
//
// Helper Functions

View File

@ -1572,11 +1572,10 @@ describe('Email DAO unit tests', function() {
var publicKeys = ["PUBLIC KEY"],
dummyMail = {
publicKeysArmored: publicKeys
};
},
msg = 'wow. such message. much rfc2822.';
it('should send encrypted and upload to sent', function(done) {
var msg = 'wow. such message. much rfc2822.';
imapClientStub.uploadMessage.withArgs({
path: sentFolder.path,
message: msg
@ -1602,8 +1601,6 @@ describe('Email DAO unit tests', function() {
});
it('should send encrypted and not upload to sent', function(done) {
var msg = 'wow. such message. much rfc2822.';
dao.ignoreUploadOnSent = true;
pgpMailerStub.send.withArgs({
@ -1625,6 +1622,22 @@ describe('Email DAO unit tests', function() {
});
});
it('should send encrypted and ignore error on upload', function(done) {
imapClientStub.uploadMessage.yields(new Error());
pgpMailerStub.send.yieldsAsync(null, msg);
dao.sendEncrypted({
email: dummyMail
}, function(err) {
expect(err).to.not.exist;
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
done();
});
});
it('should not send when pgpmailer fails', function(done) {
pgpMailerStub.send.yieldsAsync({});
@ -1655,10 +1668,9 @@ describe('Email DAO unit tests', function() {
describe('#sendPlaintext', function() {
var dummyMail = {};
var msg = 'wow. such message. much rfc2822.';
it('should send in the plain and upload to sent', function(done) {
var msg = 'wow. such message. much rfc2822.';
pgpMailerStub.send.withArgs({
smtpclient: undefined,
mail: dummyMail
@ -1680,8 +1692,6 @@ describe('Email DAO unit tests', function() {
});
it('should send in the plain and not upload to sent', function(done) {
var msg = 'wow. such message. much rfc2822.';
dao.ignoreUploadOnSent = true;
pgpMailerStub.send.withArgs({
@ -1699,6 +1709,22 @@ describe('Email DAO unit tests', function() {
});
});
it('should send and ignore error on upload', function(done) {
imapClientStub.uploadMessage.yields(new Error());
pgpMailerStub.send.yieldsAsync(null, msg);
dao.sendEncrypted({
email: dummyMail
}, function(err) {
expect(err).to.not.exist;
expect(pgpMailerStub.send.calledOnce).to.be.true;
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
done();
});
});
it('should not send due to error', function(done) {
pgpMailerStub.send.yieldsAsync({});
@ -2211,6 +2237,27 @@ describe('Email DAO unit tests', function() {
});
});
describe('#_imapUploadMessage', function() {
it('should upload a message', function(done) {
var msg = 'wow. such message. much rfc2822.';
imapClientStub.uploadMessage.withArgs({
path: draftsFolder.path,
message: msg
}).yields();
dao._imapUploadMessage({
folder: draftsFolder,
message: msg
}, function(err) {
expect(err).to.not.exist;
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
done();
});
});
});
describe('#_getBodyParts', function() {
it('should get bodyParts', function(done) {
imapClientStub.getBodyParts.withArgs({
@ -2319,5 +2366,25 @@ describe('Email DAO unit tests', function() {
});
});
describe('#_uploadToSent', function() {
it('should upload', function(done) {
var msg = 'wow. such message. much rfc2822.';
imapClientStub.uploadMessage.withArgs({
path: sentFolder.path,
message: msg
}).yields();
dao._uploadToSent({
message: msg
}, function(err) {
expect(err).to.not.exist;
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
done();
});
});
});
});
});