diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 802a282..8bd1178 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -114,7 +114,7 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { time_create: time(), target_page, content: Some(r.post_string_opt("content", 0, false)?), - visibility: PostVisibilityLevel::VISIBILITY_PUBLIC, + visibility: PostVisibilityLevel::VISIBILITY_PUBLIC, // TODO : fix kind: PostKind::POST_KIND_TEXT, }; @@ -235,4 +235,16 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { // TODO : create a notification r.set_response(ResCreatePost::new(post_id)) +} + +/// Change the visibility level of a post +pub fn set_visibility_level(r: &mut HttpRequestHandler) -> RequestResult { + let post = r.post_post_with_access("postID", PostAccessLevel::FULL_ACCESS)?; + let new_visibility = PostVisibilityLevel::from_api(&r.post_string("new_level")?); + + posts_helper::set_level(post.id, new_visibility)?; + + // TODO : Depending on new level, delete (or not) notifications about the post + + r.success("Visibility level updated") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index c6c0f1b..fbe9276 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -208,6 +208,8 @@ pub fn get_routes() -> Vec { Route::post("/posts/create", Box::new(posts_controller::create_post)), + Route::post("/posts/set_visibility_level", Box::new(posts_controller::set_visibility_level)), + // Movies controller Route::post("/movies/get_list", Box::new(movies_controller::get_list)), diff --git a/src/data/post.rs b/src/data/post.rs index 9bff65e..a94ed4b 100644 --- a/src/data/post.rs +++ b/src/data/post.rs @@ -30,6 +30,16 @@ impl PostVisibilityLevel { PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS => "members", }.to_string() } + + pub fn from_api(level: &str) -> PostVisibilityLevel { + match level { + "private" => PostVisibilityLevel::VISIBILITY_USER, + "friends" => PostVisibilityLevel::VISIBILITY_FRIENDS, + "members" => PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS, + "public" => PostVisibilityLevel::VISIBILITY_PUBLIC, + _ => PostVisibilityLevel::VISIBILITY_PUBLIC, + } + } } /// Post access level (for a given user) diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 9bd0ad6..2c73fc2 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -383,6 +383,14 @@ pub fn allow_comments_on_post(p: &Post) -> ResultBoxError { user_helper::allow_comments(p.user_page_id().unwrap_or(&UserID::invalid()))?) } +/// Set a new visibility level to a post +pub fn set_level(post_id: u64, level: PostVisibilityLevel) -> ResultBoxError { + database::UpdateInfo::new(POSTS_TABLE) + .cond_u64("ID", post_id) + .set_u32("niveau_visibilite", level.to_db()) + .exec() +} + /// Turn a post into a database entry fn db_to_post(res: &database::RowResult) -> ResultBoxError { let user_id = if res.get_u64("ID_amis")? == 0 {