diff --git a/RestControllers/PostsController.php b/RestControllers/PostsController.php index f3e98cc..3244ea9 100644 --- a/RestControllers/PostsController.php +++ b/RestControllers/PostsController.php @@ -56,6 +56,31 @@ class PostsController { return $this->parsePostsList($posts); } + /** + * Get group posts + * + * @url POST /posts/get_group + */ + public function getGroupPosts(){ + + //Get group ID + $groupID = getPostGroupIdWithAccess("groupID", GroupInfo::VIEW_ACCESS); + + //Get the startpoint for the posts + $startFrom = postInt("startFrom", 0); + + //Check whether the user can see members only posts or not + $membershipLevel = components()->groups->getMembershipLevel(userID, $groupID); + $seeAllPosts = $membershipLevel <= GroupMember::MEMBER; + + //Get the posts of the group + $posts = components()->posts->getGroupPosts($groupID, $seeAllPosts, $startFrom); + + //Return parsed list of posts + return $this->parsePostsList($posts); + + } + /** * Get the latest posts for the user * diff --git a/classes/components/posts.php b/classes/components/posts.php index 1081917..434ab49 100644 --- a/classes/components/posts.php +++ b/classes/components/posts.php @@ -159,6 +159,56 @@ class Posts { } + /** + * Get the posts of a group + * + * @param int $groupID The ID of the related group + * @param bool $all_posts Specify whether we should get all the posts of the user or not + * @param int $from Start point for the query + * @param int $limit The limit for the request (default = 10) + */ + public function getGroupPosts(int $groupID, bool $all_posts, int $from = 0, int $limit = 10){ + + //Check the value of limit (security) + if($limit < 1){ + throw new Exception("The limit of the query must absolutly be positive !"); + } + + //Get user visibility level + $visibilityLevel = $all_posts ? $this::VISIBILITY_USER : $this::VISIBILITY_PUBLIC; + + //Prepare the request on the database + $conditions = "WHERE group_id = ? AND ("; + $dataConds = array($groupID); + + //Add the visibility level conditions + $conditions .= "(niveau_visibilite <= ?)"; + $dataConds[] = $visibilityLevel; + + //Close permissions conditions + $conditions .= ")"; + + //Add startpoint condition if required (and get older messages) + if($from != 0){ + $conditions .= " AND ID <= ? "; + $dataConds[] = $from; + } + + //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 and return posts + return $this->processGetMultiple($list, TRUE); + + } + /** * Get the list of latest posts for a user * @@ -406,7 +456,7 @@ class Posts { return $this::NO_ACCESS; //Check if the group is open or not - if(!components()->groups->is_open($post_info->get_group_id())) + if(!components()->groups->isOpen($post_info->get_group_id())) return $this::NO_ACCESS; // Post public + open group > basic access diff --git a/functions/requests.php b/functions/requests.php index 4c8bebf..f662c96 100644 --- a/functions/requests.php +++ b/functions/requests.php @@ -102,15 +102,19 @@ function postBool(string $name) : bool { * This function makes a REST_Error in case of error * * @param string $name The name of the $_POST field + * @param string $default The default value (null = none) * @return int The integer */ -function postInt(string $name) : int { +function postInt(string $name, string $default = null) : int { //Check the variable - if(!isset($_POST[$name])) + if(!isset($_POST[$name]) && $default == null) Rest_fatal_error(400, "Please add a POST integer named '".$name."' in the request !"); - return (int)$_POST[$name]; + if(isset($_POST[$name])) + return (int)$_POST[$name]; + else + return (int) $default; } /**