2019-01-09 15:18:10 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2020-06-03 23:17:06 +02:00
|
|
|
/* global async */
|
|
|
|
|
/* global angular */
|
2019-01-09 15:18:10 +01:00
|
|
|
|
2021-07-07 19:07:43 +02:00
|
|
|
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('/'); });
|
|
|
|
|
|
2019-01-09 17:36:33 +01:00
|
|
|
$scope.clearAllBusy = false;
|
|
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.notifications = [];
|
|
|
|
|
$scope.activeNotification = null;
|
|
|
|
|
$scope.busy = true;
|
2021-05-29 16:18:03 -07:00
|
|
|
$scope.currentPage = 1;
|
|
|
|
|
$scope.perPage = 20;
|
2021-05-29 16:06:41 -07:00
|
|
|
|
|
|
|
|
$scope.refresh = function () {
|
2021-05-29 16:18:03 -07:00
|
|
|
Client.getNotifications({}, $scope.currentPage, $scope.perPage, function (error, result) {
|
2021-05-29 16:06:41 -07:00
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
// collapse by default
|
|
|
|
|
result.forEach(function (r) { r.isCollapsed = true; });
|
|
|
|
|
|
2021-06-23 21:59:30 -07:00
|
|
|
// attempt to translate or parse the message as json
|
2021-05-29 16:06:41 -07:00
|
|
|
result.forEach(function (r) {
|
2021-06-23 23:17:42 -07:00
|
|
|
try {
|
|
|
|
|
r.messageJson = JSON.parse(r.message);
|
|
|
|
|
} catch (e) {}
|
2019-01-09 15:18:10 +01:00
|
|
|
});
|
|
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.notifications = result;
|
2019-01-09 15:18:10 +01:00
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.busy = false;
|
|
|
|
|
});
|
|
|
|
|
};
|
2020-10-08 17:50:28 -07:00
|
|
|
|
2021-05-29 16:18:03 -07:00
|
|
|
$scope.showNextPage = function () {
|
|
|
|
|
$scope.currentPage++;
|
|
|
|
|
$scope.refresh();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.showPrevPage = function () {
|
|
|
|
|
if ($scope.currentPage > 1) $scope.currentPage--;
|
|
|
|
|
else $scope.currentPage = 1;
|
|
|
|
|
|
|
|
|
|
$scope.refresh();
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-30 17:24:41 +02:00
|
|
|
$scope.ack = function (notification) {
|
|
|
|
|
if (notification.acknowledged) return;
|
2020-10-08 17:50:28 -07:00
|
|
|
|
2021-06-30 17:24:41 +02:00
|
|
|
Client.ackNotification(notification.id, true, function (error) {
|
2021-05-29 16:06:41 -07:00
|
|
|
if (error) console.error(error);
|
2019-01-09 15:18:10 +01:00
|
|
|
|
2021-06-30 17:24:41 +02:00
|
|
|
notification.acknowledged = true;
|
|
|
|
|
$scope.$parent.notificationAcknowledged();
|
2021-05-29 16:06:41 -07:00
|
|
|
});
|
|
|
|
|
};
|
2021-05-29 12:11:17 -07:00
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.clearAll = function () {
|
|
|
|
|
$scope.clearAllBusy = true;
|
2021-05-29 12:11:17 -07:00
|
|
|
|
2022-05-23 17:36:44 +02:00
|
|
|
Client.getNotifications({ acknowledged: false }, 1, 1000, function (error, results) { // hopefully 1k unread is sufficient
|
2021-05-29 16:06:41 -07:00
|
|
|
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();
|
2022-05-23 17:30:52 +02:00
|
|
|
|
2022-05-23 17:36:44 +02:00
|
|
|
$scope.clearAllBusy = false;
|
|
|
|
|
});
|
2021-05-29 16:06:41 -07:00
|
|
|
});
|
2019-01-09 15:18:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.refresh();
|
2019-01-09 15:18:10 +01:00
|
|
|
});
|
|
|
|
|
}]);
|