Files
cloudron-box/src/views/emails.js

177 lines
5.8 KiB
JavaScript
Raw Normal View History

2020-02-11 21:06:34 +01:00
'use strict';
/* global angular:false */
/* global $:false */
angular.module('Application').controller('EmailsController', ['$scope', '$location', 'Client', function ($scope, $location, Client) {
2020-02-24 12:56:13 +01:00
Client.onReady(function () { if (!Client.getUserInfo().isAtLeastAdmin) $location.path('/'); });
2020-02-11 21:06:34 +01:00
$scope.ready = false;
$scope.config = Client.getConfig();
$scope.user = Client.getUserInfo();
2020-02-11 21:06:34 +01:00
$scope.domains = [];
2020-03-25 20:13:45 -07:00
$scope.pageItemCount = [
{ name: 'Show 20 per page', value: 20 },
{ name: 'Show 50 per page', value: 50 },
{ name: 'Show 100 per page', value: 100 }
];
$scope.activityTypes = [
{ name: 'Bounce', value: 'bounce' },
{ name: 'Deferred', value: 'deferred' },
{ name: 'Delivered', value: 'delivered' },
{ name: 'Denied', value: 'denied' },
{ name: 'Queued', value: 'queued' },
{ name: 'Received', value: 'received' },
];
2020-02-11 22:07:58 -08:00
$scope.activity = {
2020-02-12 15:37:05 +01:00
busy: true,
eventLogs: [],
activeEventLog: null,
2020-02-11 22:07:58 -08:00
currentPage: 1,
2020-03-07 02:21:21 -08:00
perPage: 20,
2020-03-25 20:13:45 -07:00
pageItems: $scope.pageItemCount[0],
selectedTypes: [],
search: '',
2020-02-11 22:07:58 -08:00
2020-02-13 12:01:19 +01:00
refresh: function () {
2020-02-12 15:37:05 +01:00
$scope.activity.busy = true;
2020-03-25 20:13:45 -07:00
var types = $scope.activity.selectedTypes.map(function (a) { return a.value; }).join(',');
Client.getMailEventLogs($scope.activity.search, types, $scope.activity.currentPage, $scope.activity.pageItems.value, function (error, result) {
2020-02-12 15:37:05 +01:00
if (error) return console.error('Failed to fetch mail eventlogs.', error);
$scope.activity.busy = false;
2020-02-12 23:17:24 -08:00
$scope.activity.eventLogs = result;
2020-02-11 22:07:58 -08:00
});
},
showNextPage: function () {
$scope.activity.currentPage++;
2020-02-13 12:01:19 +01:00
$scope.activity.refresh();
2020-02-11 22:07:58 -08:00
},
showPrevPage: function () {
if ($scope.activity.currentPage > 1) $scope.activity.currentPage--;
else $scope.activity.currentPage = 1;
2020-02-13 12:01:19 +01:00
$scope.activity.refresh();
2020-02-12 15:37:05 +01:00
},
showEventLogDetails: function (eventLog) {
if ($scope.activity.activeEventLog === eventLog) $scope.activity.activeEventLog = null;
else $scope.activity.activeEventLog = eventLog;
2020-02-18 19:48:17 -08:00
},
2020-03-25 20:13:45 -07:00
updateFilter: function (fresh) {
if (fresh) $scope.activity.currentPage = 1;
2020-02-18 19:48:17 -08:00
$scope.activity.refresh();
2020-02-11 22:07:58 -08:00
}
};
$scope.testEmail = {
busy: false,
error: {},
mailTo: '',
domain: null,
clearForm: function () {
$scope.testEmail.mailTo = '';
},
show: function (domain) {
$scope.testEmail.error = {};
$scope.testEmail.busy = false;
$scope.testEmail.domain = domain;
$scope.testEmail.mailTo = $scope.user.email;
$('#testEmailModal').modal('show');
},
submit: function () {
$scope.testEmail.error = {};
$scope.testEmail.busy = true;
Client.sendTestMail($scope.testEmail.domain.domain, $scope.testEmail.mailTo, function (error) {
$scope.testEmail.busy = false;
if (error) {
$scope.testEmail.error.generic = error.message;
console.error(error);
$('#inputTestMailTo').focus();
return;
}
$('#testEmailModal').modal('hide');
});
}
};
function refreshDomainStatuses() {
$scope.domains.forEach(function (domain) {
Client.getMailStatusForDomain(domain.domain, function (error, result) {
if (error) return console.error('Failed to fetch mail status for domain', domain.domain, error);
domain.status = result;
2020-02-27 10:37:21 -08:00
domain.statusOk = Object.keys(result).every(function (k) {
if (k === 'dns') return Object.keys(result.dns).every(function (k) { return result.dns[k].status; });
if (!('status' in result[k])) return true; // if status is not present, the test was not run
return result[k].status;
});
});
2020-02-20 12:35:51 -08:00
Client.getMailConfigForDomain(domain.domain, function (error, mailConfig) {
if (error) return console.error('Failed to fetch mail config for domain', domain.domain, error);
domain.inbound = mailConfig.enabled;
domain.outbound = mailConfig.relay.provider !== 'noop';
// do this even if no outbound since people forget to remove mailboxes
Client.getMailboxes(domain.domain, function (error, mailboxes) {
if (error) return console.error('Failed to fetch mailboxes for domain', domain.domain, error);
domain.mailboxCount = mailboxes.length;
Client.getMailUsage(domain.domain, function (error, usage) {
if (error) return console.error('Failed to fetch usage for domain', domain.domain, error);
domain.usage = 0;
Object.keys(usage).forEach(function (m) { domain.usage += usage[m].size; });
});
});
});
});
}
2020-02-11 21:06:34 +01:00
Client.onReady(function () {
Client.getDomains(function (error, domains) {
if (error) return console.error('Unable to get domain listing.', error);
$scope.domains = domains;
$scope.ready = true;
2020-02-11 22:07:58 -08:00
2020-02-13 12:01:19 +01:00
$scope.activity.refresh();
refreshDomainStatuses();
2020-02-11 21:06:34 +01:00
});
});
// setup all the dialog focus handling
['testEmailModal'].forEach(function (id) {
$('#' + id).on('shown.bs.modal', function () {
$(this).find('[autofocus]:first').focus();
});
});
2020-02-11 21:06:34 +01:00
$('.modal-backdrop').remove();
}]);