ComunicAPI/RestControllers/friendsController.php

134 lines
3.4 KiB
PHP
Raw Normal View History

2017-05-28 12:09:20 +00:00
<?php
/**
* Friends controller
*
* @author Pierre HUBERT
*/
class friendsController{
/**
* Get friends list
*
* @url POST /friends/getList
*/
public function getFriendsList(){
user_login_required(); //Login required
//Try to get friends list
2017-05-31 14:49:25 +00:00
$friendsList = CS::get()->components->friends->getList(userID);
2017-05-28 12:09:20 +00:00
//Check for errors
if($friendsList === false)
Rest_fatal_error(500, "Couldn't get friends list !");
//Update the last activity of the user
CS::get()->components->user->updateLastActivity(userID);
2017-05-28 12:09:20 +00:00
//Return list
return $friendsList;
}
2017-06-04 14:36:12 +00:00
/**
* Respond to a friendship request
*
* @url POST /friends/respondRequest
*/
public function respondRequest(){
user_login_required(); //Login required
//Check parametres
if(!isset($_POST["friendID"]) OR !isset($_POST['accept']))
2017-06-19 08:36:39 +00:00
Rest_fatal_error(400, "Please check your parametres !");
2017-06-04 14:36:12 +00:00
//Extract informations and process request
$friendID = toInt($_POST['friendID']);
2017-06-04 14:36:12 +00:00
$acceptRequest = $_POST['accept'] == "true";
//Try to perform request
$result = CS::get()->components->friends->respondRequest(userID, $friendID, $acceptRequest);
//Return result
if($result != true)
Rest_fatal_error(500, "Couldn't respond to friendship request !");
//Else it is a success
return array("success" => "A response was given to friendship request !");
}
2017-12-04 19:18:30 +00:00
/**
* Delete a friend from the list
*
* @url POST /friends/remove
*/
2017-12-20 17:53:00 +00:00
public function delete(){
user_login_required(); //Login required
2017-12-04 19:18:30 +00:00
2017-12-20 17:53:00 +00:00
//Check input parametres
if(!isset($_POST['friendID']))
Rest_fatal_error(400, "Please specify the ID of the friend to delete !");
2017-12-04 19:18:30 +00:00
//Delete the friend from the list
$friendID = toInt($_POST['friendID']);
$result = CS::get()->components->friends->remove(userID, $friendID);
//Check if the operation is a result
if(!$result)
Rest_fatal_error(500, "Couldn't remove user from the friendlist for an unexcepted reason !");
else
return array("success" => "The friend was removed from the list !");
2017-12-20 17:53:00 +00:00
}
/**
* Get the status of a friendship
*
* Check if the users are friends, or this there is a pending request...
*
* @url POST /friends/getStatus
*/
public function getStatus(){
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 !");
2017-12-04 19:18:30 +00:00
2017-12-20 17:53:00 +00:00
//Get it
$friendID = toInt($_POST['friendID']);
//Prepare the response
$response = array(
"are_friend" => false,
"sent_request" => false,
"received_request" => false,
2017-12-21 17:49:50 +00:00
"following" => false,
2017-12-20 17:53:00 +00:00
);
//Check if the two personns are friend
$response['are_friend'] =
CS::get()->components->friends->are_friend(userID, $friendID);
//Perform next check only if the personns are not already friend
if(!$response['are_friend']){
//Check if the current user has sent a request to the other user
if(CS::get()->components->friends->sent_request(userID, $friendID))
$response["sent_request"] = true;
//Check if the current user has received a friendship request
if(CS::get()->components->friends->sent_request($friendID, userID))
$response["received_request"] = true;
}
2017-12-21 17:49:50 +00:00
else {
//Perform the check specific to the real friend
if(CS::get()->components->friends->is_following(userID, $friendID))
$response['following'] = true;
}
2017-12-20 17:53:00 +00:00
//Return the response
return $response;
}
2017-05-28 12:09:20 +00:00
}