mirror of
https://github.com/pierre42100/ComunicAPI
synced 2025-06-19 16:45:17 +00:00
Can create a call
This commit is contained in:
@ -7,6 +7,12 @@
|
||||
|
||||
class CallsComponents {
|
||||
|
||||
/**
|
||||
* Calls tables names
|
||||
*/
|
||||
private const CALLS_LIST_TABLE = "comunic_calls";
|
||||
private const CALLS_MEMBERS_TABLE = "comunic_calls_members";
|
||||
|
||||
/**
|
||||
* Get and return calls configuration
|
||||
*
|
||||
@ -33,6 +39,196 @@ class CallsComponents {
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the call for a conversation
|
||||
*
|
||||
* @param $conversation_id Target conversation ID
|
||||
* @param $load_members Specify whether members information should
|
||||
* be loaded too or not
|
||||
* @return CallInformation Matching call information object / invalid object
|
||||
* in case of failure
|
||||
*/
|
||||
public function getForConversation(int $conversation_id, bool $load_members) : CallInformation {
|
||||
|
||||
$entry = db()->select(
|
||||
self::CALLS_LIST_TABLE,
|
||||
"WHERE conversation_id = ?",
|
||||
array($conversation_id)
|
||||
);
|
||||
|
||||
if(count($entry) == 0)
|
||||
return new CallInformation();
|
||||
|
||||
$info = self::DBToCallInformation($entry[0]);
|
||||
|
||||
//Load call members if required
|
||||
if($load_members && !$this->getMembers($info))
|
||||
return new CallInformation();
|
||||
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a call for a conversation
|
||||
*
|
||||
* @param $conversationID The ID of the target conversation
|
||||
* @return bool TRUE for a success / FALSE else
|
||||
*/
|
||||
public function createForConversation(int $conversationID) : bool {
|
||||
|
||||
//Generate call information
|
||||
$info = new CallInformation();
|
||||
$info->set_conversation_id($conversationID);
|
||||
$info->set_last_active(time());
|
||||
|
||||
//We need to get the list of members of the conversation to create members list
|
||||
$conversation_members = components()->conversations->getConversationMembers($conversationID);
|
||||
|
||||
//Check for errors
|
||||
if(count($conversation_members) == 0)
|
||||
return false;
|
||||
|
||||
|
||||
//Insert the call in the database to get its ID
|
||||
if(!db()->addLine(
|
||||
self::CALLS_LIST_TABLE,
|
||||
self::CallInformationToDB($info)
|
||||
))
|
||||
return false;
|
||||
$info->set_id(db()->getLastInsertedID());
|
||||
|
||||
foreach($conversation_members as $memberID){
|
||||
$member = new CallMemberInformation();
|
||||
$member->set_call_id($info->get_id());
|
||||
$member->set_userID($memberID);
|
||||
$member->set_user_call_id(random_str(190));
|
||||
$member->set_accepted(CallMemberInformation::USER_UNKNOWN);
|
||||
|
||||
//Try to add the member to the list
|
||||
if(!$this->addMember($member))
|
||||
return false;
|
||||
}
|
||||
|
||||
//Success
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new member to a call
|
||||
*
|
||||
* @param $member Information about the member to add
|
||||
* @return bool TRUE for a success / FALSE else
|
||||
*/
|
||||
private function addMember(CallMemberInformation $member) : bool {
|
||||
return db()->addLine(
|
||||
self::CALLS_MEMBERS_TABLE,
|
||||
self::CallMemberInformationToDB($member)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load information about the members related to the call
|
||||
*
|
||||
* @param $info Information about the call to process
|
||||
* @return bool TRUE in case of success / FALSE else
|
||||
*/
|
||||
private function getMembers(CallInformation $info) : bool {
|
||||
|
||||
$entries = db()->select(
|
||||
self::CALLS_MEMBERS_TABLE,
|
||||
"WHERE call_id = ?",
|
||||
array($info->get_id())
|
||||
);
|
||||
|
||||
foreach($entries as $entry)
|
||||
$info->add_member(self::DBToCallMemberInformation($entry));
|
||||
|
||||
return count($entries) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the response of a member to a call
|
||||
*
|
||||
* @param $callID The ID of the target call
|
||||
* @param $userID The ID of the target member
|
||||
* @param $accept TRUE to accept the call / FALSE else
|
||||
* @return bool TRUE for a success / FALSE else
|
||||
*/
|
||||
public function setMemberResponse(int $callID, int $userID, bool $accept) : bool {
|
||||
db()->updateDB(
|
||||
self::CALLS_MEMBERS_TABLE,
|
||||
"call_id = ? AND user_id = ?",
|
||||
array(
|
||||
"user_accepted" =>
|
||||
$accept ? CallMemberInformation::USER_ACCEPTED : CallMemberInformation::USER_REJECTED
|
||||
),
|
||||
array(
|
||||
$callID,
|
||||
$userID
|
||||
)
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a database entry into a CallInformation object
|
||||
*
|
||||
* @param $entry The entry to convert
|
||||
* @return CallInformation Generated object
|
||||
*/
|
||||
private static function DBToCallInformation(array $entry) : CallInformation {
|
||||
$info = new CallInformation();
|
||||
$info->set_id($entry["id"]);
|
||||
$info->set_conversation_id($entry["conversation_id"]);
|
||||
$info->set_last_active($entry["last_active"]);
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a CallInformation object into a database entry
|
||||
*
|
||||
* @param $call Call information object to convert
|
||||
* @return array Generated array
|
||||
*/
|
||||
private static function CallInformationToDB(CallInformation $call) : array {
|
||||
$data = array();
|
||||
$data["conversation_id"] = $call->get_conversation_id();
|
||||
$data["last_active"] = $call->get_last_active();
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a database entry into a CallMemberInformation object
|
||||
*
|
||||
* @param $entry The entry to convert
|
||||
* @return CallMemberInformation Generated object
|
||||
*/
|
||||
private static function DBToCallMemberInformation(array $entry) : CallMemberInformation {
|
||||
$member = new CallMemberInformation();
|
||||
$member->set_id($entry["id"]);
|
||||
$member->set_call_id($entry["call_id"]);
|
||||
$member->set_userID($entry["user_id"]);
|
||||
$member->set_user_call_id($entry["user_call_id"]);
|
||||
$member->set_accepted($entry["user_accepted"]);
|
||||
return $member;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a CallMemberInformation object into a database entry
|
||||
*
|
||||
* @param $member The member to convert
|
||||
* @return array Generated database entry
|
||||
*/
|
||||
private static function CallMemberInformationToDB(CallMemberInformation $member) : array {
|
||||
$data = array();
|
||||
$data["call_id"] = $member->get_call_id();
|
||||
$data["user_id"] = $member->get_userID();
|
||||
$data["user_call_id"] = $member->get_user_call_id();
|
||||
$data["user_accepted"] = $member->get_accepted();
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
//Register class
|
||||
|
Reference in New Issue
Block a user