2014-11-25 22:09:55 +01:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Mailbox
|
|
|
|
* @constructor
|
|
|
|
* @param {object} futureMailboxData - either an object literal or a promise
|
|
|
|
*/
|
2014-11-30 14:50:26 +01:00
|
|
|
function Mailbox(account, futureMailboxData) {
|
|
|
|
this.$account = account;
|
2014-11-25 22:09:55 +01:00
|
|
|
// Data is immediately available
|
|
|
|
if (typeof futureMailboxData.then !== 'function') {
|
2015-07-22 17:28:08 +02:00
|
|
|
this.init(futureMailboxData);
|
2014-12-18 21:22:29 +01:00
|
|
|
if (this.name && !this.path) {
|
|
|
|
// Create a new mailbox on the server
|
|
|
|
var newMailboxData = Mailbox.$$resource.create('createFolder', this.name);
|
|
|
|
this.$unwrap(newMailboxData);
|
|
|
|
}
|
2014-11-25 22:09:55 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// The promise will be unwrapped first
|
|
|
|
// NOTE: this condition never happen for the moment
|
|
|
|
this.$unwrap(futureMailboxData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Mailbox
|
|
|
|
* @desc The factory we'll use to register with Angular
|
|
|
|
* @returns the Mailbox constructor
|
|
|
|
*/
|
2015-07-22 17:28:08 +02:00
|
|
|
Mailbox.$factory = ['$q', '$timeout', '$log', 'sgSettings', 'Resource', 'Message', 'Acl', 'Preferences', 'sgMailbox_PRELOAD', function($q, $timeout, $log, Settings, Resource, Message, Acl, Preferences, PRELOAD) {
|
2014-11-25 22:09:55 +01:00
|
|
|
angular.extend(Mailbox, {
|
|
|
|
$q: $q,
|
|
|
|
$timeout: $timeout,
|
|
|
|
$log: $log,
|
2015-08-03 17:53:54 +02:00
|
|
|
$$resource: new Resource(Settings.activeUser('folderURL') + 'Mail', Settings.activeUser()),
|
2014-11-25 22:09:55 +01:00
|
|
|
$Message: Message,
|
2015-06-26 19:13:25 +02:00
|
|
|
$$Acl: Acl,
|
2015-07-22 17:28:08 +02:00
|
|
|
$Preferences: Preferences,
|
|
|
|
$query: { sort: 'date', asc: 0 },
|
2015-07-13 21:22:58 +02:00
|
|
|
selectedFolder: null,
|
2015-07-31 01:47:53 +02:00
|
|
|
$refreshTimeout: null,
|
2015-09-10 22:38:30 +02:00
|
|
|
$virtualMode: false,
|
2014-11-25 22:09:55 +01:00
|
|
|
PRELOAD: PRELOAD
|
|
|
|
});
|
2015-07-22 17:28:08 +02:00
|
|
|
// Initialize sort parameters from user's settings
|
|
|
|
Preferences.ready().then(function() {
|
|
|
|
if (Preferences.settings.Mail.SortingState) {
|
|
|
|
Mailbox.$query.sort = Preferences.settings.Mail.SortingState[0];
|
|
|
|
Mailbox.$query.asc = parseInt(Preferences.settings.Mail.SortingState[1]);
|
|
|
|
}
|
|
|
|
});
|
2014-11-25 22:09:55 +01:00
|
|
|
|
|
|
|
return Mailbox; // return constructor
|
|
|
|
}];
|
|
|
|
|
2015-01-06 04:34:12 +01:00
|
|
|
/**
|
|
|
|
* @module SOGo.MailerUI
|
2015-07-29 17:47:01 +02:00
|
|
|
* @desc Factory registration of Mailbox in Angular module.
|
2015-01-06 04:34:12 +01:00
|
|
|
*/
|
2015-07-29 17:47:01 +02:00
|
|
|
try {
|
|
|
|
angular.module('SOGo.MailerUI');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
angular.module('SOGo.MailerUI', ['SOGo.Common']);
|
|
|
|
}
|
2014-11-25 22:09:55 +01:00
|
|
|
angular.module('SOGo.MailerUI')
|
|
|
|
.constant('sgMailbox_PRELOAD', {
|
|
|
|
LOOKAHEAD: 50,
|
|
|
|
SIZE: 100
|
|
|
|
})
|
2015-05-06 04:06:13 +02:00
|
|
|
.factory('Mailbox', Mailbox.$factory);
|
2014-11-25 22:09:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Mailbox
|
|
|
|
* @desc Fetch list of mailboxes of a specific account
|
2014-11-30 14:50:26 +01:00
|
|
|
* @param {string} accountId - the account
|
2014-12-11 17:24:22 +01:00
|
|
|
* @return a promise of the HTTP operation
|
2014-11-25 22:09:55 +01:00
|
|
|
* @see {@link Account.$getMailboxes}
|
|
|
|
*/
|
2014-11-30 14:50:26 +01:00
|
|
|
Mailbox.$find = function(account) {
|
2014-11-25 22:09:55 +01:00
|
|
|
var path, futureMailboxData;
|
|
|
|
|
2015-03-18 19:59:06 +01:00
|
|
|
futureMailboxData = this.$$resource.fetch(account.id.toString(), 'view');
|
2014-11-25 22:09:55 +01:00
|
|
|
|
2014-11-30 14:50:26 +01:00
|
|
|
return Mailbox.$unwrapCollection(account, futureMailboxData); // a collection of mailboxes
|
2014-11-25 22:09:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Mailbox
|
|
|
|
* @desc Unwrap to a collection of Mailbox instances.
|
2014-11-30 14:50:26 +01:00
|
|
|
* @param {string} account - the account
|
2014-11-25 22:09:55 +01:00
|
|
|
* @param {promise} futureMailboxData - a promise of the mailboxes metadata
|
|
|
|
* @returns a promise of a collection of Mailbox objects
|
|
|
|
*/
|
2014-11-30 14:50:26 +01:00
|
|
|
Mailbox.$unwrapCollection = function(account, futureMailboxData) {
|
2014-11-25 22:09:55 +01:00
|
|
|
var collection = [],
|
|
|
|
// Local recursive function
|
2015-04-29 17:23:21 +02:00
|
|
|
createMailboxes = function(level, mailbox) {
|
2014-11-25 22:09:55 +01:00
|
|
|
for (var i = 0; i < mailbox.children.length; i++) {
|
2015-04-29 17:23:21 +02:00
|
|
|
mailbox.children[i].level = level;
|
2014-11-30 14:50:26 +01:00
|
|
|
mailbox.children[i] = new Mailbox(account, mailbox.children[i]);
|
2015-04-29 17:23:21 +02:00
|
|
|
createMailboxes(level+1, mailbox.children[i]);
|
2014-11-25 22:09:55 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
//collection.$futureMailboxData = futureMailboxData;
|
|
|
|
|
|
|
|
return futureMailboxData.then(function(data) {
|
|
|
|
return Mailbox.$timeout(function() {
|
|
|
|
// Each entry is spun up as a Mailbox instance
|
|
|
|
angular.forEach(data.mailboxes, function(data, index) {
|
2015-04-29 17:23:21 +02:00
|
|
|
data.level = 0;
|
2014-11-30 14:50:26 +01:00
|
|
|
var mailbox = new Mailbox(account, data);
|
2015-04-29 17:23:21 +02:00
|
|
|
createMailboxes(1, mailbox); // recursively create all sub-mailboxes
|
2014-11-25 22:09:55 +01:00
|
|
|
collection.push(mailbox);
|
|
|
|
});
|
2015-12-22 03:14:39 +01:00
|
|
|
// Update inbox quota
|
|
|
|
if (data.quotas)
|
|
|
|
account.updateQuota(data.quotas);
|
2014-11-25 22:09:55 +01:00
|
|
|
return collection;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Mailbox
|
|
|
|
* @desc Build the path of the mailbox (or account only).
|
|
|
|
* @param {string} accountId - the account ID
|
2015-03-18 19:59:06 +01:00
|
|
|
* @param {string} [mailboxPath] - the mailbox path
|
2014-11-25 22:09:55 +01:00
|
|
|
* @returns a string representing the path relative to the mail module
|
|
|
|
*/
|
|
|
|
Mailbox.$absolutePath = function(accountId, mailboxPath) {
|
|
|
|
var path = [];
|
|
|
|
|
|
|
|
if (mailboxPath) {
|
|
|
|
path = _.map(mailboxPath.split('/'), function(component) {
|
|
|
|
return 'folder' + component.asCSSIdentifier();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
path.splice(0, 0, accountId); // insert account ID
|
|
|
|
|
2015-07-03 21:17:02 +02:00
|
|
|
return path.join('/');
|
2014-11-25 22:09:55 +01:00
|
|
|
};
|
2015-01-08 17:52:10 +01:00
|
|
|
|
2015-07-22 17:28:08 +02:00
|
|
|
/**
|
|
|
|
* @function init
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Extend instance with new data and compute additional attributes.
|
|
|
|
* @param {object} data - attributes of mailbox
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.init = function(data) {
|
|
|
|
var _this = this;
|
2015-08-25 18:20:46 +02:00
|
|
|
this.$isLoading = true;
|
2015-07-22 17:28:08 +02:00
|
|
|
this.$messages = [];
|
|
|
|
this.uidsMap = {};
|
|
|
|
angular.extend(this, data);
|
|
|
|
if (this.path) {
|
|
|
|
this.id = this.$id();
|
|
|
|
this.$acl = new Mailbox.$$Acl('Mail/' + this.id);
|
|
|
|
}
|
|
|
|
if (this.type) {
|
|
|
|
this.$isEditable = this.isEditable();
|
|
|
|
}
|
2016-02-10 16:00:59 +01:00
|
|
|
this.$isNoInferiors = this.isNoInferiors();
|
2015-07-22 17:28:08 +02:00
|
|
|
if (angular.isUndefined(this.$shadowData)) {
|
|
|
|
// Make a copy of the data for an eventual reset
|
|
|
|
this.$shadowData = this.$omit();
|
|
|
|
}
|
2015-07-24 22:14:53 +02:00
|
|
|
};
|
2015-07-22 17:28:08 +02:00
|
|
|
|
2015-08-25 17:45:17 +02:00
|
|
|
/**
|
|
|
|
* @function getLength
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Used by md-virtual-repeat / md-on-demand
|
2015-12-22 03:14:39 +01:00
|
|
|
* @returns the number of messages in the mailbox
|
2015-08-25 17:45:17 +02:00
|
|
|
*/
|
|
|
|
Mailbox.prototype.getLength = function() {
|
|
|
|
return this.$messages.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function getItemAtIndex
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Used by md-virtual-repeat / md-on-demand
|
2015-12-22 03:14:39 +01:00
|
|
|
* @returns the message at the specified index
|
2015-08-25 17:45:17 +02:00
|
|
|
*/
|
|
|
|
Mailbox.prototype.getItemAtIndex = function(index) {
|
|
|
|
var message;
|
|
|
|
|
|
|
|
if (index >= 0 && index < this.$messages.length) {
|
|
|
|
message = this.$messages[index];
|
2016-02-03 16:51:59 +01:00
|
|
|
this.$lastVisibleIndex = Math.max(0, index - 3); // Magic number is NUM_EXTRA from virtual-repeater.js
|
2015-08-25 17:45:17 +02:00
|
|
|
|
|
|
|
if (this.$loadMessage(message.uid))
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
/**
|
|
|
|
* @function $id
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Build the unique ID to identified the mailbox.
|
|
|
|
* @returns a string representing the path relative to the mail module
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$id = function() {
|
2014-11-30 14:50:26 +01:00
|
|
|
return Mailbox.$absolutePath(this.$account.id, this.path);
|
2014-11-25 22:09:55 +01:00
|
|
|
};
|
|
|
|
|
2015-05-09 20:37:40 +02:00
|
|
|
/**
|
|
|
|
* @function $selectedCount
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Return the number of messages selected by the user.
|
|
|
|
* @returns the number of selected messages
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$selectedCount = function() {
|
|
|
|
var count;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
if (this.$messages) {
|
2015-07-24 22:14:53 +02:00
|
|
|
count = (_.filter(this.$messages, function(message) { return message.selected; })).length;
|
2015-05-09 20:37:40 +02:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
/**
|
2015-09-24 22:48:20 +02:00
|
|
|
* @function isSelectedMessage
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Check if the specified message is selected.
|
|
|
|
* @param {string} messageId
|
|
|
|
* @returns true if the specified message is selected
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.isSelectedMessage = function(messageId) {
|
|
|
|
return this.selectedMessage == messageId;
|
|
|
|
};
|
|
|
|
|
2015-12-22 03:14:39 +01:00
|
|
|
/**
|
2015-03-18 19:59:06 +01:00
|
|
|
* @function $filter
|
2014-11-25 22:09:55 +01:00
|
|
|
* @memberof Mailbox.prototype
|
2015-03-18 19:59:06 +01:00
|
|
|
* @desc Fetch the messages metadata of the mailbox
|
|
|
|
* @param {object} [sort] - sort preferences. Defaults to descendent by date.
|
|
|
|
* @param {string} sort.match - either AND or OR
|
|
|
|
* @param {string} sort.sort - either arrival, subject, from, to, date, or size
|
|
|
|
* @param {boolean} sort.asc - sort is ascendant if true
|
2015-07-20 22:44:26 +02:00
|
|
|
* @param {object[]} [filters] - list of filters for the query
|
|
|
|
* @param {string} filters.searchBy - either subject, from, to, cc, or body
|
|
|
|
* @param {string} filters.searchInput - the search string to match
|
|
|
|
* @param {boolean} filters.negative - negate the condition
|
2014-11-25 22:09:55 +01:00
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2015-07-22 17:28:08 +02:00
|
|
|
Mailbox.prototype.$filter = function(sortingAttributes, filters) {
|
|
|
|
var _this = this, options = {};
|
2015-03-18 19:59:06 +01:00
|
|
|
|
2015-07-31 19:32:14 +02:00
|
|
|
if (!angular.isDefined(this.unseenCount))
|
|
|
|
this.unseenCount = 0;
|
|
|
|
|
2015-12-10 17:21:29 +01:00
|
|
|
Mailbox.$timeout(function() {
|
|
|
|
_this.$isLoading = true;
|
|
|
|
});
|
2014-11-25 22:09:55 +01:00
|
|
|
|
2015-07-22 17:28:08 +02:00
|
|
|
return Mailbox.$Preferences.ready().then(function() {
|
2015-07-31 01:47:53 +02:00
|
|
|
|
|
|
|
if (Mailbox.$refreshTimeout)
|
|
|
|
Mailbox.$timeout.cancel(Mailbox.$refreshTimeout);
|
|
|
|
|
2015-07-22 17:28:08 +02:00
|
|
|
if (sortingAttributes)
|
|
|
|
// Sorting preferences are common to all mailboxes
|
|
|
|
angular.extend(Mailbox.$query, sortingAttributes);
|
|
|
|
|
|
|
|
angular.extend(options, { sortingAttributes: Mailbox.$query });
|
|
|
|
if (angular.isDefined(filters)) {
|
|
|
|
options.filters = _.reject(filters, function(filter) {
|
2015-07-24 22:14:53 +02:00
|
|
|
return angular.isUndefined(filter.searchInput) || filter.searchInput.length === 0;
|
2015-07-22 17:28:08 +02:00
|
|
|
});
|
2016-02-08 16:07:44 +01:00
|
|
|
// Decompose filters that match two fields
|
2015-07-22 17:28:08 +02:00
|
|
|
_.each(options.filters, function(filter) {
|
|
|
|
var secondFilter,
|
|
|
|
match = filter.searchBy.match(/(\w+)_or_(\w+)/);
|
|
|
|
if (match) {
|
|
|
|
options.sortingAttributes.match = 'OR';
|
|
|
|
filter.searchBy = match[1];
|
|
|
|
secondFilter = angular.copy(filter);
|
|
|
|
secondFilter.searchBy = match[2];
|
|
|
|
options.filters.push(secondFilter);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-31 01:47:53 +02:00
|
|
|
// Restart the refresh timer, if needed
|
2015-09-10 22:38:30 +02:00
|
|
|
if (!Mailbox.$virtualMode) {
|
|
|
|
var refreshViewCheck = Mailbox.$Preferences.defaults.SOGoRefreshViewCheck;
|
|
|
|
if (refreshViewCheck && refreshViewCheck != 'manually') {
|
2016-02-08 16:07:44 +01:00
|
|
|
var f = angular.bind(_this, Mailbox.prototype.$filter, null, filters);
|
2015-09-10 22:38:30 +02:00
|
|
|
Mailbox.$refreshTimeout = Mailbox.$timeout(f, refreshViewCheck.timeInterval()*1000);
|
|
|
|
}
|
2015-07-31 01:47:53 +02:00
|
|
|
}
|
|
|
|
|
2015-07-22 17:28:08 +02:00
|
|
|
var futureMailboxData = Mailbox.$$resource.post(_this.id, 'view', options);
|
|
|
|
return _this.$unwrap(futureMailboxData);
|
|
|
|
});
|
2014-11-25 22:09:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $loadMessage
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Check if the message is loaded and in any case, fetch more messages headers from the server.
|
|
|
|
* @returns true if the message metadata are already fetched
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$loadMessage = function(messageId) {
|
|
|
|
var startIndex = this.uidsMap[messageId],
|
|
|
|
endIndex,
|
2016-02-08 21:39:00 +01:00
|
|
|
index,
|
2014-11-25 22:09:55 +01:00
|
|
|
max = this.$messages.length,
|
|
|
|
loaded = false,
|
|
|
|
uids,
|
|
|
|
futureHeadersData;
|
|
|
|
if (angular.isDefined(this.uidsMap[messageId]) && startIndex < this.$messages.length) {
|
|
|
|
// Index is valid
|
|
|
|
if (angular.isDefined(this.$messages[startIndex].subject)) {// || this.$messages[startIndex].loading) {
|
|
|
|
// Message headers are loaded or data is coming
|
|
|
|
loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preload more headers if possible
|
|
|
|
endIndex = Math.min(startIndex + Mailbox.PRELOAD.LOOKAHEAD, max - 1);
|
2016-02-08 21:39:00 +01:00
|
|
|
if (angular.isDefined(this.$messages[endIndex].subject) ||
|
|
|
|
angular.isDefined(this.$messages[endIndex].loading)) {
|
|
|
|
index = Math.max(startIndex - Mailbox.PRELOAD.LOOKAHEAD, 0);
|
|
|
|
if (!angular.isDefined(this.$messages[index].subject) &&
|
|
|
|
!angular.isDefined(this.$messages[index].loading)) {
|
|
|
|
// Previous messages not loaded; preload more headers further up
|
|
|
|
endIndex = startIndex;
|
|
|
|
startIndex = Math.max(startIndex - Mailbox.PRELOAD.SIZE, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// Next messages not load; preload more headers further down
|
2016-02-23 16:23:51 +01:00
|
|
|
endIndex = Math.min(startIndex + Mailbox.PRELOAD.SIZE, max - 1);
|
2016-02-08 21:39:00 +01:00
|
|
|
|
|
|
|
if (!angular.isDefined(this.$messages[startIndex].subject) &&
|
|
|
|
!angular.isDefined(this.$messages[startIndex].loading) ||
|
|
|
|
!angular.isDefined(this.$messages[endIndex].subject) &&
|
|
|
|
!angular.isDefined(this.$messages[endIndex].loading)) {
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
for (uids = []; startIndex < endIndex && startIndex < max; startIndex++) {
|
|
|
|
if (angular.isDefined(this.$messages[startIndex].subject) || this.$messages[startIndex].loading) {
|
|
|
|
// Message at this index is already loaded; increase the end index
|
|
|
|
endIndex++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Message at this index will be loaded
|
|
|
|
uids.push(this.$messages[startIndex].uid);
|
|
|
|
this.$messages[startIndex].loading = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Mailbox.$log.debug('Loading UIDs ' + uids.join(' '));
|
|
|
|
futureHeadersData = Mailbox.$$resource.post(this.id, 'headers', {uids: uids});
|
|
|
|
this.$unwrapHeaders(futureHeadersData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return loaded;
|
|
|
|
};
|
2014-12-11 17:24:22 +01:00
|
|
|
|
2014-12-18 21:22:29 +01:00
|
|
|
/**
|
2015-07-22 17:28:08 +02:00
|
|
|
* @function isEditable
|
2014-12-18 21:22:29 +01:00
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Checks if the mailbox is editable based on its type.
|
|
|
|
* @returns true if the mailbox is not a special folder.
|
|
|
|
*/
|
2015-07-22 17:28:08 +02:00
|
|
|
Mailbox.prototype.isEditable = function() {
|
2015-07-21 21:48:31 +02:00
|
|
|
return this.type == 'folder';
|
2014-12-18 21:22:29 +01:00
|
|
|
};
|
|
|
|
|
2016-02-10 16:00:59 +01:00
|
|
|
/**
|
|
|
|
* @function isNoInferiors
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Checks if the mailbox can contain submailboxes
|
|
|
|
* @returns true if the mailbox can not contain submailboxes
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.isNoInferiors = function() {
|
|
|
|
return this.flags.indexOf('noinferiors') >= 0;
|
|
|
|
};
|
|
|
|
|
2015-01-08 17:52:10 +01:00
|
|
|
/**
|
|
|
|
* @function $rename
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Rename the addressbook and keep the list sorted
|
|
|
|
* @param {string} name - the new name
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$rename = function() {
|
|
|
|
var _this = this,
|
|
|
|
findParent,
|
|
|
|
parent,
|
|
|
|
children,
|
|
|
|
i;
|
|
|
|
|
2015-01-30 19:50:26 +01:00
|
|
|
if (this.name == this.$shadowData.name) {
|
|
|
|
// Name hasn't changed
|
2015-09-16 22:15:04 +02:00
|
|
|
return Mailbox.$q.when();
|
2015-01-30 19:50:26 +01:00
|
|
|
}
|
|
|
|
|
2015-01-08 17:52:10 +01:00
|
|
|
// Local recursive function
|
|
|
|
findParent = function(parent, children) {
|
|
|
|
var parentMailbox = null,
|
|
|
|
mailbox = _.find(children, function(o) {
|
|
|
|
return o.path == _this.path;
|
|
|
|
});
|
|
|
|
if (mailbox) {
|
|
|
|
parentMailbox = parent;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
angular.forEach(children, function(o) {
|
|
|
|
if (!parentMailbox && o.children && o.children.length > 0) {
|
|
|
|
parentMailbox = findParent(o, o.children);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return parentMailbox;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Find mailbox parent
|
|
|
|
parent = findParent(null, this.$account.$mailboxes);
|
2015-07-24 22:14:53 +02:00
|
|
|
if (parent === null)
|
2015-01-08 17:52:10 +01:00
|
|
|
children = this.$account.$mailboxes;
|
|
|
|
else
|
|
|
|
children = parent.children;
|
|
|
|
|
|
|
|
// Find index of mailbox among siblings
|
|
|
|
i = _.indexOf(_.pluck(children, 'id'), this.id);
|
|
|
|
|
2015-09-16 22:15:04 +02:00
|
|
|
return this.$save().then(function(data) {
|
2015-01-08 17:52:10 +01:00
|
|
|
var sibling;
|
|
|
|
angular.extend(_this, data); // update the path attribute
|
|
|
|
_this.id = _this.$id();
|
|
|
|
|
|
|
|
// Move mailbox among its siblings according to its new name
|
|
|
|
children.splice(i, 1);
|
|
|
|
sibling = _.find(children, function(o) {
|
|
|
|
Mailbox.$log.debug(o.name + ' ? ' + _this.name);
|
|
|
|
return (o.type == 'folder' && o.name.localeCompare(_this.name) > 0);
|
|
|
|
});
|
|
|
|
if (sibling) {
|
|
|
|
i = _.indexOf(_.pluck(children, 'id'), sibling.id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = children.length;
|
|
|
|
}
|
|
|
|
children.splice(i, 0, _this);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-07-21 15:28:54 +02:00
|
|
|
/**
|
|
|
|
* @function $compact
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Compact the mailbox
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$compact = function() {
|
2015-12-22 03:14:39 +01:00
|
|
|
var _this = this;
|
|
|
|
return Mailbox.$$resource.post(this.id, 'expunge')
|
|
|
|
.then(function(data) {
|
|
|
|
// Update inbox quota
|
|
|
|
if (data.quotas)
|
|
|
|
_this.$account.updateQuota(data.quotas);
|
|
|
|
});
|
2015-07-21 15:28:54 +02:00
|
|
|
};
|
|
|
|
|
2015-07-24 20:40:07 +02:00
|
|
|
/**
|
|
|
|
* @function $setFolderAs
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Set a folder as Drafts/Sent/Trash
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$setFolderAs = function(type) {
|
|
|
|
return Mailbox.$$resource.post(this.id, 'setAs' + type + 'Folder');
|
|
|
|
};
|
|
|
|
|
2015-07-21 15:28:54 +02:00
|
|
|
/**
|
|
|
|
* @function $emptyTrash
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Empty the Trash folder.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$emptyTrash = function() {
|
2015-08-19 03:16:05 +02:00
|
|
|
var _this = this;
|
2015-08-11 20:48:45 +02:00
|
|
|
|
2015-12-22 03:14:39 +01:00
|
|
|
return Mailbox.$$resource.post(this.id, 'emptyTrash').then(function(data) {
|
2015-08-11 20:48:45 +02:00
|
|
|
// Remove all messages from the mailbox
|
|
|
|
_this.$messages = [];
|
|
|
|
_this.uidsMap = {};
|
|
|
|
_this.unseenCount = 0;
|
2015-08-18 16:10:37 +02:00
|
|
|
|
|
|
|
// If we had any submailboxes, lets do a refresh of the mailboxes list
|
|
|
|
if (angular.isDefined(_this.children) && _this.children.length)
|
|
|
|
_this.$account.$getMailboxes({reload: true});
|
2015-12-22 03:14:39 +01:00
|
|
|
|
|
|
|
// Update inbox quota
|
|
|
|
if (data.quotas)
|
|
|
|
_this.$account.updateQuota(data.quotas);
|
2015-08-11 20:48:45 +02:00
|
|
|
});
|
2015-07-21 15:28:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $markAsRead
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Mark all messages from folder as read
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$markAsRead = function() {
|
|
|
|
return Mailbox.$$resource.post(this.id, 'markRead');
|
|
|
|
};
|
|
|
|
|
2015-08-10 19:10:45 +02:00
|
|
|
/**
|
|
|
|
* @function $flagMessages
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Add or remove a flag on a message set
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$flagMessages = function(uids, flags, operation) {
|
|
|
|
var data = {msgUIDs: uids,
|
|
|
|
flags: flags,
|
|
|
|
operation: operation};
|
|
|
|
|
|
|
|
return Mailbox.$$resource.post(this.id, 'addOrRemoveLabel', data);
|
|
|
|
};
|
|
|
|
|
2014-12-18 21:22:29 +01:00
|
|
|
/**
|
|
|
|
* @function $delete
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Delete the mailbox from the server
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$delete = function() {
|
2015-09-30 17:29:05 +02:00
|
|
|
var _this = this;
|
2014-12-18 21:22:29 +01:00
|
|
|
|
2015-09-30 17:29:05 +02:00
|
|
|
return Mailbox.$$resource.remove(this.id)
|
|
|
|
.then(function() {
|
|
|
|
_this.$account.$getMailboxes({reload: true});
|
|
|
|
return true;
|
|
|
|
});
|
2014-12-18 21:22:29 +01:00
|
|
|
};
|
|
|
|
|
2016-02-03 22:48:04 +01:00
|
|
|
/**
|
|
|
|
* @function $_deleteMessages
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Delete multiple messages from Mailbox object.
|
|
|
|
* @param {string[]} uids - the messages uids
|
|
|
|
* @param {object[]} messages - the Message instances
|
|
|
|
* @return the index of the first deleted message
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$_deleteMessages = function(uids, messages) {
|
|
|
|
var _this = this, selectedMessages, selectedUIDs, unseen, firstIndex = this.$messages.length;
|
|
|
|
|
|
|
|
// Decrement the unseen count
|
|
|
|
unseen = _.filter(messages, function(message, i) { return !message.isread; });
|
|
|
|
this.unseenCount -= unseen.length;
|
|
|
|
|
|
|
|
// Remove messages from $messages and uidsMap
|
|
|
|
_.forEachRight(this.$messages, function(message, index) {
|
|
|
|
var selectedIndex = _.findIndex(uids, function(uid) {
|
|
|
|
return message.uid == uid;
|
|
|
|
});
|
|
|
|
if (selectedIndex > -1) {
|
|
|
|
uids.splice(selectedIndex, 1);
|
|
|
|
delete _this.uidsMap[message.uid];
|
|
|
|
if (message.uid == _this.selectedMessage)
|
|
|
|
delete _this.selectedMessage;
|
|
|
|
_this.$messages.splice(index, 1);
|
|
|
|
if (index < firstIndex)
|
|
|
|
firstIndex = index;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_this.uidsMap[message.uid] -= uids.length;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return the index of the first deleted message
|
|
|
|
return firstIndex;
|
|
|
|
};
|
|
|
|
|
2014-12-11 17:24:22 +01:00
|
|
|
/**
|
|
|
|
* @function $deleteMessages
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Delete multiple messages from mailbox.
|
|
|
|
* @return a promise of the HTTP operation
|
|
|
|
*/
|
2015-12-10 17:21:29 +01:00
|
|
|
Mailbox.prototype.$deleteMessages = function(messages) {
|
|
|
|
var _this = this, uids;
|
|
|
|
|
|
|
|
uids = _.pluck(messages, 'uid');
|
|
|
|
return Mailbox.$$resource.post(this.id, 'batchDelete', {uids: uids})
|
2015-12-22 03:14:39 +01:00
|
|
|
.then(function(data) {
|
|
|
|
// Update inbox quota
|
|
|
|
if (data.quotas)
|
|
|
|
_this.$account.updateQuota(data.quotas);
|
2016-01-08 22:31:06 +01:00
|
|
|
|
2016-02-03 22:48:04 +01:00
|
|
|
return _this.$_deleteMessages(uids, messages);
|
2015-12-10 17:21:29 +01:00
|
|
|
});
|
2014-12-11 17:24:22 +01:00
|
|
|
};
|
|
|
|
|
2016-02-08 21:36:01 +01:00
|
|
|
/**
|
|
|
|
* @function $markOrUnMarkMessagesAsJunk
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Mark messages as junk/not junk
|
|
|
|
* @return a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$markOrUnMarkMessagesAsJunk = function(messages) {
|
|
|
|
var _this = this, uids;
|
|
|
|
var method = (this.type == 'junk' ? 'markMessagesAsNotJunk' : 'markMessagesAsJunk');
|
|
|
|
uids = _.pluck(messages, 'uid');
|
|
|
|
|
|
|
|
return Mailbox.$$resource.post(this.id, method, {uids: uids});
|
|
|
|
};
|
|
|
|
|
2015-06-30 23:13:55 +02:00
|
|
|
/**
|
|
|
|
* @function $copyMessages
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Copy multiple messages from the current mailbox to a target one
|
|
|
|
* @return a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$copyMessages = function(uids, folder) {
|
2016-02-03 22:48:04 +01:00
|
|
|
var _this = this;
|
|
|
|
|
2015-12-22 03:14:39 +01:00
|
|
|
return Mailbox.$$resource.post(this.id, 'copyMessages', {uids: uids, folder: folder})
|
|
|
|
.then(function(data) {
|
|
|
|
// Update inbox quota
|
|
|
|
if (data.quotas)
|
|
|
|
_this.$account.updateQuota(data.quotas);
|
|
|
|
});
|
2015-06-30 23:13:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $moveMessages
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Move multiple messages from the current mailbox to a target one
|
|
|
|
* @return a promise of the HTTP operation
|
|
|
|
*/
|
2016-02-03 22:48:04 +01:00
|
|
|
Mailbox.prototype.$moveMessages = function(messages, folder) {
|
|
|
|
var _this = this, uids;
|
|
|
|
|
|
|
|
uids = _.pluck(messages, 'uid');
|
|
|
|
return Mailbox.$$resource.post(this.id, 'moveMessages', {uids: uids, folder: folder})
|
|
|
|
.then(function() {
|
|
|
|
return _this.$_deleteMessages(uids, messages);
|
|
|
|
});
|
2015-06-30 23:13:55 +02:00
|
|
|
};
|
|
|
|
|
2015-01-08 17:52:10 +01:00
|
|
|
/**
|
|
|
|
* @function $reset
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Reset the original state the mailbox's data.
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$reset = function() {
|
|
|
|
var _this = this;
|
|
|
|
angular.forEach(this, function(value, key) {
|
|
|
|
if (key != 'constructor' && key != 'children' && key[0] != '$') {
|
|
|
|
delete _this[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
angular.extend(this, this.$shadowData);
|
|
|
|
this.$shadowData = this.$omit();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $save
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Save the mailbox to the server. This currently can only affect the name of the mailbox.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$save = function() {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
return Mailbox.$$resource.save(this.id, this.$omit()).then(function(data) {
|
|
|
|
// Make a copy of the data for an eventual reset
|
|
|
|
_this.$shadowData = _this.$omit();
|
|
|
|
Mailbox.$log.debug(JSON.stringify(data, undefined, 2));
|
|
|
|
return data;
|
|
|
|
}, function(data) {
|
|
|
|
Mailbox.$log.error(JSON.stringify(data, undefined, 2));
|
|
|
|
// Restore previous version
|
|
|
|
_this.$reset();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-12-18 22:30:17 +01:00
|
|
|
/**
|
|
|
|
* @function $newMailbox
|
2015-05-06 04:06:13 +02:00
|
|
|
* @memberof Mailbox.prototype
|
2014-12-18 22:30:17 +01:00
|
|
|
* @desc Create a new mailbox on the server and refresh the list of mailboxes.
|
|
|
|
* @returns a promise of the HTTP operations
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$newMailbox = function(path, name) {
|
|
|
|
return this.$account.$newMailbox(path, name);
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
/**
|
|
|
|
* @function $omit
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Return a sanitized object used to send to the server.
|
|
|
|
* @return an object literal copy of the Mailbox instance
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$omit = function() {
|
|
|
|
var mailbox = {};
|
|
|
|
angular.forEach(this, function(value, key) {
|
|
|
|
if (key != 'constructor' &&
|
|
|
|
key != 'children' &&
|
|
|
|
key[0] != '$') {
|
|
|
|
mailbox[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return mailbox;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $unwrap
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Unwrap a promise and instanciate new Message objects using received data.
|
|
|
|
* @param {promise} futureMailboxData - a promise of the Mailbox's metadata
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$unwrap = function(futureMailboxData) {
|
|
|
|
var _this = this,
|
|
|
|
deferred = Mailbox.$q.defer();
|
|
|
|
|
|
|
|
this.$futureMailboxData = futureMailboxData;
|
|
|
|
this.$futureMailboxData.then(function(data) {
|
|
|
|
Mailbox.$timeout(function() {
|
|
|
|
var uids, headers;
|
|
|
|
|
2016-02-10 16:00:59 +01:00
|
|
|
if (!data.uids || _this.$topIndex > data.uids.length - 1)
|
2016-02-05 16:49:50 +01:00
|
|
|
_this.$topIndex = 0;
|
|
|
|
|
2015-07-22 17:28:08 +02:00
|
|
|
_this.init(data);
|
2014-11-25 22:09:55 +01:00
|
|
|
|
|
|
|
if (_this.uids) {
|
2015-05-06 04:06:13 +02:00
|
|
|
Mailbox.$log.debug('unwrapping ' + data.uids.length + ' messages');
|
2015-08-25 17:45:17 +02:00
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
// First entry of 'headers' are keys
|
|
|
|
headers = _.invoke(_this.headers[0], 'toLowerCase');
|
|
|
|
_this.headers.splice(0, 1);
|
|
|
|
|
|
|
|
// First entry of 'uids' are keys when threaded view is enabled
|
|
|
|
if (_this.threaded) {
|
|
|
|
uids = _this.uids[0];
|
|
|
|
_this.uids.splice(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instanciate Message objects
|
|
|
|
_.reduce(_this.uids, function(msgs, msg, i) {
|
|
|
|
var data;
|
|
|
|
if (_this.threaded)
|
|
|
|
data = _.object(uids, msg);
|
|
|
|
else
|
|
|
|
data = {uid: msg.toString()};
|
|
|
|
|
|
|
|
// Build map of UID <=> index
|
|
|
|
_this.uidsMap[data.uid] = i;
|
|
|
|
|
2015-08-26 17:56:47 +02:00
|
|
|
msgs.push(new Mailbox.$Message(_this.$account.id, _this, data, true));
|
2014-11-25 22:09:55 +01:00
|
|
|
|
|
|
|
return msgs;
|
|
|
|
}, _this.$messages);
|
|
|
|
|
|
|
|
// Extend Message objects with received headers
|
|
|
|
_.each(_this.headers, function(data) {
|
|
|
|
var msg = _.object(headers, data),
|
|
|
|
i = _this.uidsMap[msg.uid.toString()];
|
|
|
|
_.extend(_this.$messages[i], msg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Mailbox.$log.debug('mailbox ' + _this.id + ' ready');
|
2015-05-06 04:06:13 +02:00
|
|
|
_this.$isLoading = false;
|
2014-11-25 22:09:55 +01:00
|
|
|
deferred.resolve(_this.$messages);
|
|
|
|
});
|
|
|
|
}, function(data) {
|
|
|
|
angular.extend(_this, data);
|
|
|
|
_this.isError = true;
|
|
|
|
deferred.reject();
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $unwrapHeaders
|
|
|
|
* @memberof Mailbox.prototype
|
|
|
|
* @desc Unwrap a promise and extend matching Message objects using received data.
|
|
|
|
* @param {promise} futureHeadersData - a promise of some messages metadata
|
|
|
|
*/
|
|
|
|
Mailbox.prototype.$unwrapHeaders = function(futureHeadersData) {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
futureHeadersData.then(function(data) {
|
|
|
|
Mailbox.$timeout(function() {
|
|
|
|
var headers, j;
|
|
|
|
if (data.length > 0) {
|
|
|
|
// First entry of 'headers' are keys
|
|
|
|
headers = _.invoke(data[0], 'toLowerCase');
|
|
|
|
data.splice(0, 1);
|
|
|
|
_.each(data, function(messageHeaders) {
|
|
|
|
messageHeaders = _.object(headers, messageHeaders);
|
|
|
|
j = _this.uidsMap[messageHeaders.uid.toString()];
|
|
|
|
if (angular.isDefined(j)) {
|
|
|
|
_.extend(_this.$messages[j], messageHeaders);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2015-01-08 17:52:10 +01:00
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
})();
|