diff --git a/assets/js/components/notifications/interface.js b/assets/js/components/notifications/interface.js index 39c61686..b6df843a 100644 --- a/assets/js/components/notifications/interface.js +++ b/assets/js/components/notifications/interface.js @@ -43,6 +43,18 @@ ComunicWeb.components.notifications.interface = { }, + // ASync version of previous request + asyncGetAllUnread: async function(getCalls) { + return await new Promise((res, err) => { + this.getAllUnread(getCalls, (data) => { + if(data.error) + err(data.error); + else + res(data) + }) + }); + }, + /** * Get the list of unread notifications * diff --git a/assets/js/components/notifications/service.js b/assets/js/components/notifications/service.js index ab8d6ffd..b0951651 100644 --- a/assets/js/components/notifications/service.js +++ b/assets/js/components/notifications/service.js @@ -9,7 +9,9 @@ ComunicWeb.components.notifications.service = { /** * Last known number of notifications */ - last_notifs_number: -1, + last_total_count: -1, + count_unread_notifications: -1, + count_unread_conv: -1, /** * Init the service @@ -21,71 +23,57 @@ ComunicWeb.components.notifications.service = { * @param {HTMLElement} target_conversations Optionnal, defins the target * for the number of conversations */ - init: function(target, auto_hide, target_conversations){ + init: async function(target, auto_hide, target_conversations){ - //Initialize interval - var interval = setInterval(function(){ + processResponse = () => { + + if(!target.isConnected || this.count_unread_notifications < 0 || this.count_unread_conv < 0) + return; + + //Update the target + target.innerHTML = this.count_unread_notifications; - //Auto-remove interval if the target has been removed - if(!target.isConnected){ - ComunicWeb.common.pageTitle.setNotificationsNumber(0); - ComunicWeb.components.notifications.service.last_notifs_number = -1; - return clearInterval(interval); - } - - - //Get the number of notifications from the API - ComunicWeb.components.notifications.interface.getAllUnread( - ComunicWeb.components.calls.controller.isAvailable(), function(response){ - - //Continue in case of success - if(response.error) - return; + //If the number of notifications equals 0, hide the target if required + target.style.display = this.count_unread_conv == 0 && auto_hide ? "none" : "block"; + + //Update the number of conversations if possible too + if(target_conversations){ //Update the target - target.innerHTML = response.notifications; + target_conversations.innerHTML = this.count_unread_conv; //If the number of notifications equals 0, hide the target if required - if(response.notifications == 0 && auto_hide) - target.style.display = "none"; - else - target.style.display = "block"; - - //Update the number of conversations if possible too - if(target_conversations){ + target_conversations.style.display = this.count_unread_conv == 0 && auto_hide ? "none" : "block"; - //Update the target - target_conversations.innerHTML = response.conversations; + } - //If the number of notifications equals 0, hide the target if required - if(response.conversations == 0 && auto_hide) - target_conversations.style.display = "none"; - else - target_conversations.style.display = "block"; + //Sum notification number + let total_number_notifs = this.count_unread_conv + this.count_unread_notifications; - } + //Update page title too + ComunicWeb.common.pageTitle.setNotificationsNumber(total_number_notifs); - //Sum notification number - var total_number_notifs = response.notifications + response.conversations; + //Play song if required + if(this.last_total_count != -1 && total_number_notifs > this.last_total_count) + ComunicWeb.components.notifications.song.play(); + + this.last_total_count = total_number_notifs; + + } - //Update page title too - ComunicWeb.common.pageTitle.setNotificationsNumber(total_number_notifs); - //Play song if required - if(ComunicWeb.components.notifications.service.last_notifs_number != -1 - && total_number_notifs > ComunicWeb.components.notifications.service.last_notifs_number) - ComunicWeb.components.notifications.song.play(); - - ComunicWeb.components.notifications.service.last_notifs_number = total_number_notifs; - - - //Process the number of calls if possible - if(response.calls && response.calls > 0) - ComunicWeb.components.calls.controller.newCallsAvailable(response.calls); - }); - - }, 2000); + // Initial load + try { + const response = await ComunicWeb.components.notifications.interface.asyncGetAllUnread(false); + this.count_unread_conv = response.conversations; + this.count_unread_notifications = response.notifications; + this.last_total_count = response.notifications + response.conversations; + processResponse(); + } catch(e) { + console.error("Could not get the number of unread notifications!") + console.error(e); + } }, } \ No newline at end of file