Optimized get friends list function

This commit is contained in:
Pierre
2018-03-05 19:04:21 +01:00
parent 7df905a988
commit 4bf4218b1d
3 changed files with 40 additions and 3 deletions

View File

@ -13,8 +13,14 @@ class friendsController{
* @url POST /friends/getList
*/
public function getFriendsList(){
user_login_required(); //Login required
//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";
//Try to get friends list
$friendsList = CS::get()->components->friends->getList(userID);
@ -27,7 +33,7 @@ class friendsController{
foreach($friendsList as $friend){
//Parse friend informations
$api_list[] = $this->parseFriendAPI($friend);
$api_list[] = $this->parseFriendAPI($friend, $all_infos);
}
@ -38,6 +44,15 @@ class friendsController{
return $api_list;
}
/**
* Get the list of friends of a specific user
*
* @url POST /friends/get_user_list
*/
public function get_user_list(){
}
/**
* Send a friendship request
*
@ -242,18 +257,29 @@ class friendsController{
* Convert a friend object into an object readable by the api
*
* @param Friend $friend The input friend
* @param bool $all_infos Specify if whether all the informations about the
* friendship should be returned or not
* @return array Informations about the friend readable by the api
*/
private function parseFriendAPI(Friend $friend) : array {
private function parseFriendAPI(Friend $friend, bool $all_infos = FALSE) : array {
//Parse informations about the friend
$data = array(
"ID_friend" => $friend->getFriendID(),
"accepted" => $friend->isAccepted() ? 1 : 0,
"following" => $friend->isFollowing() ? 1 : 0,
"time_last_activity" => $friend->getLastActivityTime()
);
//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();
}
return $data;
}
}