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

add imap folder listing functionality

This commit is contained in:
Felix Hammerl 2013-11-28 15:05:29 +01:00
parent 12860f6146
commit 5ddddb5568
2 changed files with 239 additions and 68 deletions

View File

@ -28,7 +28,8 @@ define(function(require) {
// //
EmailDAO.prototype.init = function(options, callback) { EmailDAO.prototype.init = function(options, callback) {
var self = this; var self = this,
keypair;
self._account = options.account; self._account = options.account;
@ -53,7 +54,28 @@ define(function(require) {
callback(err); callback(err);
return; return;
} }
callback(null, storedKeypair);
keypair = storedKeypair;
initFolders();
});
});
}
function initFolders() {
self._imapLogin(function(err) {
if (err) {
callback(err);
return;
}
self._imapListFolders(function(err, folders) {
if (err) {
callback(err);
return;
}
self._account.folders = folders;
callback(null, keypair);
}); });
}); });
} }
@ -113,11 +135,14 @@ define(function(require) {
}); });
}; };
// // //
// // IMAP Apis // IMAP Apis
// // //
EmailDAO.prototype.login = function(callback) { /**
* Login the imap client
*/
EmailDAO.prototype._imapLogin = function(callback) {
var self = this; var self = this;
// login IMAP client if existent // login IMAP client if existent
@ -127,72 +152,69 @@ define(function(require) {
/** /**
* Cleanup by logging the user off. * Cleanup by logging the user off.
*/ */
EmailDAO.prototype.destroy = function(callback) { EmailDAO.prototype._imapLogout = function(callback) {
var self = this; var self = this;
self._imapClient.logout(callback); self._imapClient.logout(callback);
}; };
// /**
// * Login the imap client
// */
// /** /**
// * List the folders in the user's IMAP mailbox. * List the folders in the user's IMAP mailbox.
// */ */
// EmailDAO.prototype.imapListFolders = function(callback) { EmailDAO.prototype._imapListFolders = function(callback) {
// var self = this, var self = this,
// dbType = 'folders'; dbType = 'folders';
// // check local cache // check local cache
// self._devicestorage.listItems(dbType, 0, null, function(err, stored) { self._devicestorage.listItems(dbType, 0, null, function(err, stored) {
// if (err) { if (err) {
// callback(err); callback(err);
// return; return;
// } }
// if (!stored || stored.length < 1) { if (!stored || stored.length < 1) {
// // no folders cached... fetch from server // no folders cached... fetch from server
// fetchFromServer(); fetchFromServer();
// return; return;
// } }
// callback(null, stored[0]); callback(null, stored[0]);
// }); });
// function fetchFromServer() { function fetchFromServer() {
// var folders; var folders;
// // fetch list from imap server // fetch list from imap server
// self._imapClient.listWellKnownFolders(function(err, wellKnownFolders) { self._imapClient.listWellKnownFolders(function(err, wellKnownFolders) {
// if (err) { if (err) {
// callback(err); callback(err);
// return; return;
// } }
// folders = [ folders = [
// wellKnownFolders.inbox, wellKnownFolders.inbox,
// wellKnownFolders.sent, { wellKnownFolders.sent, {
// type: 'Outbox', type: 'Outbox',
// path: 'OUTBOX' path: 'OUTBOX'
// }, },
// wellKnownFolders.drafts, wellKnownFolders.drafts,
// wellKnownFolders.trash wellKnownFolders.trash
// ]; ];
// // cache locally // cache locally
// // persist encrypted list in device storage // persist encrypted list in device storage
// self._devicestorage.storeList([folders], dbType, function(err) { self._devicestorage.storeList([folders], dbType, function(err) {
// if (err) { if (err) {
// callback(err); callback(err);
// return; return;
// } }
// callback(null, folders); callback(null, folders);
// }); });
// }); });
// } }
// }; };
// /** // /**
// * Get the number of unread message for a folder // * Get the number of unread message for a folder

View File

@ -58,9 +58,18 @@ define(function(require) {
}); });
it('should init', function(done) { it('should init', function(done) {
var loginStub, listFolderStub;
// initKeychain
devicestorageStub.init.withArgs(emailAddress).yields(); devicestorageStub.init.withArgs(emailAddress).yields();
keychainStub.getUserKeyPair.yields(null, mockKeyPair); keychainStub.getUserKeyPair.yields(null, mockKeyPair);
// initFolders
loginStub = sinon.stub(dao, '_imapLogin');
listFolderStub = sinon.stub(dao, '_imapListFolders');
loginStub.yields();
listFolderStub.yields();
dao.init({ dao.init({
account: account account: account
}, function(err, keyPair) { }, function(err, keyPair) {
@ -71,6 +80,65 @@ define(function(require) {
expect(devicestorageStub.init.calledOnce).to.be.true; expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(loginStub.calledOnce).to.be.true;
expect(listFolderStub.calledOnce).to.be.true;
done();
});
});
it('should fail due to error while listing folders', function(done) {
var loginStub, listFolderStub;
// initKeychain
devicestorageStub.init.withArgs(emailAddress).yields();
keychainStub.getUserKeyPair.yields(null, mockKeyPair);
// initFolders
loginStub = sinon.stub(dao, '_imapLogin');
listFolderStub = sinon.stub(dao, '_imapListFolders');
loginStub.yields();
listFolderStub.yields({});
dao.init({
account: account
}, function(err, keyPair) {
expect(err).to.exist;
expect(keyPair).to.not.exist;
expect(dao._account).to.equal(account);
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(loginStub.calledOnce).to.be.true;
expect(listFolderStub.calledOnce).to.be.true;
done();
});
});
it('should fail due to error during imap login', function(done) {
var loginStub = sinon.stub(dao, '_imapLogin');
// initKeychain
devicestorageStub.init.withArgs(emailAddress).yields();
keychainStub.getUserKeyPair.yields(null, mockKeyPair);
// initFolders
loginStub.yields({});
dao.init({
account: account
}, function(err, keyPair) {
expect(err).to.exist;
expect(keyPair).to.not.exist;
expect(dao._account).to.equal(account);
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(loginStub.calledOnce).to.be.true;
done(); done();
}); });
}); });
@ -218,11 +286,11 @@ define(function(require) {
}); });
}); });
describe('login', function() { describe('_imapLogin', function() {
it('should work', function(done) { it('should work', function(done) {
imapClientStub.login.yields(); imapClientStub.login.yields();
dao.login(function(err) { dao._imapLogin(function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
done(); done();
}); });
@ -231,18 +299,18 @@ define(function(require) {
it('should fail due to error in imap login', function(done) { it('should fail due to error in imap login', function(done) {
imapClientStub.login.yields({}); imapClientStub.login.yields({});
dao.login(function(err) { dao._imapLogin(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
}); });
}); });
describe('destroy', function() { describe('_imapLogout', function() {
it('should work', function(done) { it('should work', function(done) {
imapClientStub.logout.yields(); imapClientStub.logout.yields();
dao.destroy(function(err) { dao._imapLogout(function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
done(); done();
}); });
@ -251,12 +319,93 @@ define(function(require) {
it('should fail due to error in imap login', function(done) { it('should fail due to error in imap login', function(done) {
imapClientStub.logout.yields({}); imapClientStub.logout.yields({});
dao.destroy(function(err) { dao._imapLogout(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
}); });
}); });
describe('_imapListFolders', function() {
var dummyFolders = [{
type: 'Inbox',
path: 'INBOX'
}, {
type: 'Outbox',
path: 'OUTBOX'
}];
it('should list from storage', function(done) {
devicestorageStub.listItems.withArgs('folders').yields(null, [dummyFolders]);
dao._imapListFolders(function(err, folders) {
expect(err).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(folders[0].type).to.equal('Inbox');
done();
});
});
it('should not list from storage due to error', function(done) {
devicestorageStub.listItems.yields({});
dao._imapListFolders(function(err, folders) {
expect(err).to.exist;
expect(folders).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(imapClientStub.listWellKnownFolders.called).to.be.false;
done();
});
});
it('should list from imap', function(done) {
devicestorageStub.listItems.yields(null, []);
imapClientStub.listWellKnownFolders.yields(null, {
inbox: dummyFolders[0]
});
devicestorageStub.storeList.yields();
dao._imapListFolders(function(err, folders) {
expect(err).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
expect(folders[0].type).to.equal('Inbox');
done();
});
});
it('should not list from imap due to store error', function(done) {
devicestorageStub.listItems.yields(null, []);
imapClientStub.listWellKnownFolders.yields(null, {
inbox: dummyFolders[0]
});
devicestorageStub.storeList.yields({});
dao._imapListFolders(function(err, folders) {
expect(err).to.exist;
expect(folders).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
done();
});
});
it('should not list from imap due to imap error', function(done) {
devicestorageStub.listItems.yields(null, []);
imapClientStub.listWellKnownFolders.yields({});
dao._imapListFolders(function(err, folders) {
expect(err).to.exist;
expect(folders).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(devicestorageStub.storeList.called).to.be.false;
done();
});
});
});
}); });
}); });