read email with and without attachment works in ui

This commit is contained in:
Tankred Hase 2013-08-21 16:07:59 +02:00
parent fa3abc3487
commit 88b7252696
2 changed files with 45 additions and 5 deletions

View File

@ -157,7 +157,9 @@ define(function(require) {
*/
EmailDAO.prototype.imapGetMessage = function(options, callback) {
var self = this,
message;
expectedItems,
itemCounter = 0,
message, attachments = [];
// validate options
if (!options.folder || !options.uid) {
@ -169,10 +171,29 @@ define(function(require) {
function messageReady(err, gottenMessage) {
message = gottenMessage;
itemCounter++;
// remember how many items should be fetched before the callback fires
expectedItems = (message.attachments instanceof Array) ? message.attachments.length + 1 : 1;
check();
}
function attachmentReady(err, attmt) {
message.parsedAttachment = attmt;
function attachmentReady(err, gottenAttachment) {
// parse uint8array to base to make it serializable for postMessage
gottenAttachment.base64 = btoa(gottenAttachment.uint8Array);
delete gottenAttachment.uint8Array;
attachments.push(gottenAttachment);
itemCounter++;
check();
}
function check() {
// go for another round you don't yet know how mich to fetch or you haven't fetch enough
if (!expectedItems || itemCounter < expectedItems) {
return;
}
message.attachments = (attachments.length > 0) ? attachments : undefined;
callback(null, message);
}

View File

@ -172,6 +172,24 @@ define(function(require) {
});
});
it('should parse message body without attachement', function(done) {
var uid = 415;
imapClientStub.getMessage.yields(null, {
uid: uid
});
emailDao.imapGetMessage({
folder: 'INBOX',
uid: uid
}, function(err, message) {
expect(imapClientStub.getMessage.calledOnce).to.be.true;
expect(err).to.not.exist;
expect(message.uid).to.equal(uid);
expect(message.attachments).to.not.exist;
done();
});
});
it('should parse message body and attachement', function(done) {
var uid = 415,
newImapClientStub = {
@ -179,7 +197,8 @@ define(function(require) {
};
sinon.stub(newImapClientStub, 'getMessage', function(options, messageReady, attachmentReady) {
messageReady(null, {
uid: uid
uid: uid,
attachments: ['file.txt']
});
attachmentReady(null, {
uint8Array: new Uint8Array(42)
@ -194,7 +213,7 @@ define(function(require) {
expect(newImapClientStub.getMessage.calledOnce).to.be.true;
expect(err).to.not.exist;
expect(message.uid).to.equal(uid);
expect(message.parsedAttachment.uint8Array.length).to.equal(42);
expect(message.attachments[0].base64).to.exist;
emailDao._imapClient = imapClientStub;
done();
});