1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Can get group's posts

This commit is contained in:
Pierre HUBERT 2020-07-06 09:20:31 +02:00
parent bb76513d02
commit fca15e15e1
3 changed files with 58 additions and 0 deletions

View File

@ -4,6 +4,7 @@
use crate::api_data::post_api::PostAPI;
use crate::controllers::routes::RequestResult;
use crate::data::group::GroupAccessLevel;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::{posts_helper, user_helper};
@ -20,5 +21,17 @@ pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult {
.set_start_from(start_from)
.get_user(&user_id)?;
r.set_response(PostAPI::for_list(&posts, r.user_id_opt())?)
}
/// Get the list of posts of a group
pub fn get_list_group(r: &mut HttpRequestHandler) -> RequestResult {
let group_id = r.post_group_id_with_access("groupID", GroupAccessLevel::VIEW_ACCESS)?;
let start_from = r.post_u64_opt("startFrom", 0)?;
let posts = posts_helper::PostsQuery::new(r.user_id_opt())
.set_start_from(start_from)
.get_group(&group_id)?;
r.set_response(PostAPI::for_list(&posts, r.user_id_opt())?)
}

View File

@ -196,9 +196,12 @@ pub fn get_routes() -> Vec<Route> {
Route::post("/groups/delete", Box::new(groups_controller::delete_group)),
// Posts controller
Route::post("/posts/get_user", Box::new(posts_controller::get_list_user)),
Route::post("/posts/get_group", Box::new(posts_controller::get_list_group)),
// Virtual directory controller
Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),

View File

@ -2,8 +2,11 @@
//!
//! @author Pierre Hubert
use crate::constants::database_tables_names::POSTS_TABLE;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::group_id::GroupID;
use crate::data::group_member::GroupMembershipLevel;
use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink};
use crate::data::post::PostKind::{POST_KIND_COUNTDOWN, POST_KIND_IMAGE, POST_KIND_MOVIE, POST_KIND_PDF, POST_KIND_SURVEY, POST_KIND_WEBLINK, POST_KIND_YOUTUBE};
@ -70,6 +73,11 @@ impl PostsQuery {
pub fn get_user(self, user_id: &UserID) -> ResultBoxError<Vec<Post>> {
get_user(&self, user_id)
}
/// Get the posts of a group
pub fn get_group(self, group_id: &GroupID) -> ResultBoxError<Vec<Post>> {
get_group(&self, group_id)
}
}
/// Get the posts of `target_id`
@ -123,6 +131,40 @@ fn get_user(query: &PostsQuery, target_id: &UserID) -> ResultBoxError<Vec<Post>>
.exec(db_to_post)
}
/// Get the list of posts of a group
fn get_group(query: &PostsQuery, group_id: &GroupID) -> ResultBoxError<Vec<Post>> {
let membership = groups_helper::get_membership_level(group_id, query.user_id.clone())?;
let can_see_all_posts = membership <= GroupMembershipLevel::MEMBER;
let visibility_level = match can_see_all_posts {
true => PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS,
false => PostVisibilityLevel::VISIBILITY_FRIENDS
};
// Prepare request
let mut db_query = database::QueryInfo::new(POSTS_TABLE);
let mut custom_where = String::new();
// =============== VISIBILITY CONDITION ================
custom_where.push_str("(niveau_visibilite <= ?)");
db_query = db_query.add_custom_where_argument_u32(visibility_level.to_db());
// ============== /VISIBILITY CONDITION ================
// ================== START POINT ======================
if query.start_from > 0 {
custom_where.push_str(" AND ID <= ?");
db_query = db_query.add_custom_where_argument_u64(query.start_from);
}
// ================== /START POINT =====================
db_query
.cond_group_id("group_id", group_id)
.set_custom_where(&custom_where)
.set_order("ID DESC")
.set_limit(query.limit)
.exec(db_to_post)
}
/// Get the access level of a user over a post
pub fn get_access_level(p: &Post, user_id: &Option<UserID>) -> ResultBoxError<PostAccessLevel> {
if user_id == &p.user_id.as_option() {