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

View File

@ -43,6 +43,7 @@ class friends {
$this->friendsTable.".ID_amis", $this->friendsTable.".ID_amis",
$this->friendsTable.".actif", $this->friendsTable.".actif",
$this->friendsTable.".abonnement", $this->friendsTable.".abonnement",
$this->friendsTable.".autoriser_post_page"
); );
//Perform the request on the database //Perform the request on the database
@ -364,6 +365,7 @@ class friends {
$friend->setAccepted($data["actif"] == 1); $friend->setAccepted($data["actif"] == 1);
$friend->setFollowing($data["abonnement"] == 1); $friend->setFollowing($data["abonnement"] == 1);
$friend->setLastActivityTime($data["last_activity"]); $friend->setLastActivityTime($data["last_activity"]);
$friend->setCanPostTexts($data["autoriser_post_page"] == 1);
return $friend; return $friend;
} }

View File

@ -12,6 +12,7 @@ class Friend {
private $accepted; private $accepted;
private $following; private $following;
private $last_activity_time; private $last_activity_time;
private $can_post_texts;
//Set and get friend ID //Set and get friend ID
public function setFriendID(int $id){ public function setFriendID(int $id){
@ -44,4 +45,12 @@ class Friend {
public function getLastActivityTime() : int { public function getLastActivityTime() : int {
return $this->last_activity_time; return $this->last_activity_time;
} }
//Set and get the post creation authorization status
public function setCanPostTexts(bool $can_post_texts){
$this->can_post_texts = $can_post_texts;
}
public function canPostTexts() : bool {
return $this->can_post_texts;
}
} }