Can create a frienship request

This commit is contained in:
Pierre 2017-12-21 19:24:25 +01:00
parent d5eda3c1d4
commit 235428982d
2 changed files with 59 additions and 0 deletions

View File

@ -29,6 +29,43 @@ class friendsController{
return $friendsList;
}
/**
* Send a friendship request
*
* @url POST /friends/sendRequest
*/
public function sendRequest(){
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 two persons are already friend
if(CS::get()->components->friends->are_friend(userID, $friendID))
Rest_fatal_error(401, "The two personns are already friend !");
//Check if there is already a pending request
//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 have already sent a friendship request to this personn !");
//Check if the current user has received a friendship request
if(CS::get()->components->friends->sent_request($friendID, userID))
Rest_fatal_error(401, "You have already received a friendship request from this personn !");
//We can now create the request
if(!CS::get()->components->friends->send_request(userID, $friendID))
Rest_fatal_error(500, "Couldn't create friendship request !");
//Success
return array("success" => "The friendship request has been created !");
}
/**
* Respond to a friendship request
*

View File

@ -186,6 +186,28 @@ class friends {
return count($response) > 0;
}
/**
* Send a friendship request
*
* @param $userID The ID of the user creating the request
* @param $targetID The target of the friendship request
* @return TRUE in case of success / FALSE else
*/
public function send_request(int $userID, int $targetID) : bool {
//Prepare the insertion
$tableName = $this->friendsTable;
$values = array(
"ID_personne" => $targetID,
"ID_amis" => $userID,
"actif" => 0
);
//Try to perform the request
return CS::get()->db->addLine($tableName, $values) == true;
}
/**
* Check wether a user has sent a friendship
* request to another friend