Get the list of latest posts of a user

This commit is contained in:
Pierre 2018-02-03 15:40:55 +01:00
parent 9bc436b2a2
commit b6466c68e4
2 changed files with 114 additions and 9 deletions

View File

@ -52,17 +52,35 @@ class postsController {
//Get the post of the user //Get the post of the user
$posts = CS::get()->components->posts->getUserPosts(userID, $userID, $startFrom); $posts = CS::get()->components->posts->getUserPosts(userID, $userID, $startFrom);
//Process the list of posts //Return parsed list of posts
foreach($posts as $num => $infos){ return $this->parsePostsList($posts);
//Parse post informations
$posts[$num] = $this->parsePostForAPI($infos);
}
return $posts;
} }
/**
* Get the latest posts for the user
*
* @url POST /posts/get_latest
*/
public function get_latest_posts(){
user_login_required();
//Check if there is a startpoint for the posts
if(isset($_POST['startFrom'])){
$startFrom = toInt($_POST['startFrom']);
}
else
$startFrom = 0; //No start point
//Get the post of the user
$posts = CS::get()->components->posts->get_latest(userID, $startFrom, 10);
//Return parsed list of posts
return $this->parsePostsList($posts);
}
/** /**
* Get informations about a single post * Get informations about a single post
* *
@ -457,6 +475,25 @@ class postsController {
return $postID; return $postID;
} }
/**
* Process a list of post for the API
*
* @param array $list The list of posts to process
* @return array The parsed list of posts for API
*/
private function parsePostsList(array $list) : array {
//Process the list of posts
foreach($list as $num => $infos){
//Parse post informations
$list[$num] = $this->parsePostForAPI($infos);
}
return $list;
}
/** /**
* Parse a post to make it ready to be displayed on the API * Parse a post to make it ready to be displayed on the API
* *

View File

@ -162,6 +162,74 @@ class Posts {
} }
/**
* Get the list of latest posts for a user
*
* @param int $userID The ID of the user requesting its list of posts
* @param int $startPoint The startpoint of the research (default: 0 = none)
* @param int $limit The limit of the research (default: 10)
* @return array The list of newest posts for the user
*/
public function get_latest(int $userID, int $startPoint = 0, int $limit = 10) : array {
//Check the value of limit (security)
if($limit < 1){
throw new Exception("The limit of the query must absolutly be positive !");
}
//Arbitrary visibility level
$visibilityLevel = self::VISIBILITY_FRIENDS;
//Get the list of friends of the user
$friendsList = components()->friends->getList($userID);
//Prepare the request on the database
//Add the visibility level conditions
$conditions = "WHERE niveau_visibilite <= ? AND (ID_personne = ?";
$dataConds = array($visibilityLevel, $userID);
//Process the list of friends of the user
foreach($friendsList as $friend){
$friendID = $friend->getFriendID();
$conditions .= " OR ID_personne = ?";
$dataConds[] = $friendID;
}
//Close user list conditions
$conditions .= ")";
//Add startpoint condition if required (and get older messages)
if($startPoint != 0){
$conditions .= " AND ID <= ? ";
$dataConds[] = $startPoint;
}
//Specify order and limit
$conditions.= " ORDER BY ID DESC LIMIT ".$limit;
//Perform the request
$list = CS::get()->db->select(
$this::TABLE_NAME,
$conditions,
$dataConds
);
//Parse the list of posts
$posts = array();
foreach($list as $row){
//Check if the user allow the comments
$allow_comments = CS::get()->components->user->allowComments($row["ID_personne"]);
//Parse post informations
$posts[] = $this->parse_post($row, $allow_comments);
}
//Return the list of posts
return $posts;
}
/** /**
* Check whether a post exists or not * Check whether a post exists or not
* *