2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2013-09-04 12:39:26 -04:00
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
var searchTimeout;
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-17 05:17:40 -04:00
|
|
|
//
|
|
|
|
// Constants
|
|
|
|
//
|
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
var INIT_DISPLAY_LEN = 50,
|
2014-10-02 16:05:44 -04:00
|
|
|
SCROLL_DISPLAY_LEN = 10,
|
2014-10-17 05:17:40 -04:00
|
|
|
FOLDER_TYPE_INBOX = 'Inbox',
|
|
|
|
NOTIFICATION_INBOX_TIMEOUT = 5000;
|
2013-09-04 12:39:26 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
var MailListCtrl = function($scope, $timeout, $location, $filter, $q, status, notification, email, keychain, dialog, search, dummy) {
|
2014-11-20 16:53:30 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
2014-12-04 10:24:42 -05:00
|
|
|
// scope state
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
2013-09-28 10:08:12 -04:00
|
|
|
|
2014-11-20 16:53:30 -05:00
|
|
|
$scope.state.mailList = {};
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* Gathers unread notifications to be cancelled later
|
|
|
|
*/
|
|
|
|
$scope.pendingNotifications = [];
|
2014-02-21 10:22:33 -05:00
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
//
|
|
|
|
// url/history handling
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the route to a message which will go to read mode
|
|
|
|
*/
|
|
|
|
$scope.navigate = function(message) {
|
2015-04-07 10:14:40 -04:00
|
|
|
if (!message || !message.from) {
|
|
|
|
// early return if message has not finished loading yet
|
|
|
|
return;
|
|
|
|
}
|
2014-12-04 06:46:55 -05:00
|
|
|
$location.search('uid', message.uid);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.loc = $location;
|
|
|
|
$scope.$watch('(loc.search()).uid', function(uid) {
|
2014-12-11 09:02:08 -05:00
|
|
|
uid = parseInt(uid, 10);
|
|
|
|
if (isNaN(uid)) {
|
|
|
|
// no (or nonsensical) uid specified in url... select no message
|
2014-12-04 06:46:55 -05:00
|
|
|
$scope.select();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-11 09:02:08 -05:00
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
// select the message specified by the uid in the url
|
|
|
|
$scope.select(_.findWhere(currentFolder().messages, {
|
2014-12-11 09:02:08 -05:00
|
|
|
uid: uid
|
2014-12-04 06:46:55 -05:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
|
|
|
// scope functions
|
|
|
|
//
|
2014-02-17 08:31:14 -05:00
|
|
|
|
2015-03-31 09:43:42 -04:00
|
|
|
$scope.getBody = function(messages) {
|
2014-12-17 12:18:26 -05:00
|
|
|
return $q(function(resolve) {
|
|
|
|
resolve();
|
2013-11-18 11:44:59 -05:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).then(function() {
|
|
|
|
return email.getBody({
|
|
|
|
folder: currentFolder(),
|
2015-03-31 09:43:42 -04:00
|
|
|
messages: messages
|
2014-12-17 12:18:26 -05:00
|
|
|
});
|
2014-07-21 10:05:29 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).catch(function(err) {
|
|
|
|
if (err.code !== 42) {
|
|
|
|
dialog.error(err);
|
2014-07-21 10:05:29 -04:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2014-02-21 04:47:49 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
2014-11-20 09:14:39 -05:00
|
|
|
* Called when clicking on an message list item
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-11-20 09:14:39 -05:00
|
|
|
$scope.select = function(message) {
|
2014-10-02 16:05:44 -04:00
|
|
|
// unselect an item
|
2014-11-20 09:14:39 -05:00
|
|
|
if (!message) {
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.state.mailList.selected = undefined;
|
|
|
|
return;
|
|
|
|
}
|
2014-02-17 08:31:14 -05:00
|
|
|
|
2014-11-20 09:14:39 -05:00
|
|
|
$scope.state.mailList.selected = message;
|
2013-12-05 12:28:18 -05:00
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
if ($location.search().dev) {
|
|
|
|
// stop here in dev mode
|
|
|
|
return;
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
return $q(function(resolve) {
|
|
|
|
resolve();
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).then(function() {
|
|
|
|
return keychain.refreshKeyForUserId({
|
|
|
|
userId: message.from[0].address
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).then(function() {
|
|
|
|
return email.decryptBody({
|
2014-11-20 09:14:39 -05:00
|
|
|
message: message
|
2014-12-17 12:18:26 -05:00
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).then(function() {
|
2014-11-20 09:14:39 -05:00
|
|
|
// if the message is unread, please sync the new state.
|
2014-10-02 16:05:44 -04:00
|
|
|
// otherweise forget about it.
|
2014-11-20 09:14:39 -05:00
|
|
|
if (!message.unread) {
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// let's close pending notifications for unread messages in the inbox
|
|
|
|
if (currentFolder().type === FOLDER_TYPE_INBOX) {
|
|
|
|
while ($scope.pendingNotifications.length) {
|
|
|
|
notification.close($scope.pendingNotifications.shift());
|
2014-09-16 13:01:49 -04:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
2014-09-16 13:01:49 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
return $scope.state.actionBar.markMessage(message, false, true);
|
|
|
|
|
|
|
|
}).catch(dialog.error);
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2013-11-11 09:53:34 -05:00
|
|
|
|
2014-12-02 12:36:15 -05:00
|
|
|
$scope.flag = function(message, flagged) {
|
2014-12-17 12:18:26 -05:00
|
|
|
return $scope.state.actionBar.flagMessage(message, flagged);
|
2014-12-02 12:36:15 -05:00
|
|
|
};
|
|
|
|
|
2014-12-01 04:04:20 -05:00
|
|
|
/**
|
|
|
|
* Date formatting
|
|
|
|
*/
|
|
|
|
$scope.formatDate = function(date) {
|
2015-03-31 09:43:42 -04:00
|
|
|
if (!date) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:16:32 -05:00
|
|
|
if (typeof date === 'string') {
|
|
|
|
date = new Date(date);
|
|
|
|
}
|
|
|
|
|
2014-12-01 04:04:20 -05:00
|
|
|
var now = new Date();
|
|
|
|
|
|
|
|
// return time if mail is from today
|
2014-12-01 11:33:09 -05:00
|
|
|
if (now.getDay() === date.getDay() && now.getMonth() === date.getMonth() && now.getFullYear() === date.getFullYear()) {
|
2014-12-01 04:04:20 -05:00
|
|
|
return $filter('date')(date, 'shortTime');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filter('date')(date, 'mediumDate');
|
|
|
|
};
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
|
|
|
// watch tasks
|
|
|
|
//
|
2013-12-09 13:21:52 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
2014-11-20 09:14:39 -05:00
|
|
|
* List messages from folder when user changes folder
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
|
|
|
$scope._stopWatchTask = $scope.$watch('state.nav.currentFolder', function() {
|
|
|
|
if (!currentFolder()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-09 13:21:52 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// reset searchFilter
|
|
|
|
$scope.searchText = undefined;
|
2013-10-10 13:15:16 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// in development, display dummy mail objects
|
2014-12-04 06:46:55 -05:00
|
|
|
if ($location.search().dev) {
|
2014-12-04 10:24:42 -05:00
|
|
|
status.update('Last update: ', new Date());
|
2014-11-26 06:59:44 -05:00
|
|
|
currentFolder().messages = dummy.listMails();
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// display and select first
|
2014-12-17 12:18:26 -05:00
|
|
|
return openCurrentFolder();
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
2013-11-21 11:37:07 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.watchMessages = $scope.$watchCollection('state.nav.currentFolder.messages', function(messages) {
|
|
|
|
if (!messages) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-04 12:39:26 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// sort message by uid
|
2014-12-04 06:46:55 -05:00
|
|
|
messages.sort(byUidDescending);
|
2014-12-05 07:04:10 -05:00
|
|
|
// Unselect message if it has been deleted from the messages array
|
|
|
|
if (messages.indexOf(currentMessage()) === -1) {
|
|
|
|
$scope.select();
|
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
// set display buffer to first messages
|
2014-12-04 06:46:55 -05:00
|
|
|
$scope.displayMessages = messages.slice(0, INIT_DISPLAY_LEN);
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* display more items (for infinite scrolling)
|
|
|
|
*/
|
|
|
|
$scope.displayMore = function() {
|
|
|
|
if (!currentFolder() || !$scope.displayMessages) {
|
|
|
|
// folders not yet initialized
|
|
|
|
return;
|
|
|
|
}
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var len = currentFolder().messages.length,
|
|
|
|
dLen = $scope.displayMessages.length;
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if (dLen === len || $scope.searchText) {
|
|
|
|
// all messages are already displayed or we're in search mode
|
|
|
|
return;
|
|
|
|
}
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// copy next interval of messages to the end of the display messages array
|
|
|
|
var next = currentFolder().messages.slice(dLen, dLen + SCROLL_DISPLAY_LEN);
|
|
|
|
Array.prototype.push.apply($scope.displayMessages, next);
|
|
|
|
};
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-12-01 11:33:09 -05:00
|
|
|
/**
|
|
|
|
* Handle search event in other parts of the app by filtering messages in the mail-list
|
|
|
|
*/
|
|
|
|
$scope.$on('search', function(e, query) {
|
|
|
|
$scope.displaySearchResults(query);
|
|
|
|
});
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* This method is called when the user changes the searchText
|
|
|
|
*/
|
|
|
|
$scope.displaySearchResults = function(searchText) {
|
|
|
|
if (searchTimeout) {
|
|
|
|
// remove timeout to wait for user typing query
|
|
|
|
clearTimeout(searchTimeout);
|
|
|
|
}
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if (!searchText) {
|
|
|
|
// set display buffer to first messages
|
|
|
|
$scope.displayMessages = currentFolder().messages.slice(0, INIT_DISPLAY_LEN);
|
2014-12-04 10:24:42 -05:00
|
|
|
status.setSearching(false);
|
|
|
|
status.update('Online');
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// display searching spinner
|
2014-12-04 10:24:42 -05:00
|
|
|
status.setSearching(true);
|
|
|
|
status.update('Searching ...');
|
2014-10-02 16:05:44 -04:00
|
|
|
searchTimeout = setTimeout(function() {
|
|
|
|
$scope.$apply(function() {
|
|
|
|
// filter relevant messages
|
2014-11-26 06:59:44 -05:00
|
|
|
$scope.displayMessages = search.filter(currentFolder().messages, searchText);
|
2014-12-04 10:24:42 -05:00
|
|
|
status.setSearching(false);
|
|
|
|
status.update('Matches in this folder');
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
}, 500);
|
|
|
|
};
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* Sync current folder when client comes back online
|
|
|
|
*/
|
|
|
|
$scope.watchOnline = $scope.$watch('account.online', function(isOnline) {
|
2014-11-26 15:12:40 -05:00
|
|
|
// wait one cycle for the status display controllers to init
|
2014-12-17 12:18:26 -05:00
|
|
|
return $timeout(function() {
|
2014-11-26 15:12:40 -05:00
|
|
|
if (isOnline) {
|
2014-12-04 10:24:42 -05:00
|
|
|
status.update('Online');
|
2014-12-17 12:18:26 -05:00
|
|
|
return openCurrentFolder();
|
2014-11-26 15:12:40 -05:00
|
|
|
} else {
|
2014-12-04 10:24:42 -05:00
|
|
|
status.update('Offline mode');
|
2014-11-26 15:12:40 -05:00
|
|
|
}
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
}, true);
|
2013-09-30 15:22:46 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
|
|
|
// Helper Functions
|
|
|
|
//
|
2014-05-23 08:23:50 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
function openCurrentFolder() {
|
2014-10-22 11:21:28 -04:00
|
|
|
if (!currentFolder()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
return $q(function(resolve) {
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
}).then(function() {
|
|
|
|
return email.openFolder({
|
|
|
|
folder: currentFolder()
|
|
|
|
}).catch(function(err) {
|
|
|
|
// don't display err for offline case
|
|
|
|
if (err.code !== 42) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}).then(function() {
|
2014-10-02 16:05:44 -04:00
|
|
|
// dont wait until scroll to load visible mail bodies
|
|
|
|
$scope.loadVisibleBodies();
|
2014-05-08 10:25:20 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).catch(dialog.error);
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
2014-05-08 10:25:20 -04:00
|
|
|
|
2015-01-15 04:23:15 -05:00
|
|
|
$scope.$on('read', function(e, state) {
|
|
|
|
if (!state) {
|
|
|
|
// load bodies after closing read mode
|
|
|
|
$scope.loadVisibleBodies();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
function currentFolder() {
|
2014-11-26 11:57:14 -05:00
|
|
|
return $scope.state.nav && $scope.state.nav.currentFolder;
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
2014-05-08 10:25:20 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
function currentMessage() {
|
|
|
|
return $scope.state.mailList.selected;
|
|
|
|
}
|
2014-05-08 10:25:20 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
|
|
|
// Notification API
|
|
|
|
//
|
2014-05-08 10:25:20 -04:00
|
|
|
|
2014-12-05 07:04:10 -05:00
|
|
|
email.onIncomingMessage = function(msgs) {
|
2014-10-02 16:05:44 -04:00
|
|
|
var note, title, message, unreadMsgs;
|
2014-05-08 10:25:20 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
unreadMsgs = msgs.filter(function(msg) {
|
|
|
|
return msg.unread;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (unreadMsgs.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-16 13:01:49 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if (unreadMsgs.length === 1) {
|
|
|
|
title = unreadMsgs[0].from[0].name || unreadMsgs[0].from[0].address;
|
|
|
|
message = unreadMsgs[0].subject;
|
|
|
|
} else {
|
|
|
|
title = unreadMsgs.length + ' new messages';
|
|
|
|
message = _.pluck(unreadMsgs, 'subject').join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
note = notification.create({
|
|
|
|
title: title,
|
|
|
|
message: message,
|
|
|
|
onClick: function() {
|
|
|
|
// remove from pending notificatiosn
|
|
|
|
var index = $scope.pendingNotifications.indexOf(note);
|
|
|
|
if (index !== -1) {
|
|
|
|
$scope.pendingNotifications.splice(index, 1);
|
2014-09-16 13:01:49 -04:00
|
|
|
}
|
2014-12-04 06:46:55 -05:00
|
|
|
// open the message
|
2014-12-02 10:12:42 -05:00
|
|
|
$scope.navigate(_.findWhere(currentFolder().messages, {
|
2014-10-02 16:05:44 -04:00
|
|
|
uid: unreadMsgs[0].uid
|
|
|
|
}));
|
2014-10-17 05:17:40 -04:00
|
|
|
},
|
|
|
|
timeout: NOTIFICATION_INBOX_TIMEOUT
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
$scope.pendingNotifications.push(note);
|
2013-09-26 07:26:57 -04:00
|
|
|
};
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2013-09-05 05:53:14 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
2014-12-10 15:41:16 -05:00
|
|
|
// helper functions
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
2014-05-23 08:23:50 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
function byUidDescending(a, b) {
|
|
|
|
if (a.uid < b.uid) {
|
|
|
|
return 1;
|
|
|
|
} else if (b.uid < a.uid) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2014-07-16 06:47:25 -04:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = MailListCtrl;
|