frontend: browser notifications of challenge events

This commit is contained in:
nemunaire 2018-12-01 18:15:03 +01:00
parent 8749a7c164
commit 12eddadc07
2 changed files with 61 additions and 0 deletions

View File

@ -46,6 +46,14 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.current_tag = undefined;
$rootScope.notify_field = 0;
Notification.requestPermission().then(function(result) {
if (result == "granted")
$rootScope.notify_field = 7;
else
$rootScope.notify_field = 0;
});
})
.controller("DataController", function($sce, $scope, $http, $rootScope, $interval, $location) {
var actMenu = function() {
@ -146,6 +154,52 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
});
}
var refreshEventsInterval;
var eventsLastRefresh;
var eventsLastTreated;
var refreshEvents = function() {
if (refreshEventsInterval)
$interval.cancel(refreshEventsInterval);
var refreshRate = 1200;
if ($rootScope.notify_field == 0 && eventsLastTreated)
refreshRate = 30000;
refreshEventsInterval = $interval(refreshEvents, Math.floor(Math.random() * refreshRate * 2) + refreshRate);
if (!eventsLastTreated) {
eventsLastTreated = $rootScope.getSrvTime();
if (!eventsLastTreated)
return;
}
$http.get("/events.json").then(function(response) {
if (eventsLastRefresh != undefined && eventsLastRefresh == response.headers()["last-modified"])
return;
eventsLastRefresh = response.headers()["last-modified"];
var maxTimeSeen = eventsLastTreated;
for (var i = response.data.length - 1; i >= 0; i--) {
var event = response.data[i];
event.time = new Date(event.time);
if (event.time <= eventsLastTreated)
continue;
else if (event.time > maxTimeSeen)
maxTimeSeen = event.time;
if ($rootScope.notify_field != 0) {
var notification = new Notification("Challenge forensic", {body: event.txt.replace(/(<([^>]+)>)/ig,""), icon: "/favicon.ico"});
notification.onclick = function(ev) {
$location.url("/edit");
};
setTimeout(notification.close.bind(notification), 4000);
}
};
if (maxTimeSeen > eventsLastTreated)
eventsLastTreated = maxTimeSeen;
});
}
var refreshMyInterval;
var refreshMy = function() {
if (refreshMyInterval)
@ -219,6 +273,7 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
refreshSettings();
refreshThemes();
$rootScope.refreshTeams();
refreshEvents();
}
else if (justSettings) {
refreshSettings();

View File

@ -138,6 +138,12 @@ angular.module("FICApp")
$scope.time = {};
$rootScope.getSrvTime = function() {
if (time.cu && time.he)
return new Date(Date.now() + (time.cu - time.he));
else
return undefined;
}
$rootScope.recvTime = function(response) {
time = {
"cu": Math.floor(response.headers("x-fic-time") * 1000),