From 697a01b6df8a7f489683c48f6f298d5a7f472b9b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 9 Jul 2020 09:27:47 +0200 Subject: [PATCH] Can update the content of the posts --- src/controllers/posts_controller.rs | 12 ++++++++++++ src/controllers/routes.rs | 2 ++ src/data/http_request_handler.rs | 17 ++++++++++++++++- src/helpers/posts_helper.rs | 8 ++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 6355ef8..d35445e 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -247,4 +247,16 @@ pub fn set_visibility_level(r: &mut HttpRequestHandler) -> RequestResult { // TODO : Depending on new level, delete (or not) notifications about the post r.success("Visibility level updated") +} + +/// Update the content of a post +pub fn update_content(r: &mut HttpRequestHandler) -> RequestResult { + let post = r.post_post_with_access("postID", PostAccessLevel::FULL_ACCESS)?; + let new_content = r.post_content("new_content", 2, true)?; + + posts_helper::set_content(post.id, &new_content)?; + + // TODO : Delete the notifications targeting the current user about this post + + r.success("Content updated") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index fbe9276..92d5e43 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -210,6 +210,8 @@ pub fn get_routes() -> Vec { Route::post("/posts/set_visibility_level", Box::new(posts_controller::set_visibility_level)), + Route::post("/posts/update_content", Box::new(posts_controller::update_content)), + // Movies controller Route::post("/movies/get_list", Box::new(movies_controller::get_list)), diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index ca36e3d..2c9d90b 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -21,7 +21,7 @@ use crate::data::user::UserID; use crate::helpers::{account_helper, api_helper, conversations_helper, friends_helper, groups_helper, movies_helper, posts_helper, user_helper, virtual_directory_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; use crate::utils::pdf_utils::is_valid_pdf; -use crate::utils::string_utils::{check_url, remove_html_nodes}; +use crate::utils::string_utils::{check_string_before_insert, check_url, remove_html_nodes}; use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path}; use crate::utils::virtual_directories_utils::check_virtual_directory; @@ -595,4 +595,19 @@ impl HttpRequestHandler { Ok(movie_id) } + + /// Get a content of a post and satinize it + pub fn post_content(&mut self, name: &str, min_len: usize, required: bool) -> ResultBoxError { + let content = self.post_string_opt(name, min_len, required)?; + + if content.contains("data:image") { + self.forbidden("Please do not include inline images!".to_string())?; + } + + if !check_string_before_insert(&content) { + self.forbidden(format!("The content inside {} was rejected!", name))?; + } + + Ok(remove_html_nodes(&content)) + } } \ No newline at end of file diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 2c73fc2..bf3ff18 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -391,6 +391,14 @@ pub fn set_level(post_id: u64, level: PostVisibilityLevel) -> ResultBoxError { .exec() } +/// Set a new content to the post +pub fn set_content(post_id: u64, new_content: &str) -> ResultBoxError { + database::UpdateInfo::new(POSTS_TABLE) + .cond_u64("ID", post_id) + .set_str("texte", new_content) + .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 {