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

160 lines
5.5 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-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-02-14 09:04:54 -08:00
perPage: 20,
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;
Client.getMailEventLogs($scope.activity.search, $scope.activity.currentPage, $scope.activity.perPage, 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
},
updateFilter: function () {
$scope.activity.currentPage = 1;
$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;
// those states always exist
domain.statusOk = result.relay.status && result.dns.mx.status;
// those states only exist if no relay is used
if (typeof result.dns.spf !== 'undefined') domain.statusOk = domain.statusOk && result.dns.spf.status;
if (typeof result.dns.dkim !== 'undefined') domain.statusOk = domain.statusOk && result.dns.dkim.status;
if (typeof result.dns.dmarc !== 'undefined') domain.statusOk = domain.statusOk && result.dns.dmarc.status;
if (typeof result.dns.ptr !== 'undefined') domain.statusOk = domain.statusOk && result.dns.ptr.status;
if (typeof result.rbl.status === 'boolean') domain.statusOk = domain.statusOk && result.rbl.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();
}]);