1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-22 15:28:49 -05:00

cleanup error handling in email dao

This commit is contained in:
Tankred Hase 2013-10-04 15:47:30 +02:00
parent 6e6012bd78
commit f045a71ebe
2 changed files with 25 additions and 19 deletions

View File

@ -320,14 +320,10 @@ define(function(require) {
callback(err);
return;
}
if (emails.length === 0) {
callback(null, []);
return;
}
// find encrypted items
emails.forEach(function(i) {
if (i.body.indexOf(str.cryptPrefix) !== -1 && i.body.indexOf(str.cryptSuffix) !== -1) {
if (typeof i.body === 'string' && i.body.indexOf(str.cryptPrefix) !== -1 && i.body.indexOf(str.cryptSuffix) !== -1) {
// add item to plaintext list for display later
displayList.push(i);
// parse ct object from ascii armored message block
@ -335,6 +331,11 @@ define(function(require) {
}
});
if (encryptedList.length === 0) {
callback(null, []);
return;
}
// decrypt items
decryptList(encryptedList, function(err, decryptedList) {
if (err) {
@ -425,17 +426,21 @@ define(function(require) {
return;
}
fetchList(function(emails) {
fetchList(function(err, emails) {
if (err) {
callback(err);
return;
}
// delete old items from db
self._devicestorage.removeList(dbType, function(err) {
if (err) {
callback(err);
return;
}
// persist encrypted list in device storage
self._devicestorage.storeList(emails, dbType, function(err) {
callback(err);
});
self._devicestorage.storeList(emails, dbType, callback);
});
});
@ -449,7 +454,7 @@ define(function(require) {
num: options.num
}, function(err, emails) {
if (err) {
console.log(err);
callback(err);
return;
}
@ -461,9 +466,7 @@ define(function(require) {
});
// fetch message bodies
fetchBodies(encryptedList, function(messages) {
callback(messages);
});
fetchBodies(encryptedList, callback);
});
}
@ -471,12 +474,12 @@ define(function(require) {
var emails = [];
if (messageList.length < 1) {
callback(emails);
callback(null, emails);
return;
}
var after = _.after(messageList.length, function() {
callback(emails);
callback(null, emails);
});
_.each(messageList, function(messageItem) {
@ -485,7 +488,7 @@ define(function(require) {
uid: messageItem.uid
}, function(err, message) {
if (err) {
console.log(err);
callback(err);
return;
}

View File

@ -426,14 +426,17 @@ define(function(require) {
it('should work', function(done) {
devicestorageStub.listItems.yields(null, [{
body: ''
body: app.string.cryptPrefix + btoa(JSON.stringify({})) + app.string.cryptSuffix
}]);
keychainStub.getPublicKeys.yields(null, [{
_id: "fcf8b4aa-5d09-4089-8b4f-e3bc5091daf3",
userId: "safewithme.testuser@gmail.com",
publicKey: publicKey
}]);
cryptoStub.decryptListForUser.yields(null, []);
cryptoStub.decryptListForUser.yields(null, [{
body: 'test body',
subject: 'test subject'
}]);
emailDao.listMessages({
folder: 'INBOX',
@ -444,7 +447,7 @@ define(function(require) {
expect(keychainStub.getPublicKeys.calledOnce).to.be.true;
expect(cryptoStub.decryptListForUser.calledOnce).to.be.true;
expect(err).to.not.exist;
expect(emails.length).to.equal(0);
expect(emails.length).to.equal(1);
done();
});
});