diff --git a/assets/js/components/conversations/chatWindows.js b/assets/js/components/conversations/chatWindows.js index 30489053..55d70ef7 100644 --- a/assets/js/components/conversations/chatWindows.js +++ b/assets/js/components/conversations/chatWindows.js @@ -887,7 +887,34 @@ ComunicWeb.components.conversations.chatWindows = { scrollDetectionLocked = true; //Fetch older messages - console.log("Fetch old messages"); + ComunicWeb.components.conversations.interface.getOlderMessages( + conversationID, + ComunicWeb.components.conversations.service.getOldestMessageID(conversationID), + 10, + function(result){ + + //Unlock scroll detection + scrollDetectionLocked = false; + + //Check for errors + if(result.error){ + notify("An error occured while trying to fetch older messages for the conversation !"); + return; + } + + //Check for results + if(result.length == 0){ + //Lock scroll detection in order to avoid useless traffic + scrollDetectionLocked = true; + return; + } + + //Save the ID of the oldest message + ComunicWeb.components.conversations.service.setOldestMessageID(result[0].ID); + + //Process the messages in reverse order + } + ); }); } } diff --git a/assets/js/components/conversations/interface.js b/assets/js/components/conversations/interface.js index 2a5457ff..be6917b2 100644 --- a/assets/js/components/conversations/interface.js +++ b/assets/js/components/conversations/interface.js @@ -322,6 +322,27 @@ ComunicWeb.components.conversations.interface = { }, + /** + * Get older message of a conversation + * + * @param {number} conversationID The ID of the conversation + * @param {number} oldestMessageID The ID of the oldest message known + * @param {number} limit The limit + * @param {function} callback + */ + getOlderMessages: function(conversationID, oldestMessageID, limit, callback){ + + //Perform a request on the API + var apiURI = "conversations/get_older_messages"; + var params = { + conversationID: conversationID, + oldest_message_id: oldestMessageID, + limit: limit + }; + + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); + }, + /** * Empty conversations cache *