diff --git a/RestControllers/friendsController.php b/RestControllers/friendsController.php index c9a78c8..7cc38c1 100644 --- a/RestControllers/friendsController.php +++ b/RestControllers/friendsController.php @@ -66,6 +66,33 @@ class friendsController{ } + /** + * Remove a previously send frienship request + * + * @url POST /friends/removeRequest + */ + public function removeRequest(){ + user_login_required(); //Login required + + //Check parametres + if(!isset($_POST["friendID"])) + Rest_fatal_error(400, "Please specify a user ID !"); + + //Extract informations and process request + $friendID = toInt($_POST['friendID']); + + //Check if the current user has sent a request to the other user + if(!CS::get()->components->friends->sent_request(userID, $friendID)) + Rest_fatal_error(401, "You didn't send a friendship request to this user !"); + + //Try to remove the friendship request + if(!CS::get()->components->friends->remove_request(userID, $friendID)) + Rest_fatal_error(500, "An error occured while trying to remove the friendship request !"); + + //This is a success + return array("success" => "The friendship request has been removed!"); + } + /** * Respond to a friendship request * diff --git a/classes/components/friends.php b/classes/components/friends.php index cb005b9..32224fd 100644 --- a/classes/components/friends.php +++ b/classes/components/friends.php @@ -208,6 +208,25 @@ class friends { } + /** + * Delete a friendship request previously created + * + * @param $userID The ID of the user removing its request + * @param $targetID The ID of the target of the request + * @return TRUE in case of success / false else + */ + public function remove_request(int $userID, int $targetID) : bool { + + //Prepare the request on the database + $tableName = $this->friendsTable; + $conditions = "ID_personne = ? AND ID_amis = ? AND actif = 0"; + $values = array($targetID, $userID); + + //Try to perform the request + return CS::get()->db->deleteEntry($tableName, $conditions, $values); + + } + /** * Check wether a user has sent a friendship * request to another friend