Update conversation page to use websockets

This commit is contained in:
Pierre HUBERT 2020-04-01 15:10:03 +02:00
parent 6eaea7cc1e
commit 27d7c526b8
2 changed files with 53 additions and 51 deletions

View File

@ -36,6 +36,8 @@ ComunicWeb.common.url = {
//Apply it //Apply it
window.history.pushState("object or string", newTitle, newURL); window.history.pushState("object or string", newTitle, newURL);
SendEvent("changeURI");
//Everything is OK //Everything is OK
return true; return true;
}, },

View File

@ -138,70 +138,62 @@ ComunicWeb.pages.conversations.conversation = {
* *
* @param {Object} info Information about the conversation * @param {Object} info Information about the conversation
*/ */
onGotInfo: function(info){ onGotInfo: async function(info){
//Get and apply the name of the conversation try {
ComunicWeb.components.conversations.utils.getName(info, function(name){ //Get and apply the name of the conversation
ComunicWeb.pages.conversations.conversation._conv_info.window.title.innerHTML = name; ComunicWeb.components.conversations.utils.getName(info, function(name){
}); ComunicWeb.pages.conversations.conversation._conv_info.window.title.innerHTML = name;
});
//Add send message form //Add send message form
this.addSendMessageForm(); this.addSendMessageForm();
//Defines an intervall to refresh the conversation //Defines an intervall to refresh the conversation
var windowBody = this._conv_info.window.body; const windowBody = this._conv_info.window.body;
var locker = this._conv_info.locker;
var interval = setInterval(function(){
//Check if the conversation body element is still connected or not on the screen // Register the conversation
if(!windowBody.isConnected){ await ComunicWeb.components.conversations.interface.register(this._conv_info.id);
clearInterval(interval);
return;
}
//Check if the system is locked // Get the last message
if(locker.locked){ const list = await ComunicWeb.components.conversations.interface.asyncRefreshSingle(this._conv_info.id, 0);
ComunicWeb.debug.logMessage("Skip conversation refresh : locked");
return;
}
//Lock the system // Apply the list of messages
locker.locked = true; this.applyMessages(list)
//Refresh the conversation // Automatically unregister conversations when it becoms required
ComunicWeb.pages.conversations.conversation.refresh(); let reg = true;
const convID = this._conv_info.id;
document.addEventListener("changeURI", async () => {
if(reg) {
reg = false;
await ComunicWeb.components.conversations.interface.unregister(convID);
}
})
}, 1500); } catch(e) {
console.error(e)
notify("Could not refresh conversation!", "danger")
}
}, },
/** /**
* Refresh the current conversation * Apply a new list of messages
*/ */
refresh: function(){ applyMessages: function(list){
//Peform the request over the API //Check if there are responses to process
ComunicWeb.components.conversations.interface.refreshSingleConversation(this._conv_info.id, this._conv_info.last_message_id, function(response){ if(list.length == 0)
return; //Do not process messages list (avoid unwanted scrolling)
//Unlock service //Process the list of messages
ComunicWeb.pages.conversations.conversation._conv_info.locker.locked = false; list.forEach(function(message){
ComunicWeb.pages.conversations.conversation.addMessage(message);
//Check for errors
if(response.error)
return notify("Could not get the latest messages of the conversation!", "danger");
//Check if there are responses to process
if(response.length == 0)
return; //Do not process messages list (avoid unwanted scrolling)
//Process the list of messages
response.forEach(function(message){
ComunicWeb.pages.conversations.conversation.addMessage(message);
});
//Init top scroll detection (if available)
ComunicWeb.pages.conversations.conversation.initTopScrollDetection();
}); });
//Init top scroll detection (if available)
ComunicWeb.pages.conversations.conversation.initTopScrollDetection();
}, },
/** /**
@ -558,7 +550,7 @@ ComunicWeb.pages.conversations.conversation = {
//Check for errors //Check for errors
if(response.error) if(response.error)
return notify("An error occured while trying to retrive older messages !", "danger"); return notify("An error occured while trying to retrieve older messages !", "danger");
//Check if there is not any message to display //Check if there is not any message to display
if(response.length == 0){ if(response.length == 0){
@ -593,4 +585,12 @@ ComunicWeb.pages.conversations.conversation = {
}); });
}, },
}; };
// Register to new messages
document.addEventListener("newConvMessage", (e) => {
const msg = e.detail;
if(ComunicWeb.pages.conversations.conversation._conv_info.id == msg.convID)
ComunicWeb.pages.conversations.conversation.applyMessages([msg]);
})