1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 02:42:17 -05:00

listing messages in ui works, change app controller to use spaces instead of tabs

This commit is contained in:
Tankred Hase 2013-08-20 19:48:49 +02:00
parent 7776255784
commit feae2b0c2b
4 changed files with 239 additions and 184 deletions

View File

@ -2,200 +2,204 @@
* The main application controller
*/
define(function(require) {
'use strict';
'use strict';
var $ = require('jquery'),
ImapClient = require('ImapClient'),
SmtpClient = require('SmtpClient'),
EmailDAO = require('js/dao/email-dao'),
KeychainDAO = require('js/dao/keychain-dao'),
cloudstorage = require('js/dao/cloudstorage-dao'),
app = require('js/app-config');
require('cordova');
var $ = require('jquery'),
ImapClient = require('ImapClient'),
SmtpClient = require('SmtpClient'),
EmailDAO = require('js/dao/email-dao'),
KeychainDAO = require('js/dao/keychain-dao'),
cloudstorage = require('js/dao/cloudstorage-dao'),
app = require('js/app-config');
require('cordova');
var self = {};
var self = {};
/**
* Start the application by loading the view templates
*/
self.start = function(callback) {
// the views to load
var views = [
'login', 'compose', 'folderlist', 'folderlistitem',
'messagelist', 'messagelistitem', 'read'
];
/**
* Start the application by loading the view templates
*/
self.start = function(callback) {
// the views to load
var views = [
'login', 'compose', 'folderlist', 'folderlistitem',
'messagelist', 'messagelistitem', 'read'
];
// are we running in native app or in browser?
if (document.URL.indexOf("http") === 0 || document.URL.indexOf("app") === 0 || document.URL.indexOf("chrome") === 0) {
console.log('Assuming Browser environment...');
onDeviceReady();
} else {
console.log('Assuming Cordova environment...');
document.addEventListener("deviceready", onDeviceReady, false);
}
// are we running in native app or in browser?
if (document.URL.indexOf("http") === 0 || document.URL.indexOf("app") === 0 || document.URL.indexOf("chrome") === 0) {
console.log('Assuming Browser environment...');
onDeviceReady();
} else {
console.log('Assuming Cordova environment...');
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
console.log('Starting app.');
loadTemplates(views, callback);
}
};
function onDeviceReady() {
console.log('Starting app.');
loadTemplates(views, callback);
}
};
/**
* Executes a number of commands
*/
self.execute = function(cmd, args, callback) {
if (cmd === 'login') {
// login user
fetchOAuthToken(args.password, function(err, userId) {
callback({
err: err,
userId: userId
});
});
/**
* Executes a number of commands
*/
self.execute = function(cmd, args, callback) {
if (cmd === 'login') {
// login user
fetchOAuthToken(args.password, function(err, userId) {
callback({
err: err,
userId: userId
});
});
} else if (cmd === 'listFolders') {
// list folders in users mailbox
self._emailDao.imapListFolders(function(err, folders) {
callback({
err: err,
folders: folders
});
});
} else if (cmd === 'listFolders') {
// list folders in users mailbox
self._emailDao.imapListFolders(function(err, folders) {
callback({
err: err,
folders: folders
});
});
} else if (cmd === 'syncEmails') {
// list emails from folder
self._emailDao.syncFromCloud(args.folder, function(err) {
callback({
err: err
});
});
} else if (cmd === 'syncEmails') {
// self._emailDao.syncFromCloud(args.folder, function(err) {
// callback({
// err: err
// });
// });
} else if (cmd === 'listEmails') {
// list emails from folder
self._emailDao.listItems(args.folder, args.offset, args.num, function(err, emails) {
callback({
err: err,
emails: emails
});
});
// Syncing to local storage is not yet supported for imap
callback({
err: 'Not yet implemented!'
});
} else if (cmd === 'getEmail') {
// list emails from folder
var mail = self._emailDao.getItem(args.folder, args.messageId);
callback({
err: null,
email: mail
});
} else if (cmd === 'listEmails') {
// list emails from folder
self._emailDao.imapListMessages(args, function(err, emails) {
callback({
err: err,
emails: emails
});
});
} else if (cmd === 'sendEmail') {
// list emails from folder
self._emailDao.smtpSend(args.email, function(err) {
callback({
err: err
});
});
} else if (cmd === 'getEmail') {
// list emails from folder
var mail = self._emailDao.getItem(args.folder, args.messageId);
callback({
err: null,
email: mail
});
} else {
// error: invalid message from sandbox
callback({
err: {
errMsg: 'Invalid message posted from sandbox!'
}
});
}
};
} else if (cmd === 'sendEmail') {
// list emails from folder
self._emailDao.smtpSend(args.email, function(err) {
callback({
err: err
});
});
//
// Helper methods
//
} else {
// error: invalid message from sandbox
callback({
err: {
errMsg: 'Invalid message posted from sandbox!'
}
});
}
};
function fetchOAuthToken(password, callback) {
// get OAuth Token from chrome
chrome.identity.getAuthToken({
'interactive': true
},
function(token) {
// fetch gmail user's email address from the Google Authorization Server endpoint
$.ajax({
url: 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token,
type: 'GET',
dataType: 'json',
success: function(info) {
// login using the received email address
self.login(info.email, password, token, function(err) {
// send email address to sandbox
callback(err, info.email);
});
},
error: function(xhr, textStatus, err) {
callback({
errMsg: xhr.status + ': ' + xhr.statusText,
err: err
});
}
});
//
// Helper methods
//
}
);
}
function fetchOAuthToken(password, callback) {
// get OAuth Token from chrome
chrome.identity.getAuthToken({
'interactive': true
},
function(token) {
// fetch gmail user's email address from the Google Authorization Server endpoint
$.ajax({
url: 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token,
type: 'GET',
dataType: 'json',
success: function(info) {
// login using the received email address
self.login(info.email, password, token, function(err) {
// send email address to sandbox
callback(err, info.email);
});
},
error: function(xhr, textStatus, err) {
callback({
errMsg: xhr.status + ': ' + xhr.statusText,
err: err
});
}
});
self.login = function(userId, password, token, callback) {
var auth, imapOptions, smtpOptions,
keychain, imapClient, smtpClient;
}
);
}
// create mail credentials objects for imap/smtp
auth = {
XOAuth2: {
user: userId,
clientId: '440907777130.apps.googleusercontent.com',
accessToken: token
}
};
imapOptions = {
secure: true,
port: 993,
host: 'imap.gmail.com',
auth: auth
};
smtpOptions = {
secure: true,
port: 465,
host: 'smtp.gmail.com',
auth: auth
};
self.login = function(userId, password, token, callback) {
var auth, imapOptions, smtpOptions,
keychain, imapClient, smtpClient;
// init objects and inject dependencies
keychain = new KeychainDAO(cloudstorage);
imapClient = new ImapClient(imapOptions);
smtpClient = new SmtpClient(smtpOptions);
self._emailDao = new EmailDAO(keychain, imapClient, smtpClient);
// create mail credentials objects for imap/smtp
auth = {
XOAuth2: {
user: userId,
clientId: '440907777130.apps.googleusercontent.com',
accessToken: token
}
};
imapOptions = {
secure: true,
port: 993,
host: 'imap.gmail.com',
auth: auth
};
smtpOptions = {
secure: true,
port: 465,
host: 'smtp.gmail.com',
auth: auth
};
// init email dao
var account = {
emailAddress: userId,
symKeySize: app.config.symKeySize,
symIvSize: app.config.symIvSize,
asymKeySize: app.config.asymKeySize
};
self._emailDao.init(account, password, callback);
};
// init objects and inject dependencies
keychain = new KeychainDAO(cloudstorage);
imapClient = new ImapClient(imapOptions);
smtpClient = new SmtpClient(smtpOptions);
self._emailDao = new EmailDAO(keychain, imapClient, smtpClient);
function loadTemplates(names, callback) {
var loadTemplate = function(index) {
var name = names[index];
console.log('Loading template: ' + name);
$.get('tpl/' + name + '.html', function(data) {
app.util.tpl.templates[name] = data;
index++;
if (index < names.length) {
loadTemplate(index);
} else {
callback();
}
});
};
loadTemplate(0);
}
// init email dao
var account = {
emailAddress: userId,
symKeySize: app.config.symKeySize,
symIvSize: app.config.symIvSize,
asymKeySize: app.config.asymKeySize
};
self._emailDao.init(account, password, callback);
};
return self;
function loadTemplates(names, callback) {
var loadTemplate = function(index) {
var name = names[index];
console.log('Loading template: ' + name);
$.get('tpl/' + name + '.html', function(data) {
app.util.tpl.templates[name] = data;
index++;
if (index < names.length) {
loadTemplate(index);
} else {
callback();
}
});
};
loadTemplate(0);
}
return self;
});

