feat(mail): Deletion of mail older than x. Closes #6023.

This commit is contained in:
smizrahi
2024-11-20 16:34:30 +01:00
parent bce575a53d
commit d5e86f7387
16 changed files with 375 additions and 63 deletions
@@ -1313,9 +1313,20 @@
return this.$highlightWords;
};
/* TODO */
Mailbox.prototype.cleanMailbox = function () {
Mailbox.$$resource.post(this.id, 'cleanMailbox');
/**
* @function cleanMailbox
* @memberof Mailbox.prototype
* @desc Cleans up the mailbox by applying the specified parameters.
* This can include operations such as moving emails to trash,
* filtering emails based on a duration, and applying actions to subfolders.
* @param {Object} parameters - An object containing the parameters for cleaning the mailbox.
* @param {boolean} [parameters.applyToSubfolders=false] - Whether to apply the cleaning operation to subfolders.
* @param {boolean} [parameters.moveToTrash=false] - Whether to move the emails to the trash folder.
* @param {string|null} [parameters.filterDuration=null] - A duration filter (e.g., "3m", "6m") to select emails older than the specified duration.
* @returns {Promise} A promise that resolves when the cleaning operation is completed, or rejects with an error if the operation fails.
*/
Mailbox.prototype.cleanMailbox = function (parameters) {
return parameters.folders.length > 0 ? Mailbox.$$resource.post(this.id.split("/")[0], 'cleanMailbox', parameters) : Mailbox.$$resource.post(this.id, 'cleanMailbox', parameters);
};
})();
@@ -54,8 +54,8 @@
vm.reset();
});
$rootScope.$on('showRemoveOldEmailsPanel', function (e, d) {
vm.showRemoveOldEmailsPanel(d.folder);
$rootScope.$on('showCleanMailboxPanel', function (e, d) {
vm.showCleanMailboxPanel(d.folder, d.account);
});
};
@@ -451,32 +451,119 @@
});
};
this.showRemoveOldEmailsPanel = function (folder) {
this.showCleanMailboxPanel = function (folder, account) {
// Close sidenav on small devices
if (!$mdMedia(sgConstant['gt-md']))
$mdSidenav('left').close();
$mdDialog.show({
template: document.getElementById('removeOldEmails').innerHTML,
template: document.getElementById('CleanMailbox').innerHTML,
parent: angular.element(document.body),
controller: function () {
var dialogCtrl = this;
this.$onInit = function () {
// Pass main controller
this.mainController = vm;
this.folder = folder;
this.folderName = folder.$displayName;
this.isMailbox = (folder ? false : true);
this.name = folder ? folder.$displayName : account.name;
this.loading = false;
this.date = null;
this.form = {
filterDuration: "3m",
permanentlyDelete: false,
confirmDelete: false,
filterDurationDate: null
};
var today = new Date();
var maxDate = new Date(today);
maxDate.setMonth(today.getMonth() - 3);
this.maxDate = maxDate;
};
dialogCtrl.closeDialog = function () {
$mdDialog.hide();
};
dialogCtrl.isLoading = function () {
return this.loading;
}
dialogCtrl.isWarningDisplayed = function () {
return (this.form && this.form.permanentlyDelete);
}
dialogCtrl.isApplyDisabled = function () {
return !(!this.loading
&& (!this.form.permanentlyDelete || (this.form.permanentlyDelete && this.form.confirmDelete))
&& (this.form.filterDuration != 'custom' || (this.form.filterDuration == 'custom' && this.form.filterDurationDate))
);
}
dialogCtrl.apply = function () {
console.log(this.mailbox);
this.folder.cleanMailbox();
// $mdDialog.hide();
var folders = [];
var i;
if (account) {
for (i = 0; i < account.$mailboxes.length ; i++) {
folders.push(account.$mailboxes[i].id);
}
this.folder = account.$mailboxes[0];
}
var date = '';
var durationMonth = 12;
var date = new Date();
switch (this.form.filterDuration) {
case '3m':
durationMonth = 3;
date.setMonth(date.getMonth() - durationMonth);
break;
case '6m':
durationMonth = 6;
date.setMonth(date.getMonth() - durationMonth);
break;
case '9m':
durationMonth = 9;
date.setMonth(date.getMonth() - durationMonth);
break;
case '1y':
durationMonth = 12;
date.setMonth(date.getMonth() - durationMonth);
break;
case 'custom':
date = this.form.filterDurationDate;
break;
}
var year = date.getFullYear();
var month = String(date.getMonth() + 1).padStart(2, '0');
var day = String(date.getDate()).padStart(2, '0');
this.date = `${year}-${month}-${day}`;
this.folder.cleanMailbox({
'applyToSubfolders': (this.form && this.form.applyToSubfolders) ? this.form.applyToSubfolders : false,
'permanentlyDelete': (this.form && this.form.permanentlyDelete) ? this.form.permanentlyDelete : false,
'date': this.date,
'folders': folders
}).then(function (data) {
dialogCtrl.loading = true;
Mailbox.selectedFolder.$filter({
"sort": "date",
"asc": false,
"match": "OR"
}).then(function () {
$state.go('mail.account.mailbox', { accountId: vm.accounts[0].id, mailboxId: encodeUriFilter(Mailbox.selectedFolder.path) });
dialogCtrl.loading = false;
$mdDialog.hide();
$mdToast.show(
$mdToast.simple()
.textContent(l('%{0} message(s) deleted', data.nbMessageDeleted))
.position(sgConstant.toastPosition)
.hideDelay(2000));
});
}).catch(function () {
dialogCtrl.loading = false;
$mdDialog.hide();
});
};
},
controllerAs: 'dialogCtrl',
@@ -271,12 +271,12 @@
});
};
this.removeOldEmails = function () {
this.cleanMailbox = function () {
// Close sidenav on small devices
if (!$mdMedia(sgConstant['gt-md']))
$mdSidenav('left').close();
$rootScope.$broadcast('showRemoveOldEmailsPanel', {folder: this.folder}); // Show remove old emails panel (broadcast event to MailboxesController)
$rootScope.$broadcast('showCleanMailboxPanel', {folder: this.folder, account: null}); // Show remove old emails panel (broadcast event to MailboxesController)
};
this.emptyJunkFolder = function() {