diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index efed024..969708f 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -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())?) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 831682a..2f20a62 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -196,9 +196,12 @@ pub fn get_routes() -> Vec { 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)), diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 5a56135..3e40019 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -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> { get_user(&self, user_id) } + + /// Get the posts of a group + pub fn get_group(self, group_id: &GroupID) -> ResultBoxError> { + get_group(&self, group_id) + } } /// Get the posts of `target_id` @@ -123,6 +131,40 @@ fn get_user(query: &PostsQuery, target_id: &UserID) -> ResultBoxError> .exec(db_to_post) } +/// Get the list of posts of a group +fn get_group(query: &PostsQuery, group_id: &GroupID) -> ResultBoxError> { + 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) -> ResultBoxError { if user_id == &p.user_id.as_option() {