diff --git a/RestControllers/CallsController.php b/RestControllers/CallsController.php index cdfc4f9..4c5ff7f 100644 --- a/RestControllers/CallsController.php +++ b/RestControllers/CallsController.php @@ -85,6 +85,50 @@ class CallsController { return self::CallInformationToAPI($call); } + /** + * Respond to a call + * + * @url POST /calls/respond + */ + public function respondToCall(){ + user_login_required(); + + //Get target call ID + $call_id = $this->GetSafeCallIDFromRequest("call_id"); + + //Get target response + $accept = postBool("accept"); + + //Set user response to call + if(!components()->calls->setMemberResponse($call_id, userID, $accept)) + Rest_fatal_error(500, "Could not set response of user to call!"); + + return array( + "success" => "User response to call has been successfully set!" + ); + } + + /** + * Get safely the ID of a call from the request + * + * @param $name The name of the POST field containing call ID + * @return int The ID of the call + */ + private function GetSafeCallIDFromRequest(string $name) : int { + + //Get call ID + $call_id = postInt($name); + + if($call_id < 1) + Rest_fatal_error(401, "Invalid call id !"); + + //Check if the user belongs to the call or not + if(!components()->calls->doesUserBelongToCall($call_id, userID)) + Rest_fatal_error(401, "You do not belong to this call!"); + + return $call_id; + } + /** * Turn a CallsConfig object into an API entry * diff --git a/classes/components/CallsComponent.php b/classes/components/CallsComponent.php index d8fc7b0..7e50800 100644 --- a/classes/components/CallsComponent.php +++ b/classes/components/CallsComponent.php @@ -220,6 +220,21 @@ class CallsComponents { } + /** + * Check out whether a user belongs to a call or not + * + * @param $callID The ID of the target call + * @param $userID The ID of the target user + * @return bool TRUE if the user belongs to the call / FALSE else + */ + public function doesUserBelongToCall(int $callID, int $userID) : bool { + return db()->count( + self::CALLS_MEMBERS_TABLE, + "WHERE call_id = ? AND user_id = ?", + array($callID, $userID) + ) > 0; + } + /** * Set the response of a member to a call *