diff --git a/src/js/bo/outbox.js b/src/js/bo/outbox.js index f488678..dce5102 100644 --- a/src/js/bo/outbox.js +++ b/src/js/bo/outbox.js @@ -12,9 +12,9 @@ define(function(require) { * The local outbox takes care of the emails before they are being sent. * It also checks periodically if there are any mails in the local device storage to be sent. */ - var OutboxBO = function(email, keychain, devicestorage, invitation) { + var OutboxBO = function(emailDao, keychain, devicestorage, invitationDao) { /** @private */ - this._email = email; + this._emailDao = emailDao; /** @private */ this._keychain = keychain; @@ -24,7 +24,7 @@ define(function(require) { /** @private */ - this._invitation = invitation; + this._invitationDao = invitationDao; /** * Semaphore-esque flag to avoid 'concurrent' calls to _processOutbox when the timeout fires, but a call is still in process. @@ -79,7 +79,7 @@ define(function(require) { self._outboxBusy = true; // get last item from outbox - self._email.list(function(err, pending) { + self._emailDao.list(function(err, pending) { if (err) { self._outboxBusy = false; callback(err); @@ -147,7 +147,7 @@ define(function(require) { // invite the unregistered receivers, if necessary function invite(addresses) { - var sender = self._email._account.emailAddress; + var sender = self._emailDao._account.emailAddress; var invitationFinished = _.after(addresses.length, function() { // after all of the invitations are checked and sent (if necessary), @@ -158,7 +158,7 @@ define(function(require) { addresses.forEach(function(recipient) { var recipientAddress = recipient.address; - self._invitation.check({ + self._invitationDao.check({ recipient: recipientAddress, sender: sender }, function(err, status) { @@ -175,7 +175,7 @@ define(function(require) { } // the recipient is not yet invited, so let's do that - self._invitation.invite({ + self._invitationDao.invite({ recipient: recipientAddress, sender: sender }, function(err, status) { @@ -209,7 +209,7 @@ define(function(require) { }; // send invitation mail - self._email.send(invitationMail, function(err) { + self._emailDao.send(invitationMail, function(err) { if (err) { self._outboxBusy = false; callback(err); @@ -222,7 +222,7 @@ define(function(require) { function sendEncrypted(email) { removeFromPendingMails(email); - self._email.encryptedSend(email, function(err) { + self._emailDao.encryptedSend(email, function(err) { if (err) { self._outboxBusy = false; callback(err); diff --git a/src/js/dao/email-dao-2.js b/src/js/dao/email-dao-2.js index d2f54f4..b9cc8b1 100644 --- a/src/js/dao/email-dao-2.js +++ b/src/js/dao/email-dao-2.js @@ -106,6 +106,10 @@ define(function(require) { return; } + handleGenerated(generatedKeypair); + }); + + function handleGenerated(generatedKeypair) { // import the new key pair into crypto module self._crypto.importKeys({ passphrase: options.passphrase, @@ -132,7 +136,7 @@ define(function(require) { }; self._keychain.putUserKeyPair(newKeypair, callback); }); - }); + } }; EmailDAO.prototype.sync = function(options, callback) { @@ -166,6 +170,13 @@ define(function(require) { // initial filling from local storage is an exception from the normal sync. // after reading from local storage, do imap sync if (!isFolderInitialized) { + initFolderMessages(); + return; + } + + doLocalDelta(); + + function initFolderMessages() { folder.messages = []; self._localListMessages({ folder: folder.path @@ -193,12 +204,8 @@ define(function(require) { }); }); }); - - return; } - doLocalDelta(); - function doLocalDelta() { self._localListMessages({ folder: folder.path @@ -232,8 +239,18 @@ define(function(require) { uid: message.uid }; - self._imapDeleteMessage(deleteMe, function() { - self._localDeleteMessage(deleteMe, function() { + self._imapDeleteMessage(deleteMe, function(err) { + if (err) { + callback(err); + return; + } + + self._localDeleteMessage(deleteMe, function(err) { + if (err) { + callback(err); + return; + } + after(); }); }); @@ -336,7 +353,7 @@ define(function(require) { return; } - // encrypt and add to folder in memory + // decrypt and add to folder in memory handleMessage(message, function(err, cleartextMessage) { if (err) { callback(err); @@ -364,8 +381,7 @@ define(function(require) { for (i = a.length - 1; i >= 0; i--) { msg = a[i]; exists = _.findWhere(b, { - uid: msg.uid, - subject: msg.subject + uid: msg.uid }); if (!exists) { delta.push(msg); @@ -450,7 +466,7 @@ define(function(require) { }, callback); }; - EmailDAO.prototype.encryptedSend = function(options, callback) { + EmailDAO.prototype.sendEncrypted = function(options, callback) { var self = this, email = options.email; @@ -505,14 +521,12 @@ define(function(require) { return; } - self.send({ - email: email - }, callback); + self._smtpClient.send(email, callback); }); }); }; - EmailDAO.prototype.send = function(options, callback) { + EmailDAO.prototype.sendPlaintext = function(options, callback) { this._smtpClient.send(options.email, callback); }; @@ -577,6 +591,12 @@ define(function(require) { }; EmailDAO.prototype._localDeleteMessage = function(options, callback) { + if (!options.folder || !options.uid) { + callback({ + errMsg: 'Invalid options!' + }); + return; + } var dbType = 'email_' + options.folder + '_' + options.uid; this._devicestorage.removeList(dbType, callback); }; diff --git a/test/new-unit/email-dao-2-test.js b/test/new-unit/email-dao-2-test.js index beee393..0dafbe7 100644 --- a/test/new-unit/email-dao-2-test.js +++ b/test/new-unit/email-dao-2-test.js @@ -523,6 +523,17 @@ define(function(require) { uid: uid }, done); }); + + it('should fail when uid is missing', function(done) { + var folder = 'FOLDAAAA'; + + dao._localDeleteMessage({ + folder: folder + }, function(err) { + expect(err).to.exist; + done(); + }); + }); }); describe('sync', function() { @@ -727,11 +738,11 @@ define(function(require) { }); }); - describe('send', function() { + describe('sendPlaintext', function() { it('should work', function(done) { smtpClientStub.send.withArgs(dummyEncryptedMail).yields(); - dao.send({ + dao.sendPlaintext({ email: dummyEncryptedMail }, function(err) { expect(err).to.not.exist; @@ -741,7 +752,7 @@ define(function(require) { }); }); - describe('encryptedSend', function() { + describe('sendEncrypted', function() { it('should work', function(done) { var encryptStub = sinon.stub(dao, '_encrypt').yields(null, {}); keychainStub.getReceiverPublicKey.withArgs(dummyDecryptedMail.to[0].address).yields(null, { @@ -751,7 +762,7 @@ define(function(require) { }); smtpClientStub.send.yields(); - dao.encryptedSend({ + dao.sendEncrypted({ email: dummyDecryptedMail }, function(err) { expect(err).to.not.exist; @@ -771,7 +782,7 @@ define(function(require) { publicKey: publicKey }); - dao.encryptedSend({ + dao.sendEncrypted({ email: dummyDecryptedMail }, function(err) { expect(err).to.exist; @@ -787,7 +798,7 @@ define(function(require) { var encryptStub = sinon.stub(dao, '_encrypt'); keychainStub.getReceiverPublicKey.withArgs(dummyDecryptedMail.to[0].address).yields({}); - dao.encryptedSend({ + dao.sendEncrypted({ email: dummyDecryptedMail }, function(err) { expect(err).to.exist; @@ -803,7 +814,7 @@ define(function(require) { var encryptStub = sinon.stub(dao, '_encrypt'); dummyDecryptedMail.to[0].address = 'asd@asd'; - dao.encryptedSend({ + dao.sendEncrypted({ email: dummyDecryptedMail }, function(err) { expect(err).to.exist; @@ -819,7 +830,7 @@ define(function(require) { var encryptStub = sinon.stub(dao, '_encrypt'); dummyDecryptedMail.from[0].address = 'asd@asd'; - dao.encryptedSend({ + dao.sendEncrypted({ email: dummyDecryptedMail }, function(err) { expect(err).to.exist; @@ -835,7 +846,7 @@ define(function(require) { var encryptStub = sinon.stub(dao, '_encrypt'); delete dummyDecryptedMail.to; - dao.encryptedSend({ + dao.sendEncrypted({ email: dummyDecryptedMail }, function(err) { expect(err).to.exist; @@ -851,7 +862,7 @@ define(function(require) { var encryptStub = sinon.stub(dao, '_encrypt'); delete dummyDecryptedMail.from; - dao.encryptedSend({ + dao.sendEncrypted({ email: dummyDecryptedMail }, function(err) { expect(err).to.exist; diff --git a/test/new-unit/outbox-bo-test.js b/test/new-unit/outbox-bo-test.js index 527d449..6d3bb79 100644 --- a/test/new-unit/outbox-bo-test.js +++ b/test/new-unit/outbox-bo-test.js @@ -29,10 +29,10 @@ define(function(require) { describe('init', function() { it('should work', function() { expect(outbox).to.exist; - expect(outbox._email).to.equal(emailDaoStub); + expect(outbox._emailDao).to.equal(emailDaoStub); expect(outbox._keychain).to.equal(keychainStub); expect(outbox._devicestorage).to.equal(devicestorageStub); - expect(outbox._invitation).to.equal(invitationDaoStub); + expect(outbox._invitationDao).to.equal(invitationDaoStub); expect(outbox._outboxBusy).to.be.false; expect(outbox.pendingEmails).to.be.empty; });