mirror of
https://github.com/pierre42100/ComunicWeb
synced 2024-11-25 21:39:21 +00:00
Update conversation page to use websockets
This commit is contained in:
parent
6eaea7cc1e
commit
27d7c526b8
@ -36,6 +36,8 @@ ComunicWeb.common.url = {
|
||||
//Apply it
|
||||
window.history.pushState("object or string", newTitle, newURL);
|
||||
|
||||
SendEvent("changeURI");
|
||||
|
||||
//Everything is OK
|
||||
return true;
|
||||
},
|
||||
|
@ -138,8 +138,9 @@ ComunicWeb.pages.conversations.conversation = {
|
||||
*
|
||||
* @param {Object} info Information about the conversation
|
||||
*/
|
||||
onGotInfo: function(info){
|
||||
onGotInfo: async function(info){
|
||||
|
||||
try {
|
||||
//Get and apply the name of the conversation
|
||||
ComunicWeb.components.conversations.utils.getName(info, function(name){
|
||||
ComunicWeb.pages.conversations.conversation._conv_info.window.title.innerHTML = name;
|
||||
@ -149,59 +150,50 @@ ComunicWeb.pages.conversations.conversation = {
|
||||
this.addSendMessageForm();
|
||||
|
||||
//Defines an intervall to refresh the conversation
|
||||
var windowBody = this._conv_info.window.body;
|
||||
var locker = this._conv_info.locker;
|
||||
var interval = setInterval(function(){
|
||||
const windowBody = this._conv_info.window.body;
|
||||
|
||||
//Check if the conversation body element is still connected or not on the screen
|
||||
if(!windowBody.isConnected){
|
||||
clearInterval(interval);
|
||||
return;
|
||||
// Register the conversation
|
||||
await ComunicWeb.components.conversations.interface.register(this._conv_info.id);
|
||||
|
||||
// Get the last message
|
||||
const list = await ComunicWeb.components.conversations.interface.asyncRefreshSingle(this._conv_info.id, 0);
|
||||
|
||||
// Apply the list of messages
|
||||
this.applyMessages(list)
|
||||
|
||||
// Automatically unregister conversations when it becoms required
|
||||
let reg = true;
|
||||
const convID = this._conv_info.id;
|
||||
document.addEventListener("changeURI", async () => {
|
||||
if(reg) {
|
||||
reg = false;
|
||||
await ComunicWeb.components.conversations.interface.unregister(convID);
|
||||
}
|
||||
})
|
||||
|
||||
//Check if the system is locked
|
||||
if(locker.locked){
|
||||
ComunicWeb.debug.logMessage("Skip conversation refresh : locked");
|
||||
return;
|
||||
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
notify("Could not refresh conversation!", "danger")
|
||||
}
|
||||
|
||||
//Lock the system
|
||||
locker.locked = true;
|
||||
|
||||
//Refresh the conversation
|
||||
ComunicWeb.pages.conversations.conversation.refresh();
|
||||
|
||||
}, 1500);
|
||||
},
|
||||
|
||||
/**
|
||||
* Refresh the current conversation
|
||||
* Apply a new list of messages
|
||||
*/
|
||||
refresh: function(){
|
||||
|
||||
//Peform the request over the API
|
||||
ComunicWeb.components.conversations.interface.refreshSingleConversation(this._conv_info.id, this._conv_info.last_message_id, function(response){
|
||||
|
||||
//Unlock service
|
||||
ComunicWeb.pages.conversations.conversation._conv_info.locker.locked = false;
|
||||
|
||||
//Check for errors
|
||||
if(response.error)
|
||||
return notify("Could not get the latest messages of the conversation!", "danger");
|
||||
applyMessages: function(list){
|
||||
|
||||
//Check if there are responses to process
|
||||
if(response.length == 0)
|
||||
if(list.length == 0)
|
||||
return; //Do not process messages list (avoid unwanted scrolling)
|
||||
|
||||
//Process the list of messages
|
||||
response.forEach(function(message){
|
||||
list.forEach(function(message){
|
||||
ComunicWeb.pages.conversations.conversation.addMessage(message);
|
||||
});
|
||||
|
||||
//Init top scroll detection (if available)
|
||||
ComunicWeb.pages.conversations.conversation.initTopScrollDetection();
|
||||
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
@ -558,7 +550,7 @@ ComunicWeb.pages.conversations.conversation = {
|
||||
|
||||
//Check for errors
|
||||
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
|
||||
if(response.length == 0){
|
||||
@ -594,3 +586,11 @@ 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]);
|
||||
})
|
Loading…
Reference in New Issue
Block a user