From 05b743fa3630115768f4af70b077ae0bd5b105d6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 9 Jul 2020 13:26:39 +0200 Subject: [PATCH] Can get the list of available target to create new posts --- src/api_data/mod.rs | 3 ++- src/api_data/posts_targets_api.rs | 22 ++++++++++++++++++++++ src/controllers/posts_controller.rs | 11 ++++++++++- src/controllers/routes.rs | 2 ++ src/helpers/friends_helper.rs | 9 +++++++++ src/helpers/groups_helper.rs | 19 +++++++++++++++++++ 6 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/api_data/posts_targets_api.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 90fd5d9..0c5a983 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -35,4 +35,5 @@ pub mod movie_api; pub mod survey_choice_api; pub mod survey_api; pub mod comment_api; -pub mod res_create_post; \ No newline at end of file +pub mod res_create_post; +pub mod posts_targets_api; \ No newline at end of file diff --git a/src/api_data/posts_targets_api.rs b/src/api_data/posts_targets_api.rs new file mode 100644 index 0000000..96c7003 --- /dev/null +++ b/src/api_data/posts_targets_api.rs @@ -0,0 +1,22 @@ +//! # Posts targets +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::data::group_id::GroupID; +use crate::data::user::UserID; + +#[derive(Serialize)] +pub struct PostsTargets { + friends: Vec, + groups: Vec, +} + +impl PostsTargets { + pub fn new(f: &Vec, g: &Vec) -> PostsTargets { + PostsTargets { + friends: f.iter().map(|f| f.id()).collect(), + groups: g.iter().map(|f| f.id()).collect(), + } + } +} \ No newline at end of file diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 6d34043..a0d17e9 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -3,6 +3,7 @@ //! @author Pierre Hubert use crate::api_data::post_api::PostAPI; +use crate::api_data::posts_targets_api::PostsTargets; use crate::api_data::res_create_post::ResCreatePost; use crate::constants::{PATH_POST_IMAGES, PATH_POST_PDF}; use crate::controllers::routes::RequestResult; @@ -11,7 +12,7 @@ use crate::data::group::GroupAccessLevel; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::new_survey::NewSurvey; use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink}; -use crate::helpers::{groups_helper, posts_helper, survey_helper, user_helper}; +use crate::helpers::{friends_helper, groups_helper, posts_helper, survey_helper, user_helper}; use crate::utils::date_utils::time; use crate::utils::string_utils::{check_string_before_insert, check_youtube_id}; use crate::utils::user_data_utils::user_data_path; @@ -268,4 +269,12 @@ pub fn delete(r: &mut HttpRequestHandler) -> RequestResult { posts_helper::delete(&post)?; r.success("Post deleted.") +} + +/// Get the list of targets where the current user can create posts +pub fn get_targets(r: &mut HttpRequestHandler) -> RequestResult { + let friends = friends_helper::get_list_that_allow_posts_from_user(r.user_id_ref()?)?; + let groups = groups_helper::get_list_where_user_can_create_posts(r.user_id_ref()?)?; + + r.set_response(PostsTargets::new(&friends, &groups)) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index fabc78b..8c79ff9 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -214,6 +214,8 @@ pub fn get_routes() -> Vec { Route::post("/posts/delete", Box::new(posts_controller::delete)), + Route::post("/posts/getAvailableTargets", Box::new(posts_controller::get_targets)), + // Movies controller Route::post("/movies/get_list", Box::new(movies_controller::get_list)), diff --git a/src/helpers/friends_helper.rs b/src/helpers/friends_helper.rs index c809533..ad256e5 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -83,6 +83,15 @@ fn get_list(friend_query: &GetFriendsQuery) -> ResultBoxError> { query.exec(db_to_friend) } +/// get the list of friends that allows a given user to create posts on their page +pub fn get_list_that_allow_posts_from_user(user_id: &UserID) -> ResultBoxError> { + database::QueryInfo::new(FRIENDS_TABLE) + .cond_legacy_bool("autoriser_post_page", true) + .cond_user_id("ID_amis", user_id) + .add_field("ID_personne as user_id") + .exec(|r| r.get_user_id("user_id")) +} + /// Send a new friendship request pub fn send_request(user_id: &UserID, target_user: &UserID) -> ResultBoxError { database::InsertQuery::new(FRIENDS_TABLE) diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 04fecfd..a2e7b90 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -164,6 +164,25 @@ pub fn get_list_user(user_id: &UserID, only_followed: bool) -> ResultBoxError ResultBoxError> { + database::QueryInfo::new(GROUPS_MEMBERS_TABLE) + .alias("m") + .join(GROUPS_LIST_TABLE, "g", "m.groups_id = g.id") + + .cond_user_id("user_id", user_id) + + .set_custom_where("level = ? OR level = ? OR (level = ? AND posts_level = ?)") + .add_custom_where_argument_u32(GroupMembershipLevel::ADMINISTRATOR.to_db()) + .add_custom_where_argument_u32(GroupMembershipLevel::MODERATOR.to_db()) + .add_custom_where_argument_u32(GroupMembershipLevel::MEMBER.to_db()) + .add_custom_where_argument_u32(GroupPostsCreationLevel::POSTS_LEVEL_ALL_MEMBERS.to_db()) + + .add_field("g.id") + .exec(|r| r.get_group_id("id")) +} + /// Get information about a group pub fn get_info(group_id: &GroupID) -> ResultBoxError { database::QueryInfo::new(GROUPS_LIST_TABLE)