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

@ -2,9 +2,9 @@ define(function(require) {
'use strict';
var util = require('cryptoLib/util');
// _ = require('underscore'),
// str = require('js/app-config').string,
// config = require('js/app-config').config;
// _ = require('underscore'),
// str = require('js/app-config').string,
// config = require('js/app-config').config;
var EmailDAO = function(keychain, imapClient, smtpClient, crypto, devicestorage) {
var self = this;
@ -28,7 +28,8 @@ define(function(require) {
//
EmailDAO.prototype.init = function(options, callback) {
var self = this;
var self = this,
keypair;
self._account = options.account;
@ -53,7 +54,28 @@ define(function(require) {
callback(err);
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;
// login IMAP client if existent
@ -127,72 +152,69 @@ define(function(require) {
/**
* Cleanup by logging the user off.
*/
EmailDAO.prototype.destroy = function(callback) {
EmailDAO.prototype._imapLogout = function(callback) {
var self = this;
self._imapClient.logout(callback);
};
// /**
// * Login the imap client
// */
// /**
// * List the folders in the user's IMAP mailbox.
// */
// EmailDAO.prototype.imapListFolders = function(callback) {
// var self = this,
// dbType = 'folders';
// // check local cache
// self._devicestorage.listItems(dbType, 0, null, function(err, stored) {
// if (err) {
// callback(err);
// return;
// }
/**
* List the folders in the user's IMAP mailbox.
*/
EmailDAO.prototype._imapListFolders = function(callback) {
var self = this,
dbType = 'folders';
// if (!stored || stored.length < 1) {
// // no folders cached... fetch from server
// fetchFromServer();
// return;
// }
// check local cache
self._devicestorage.listItems(dbType, 0, null, function(err, stored) {
if (err) {
callback(err);
return;
}
// callback(null, stored[0]);
// });
if (!stored || stored.length < 1) {
// no folders cached... fetch from server
fetchFromServer();
return;
}
// function fetchFromServer() {
// var folders;
callback(null, stored[0]);
});
// // fetch list from imap server
// self._imapClient.listWellKnownFolders(function(err, wellKnownFolders) {
// if (err) {
// callback(err);
// return;
// }
function fetchFromServer() {
var folders;
// folders = [
// wellKnownFolders.inbox,
// wellKnownFolders.sent, {
// type: 'Outbox',
// path: 'OUTBOX'
// },
// wellKnownFolders.drafts,
// wellKnownFolders.trash
// ];
// fetch list from imap server
self._imapClient.listWellKnownFolders(function(err, wellKnownFolders) {
if (err) {
callback(err);
return;
}
// // cache locally
// // persist encrypted list in device storage
// self._devicestorage.storeList([folders], dbType, function(err) {
// if (err) {
// callback(err);
// return;
// }
folders = [
wellKnownFolders.inbox,
wellKnownFolders.sent, {
type: 'Outbox',
path: 'OUTBOX'
},
wellKnownFolders.drafts,
wellKnownFolders.trash
];
// callback(null, folders);
// });
// });
// }
// };
// cache locally
// persist encrypted list in device storage
self._devicestorage.storeList([folders], dbType, function(err) {
if (err) {
callback(err);
return;
}
callback(null, folders);
});
});
}
};
// /**
// * Get the number of unread message for a folder

View File

@ -58,9 +58,18 @@ define(function(require) {
});
it('should init', 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) {
@ -71,6 +80,65 @@ define(function(require) {
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 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();
});
});
@ -218,11 +286,11 @@ define(function(require) {
});
});
describe('login', function() {
describe('_imapLogin', function() {
it('should work', function(done) {
imapClientStub.login.yields();
dao.login(function(err) {
dao._imapLogin(function(err) {
expect(err).to.not.exist;
done();
});
@ -231,18 +299,18 @@ define(function(require) {
it('should fail due to error in imap login', function(done) {
imapClientStub.login.yields({});
dao.login(function(err) {
dao._imapLogin(function(err) {
expect(err).to.exist;
done();
});
});
});
describe('destroy', function() {
describe('_imapLogout', function() {
it('should work', function(done) {
imapClientStub.logout.yields();
dao.destroy(function(err) {
dao._imapLogout(function(err) {
expect(err).to.not.exist;
done();
});
@ -251,12 +319,93 @@ define(function(require) {
it('should fail due to error in imap login', function(done) {
imapClientStub.logout.yields({});
dao.destroy(function(err) {
dao._imapLogout(function(err) {
expect(err).to.exist;
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();
});
});
});
});
});