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
*

View File

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