From 11b11ff289219269165ce6e25cb780399535e8ee Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 1 Apr 2020 14:14:08 +0200 Subject: [PATCH] Start to migrate conversation system --- assets/js/common/ws.js | 3 + .../js/components/conversations/interface.js | 67 +++++- assets/js/components/conversations/service.js | 212 +++--------------- 3 files changed, 101 insertions(+), 181 deletions(-) diff --git a/assets/js/common/ws.js b/assets/js/common/ws.js index 8e95dcf0..75553a09 100644 --- a/assets/js/common/ws.js +++ b/assets/js/common/ws.js @@ -94,6 +94,9 @@ class UserWebSocket { static async Closed(e) { console.error("WS closed", e) + // Notify the application + SendEvent("wsClosed"); + // Reset requests queue requests = {}; diff --git a/assets/js/components/conversations/interface.js b/assets/js/components/conversations/interface.js index ab2604c3..1abc5d64 100644 --- a/assets/js/components/conversations/interface.js +++ b/assets/js/components/conversations/interface.js @@ -11,6 +11,11 @@ ComunicWeb.components.conversations.interface = { */ __conversationsList: {}, + /** + * @var {any} __registeredList The list of conversatins + */ + __registeredList: {}, + /** * Get and return the list of available conversations * @@ -363,6 +368,62 @@ ComunicWeb.components.conversations.interface = { }, + /** + * Asynchronoulsy refresh a single conversation + * + * @param convID The ID of the target conversation + * @param lastMessageID The ID of the last known message + */ + asyncRefreshSingle: function(convID, lastMessageID) { + return new Promise((res, err) => { + this.refreshSingleConversation(convID, lastMessageID, (list) => { + if(list.error) + err(list.error); + else + res(list); + }) + }); + }, + + /** + * Register a conversation remotly + * + * @param {Number} convID + */ + register: async function(convID) { + if(this.__registeredList.hasOwnProperty(convID)) + this.__registeredList[convID]++; + + else { + this.__registeredList[convID] = 1; + + await ws("$main/register_conv", { + convID: convID + }); + } + }, + + /** + * Unregister to new messages of a conversation + * + * @param {Number} convID + */ + unregister: async function(convID) { + + if(!this.__registeredList.hasOwnProperty(convID)) + return; + + this.__registeredList[convID]--; + + if(this.__registeredList[convID] == 0) { + delete this.__registeredList[convID]; + + await ws("$main/unregister_conv", { + convID: convID + }); + } + }, + /** * Intend to update the content of a single message * @@ -426,4 +487,8 @@ ComunicWeb.components.conversations.interface = { } //Register conversations cache cleaning function -ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.conversations.interface.emptyCache"); \ No newline at end of file +ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.conversations.interface.emptyCache"); + +document.addEventListener("wsClosed", () => { + ComunicWeb.components.conversations.interface.__registeredList = {}; +}) \ No newline at end of file diff --git a/assets/js/components/conversations/service.js b/assets/js/components/conversations/service.js index 752a2eb4..08d3f245 100644 --- a/assets/js/components/conversations/service.js +++ b/assets/js/components/conversations/service.js @@ -7,21 +7,11 @@ */ ComunicWeb.components.conversations.service = { - - /** - * @var {Integer} __intervalID The ID of the current service interval - */ - __intervalID: false, /** * @var {Object} __serviceCache The service cache */ __serviceCache: false, - - /** - * @var {Boolean} __serviceLock Specify whether the service is already in use or not - */ - __serviceLock: false, /** * Initializate conversation service @@ -29,155 +19,9 @@ ComunicWeb.components.conversations.service = { * @return {Boolean} True for a success */ init: function(){ - //Make sure the cache is empty this.emptyCache(); - - //Check if an interval already exists or not - if(this.__intervalID) - clearInterval(this.__intervalID); //Remove old interval - //Force the service to unlock - this.__serviceLock = false; - - //Initializate interval - this.__intervalID = setInterval(function(){ - ComunicWeb.components.conversations.service.call(); - }, 2000); - ComunicWeb.common.cacheManager.registerInterval(this.__intervalID); - - //Success - return true; - }, - - /** - * Call this service - * - * @return {Boolean} True for a success - */ - call: function(){ - - //Check if the conversation element still exists or not - if(!byId("conversationsElem")){ - ComunicWeb.debug.logMessage("Conversation Service : Couldn't locate conversations element, unregistering service !"); - clearInterval(this.__intervalID); - return false; - } - - //Check at least one conversation is opened - if(!this.__serviceCache){ - ComunicWeb.debug.logMessage("Conversation Service : task skipped : the service cache is empty (equals to false)."); - return false; - } - if(JSON.stringify(this.__serviceCache) == "{}"){ - ComunicWeb.debug.logMessage("Conversation Service : task skipped : the service cache is empty. (equals to {})"); - return false; - } - - //Check if the service is locked or not - if(this.__serviceLock){ - ComunicWeb.debug.logMessage("Conversation Service : task skipped : the service is locked."); - return false; - } - - //Lock service - this.__serviceLock = true; - - //Perform service task - this.performTask(); - }, - - /** - * Perform service task - * - * @return {Boolean} True for a success - */ - performTask: function(){ - - //Prepare interface request - var newConversations = []; - var conversationsToRefresh = {} - - //Process each conversation - var i; - for(i in this.__serviceCache){ - - //Extract conversation ID - var processConversation = this.__serviceCache[i].conversationID; - - //Check if it is new conversation - if(this.__serviceCache[i].last_message_id === 0) - newConversations.push(processConversation); - - //Else perform a simple update of the conversation - else { - conversationsToRefresh["conversation-"+processConversation] = { - last_message_id: this.__serviceCache[i].last_message_id, - }; - } - } - - //Perform a request on the interface - ComunicWeb.components.conversations.interface.refreshConversations( - newConversations, - conversationsToRefresh, - function(result){ - //Call callback function - ComunicWeb.components.conversations.service.callback(result); - } - ); - - //Success - return true; - }, - - /** - * Service callback function - * - * @param {Object} result The result of the request - * @return {Boolean} True for a success - */ - callback: function(result){ - - //Check for errors - if(result.error){ - ComunicWeb.debug.logMessage("Conversations Service : Couldn't update conversations !"); - - //Display a notification - ComunicWeb.common.notificationSystem.showNotification("An error occured while trying to refresh conversations system !", "danger", 1.5); - } - else { - //We can continue with the result - //Process each conversation - var i; - for(i in result){ - - //Check if new entries are available - if(result[i].length === 0 || !this.__serviceCache[i]) - continue; //Nothing to be done - - //Extract conversation ID - var conversationID = this.__serviceCache[i].conversationID; - - //Extract conversation ID - var messages = result[i]; - - //We update first and last message ID with the last message ID - if(messages[0].ID < this.__serviceCache[i].first_message_id || this.__serviceCache[i].first_message_id == 0) - this.__serviceCache[i].first_message_id = messages[0].ID; - this.__serviceCache[i].last_message_id = messages[messages.length-1].ID; - - //We process each message by calling chat windows script to ask it to add messages - var j; - for(j in messages) - ComunicWeb.components.conversations.chatWindows.addMessage(conversationID, messages[j]); - - } - } - - //Unlock service - this.__serviceLock = false; - //Success return true; }, @@ -188,21 +32,33 @@ ComunicWeb.components.conversations.service = { * @param {Integer} conversationID The ID of the conversation to register * @return {Boolean} True for a success */ - registerConversation: function(conversationID){ + registerConversation: async function(conversationID){ - //Create conversation object - if(!this.__serviceCache) - this.__serviceCache = {}; //Create service cache object - - //Register conversation - this.__serviceCache['conversation-' + conversationID] = { - conversationID: conversationID, - first_message_id: 0, - last_message_id: 0, - }; + try { - //Success - return true; + //Create conversation object + if(!this.__serviceCache) + this.__serviceCache = {}; //Create service cache object + + // Get the last messages of the conversations + const list = await ComunicWeb.components.conversations.interface.asyncRefreshSingle(conversationID, 0); + + //Register conversation locally + this.__serviceCache['conversation-' + conversationID] = { + conversationID: conversationID, + first_message_id: list.length == 0 ? 0 : list[0].ID, + }; + + // Register conversation remotly + await ComunicWeb.components.conversations.interface.register(conversationID) + + for(const msg of list) + ComunicWeb.components.conversations.chatWindows.addMessage(conversationID, msg); + + } catch(e) { + console.error(e); + notify("Could not open conversation!", "danger"); + } }, /** @@ -211,19 +67,17 @@ ComunicWeb.components.conversations.service = { * @param {Integer} conversationID The ID of the conversation to remove * @return {Boolean} True for a success */ - unregisterConversation: function(conversationID){ + unregisterConversation: async function(conversationID){ //Log action ComunicWeb.debug.logMessage("Unregistering conversation " + conversationID + " from service."); - if(this.__serviceCache){ - if(this.__serviceCache['conversation-'+conversationID]){ - delete this.__serviceCache['conversation-'+conversationID]; //Remove conversation - } + if(this.__serviceCache && this.__serviceCache['conversation-'+conversationID]){ + delete this.__serviceCache['conversation-'+conversationID]; //Remove conversation } - //Success - return true; + // Unregister remotly + await ComunicWeb.components.conversations.interface.unregister(conversationID) }, /** @@ -271,13 +125,11 @@ ComunicWeb.components.conversations.service = { clearObject(this.__serviceCache); } - //Unlock service - this.__serviceLock = false; - //Success return true; }, } //Register service cache -ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.conversations.service.emptyCache"); \ No newline at end of file +ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.conversations.service.emptyCache"); +