From 9eab6c7e2e75d4679266e64b9e8fd13400217c4e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 25 Jan 2019 09:43:19 +0100 Subject: [PATCH] Can respond to calls --- assets/js/components/calls/controller.js | 100 +++++++++++++++++- assets/js/components/calls/interface.js | 34 ++++++ .../js/components/notifications/interface.js | 7 +- assets/js/components/notifications/service.js | 8 +- 4 files changed, 143 insertions(+), 6 deletions(-) diff --git a/assets/js/components/calls/controller.js b/assets/js/components/calls/controller.js index a5fad5dc..fbc3c3de 100644 --- a/assets/js/components/calls/controller.js +++ b/assets/js/components/calls/controller.js @@ -12,6 +12,12 @@ ComunicWeb.components.calls.controller = { */ _is_init: false, + /** + * This variable contains whether the user is being + * notified of a call or not + */ + _is_processing_call: false, + /** * Initialize calls component */ @@ -107,11 +113,97 @@ ComunicWeb.components.calls.controller = { if(call.error) return notify("Could not get a call for this conversation!", "danger"); - //Add the call to the list of opened calls - ComunicWeb.components.calls.currentList.addCallToList(call.id); + ComunicWeb.components.calls.controller.open(call); - //Initialize call - ComunicWeb.components.calls.callWindow.initCall(call); + }); + + }, + + /** + * Call this method to initialize a call for a call we have information about + * + * @param {Object} call Information about the call + */ + open: function(call){ + + //Add the call to the list of opened calls + ComunicWeb.components.calls.currentList.addCallToList(call.id); + + //Initialize call + ComunicWeb.components.calls.callWindow.initCall(call); + }, + + /** + * This method is called each time the notification service + * detect that the number of pending calls has increased. It + * must in fact be "thread-safe" to avoid to do twice things + * that should be one only once + * + * @param {number} number The number of pending calls + */ + newCallsAvailable: function(number){ + + //Check if user is already processing a call + if(this._is_processing_call) + return; + this._is_processing_call = true; + + /** + * Switch processing call to false + */ + var undoIsProcessing = function(){ + ComunicWeb.components.calls.controller._is_processing_call = false; + } + + //Get information about the next pending call + ComunicWeb.components.calls.interface.getNextPendingCall(function(call){ + + //Check if there is no pending call + if(call.notice) + return undoIsProcessing(); + + ComunicWeb.components.conversations.utils.getNameForID(call.conversation_id, function(name){ + + //Check for errors + if(!name){ + ComunicWeb.debug.logMessage("Could not get the name of the conversation for a call, cannot process it!"); + undoIsProcessing(); + return; + } + + //Show ring screen + ComunicWeb.components.calls.ringScreen.show(name, 30, function(accept){ + + undoIsProcessing(); + + ComunicWeb.components.calls.controller.applyReponseForCall(call, accept); + + }); + }); + + }); + }, + + /** + * Apply a response for the call + * + * @param {Object} call Information about the target call + * @param {Boolean} accept TRUE to accept call / FALSE else + */ + applyReponseForCall: function(call, accept){ + + //Send response to server + ComunicWeb.components.calls.interface.respondToCall(call.id, accept, function(r){ + + //Check for error + if(r.error) + return notify("Could not send response to call to server!", "danger"); + + if(!accept) + return; + + //We may start the call now + ComunicWeb.components.calls.controller.open(call); }); diff --git a/assets/js/components/calls/interface.js b/assets/js/components/calls/interface.js index 08ef8ea6..0aa2ad18 100644 --- a/assets/js/components/calls/interface.js +++ b/assets/js/components/calls/interface.js @@ -31,5 +31,39 @@ ComunicWeb.components.calls.interface = { true, callback ); + }, + + /** + * Get and return the next pending call for the + * user + * + * @param {function} callback + */ + getNextPendingCall: function(callback){ + ComunicWeb.common.api.makeAPIrequest( + "calls/nextPending", + {}, + true, + callback + ); + }, + + /** + * Respond to call + * + * @param {number} call_id The ID of the target call + * @param {boolean} accept TRUE to accept call / FALSE els + * @param {function} callback Function to call once response has been set + */ + respondToCall: function(call_id, accept, callback){ + ComunicWeb.common.api.makeAPIrequest( + "calls/respond", + { + call_id: call_id, + accept: accept + }, + true, + callback + ); } } \ No newline at end of file diff --git a/assets/js/components/notifications/interface.js b/assets/js/components/notifications/interface.js index ea003cbf..39c61686 100644 --- a/assets/js/components/notifications/interface.js +++ b/assets/js/components/notifications/interface.js @@ -25,14 +25,19 @@ ComunicWeb.components.notifications.interface = { /** * Get the number of unread news such as notifications or conversations * + * @param {boolean} get_calls Get the number of pending calls * @param {function} callback */ - getAllUnread: function(callback){ + getAllUnread: function(get_calls, callback){ //Perform API request var apiURI = "notifications/count_all_news"; var params = {}; + //Check if we have to get the number of pending calls + if(get_calls) + params.include_calls = true; + //Perform the request ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); diff --git a/assets/js/components/notifications/service.js b/assets/js/components/notifications/service.js index 8f8e46f3..ab8d6ffd 100644 --- a/assets/js/components/notifications/service.js +++ b/assets/js/components/notifications/service.js @@ -35,7 +35,8 @@ ComunicWeb.components.notifications.service = { //Get the number of notifications from the API - ComunicWeb.components.notifications.interface.getAllUnread(function(response){ + ComunicWeb.components.notifications.interface.getAllUnread( + ComunicWeb.components.calls.controller.isAvailable(), function(response){ //Continue in case of success if(response.error) @@ -76,6 +77,11 @@ ComunicWeb.components.notifications.service = { ComunicWeb.components.notifications.song.play(); ComunicWeb.components.notifications.service.last_notifs_number = total_number_notifs; + + + //Process the number of calls if possible + if(response.calls && response.calls > 0) + ComunicWeb.components.calls.controller.newCallsAvailable(response.calls); }); }, 2000);