ComunicAPI/RestControllers/CallsController.php

132 lines
3.5 KiB
PHP
Raw Normal View History

2019-01-22 16:39:45 +00:00
<?php
/**
* Calls controller
*
* @author Pierre HUBERT
*/
class CallsController {
2019-01-24 08:22:50 +00:00
/**
* Single user response to call
*/
const USER_RESPONSE_TO_CALL = array(
CallMemberInformation::USER_ACCEPTED => "accepted",
CallMemberInformation::USER_REJECTED => "rejected",
CallMemberInformation::USER_UNKNOWN => "unknown"
);
2019-01-22 16:39:45 +00:00
/**
* Get call configuration
*
* @url POST /calls/config
*/
public function getConfig(){
user_login_required();
return self::CallsConfigToAPI(components()->calls->getConfig());
}
2019-01-24 08:22:50 +00:00
/**
* Create a call for a conversation
*
* @url POST /calls/createForConversation
*/
public function createForConversation(){
user_login_required();
//Get conversation ID
$conversationID = getPostConversationID("conversationID");
//Check if the user belongs to the conversation
if(!components()->conversations->userBelongsTo(userID, $conversationID))
Rest_fatal_error(401, "Specified user doesn't belongs to the conversation number ".$conversationID." !");
//First, check if a call alreay exists for this conversation
$call = components()->calls->getForConversation($conversationID, true);
//If could not get a call, try to create a new one
if(!$call->isValid()){
//Try to create the call
if(!components()->calls->createForConversation($conversationID))
Rest_fatal_error(500, "Could not create the call");
//Get the conversation again
$call = components()->calls->getForConversation($conversationID, true);
if(!$call->isValid())
Rest_fatal_error(500, "Could not get information about created call!");
}
//The user automatically accept the call
components()->calls->setMemberResponse($call->get_id(), userID, true);
//Returns information about the call
return self::CallInformationToAPI($call);
}
2019-01-22 16:39:45 +00:00
/**
* Turn a CallsConfig object into an API entry
*
* @param $config The config to convert
* @return array Generated API entry
*/
private static function CallsConfigToAPI(CallsConfig $config) : array {
$data = array();
$data["enabled"] = $config->get_enabled();
//Give full configuration calls are enabled
if($config->get_enabled()){
$data["signal_server_name"] = $config->get_signal_server_name();
$data["signal_server_port"] = $config->get_signal_server_port();
$data["stun_server"] = $config->get_stun_server();
$data["turn_server"] = $config->get_turn_server();
$data["turn_username"] = $config->get_turn_username();
$data["turn_password"] = $config->get_turn_password();
}
return $data;
}
2019-01-24 08:22:50 +00:00
/**
* Turn a CallInformation object into an API entry
*
* @param $call The call to convert
* @return array Generated API entry
*/
private static function CallInformationToAPI(CallInformation $call) : array {
$data = array(
"id" => $call->get_id(),
"conversation_id" => $call->get_conversation_id(),
"last_active" => $call->get_last_active(),
"members" => array()
);
foreach($call->get_members() as $member)
$data["members"][] = self::CallMemberInformationToAPI($member);
return $data;
}
/**
* Turn a CallMemberInformation object into an API entry
*
* @param $member Member information
* @return array User information API entry
*/
private static function CallMemberInformationToAPI(CallMemberInformation $member) : array {
return array(
"id" => $member->get_id(),
"userID" => $member->get_userID(),
"call_id" => $member->get_call_id(),
"user_call_id" => $member->get_user_call_id(),
"accepted" => self::USER_RESPONSE_TO_CALL[$member->get_accepted()]
);
}
2019-01-22 16:39:45 +00:00
}