Can get the posts of a group and returns them to the API

This commit is contained in:
Pierre HUBERT 2018-07-16 15:06:23 +02:00
parent cd772c03c3
commit 2d820403f5
3 changed files with 83 additions and 4 deletions

View File

@ -56,6 +56,31 @@ class PostsController {
return $this->parsePostsList($posts); 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 * Get the latest posts for the user
* *

View File

@ -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 * Get the list of latest posts for a user
* *
@ -406,7 +456,7 @@ class Posts {
return $this::NO_ACCESS; return $this::NO_ACCESS;
//Check if the group is open or not //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; return $this::NO_ACCESS;
// Post public + open group > basic access // Post public + open group > basic access

View File

@ -102,15 +102,19 @@ function postBool(string $name) : bool {
* This function makes a REST_Error in case of error * This function makes a REST_Error in case of error
* *
* @param string $name The name of the $_POST field * @param string $name The name of the $_POST field
* @param string $default The default value (null = none)
* @return int The integer * @return int The integer
*/ */
function postInt(string $name) : int { function postInt(string $name, string $default = null) : int {
//Check the variable //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 !"); 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;
} }
/** /**