1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-23 07:48:48 -05:00

[WO-786] Select inbox after onConnect

This commit is contained in:
Felix Hammerl 2014-12-11 15:02:08 +01:00 committed by Tankred Hase
parent 8c88f25875
commit c3efeb1132
6 changed files with 66 additions and 56 deletions

View File

@ -37,14 +37,16 @@ var MailListCtrl = function($scope, $timeout, $location, $filter, status, notifi
$scope.loc = $location; $scope.loc = $location;
$scope.$watch('(loc.search()).uid', function(uid) { $scope.$watch('(loc.search()).uid', function(uid) {
if (typeof uid === 'undefined') { uid = parseInt(uid, 10);
// no uid specified in url... select no message if (isNaN(uid)) {
// no (or nonsensical) uid specified in url... select no message
$scope.select(); $scope.select();
return; return;
} }
// select the message specified by the uid in the url // select the message specified by the uid in the url
$scope.select(_.findWhere(currentFolder().messages, { $scope.select(_.findWhere(currentFolder().messages, {
uid: typeof uid === 'string' ? parseInt(uid, 10) : uid uid: uid
})); }));
}); });

View File

@ -35,15 +35,38 @@ var NavigationCtrl = function($scope, $location, account, email, outbox, notific
// url/history handling // url/history handling
// //
$scope.loc = $location;
/** /**
* Close read mode and go to folder * Close read mode and go to folder
*/ */
$scope.navigate = function(folderIndex) { $scope.navigate = function(folderIndex) {
$location.search('uid', null); $location.search('uid', null); // close the read mode
$location.search('folder', folderIndex); $location.search('folder', folderIndex); // open the n-th folder
}; };
$scope.loc = $location; // folder index url watcher
$scope.$watch('(loc.search()).folder', function(folderIndex) {
if (!$scope.account.folders || !$scope.account.folders.length) {
// there's no folder to navigate to
return;
}
// normalize folder index to [0 ; $scope.account.folders.length - 1]
folderIndex = parseInt(folderIndex, 10);
if (isNaN(folderIndex) || folderIndex < 0 || folderIndex > ($scope.account.folders.length - 1)) {
// array index out of bounds or nonsensical data
$location.search('folder', 0);
return;
}
// navigate to folder[folderIndex]
if ($scope.account.folders && $scope.account.folders.length > folderIndex) {
// navigate to the selected folder index
$scope.state.nav.currentFolder = $scope.account.folders[folderIndex];
$scope.state.nav.toggle(false);
}
});
// nav open/close state url watcher // nav open/close state url watcher
$scope.$watch('(loc.search()).nav', function(open) { $scope.$watch('(loc.search()).nav', function(open) {
@ -69,11 +92,6 @@ var NavigationCtrl = function($scope, $location, account, email, outbox, notific
// scope functions // scope functions
// //
$scope.openFolder = function(folder) {
$scope.state.nav.currentFolder = folder;
$scope.state.nav.toggle(false);
};
$scope.onOutboxUpdate = function(err, count) { $scope.onOutboxUpdate = function(err, count) {
if (err) { if (err) {
dialog.error(err); dialog.error(err);
@ -85,7 +103,6 @@ var NavigationCtrl = function($scope, $location, account, email, outbox, notific
type: config.outboxMailboxType type: config.outboxMailboxType
}); });
ob.count = count; ob.count = count;
$scope.$apply();
email.refreshFolder({ email.refreshFolder({
folder: ob folder: ob
@ -111,20 +128,6 @@ var NavigationCtrl = function($scope, $location, account, email, outbox, notific
// init folders // init folders
initializeFolders(); initializeFolders();
// folder index url watcher
$scope.$watch('(loc.search()).folder', function(folderIndex) {
if (typeof folderIndex === 'undefined') {
$location.search('folder', 0); // navigate to inbox by default
return;
}
// select current folder
folderIndex = typeof folderIndex === 'string' ? parseInt(folderIndex, 10) : folderIndex;
if ($scope.account.folders && $scope.account.folders.length > folderIndex) {
// navigate to the selected folder index
$scope.openFolder($scope.account.folders[folderIndex]);
}
});
// connect imap/smtp clients on first startup // connect imap/smtp clients on first startup
account.onConnect(function(err) { account.onConnect(function(err) {
if (err) { if (err) {
@ -134,8 +137,7 @@ var NavigationCtrl = function($scope, $location, account, email, outbox, notific
// select inbox if not yet selected // select inbox if not yet selected
if (!$scope.state.nav.currentFolder) { if (!$scope.state.nav.currentFolder) {
$scope.openFolder($scope.account.folders[0]); $location.search('folder', 0);
$scope.$apply();
} }
}); });
@ -155,17 +157,16 @@ var NavigationCtrl = function($scope, $location, account, email, outbox, notific
$scope.$root.account = account.list()[0]; $scope.$root.account = account.list()[0];
// set notificatio handler for sent messages // set notificatio handler for sent messages
outbox.onSent = sentNotification; outbox.onSent = function(message) {
// start checking outbox periodically
outbox.startChecking($scope.onOutboxUpdate);
}
function sentNotification(message) {
notification.create({ notification.create({
title: 'Message sent', title: 'Message sent',
message: message.subject, message: message.subject,
timeout: NOTIFICATION_SENT_TIMEOUT timeout: NOTIFICATION_SENT_TIMEOUT
}, function() {}); }, function() {});
};
// start checking outbox periodically
outbox.startChecking($scope.onOutboxUpdate);
} }
}; };

View File

@ -137,10 +137,12 @@ Account.prototype.isOnline = function() {
/** /**
* Event that is called when the user agent goes online. This create new instances of the imap-client and pgp-mailer and connects to the mail server. * Event that is called when the user agent goes online. This create new instances of the imap-client and pgp-mailer and connects to the mail server.
*/ */
Account.prototype.onConnect = function() { Account.prototype.onConnect = function(callback) {
var self = this; var self = this;
var config = self._appConfig.config; var config = self._appConfig.config;
callback = callback || self._dialog.error;
if (!self.isOnline() || !self._emailDao || !self._emailDao._account) { if (!self.isOnline() || !self._emailDao || !self._emailDao._account) {
// prevent connection infinite loop // prevent connection infinite loop
return; return;
@ -148,7 +150,7 @@ Account.prototype.onConnect = function() {
self._auth.getCredentials(function(err, credentials) { self._auth.getCredentials(function(err, credentials) {
if (err) { if (err) {
self._dialog.error(err); callback(err);
return; return;
} }
@ -176,7 +178,7 @@ Account.prototype.onConnect = function() {
imapClient: imapClient, imapClient: imapClient,
pgpMailer: pgpMailer, pgpMailer: pgpMailer,
ignoreUploadOnSent: self._emailDao.checkIgnoreUploadOnSent(credentials.imap.host) ignoreUploadOnSent: self._emailDao.checkIgnoreUploadOnSent(credentials.imap.host)
}, self._dialog.error); }, callback);
} }
function onConnectionError(error) { function onConnectionError(error) {

View File

@ -1197,18 +1197,32 @@ Email.prototype.onConnect = function(options, callback) {
// set status to online after setting cache to prevent race condition // set status to online after setting cache to prevent race condition
self._account.online = true; self._account.online = true;
// set up the imap client to listen for changes in the inbox // by default, select the inbox (if there is one) after connecting the imap client.
// this avoids race conditions between the listening imap connection and the one where the work is done
var inbox = _.findWhere(self._account.folders, { var inbox = _.findWhere(self._account.folders, {
type: FOLDER_TYPE_INBOX type: FOLDER_TYPE_INBOX
}); });
if (!inbox) { if (!inbox) {
// if there is no inbox, that's ok, too
return callback(); return callback();
} }
self.openFolder({
folder: inbox
}, function(err) {
if (err) {
callback(err);
return;
}
// set up the imap client to listen for changes in the inbox
self._imapClient.listenForChanges({ self._imapClient.listenForChanges({
path: inbox.path path: inbox.path
}, callback); }, function() {});
callback();
});
}); });
}); });
}; };

View File

@ -57,7 +57,6 @@ describe('Navigation Controller unit test', function() {
expect(scope.state).to.exist; expect(scope.state).to.exist;
expect(scope.state.lightbox).to.be.undefined; expect(scope.state.lightbox).to.be.undefined;
expect(scope.account.folders).to.not.be.empty; expect(scope.account.folders).to.not.be.empty;
expect(scope.openFolder).to.exist;
}); });
}); });
@ -71,16 +70,6 @@ describe('Navigation Controller unit test', function() {
}); });
}); });
describe('open folder', function() {
it('should work', function() {
scope.state.nav.open = true;
scope.openFolder('asd');
expect(scope.state.nav.currentFolder).to.equal('asd');
expect(scope.state.nav.open).to.be.false;
});
});
describe('empty outbox', function() { describe('empty outbox', function() {
it('should work', function() { it('should work', function() {
var callback; var callback;

View File

@ -1847,7 +1847,8 @@ describe('Email DAO unit tests', function() {
modseq: '123' modseq: '123'
}]; }];
imapClientStub.login.yieldsAsync(); imapClientStub.login.yieldsAsync();
imapClientStub.listenForChanges.yieldsAsync(); imapClientStub.selectMailbox.yields();
imapClientStub.listenForChanges.yields();
initFoldersStub.yieldsAsync(); initFoldersStub.yieldsAsync();
dao.onConnect({ dao.onConnect({
@ -1858,6 +1859,7 @@ describe('Email DAO unit tests', function() {
expect(err).to.not.exist; expect(err).to.not.exist;
expect(dao.ignoreUploadOnSent).to.be.false; expect(dao.ignoreUploadOnSent).to.be.false;
expect(imapClientStub.login.calledOnce).to.be.true; expect(imapClientStub.login.calledOnce).to.be.true;
expect(imapClientStub.selectMailbox.calledOnce).to.be.true;
expect(initFoldersStub.calledOnce).to.be.true; expect(initFoldersStub.calledOnce).to.be.true;
expect(imapClientStub.mailboxCache).to.deep.equal({ expect(imapClientStub.mailboxCache).to.deep.equal({
'INBOX': { 'INBOX': {