From b6466c68e4724b3bc5e7fb9aa07858745f79b349 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 3 Feb 2018 15:40:55 +0100 Subject: [PATCH] Get the list of latest posts of a user --- RestControllers/postsController.php | 55 +++++++++++++++++++---- classes/components/posts.php | 68 +++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 9 deletions(-) diff --git a/RestControllers/postsController.php b/RestControllers/postsController.php index 58134e8..119ae3a 100644 --- a/RestControllers/postsController.php +++ b/RestControllers/postsController.php @@ -52,17 +52,35 @@ class postsController { //Get the post of the user $posts = CS::get()->components->posts->getUserPosts(userID, $userID, $startFrom); - //Process the list of posts - foreach($posts as $num => $infos){ - - //Parse post informations - $posts[$num] = $this->parsePostForAPI($infos); - - } - - return $posts; + //Return parsed list of posts + return $this->parsePostsList($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 * @@ -457,6 +475,25 @@ class postsController { 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 * diff --git a/classes/components/posts.php b/classes/components/posts.php index c0b8fa9..a2a76c7 100644 --- a/classes/components/posts.php +++ b/classes/components/posts.php @@ -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 *