View File

@ -130,13 +130,25 @@ define(function(require) {
/**
* List messages from an imap folder. This will not yet fetch the email body.
* @param {String} options.folderName The name of the imap folder.
* @param {Number} offset The offset of items to fetch (0 is the last stored item)
* @param {Number} num The number of items to fetch (null means fetch all)
* @param {Number} options.offset The offset of items to fetch (0 is the last stored item)
* @param {Number} options.num The number of items to fetch (null means fetch all)
*/
EmailDAO.prototype.imapListMessages = function(options, callback) {
callback({
errMsg: 'Not yet implemented!'
});
var self = this;
// validate options
if (!options.folder || typeof options.offset === 'undefined' || typeof options.num === 'undefined') {
callback({
errMsg: 'Invalid options!'
});
return;
}
self._imapClient.listMessages({
path: options.folder,
offset: options.offset,
length: options.num
}, callback);
};
/**

View File

@ -59,8 +59,8 @@ define(function(require) {
}, function(resArgs) {
expect(resArgs.err).to.not.exist;
expect(resArgs.userId).to.equal(appControllerTest.user);
expect($.ajax.called).to.be.true;
expect(window.chrome.identity.getAuthToken.called).to.be.true;
expect($.ajax.calledOnce).to.be.true;
expect(window.chrome.identity.getAuthToken.calledOnce).to.be.true;
done();
});
});
@ -73,7 +73,7 @@ define(function(require) {
password: appControllerTest.passphrase
}, function(resArgs) {
expect(resArgs.err).to.not.exist;
expect(controller._emailDao.smtpSend.called).to.be.true;
expect(controller._emailDao.smtpSend.calledOnce).to.be.true;
done();
});
});
@ -87,7 +87,23 @@ define(function(require) {
}, function(resArgs) {
expect(resArgs.err).to.not.exist;
expect(resArgs.folders[1]).to.equal('sent');
expect(controller._emailDao.imapListFolders.called).to.be.true;
expect(controller._emailDao.imapListFolders.calledOnce).to.be.true;
done();
});
});
});
describe('listEmails', function() {
it('should work', function(done) {
controller._emailDao.imapListMessages.yields(null, []);
controller.execute('listEmails', {
folder: 'INBOX',
offset: 0,
num: 10
}, function(resArgs) {
expect(resArgs.err).to.not.exist;
expect(resArgs.emails).to.a('Array');
expect(controller._emailDao.imapListMessages.calledOnce).to.be.true;
done();
});
});

View File

@ -137,6 +137,29 @@ define(function(require) {
});
});
});
describe('list IMAP messages from folder', function() {
it('should fail due to bad options', function(done) {
emailDao.imapListMessages({}, function(err) {
expect(imapClientStub.listMessages.called).to.be.false;
expect(err).to.exist;
done();
});
});
it('should work', function(done) {
imapClientStub.listMessages.yields();
emailDao.imapListMessages({
folder: 'INBOX',
offset: 0,
num: 10
}, function(err) {
expect(imapClientStub.listMessages.calledOnce).to.be.true;
expect(err).to.not.exist;
done();
});
});
});
});
});