diff --git a/assets/js/components/conversations/interface.js b/assets/js/components/conversations/interface.js index d0bde9d7..8140826d 100644 --- a/assets/js/components/conversations/interface.js +++ b/assets/js/components/conversations/interface.js @@ -203,6 +203,39 @@ ComunicWeb.components.conversations.interface = { return true; }, + /** + * Search for private conversation + * + * @param {Integer} otherUser The ID of the other user + * @param {Boolean} allowCreation Allow the server to create the conversation if not found + * @param {function} callback What to do once the request is completed + * @return {Boolean} True for a success + */ + searchPrivate: function(otherUser, allowCreation, callback){ + + //Perform an API request + var apiURI = "conversations/getPrivate"; + var params = { + otherUser: otherUser, + allowCreate: allowCreation + } + + //Perform API request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(result){ + + //Check for errors + if(result.error) + ComunicWeb.debug.logMessage("An error occured while trying to get a private conversation ID !"); + + //Perfrorm next action + callback(result); + + }); + + //Success + return true; + }, + /** * Empty conversations cache * @@ -218,7 +251,7 @@ ComunicWeb.components.conversations.interface = { //Success return true; - } + }, } //Register conversations cache cleaning function diff --git a/assets/js/components/conversations/manager.js b/assets/js/components/conversations/manager.js index 909e2cff..9f41fb45 100644 --- a/assets/js/components/conversations/manager.js +++ b/assets/js/components/conversations/manager.js @@ -129,4 +129,38 @@ ComunicWeb.components.conversations.manager = { //Success return true; }, + + /** + * Open a private conversation with only one user + * + * @param {Integer} otherID The ID of the user with who the conversation will be started + * @return {Boolean} True for a success + */ + openPrivate: function(otherID){ + + //Search for such conversation in the database, create it in case of failure + //Prepare what to do next + var callback = function(result){ + + //In case of error + if(result.error){ + //Notify user + ComunicWeb.common.notificationSystem.showNotification("Couldn't create a conversation with this user ! Please try again...", "danger", 2); + return false; + } + + //Open the first conversation + ComunicWeb.components.conversations.manager.addConversation({ + conversationID: result.conversationsID[0], + }); + + }; + + //Peform request + ComunicWeb.components.conversations.interface.searchPrivate(otherID, true, callback); + + //Success + return true; + + }, } \ No newline at end of file