From 5ab4086cbe62fc9ea9f4fca63e2642862342ac6c Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Fri, 17 Apr 2020 13:32:22 +0200 Subject: [PATCH] Notifications: Regulate fetching via cookie We introduce a doNotQueryNotifications cookie with a short lifetime, which is used to determine wether getNotifications should be called or not. This prevents notifications from being fetched at every page load, unless the cookie is expired. --- src/scripts/tutti/5_1_notifications.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/scripts/tutti/5_1_notifications.js b/src/scripts/tutti/5_1_notifications.js index 68a3e35f..facb7ec7 100644 --- a/src/scripts/tutti/5_1_notifications.js +++ b/src/scripts/tutti/5_1_notifications.js @@ -1,5 +1,3 @@ - - /** * Store the number of unread notifications on load. * That way, if the number got higher while the page was @@ -19,8 +17,24 @@ function clearNotificationIcon(){ } -// Get notifications by fetching /notifications/ JSON every 30 seconds +/** + * Get notifications by fetching /notifications/ and update ul#notifications-list + * + * This is called every 60 seconds by getNotificationsLoop() but the endpoint is queried only if the + * doNotQueryNotifications cookie is expired. If so, the cookie is created again, with a lifetime + * of 65 seconds. + */ + function getNotifications(){ + //- Check if the cookie is still valid, in which case, return + if( Cookies('doNotQueryNotifications') != null ) {return;} + + //- Create a new cookie, which expires in 65 seconds + Cookies.set('doNotQueryNotifications', 'true', { + expires: new Date(new Date().getTime() + 65 * 1000) + }); + + //- Fetch data and update the interface $.getJSON( "/notifications/", function( data ) { if (!first_load) {