Files
cloudron-box/dashboard/public/views/notifications.js

114 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
2020-06-03 23:17:06 +02:00
/* global async */
2024-12-11 18:24:20 +01:00
/* global angular, $ */
angular.module('Application').controller('NotificationsController', ['$scope', '$location', '$timeout', '$translate', '$interval', 'Client', function ($scope, $location, $timeout, $translate, $interval, Client) {
Client.onReady(function () { if (!Client.getUserInfo().isAtLeastAdmin) $location.path('/'); });
$scope.clearAllBusy = false;
$scope.notifications = [];
$scope.activeNotification = null;
$scope.busy = true;
$scope.currentPage = 1;
$scope.perPage = 20;
$scope.refresh = function () {
Client.getNotifications({}, $scope.currentPage, $scope.perPage, function (error, result) {
if (error) return console.error(error);
// collapse by default
result.forEach(function (r) { r.isCollapsed = true; });
// attempt to translate or parse the message as json
result.forEach(function (r) {
2021-06-23 23:17:42 -07:00
try {
r.messageJson = JSON.parse(r.message);
} catch (e) {}
});
$scope.notifications = result;
$scope.busy = false;
});
};
$scope.showNextPage = function () {
$scope.currentPage++;
$scope.refresh();
};
$scope.showPrevPage = function () {
if ($scope.currentPage > 1) $scope.currentPage--;
else $scope.currentPage = 1;
$scope.refresh();
};
$scope.ack = function (notification) {
if (notification.acknowledged) return;
Client.ackNotification(notification.id, true, function (error) {
if (error) console.error(error);
notification.acknowledged = true;
$scope.$parent.notificationAcknowledged();
});
};
$scope.clearAll = function () {
$scope.clearAllBusy = true;
2022-05-23 17:36:44 +02:00
Client.getNotifications({ acknowledged: false }, 1, 1000, function (error, results) { // hopefully 1k unread is sufficient
if (error) console.error(error);
2022-05-23 17:36:44 +02:00
async.eachLimit(results, 20, function (notification, callback) {
Client.ackNotification(notification.id, true, function (error) {
if (error) console.error(error);
callback();
});
}, function (error) {
if (error) console.error(error);
// refresh the main navbar indicator
$scope.$parent.notificationAcknowledged();
$scope.refresh();
2022-05-23 17:36:44 +02:00
$scope.clearAllBusy = false;
});
});
};
2024-12-11 18:24:20 +01:00
$scope.settings = {
busy: false,
config: {},
show: function () {
for (const s of Client.getUserInfo().notificationConfig) {
$scope.settings.config[s] = true;
}
$('#notificationsSettingsModal').modal('show');
},
submit: function () {
const config = Object.keys($scope.settings.config).filter(c => $scope.settings.config[c] === true);
Client.setNotificationConfig(config, function (error) {
if (error) return Client.error(error);
2024-12-11 23:01:00 +01:00
Client.refreshProfile();
2024-12-11 18:24:20 +01:00
$('#notificationsSettingsModal').modal('hide');
});
},
};
Client.onReady(function () {
2021-06-23 23:17:42 -07:00
var refreshTimer = $interval($scope.refresh, 60 * 1000); // keep this interval in sync with the notification count indicator in main.js
$scope.$on('$destroy', function () {
$interval.cancel(refreshTimer);
});
$scope.refresh();
});
}]);