fix(mail(js)): limit number of messages to batch delete per API call
parent
3433a2e765
commit
4e2d5098c7
|
@ -31,7 +31,7 @@
|
||||||
* @desc The factory we'll use to register with Angular
|
* @desc The factory we'll use to register with Angular
|
||||||
* @returns the Mailbox constructor
|
* @returns the Mailbox constructor
|
||||||
*/
|
*/
|
||||||
Mailbox.$factory = ['$q', '$timeout', '$log', 'sgSettings', 'Resource', 'Message', 'Acl', 'Preferences', 'sgMailbox_PRELOAD', function($q, $timeout, $log, Settings, Resource, Message, Acl, Preferences, PRELOAD) {
|
Mailbox.$factory = ['$q', '$timeout', '$log', 'sgSettings', 'Resource', 'Message', 'Acl', 'Preferences', 'sgMailbox_PRELOAD', 'sgMailbox_BATCH_DELETE_LIMIT', function($q, $timeout, $log, Settings, Resource, Message, Acl, Preferences, PRELOAD, BATCH_DELETE_LIMIT) {
|
||||||
angular.extend(Mailbox, {
|
angular.extend(Mailbox, {
|
||||||
$q: $q,
|
$q: $q,
|
||||||
$timeout: $timeout,
|
$timeout: $timeout,
|
||||||
|
@ -45,7 +45,8 @@
|
||||||
$refreshTimeout: null,
|
$refreshTimeout: null,
|
||||||
$virtualMode: false,
|
$virtualMode: false,
|
||||||
$virtualPath: false,
|
$virtualPath: false,
|
||||||
PRELOAD: PRELOAD
|
PRELOAD: PRELOAD,
|
||||||
|
BATCH_DELETE_LIMIT: BATCH_DELETE_LIMIT
|
||||||
});
|
});
|
||||||
// Initialize sort parameters from user's settings
|
// Initialize sort parameters from user's settings
|
||||||
if (Preferences.settings.Mail.SortingState) {
|
if (Preferences.settings.Mail.SortingState) {
|
||||||
|
@ -71,6 +72,7 @@
|
||||||
LOOKAHEAD: 50,
|
LOOKAHEAD: 50,
|
||||||
SIZE: 100
|
SIZE: 100
|
||||||
})
|
})
|
||||||
|
.constant('sgMailbox_BATCH_DELETE_LIMIT', 1000)
|
||||||
.factory('Mailbox', Mailbox.$factory);
|
.factory('Mailbox', Mailbox.$factory);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -769,25 +771,36 @@
|
||||||
/**
|
/**
|
||||||
* @function $deleteMessages
|
* @function $deleteMessages
|
||||||
* @memberof Mailbox.prototype
|
* @memberof Mailbox.prototype
|
||||||
* @desc Delete multiple messages from mailbox.
|
* @desc Delete multiple messages from mailbox by batch of 1000 messages (see constant sgMailbox_BATCH_DELETE_LIMIT).
|
||||||
* @param {object} [options] - additional options (use {withoutTrash: true} to delete immediately)
|
* @param {object} [options] - additional options (use {withoutTrash: true} to delete immediately)
|
||||||
* @return a promise of the HTTP operation
|
* @return a promise of the HTTP operation
|
||||||
*/
|
*/
|
||||||
Mailbox.prototype.$deleteMessages = function(messages, options) {
|
Mailbox.prototype.$deleteMessages = function(messages, options) {
|
||||||
var _this = this, uids, data;
|
var _this = this, uids,
|
||||||
|
batchSize = Mailbox.BATCH_DELETE_LIMIT;
|
||||||
|
|
||||||
uids = _.map(messages, 'uid');
|
uids = _.map(messages, 'uid');
|
||||||
data = { uids: uids };
|
|
||||||
if (options) angular.extend(data, options);
|
|
||||||
|
|
||||||
return Mailbox.$$resource.post(this.id, 'batchDelete', data)
|
// Recursive function to synchronously delete batch of messages
|
||||||
.then(function(data) {
|
function _deleteMessages(start, end) {
|
||||||
// Update inbox quota
|
var currentUids = uids.slice(start, end),
|
||||||
if (data.quotas)
|
currentMessages = uids.slice(start, end),
|
||||||
_this.$account.updateQuota(data.quotas);
|
data = { uids: currentUids };
|
||||||
|
return Mailbox.$$resource.post(_this.id, 'batchDelete', data).then(function(data) {
|
||||||
return _this.$_deleteMessages(uids, messages);
|
if (end < uids.length) {
|
||||||
|
_this.$_deleteMessages(currentUids, currentMessages);
|
||||||
|
return _deleteMessages(end, Math.min(end + batchSize, uids.length));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Update inbox quota
|
||||||
|
if (data.quotas)
|
||||||
|
_this.$account.updateQuota(data.quotas);
|
||||||
|
return _this.$_deleteMessages(currentUids, currentMessages);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return _deleteMessages(0, Math.min(batchSize, uids.length));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue