ComunicAPI/RestControllers/friendsController.php

307 lines
8.6 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(){
2018-03-05 18:04:21 +00:00
2017-05-28 12:09:20 +00:00
user_login_required(); //Login required
2018-03-05 18:04:21 +00:00
//Check if the user want all the informations about its friends or not
$all_infos = false;
if(isset($_POST['complete']))
$all_infos = $_POST['complete'] === "true";
2017-05-28 12:09:20 +00:00
//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 !");
2018-02-03 14:25:58 +00:00
//Process the list
$api_list = array();
foreach($friendsList as $friend){
//Parse friend informations
2018-03-05 18:04:21 +00:00
$api_list[] = $this->parseFriendAPI($friend, $all_infos);
2018-02-03 14:25:58 +00:00
}
//Update the last activity of the user
CS::get()->components->user->updateLastActivity(userID);
2018-02-03 14:25:58 +00:00
2017-05-28 12:09:20 +00:00
//Return list
2018-02-03 14:25:58 +00:00
return $api_list;
2017-05-28 12:09:20 +00:00
}
2018-03-05 18:04:21 +00:00
/**
* Get the list of friends of a specific user
*
* @url POST /friends/get_user_list
*/
public function get_user_list(){
2018-03-05 18:24:42 +00:00
//Get the ID of the target user
$userID = getPostUserID("userID");
//Check whether the friends list of the user is public or not
if(!components()->user->userAllowed(userID, $userID))
Rest_fatal_error(401, "You are not allowed to access these informations !");
//Check if the friendlist of the user is public or not
if(!components()->user->isFriendsListPublic($userID))
Rest_fatal_error(401, "The friends list of the user is not public !");
//Get the list of friend of the user
$friends = CS::get()->components->friends->getList($userID);
//Process and return it
$IDs = array();
foreach($friends as $friend){
$IDs[] = $friend->getFriendID();
}
return $IDs;
2018-03-05 18:04:21 +00:00
}
2017-12-21 18:24:25 +00:00
/**
* Send a friendship request
*
* @url POST /friends/sendRequest
*/
public function sendRequest(){
user_login_required(); //Login required
2017-12-25 08:17:38 +00:00
//Get target ID
$friendID = getPostUserID('friendID');
2017-12-21 18:24:25 +00:00
//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 !");
//Create notification
create_friendship_notification(userID, $friendID, Notification::SENT_FRIEND_REQUEST);
2017-12-21 18:24:25 +00:00
//Success
return array("success" => "The friendship request has been created !");
}
/**
* Remove a previously sent frienship request
*
* @url POST /friends/removeRequest
*/
public function removeRequest(){
user_login_required(); //Login required
2017-12-25 08:17:38 +00:00
//Get friendID
$friendID = getPostUserID('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 !");
//Delete all related notifications
delete_notifications_friendship_request(userID, $friendID);
//This is a success
return array("success" => "The friendship request has been removed!");
}
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 !");
//Send notification
create_friendship_notification(userID, $friendID,
$acceptRequest ? Notification::ACCEPTED_FRIEND_REQUEST : Notification::REJECTED_FRIEND_REQUEST
);
2017-06-04 14:36:12 +00:00
//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 success
2017-12-04 19:18:30 +00:00
if(!$result)
Rest_fatal_error(500, "Couldn't remove user from the friendlist for an unexcepted reason !");
//Delete any related notification
delete_notifications_friendship_request(userID, $friendID);
//Success
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
2017-12-25 08:17:38 +00:00
//Get friendID
2017-12-24 16:45:05 +00:00
$friendID = getPostUserID('friendID');
2017-12-20 17:53:00 +00:00
//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
//Check if the user is following his friend or not
2017-12-21 17:49:50 +00:00
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-12-23 14:25:18 +00:00
/**
* 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
2017-12-25 08:17:38 +00:00
$friendID = getPostUserID('friendID');
2017-12-23 14:25:18 +00:00
//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!");
}
2018-02-03 14:25:58 +00:00
/**
* Convert a friend object into an object readable by the api
*
* @param Friend $friend The input friend
2018-03-05 18:04:21 +00:00
* @param bool $all_infos Specify if whether all the informations about the
* friendship should be returned or not
2018-02-03 14:25:58 +00:00
* @return array Informations about the friend readable by the api
*/
2018-03-05 18:04:21 +00:00
private function parseFriendAPI(Friend $friend, bool $all_infos = FALSE) : array {
2018-02-03 14:25:58 +00:00
//Parse informations about the friend
$data = array(
"ID_friend" => $friend->getFriendID(),
"accepted" => $friend->isAccepted() ? 1 : 0,
"time_last_activity" => $friend->getLastActivityTime()
);
2018-03-05 18:04:21 +00:00
//Check if all the informations about the friendship should be returned or not
if($all_infos){
//Following status
$data["following"] = $friend->isFollowing() ? 1 : 0;
//Can posts text on page
$data["canPostTexts"] = $friend->canPostTexts();
}
2018-02-03 14:25:58 +00:00
return $data;
}
2017-05-28 12:09:20 +00:00
}