2019-01-23 14:19:34 +00:00
|
|
|
/**
|
|
|
|
* Calls controller
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.components.calls.controller = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This variable contains the initialization state
|
|
|
|
* of the call component
|
|
|
|
*/
|
|
|
|
_is_init: false,
|
|
|
|
|
2019-01-25 08:43:19 +00:00
|
|
|
/**
|
|
|
|
* This variable contains whether the user is being
|
|
|
|
* notified of a call or not
|
|
|
|
*/
|
|
|
|
_is_processing_call: false,
|
|
|
|
|
2019-01-23 14:19:34 +00:00
|
|
|
/**
|
|
|
|
* Initialize calls component
|
|
|
|
*/
|
|
|
|
init: function(){
|
|
|
|
|
|
|
|
//We init this component just once
|
|
|
|
if(this._is_init)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ComunicWeb.debug.logMessage("Initialize calls component");
|
|
|
|
|
2019-01-25 09:08:55 +00:00
|
|
|
//Initialize call container
|
|
|
|
var initializeCallContainer = function(){
|
2019-01-24 13:40:36 +00:00
|
|
|
|
|
|
|
//Signed out users can not make calls
|
2019-01-25 09:08:55 +00:00
|
|
|
if(!signed_in()){
|
|
|
|
ComunicWeb.components.calls.controller.userSignedOut();
|
2019-01-24 13:40:36 +00:00
|
|
|
return;
|
2019-01-25 09:08:55 +00:00
|
|
|
}
|
2019-01-24 13:40:36 +00:00
|
|
|
|
|
|
|
//Need a wrapper to continue
|
|
|
|
if(!byId("wrapper"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Check if calls target already exists
|
|
|
|
if(byId("callsTarget"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Call system must be available
|
|
|
|
if(!ComunicWeb.components.calls.controller.isAvailable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Create call target
|
|
|
|
createElem2({
|
|
|
|
appendTo: byId("wrapper"),
|
|
|
|
type: "div",
|
|
|
|
id: "callsTarget"
|
|
|
|
});
|
2019-01-25 09:08:55 +00:00
|
|
|
|
|
|
|
//Now we have to reopen current calls
|
|
|
|
ComunicWeb.components.calls.controller.reopenCurrentCalls();
|
|
|
|
}
|
|
|
|
|
|
|
|
//We wait the user to be connected before trying to get
|
|
|
|
// call configuration
|
|
|
|
document.addEventListener("got_user_id", function(){
|
|
|
|
|
|
|
|
//Check if we have already the call configuration
|
|
|
|
if(ComunicWeb.components.calls.__config !== undefined)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ComunicWeb.components.calls.interface.getConfig(function(config){
|
|
|
|
|
|
|
|
//Check if we could not get calls configuration
|
|
|
|
if(config.error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Save calls configuration
|
|
|
|
ComunicWeb.components.calls.__config = config;
|
|
|
|
|
|
|
|
initializeCallContainer();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Each time a page is opened, wec check if we have to create calls target
|
|
|
|
document.addEventListener("openPage", function(){
|
|
|
|
initializeCallContainer();
|
2019-01-24 13:40:36 +00:00
|
|
|
});
|
2019-01-23 14:45:19 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access calls configuration
|
|
|
|
*
|
|
|
|
* @return Cached calls configuration
|
|
|
|
*/
|
2019-01-26 15:46:36 +00:00
|
|
|
getConfig: function() {
|
2019-01-23 14:45:19 +00:00
|
|
|
return ComunicWeb.components.calls.__config;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the call feature is available or not
|
|
|
|
*/
|
|
|
|
isAvailable: function(){
|
|
|
|
|
2019-02-02 07:13:06 +00:00
|
|
|
//First, check if this browser support webrtc
|
|
|
|
if(!SimplePeer.WEBRTC_SUPPORT)
|
|
|
|
return false;
|
|
|
|
|
2019-01-23 14:45:19 +00:00
|
|
|
//If do not have any call configuration, it is not possible to
|
|
|
|
//make calls
|
|
|
|
if(this.getConfig() == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//Read configuration
|
|
|
|
return this.getConfig().enabled;
|
2019-01-24 13:40:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate a call for a conversation
|
|
|
|
*
|
|
|
|
* @param {Number} conversationID The ID of the target conversation
|
|
|
|
*/
|
|
|
|
call: function(conversationID){
|
|
|
|
|
|
|
|
//Create / Get call information for the conversation
|
|
|
|
ComunicWeb.components.calls.interface.createForConversation(conversationID, function(call){
|
2019-01-23 14:19:34 +00:00
|
|
|
|
2019-01-24 13:40:36 +00:00
|
|
|
if(call.error)
|
|
|
|
return notify("Could not get a call for this conversation!", "danger");
|
|
|
|
|
2019-01-25 08:43:19 +00:00
|
|
|
ComunicWeb.components.calls.controller.open(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
|
2019-01-26 14:59:06 +00:00
|
|
|
var prompting = true;
|
|
|
|
var ringScreenInfo = ComunicWeb.components.calls.ringScreen.show(name, 30, function(accept){
|
2019-01-25 08:43:19 +00:00
|
|
|
|
2019-01-26 14:59:06 +00:00
|
|
|
prompting = false;
|
|
|
|
|
2019-01-25 08:43:19 +00:00
|
|
|
undoIsProcessing();
|
|
|
|
|
|
|
|
ComunicWeb.components.calls.controller.applyReponseForCall(call, accept);
|
|
|
|
|
|
|
|
});
|
2019-01-26 14:59:06 +00:00
|
|
|
|
|
|
|
//Regulary check if the call is still valid
|
|
|
|
var interval = setInterval(function(){
|
|
|
|
|
|
|
|
if(!prompting)
|
|
|
|
return clearInterval(interval);
|
|
|
|
|
|
|
|
ComunicWeb.components.calls.interface.getInfo(call.id, function(info){
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(info.error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Refuse the call if everyone has left it
|
|
|
|
if(ComunicWeb.components.calls.utils.hasEveryoneLeft(info))
|
|
|
|
ringScreenInfo.respond(false);
|
|
|
|
});
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
|
2019-01-25 08:43:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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");
|
2019-01-24 13:40:36 +00:00
|
|
|
|
2019-01-25 08:43:19 +00:00
|
|
|
if(!accept)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//We may start the call now
|
|
|
|
ComunicWeb.components.calls.controller.open(call);
|
2019-01-24 13:40:36 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2019-01-25 09:08:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reopen all current calls
|
|
|
|
*/
|
|
|
|
reopenCurrentCalls: function(){
|
|
|
|
|
|
|
|
//Process each call to open it
|
|
|
|
ComunicWeb.components.calls.currentList.getCurrentCallsList().forEach(function(entry){
|
|
|
|
|
|
|
|
ComunicWeb.components.calls.interface.getInfo(entry, function(call){
|
|
|
|
|
|
|
|
if(call.error){
|
|
|
|
ComunicWeb.components.calls.currentList.removeCallFromList(entry);
|
|
|
|
return notify("Could not get information about a call!", "danger");
|
|
|
|
}
|
|
|
|
|
|
|
|
ComunicWeb.components.calls.controller.open(call);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this method only if the system is sure that
|
|
|
|
* nobody is signed in the current tab
|
|
|
|
*/
|
|
|
|
userSignedOut: function(){
|
|
|
|
|
|
|
|
//Remove all the current calls from the list
|
|
|
|
ComunicWeb.components.calls.currentList.removeAllCalls();
|
|
|
|
|
2019-01-24 13:40:36 +00:00
|
|
|
}
|
2019-01-23 14:19:34 +00:00
|
|
|
}
|