Refactor notifications service

This commit is contained in:
Pierre HUBERT 2020-03-31 11:14:44 +02:00
parent 1f4c4f595c
commit ba3dd74756
2 changed files with 53 additions and 53 deletions

View File

@ -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 * Get the list of unread notifications
* *

View File

@ -9,7 +9,9 @@ ComunicWeb.components.notifications.service = {
/** /**
* Last known number of notifications * Last known number of notifications
*/ */
last_notifs_number: -1, last_total_count: -1,
count_unread_notifications: -1,
count_unread_conv: -1,
/** /**
* Init the service * Init the service
@ -21,71 +23,57 @@ ComunicWeb.components.notifications.service = {
* @param {HTMLElement} target_conversations Optionnal, defins the target * @param {HTMLElement} target_conversations Optionnal, defins the target
* for the number of conversations * for the number of conversations
*/ */
init: function(target, auto_hide, target_conversations){ init: async function(target, auto_hide, target_conversations){
//Initialize interval processResponse = () => {
var interval = setInterval(function(){
//Auto-remove interval if the target has been removed if(!target.isConnected || this.count_unread_notifications < 0 || this.count_unread_conv < 0)
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; return;
//Update the target //Update the target
target.innerHTML = response.notifications; target.innerHTML = this.count_unread_notifications;
//If the number of notifications equals 0, hide the target if required //If the number of notifications equals 0, hide the target if required
if(response.notifications == 0 && auto_hide) target.style.display = this.count_unread_conv == 0 && auto_hide ? "none" : "block";
target.style.display = "none";
else
target.style.display = "block";
//Update the number of conversations if possible too //Update the number of conversations if possible too
if(target_conversations){ if(target_conversations){
//Update the target //Update the target
target_conversations.innerHTML = response.conversations; target_conversations.innerHTML = this.count_unread_conv;
//If the number of notifications equals 0, hide the target if required //If the number of notifications equals 0, hide the target if required
if(response.conversations == 0 && auto_hide) target_conversations.style.display = this.count_unread_conv == 0 && auto_hide ? "none" : "block";
target_conversations.style.display = "none";
else
target_conversations.style.display = "block";
} }
//Sum notification number //Sum notification number
var total_number_notifs = response.notifications + response.conversations; let total_number_notifs = this.count_unread_conv + this.count_unread_notifications;
//Update page title too //Update page title too
ComunicWeb.common.pageTitle.setNotificationsNumber(total_number_notifs); ComunicWeb.common.pageTitle.setNotificationsNumber(total_number_notifs);
//Play song if required //Play song if required
if(ComunicWeb.components.notifications.service.last_notifs_number != -1 if(this.last_total_count != -1 && total_number_notifs > this.last_total_count)
&& total_number_notifs > ComunicWeb.components.notifications.service.last_notifs_number)
ComunicWeb.components.notifications.song.play(); ComunicWeb.components.notifications.song.play();
ComunicWeb.components.notifications.service.last_notifs_number = total_number_notifs; this.last_total_count = total_number_notifs;
}
//Process the number of calls if possible // Initial load
if(response.calls && response.calls > 0) try {
ComunicWeb.components.calls.controller.newCallsAvailable(response.calls); const response = await ComunicWeb.components.notifications.interface.asyncGetAllUnread(false);
}); this.count_unread_conv = response.conversations;
this.count_unread_notifications = response.notifications;
}, 2000); this.last_total_count = response.notifications + response.conversations;
processResponse();
} catch(e) {
console.error("Could not get the number of unread notifications!")
console.error(e);
}
}, },
} }