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){ return;
ComunicWeb.common.pageTitle.setNotificationsNumber(0);
ComunicWeb.components.notifications.service.last_notifs_number = -1;
return clearInterval(interval);
}
//Update the target
target.innerHTML = this.count_unread_notifications;
//Get the number of notifications from the API //If the number of notifications equals 0, hide the target if required
ComunicWeb.components.notifications.interface.getAllUnread( target.style.display = this.count_unread_conv == 0 && auto_hide ? "none" : "block";
ComunicWeb.components.calls.controller.isAvailable(), function(response){
//Continue in case of success //Update the number of conversations if possible too
if(response.error) if(target_conversations){
return;
//Update the target //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 the number of notifications equals 0, hide the target if required
if(response.notifications == 0 && auto_hide) target_conversations.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 }
if(target_conversations){
//Update the target //Sum notification number
target_conversations.innerHTML = response.conversations; let total_number_notifs = this.count_unread_conv + this.count_unread_notifications;
//If the number of notifications equals 0, hide the target if required //Update page title too
if(response.conversations == 0 && auto_hide) ComunicWeb.common.pageTitle.setNotificationsNumber(total_number_notifs);
target_conversations.style.display = "none";
else
target_conversations.style.display = "block";
} //Play song if required
if(this.last_total_count != -1 && total_number_notifs > this.last_total_count)
ComunicWeb.components.notifications.song.play();
//Sum notification number this.last_total_count = total_number_notifs;
var total_number_notifs = response.notifications + response.conversations;
//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 // 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);
}
}, },
} }