diff --git a/RestControllers/friendsController.php b/RestControllers/friendsController.php index 7cc38c1..bf2894d 100644 --- a/RestControllers/friendsController.php +++ b/RestControllers/friendsController.php @@ -201,4 +201,36 @@ class friendsController{ //Return the response return $response; } + + /** + * Update the following status of a friendship + * + * @url POST /friends/setFollowing + */ + public function update_following(){ + user_login_required(); //Login required + + //Check if the a friendID has been specified + if(!isset($_POST['friendID'])) + Rest_fatal_error(400, "Please specify a friend ID !"); + + $friendID = toInt($_POST['friendID']); + + //Check if a follow status has been specified + if(!isset($_POST['follow'])) + Rest_fatal_error(400, "Please specify a follow status!"); + + $following = $_POST['follow'] === "true"; + + //Check if the two personns are friend + if(!CS::get()->components->friends->are_friend(userID, $friendID)) + Rest_fatal_error(401, "You are not friend with this personn!"); + + //Update following status + if(!CS::get()->components->friends->set_following(userID, $friendID, $following)) + Rest_fatal_error(500, "Couldn't update friendship status!"); + + //Success + return array("success" => "Friendship status has been updated!"); + } } \ No newline at end of file diff --git a/classes/components/friends.php b/classes/components/friends.php index 32224fd..e08143c 100644 --- a/classes/components/friends.php +++ b/classes/components/friends.php @@ -299,7 +299,28 @@ class friends { return $response[0]['autoriser_post_page'] == 1; + } + /** + * Update the following status for a friendship + * + * @param int $userID The ID of the user updating the status + * @param int $friendID The ID of the target friend + * @param boolean $following The new status + * @return bool True in case of succcess / false else + */ + public function set_following(int $userID, int $friendID, bool $following) : bool { + + //Update the table + $tableName = $this->friendsTable; + $conditions = "ID_personne = ? AND ID_amis = ?"; + $conditionsValues = array($userID, $friendID); + $newValues = array( + "abonnement" => $following ? 1 : 0 + ); + + //Perform the request + return CS::get()->db->updateDB($tableName, $conditions, $newValues, $conditionsValues); } }