diff --git a/src/js/dao/email-dao-2.js b/src/js/dao/email-dao-2.js index 56fd7c0..daf0add 100644 --- a/src/js/dao/email-dao-2.js +++ b/src/js/dao/email-dao-2.js @@ -809,5 +809,64 @@ define(function(require) { } }; + // to be removed and solved with IMAP! + EmailDAO.prototype.store = function(email, callback) { + var self = this, + dbType = 'email_OUTBOX'; + + email.id = util.UUID(); + + // encrypt + self._encrypt({ + email: email + }, function(err, email) { + if (err) { + callback(err); + return; + } + + // store to local storage + self._devicestorage.storeList([email], dbType, callback); + }); + }; + + // to be removed and solved with IMAP! + EmailDAO.prototype.list = function(callback) { + var self = this, + dbType = 'email_OUTBOX'; + + self._devicestorage.listItems(dbType, 0, null, function(err, mails) { + if (err) { + callback(err); + return; + } + + if (mails.length === 0) { + callback(null, []); + return; + } + + self._crypto.exportKeys(function(err, ownKeys) { + if (err) { + callback(err); + return; + } + + var after = _.after(mails.length, function() { + callback(null, mails); + }); + + mails.forEach(function(mail) { + mail.body = str.cryptPrefix + mail.body.split(str.cryptPrefix)[1].split(str.cryptSuffix)[0] + str.cryptSuffix; + self._crypto.decrypt(mail.body, ownKeys.publicKeyArmored, function(err, decrypted) { + mail.body = err ? err.errMsg : decrypted; + after(); + }); + }); + + }); + }); + }; + return EmailDAO; }); \ No newline at end of file diff --git a/test/new-unit/email-dao-2-test.js b/test/new-unit/email-dao-2-test.js index 8963202..0616212 100644 --- a/test/new-unit/email-dao-2-test.js +++ b/test/new-unit/email-dao-2-test.js @@ -1636,5 +1636,48 @@ define(function(require) { }); }); }); + + describe('store', function() { + it('should work', function(done) { + pgpStub.exportKeys.yields(null, { + publicKeyArmored: 'omgsocrypto' + }); + pgpStub.encrypt.yields(null, 'asdfasfd'); + devicestorageStub.storeList.yields(); + + dao.store(dummyDecryptedMail, function(err) { + expect(err).to.not.exist; + expect(pgpStub.exportKeys.calledOnce).to.be.true; + expect(pgpStub.encrypt.calledOnce).to.be.true; + expect(devicestorageStub.storeList.calledOnce).to.be.true; + + done(); + }); + }); + }); + + describe('list', function() { + it('should work', function(done) { + devicestorageStub.listItems.yields(null, [dummyEncryptedMail]); + pgpStub.exportKeys.yields(null, { + publicKeyArmored: 'omgsocrypto' + }); + pgpStub.decrypt.yields(null, dummyDecryptedMail.body); + + dao.list(function(err, mails) { + expect(err).to.not.exist; + + expect(devicestorageStub.listItems.calledOnce).to.be.true; + expect(pgpStub.exportKeys.calledOnce).to.be.true; + expect(pgpStub.decrypt.calledOnce).to.be.true; + expect(mails.length).to.equal(1); + expect(mails[0].body).to.equal(dummyDecryptedMail.body); + expect(mails[0].subject).to.equal(dummyDecryptedMail.subject); + + done(); + }); + }); + }); + }); }); \ No newline at end of file