From 1850ca0626de0487c3c83064a5cfcce37260da07 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 14 Jul 2020 11:36:15 +0200 Subject: [PATCH] Can set general settings --- src/controllers/routes.rs | 1 + src/controllers/settings_controller.rs | 37 +++++++++++++++++++++++++- src/data/general_settings.rs | 19 +++++++++++++ src/data/http_request_handler.rs | 6 ++--- src/data/mod.rs | 3 ++- src/helpers/account_helper.rs | 22 ++++++++++++++- 6 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 src/data/general_settings.rs diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 19bbee9..1572d3c 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -94,6 +94,7 @@ pub fn get_routes() -> Vec { // Settings controller Route::post("/settings/get_general", Box::new(settings_controller::get_general)), + Route::post("/settings/set_general", Box::new(settings_controller::set_general)), // Friends controller Route::post("/friends/getList", Box::new(friends_controller::get_list)), diff --git a/src/controllers/settings_controller.rs b/src/controllers/settings_controller.rs index af16425..abfcaf3 100644 --- a/src/controllers/settings_controller.rs +++ b/src/controllers/settings_controller.rs @@ -4,12 +4,47 @@ use crate::api_data::general_settings_api::GeneralSettingsAPI; use crate::controllers::routes::RequestResult; +use crate::data::general_settings::GeneralSettings; use crate::data::http_request_handler::HttpRequestHandler; -use crate::helpers::user_helper; +use crate::data::user::UserPageStatus; +use crate::helpers::{account_helper, user_helper}; +use crate::helpers::virtual_directory_helper::VirtualDirType; /// Get the general settings of the user pub fn get_general(r: &mut HttpRequestHandler) -> RequestResult { let user = user_helper::find_user_by_id(r.user_id_ref()?)?; r.set_response(GeneralSettingsAPI::new(&user)) +} + +/// Set the general settings of the user +pub fn set_general(r: &mut HttpRequestHandler) -> RequestResult { + let page_status = match (r.post_bool("isPublic")?, r.post_bool("isOpen")?) { + (true, true) => UserPageStatus::OPEN, + (true, false) => UserPageStatus::PUBLIC, + (_, _) => UserPageStatus::PRIVATE, + }; + + let personal_website = r.post_url_opt("personnalWebsite", false)?; + + let virtual_directory = r.post_checked_virtual_directory_opt( + "virtualDirectory", r.user_id_ref()?.id(), VirtualDirType::USER)?; + + let new_settings = GeneralSettings { + id: r.user_id()?, + first_name: r.post_content("firstName", 3, true)?, + last_name: r.post_content("lastName", 3, true)?, + page_status, + block_comments: !r.post_bool("allowComments")?, + allow_posts_from_friends: r.post_bool("allowPostsFromFriends")?, + friends_list_public: r.post_bool("publicFriendsList")?, + personal_website, + virtual_directory, + allow_mails: r.post_bool("allow_comunic_mails")?, + public_note: Some(r.post_content("publicNote", 0, false)?), + }; + + account_helper::set_general(&new_settings)?; + + r.success("Settings updated") } \ No newline at end of file diff --git a/src/data/general_settings.rs b/src/data/general_settings.rs new file mode 100644 index 0000000..58ae2a9 --- /dev/null +++ b/src/data/general_settings.rs @@ -0,0 +1,19 @@ +//! # General settings +//! +//! @author Pierre Hubert + +use crate::data::user::{UserID, UserPageStatus}; + +pub struct GeneralSettings { + pub id: UserID, + pub first_name: String, + pub last_name: String, + pub page_status: UserPageStatus, + pub block_comments: bool, + pub allow_posts_from_friends: bool, + pub friends_list_public: bool, + pub personal_website: Option, + pub virtual_directory: Option, + pub allow_mails: bool, + pub public_note: Option, +} \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 2f6753b..5329fc3 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -10,6 +10,7 @@ use image::{GenericImageView, ImageFormat}; use serde::Serialize; use crate::api_data::http_error::HttpError; +use crate::constants::PASSWORD_MIN_LENGTH; use crate::controllers::routes::RequestResult; use crate::data::api_client::APIClient; use crate::data::comment::Comment; @@ -25,7 +26,6 @@ use crate::utils::pdf_utils::is_valid_pdf; 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; -use crate::constants::PASSWORD_MIN_LENGTH; /// Http request handler /// @@ -654,7 +654,7 @@ impl HttpRequestHandler { self.forbidden("Please do not include inline images!".to_string())?; } - if !check_string_before_insert(&content) { + if min_len > 0 && required && !check_string_before_insert(&content) { self.forbidden(format!("The content inside {} was rejected!", name))?; } @@ -662,7 +662,7 @@ impl HttpRequestHandler { } /// Check the password of the current user - pub fn need_user_password(&mut self, field: &str) ->ResultBoxError { + pub fn need_user_password(&mut self, field: &str) -> ResultBoxError { let password = self.post_string_opt(field, PASSWORD_MIN_LENGTH, true)?; if !account_helper::check_user_password(self.user_id_ref()?, &password)? { diff --git a/src/data/mod.rs b/src/data/mod.rs index 712f53e..379a0dc 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -29,4 +29,5 @@ pub mod user_membership; pub mod new_account; pub mod account_export; pub mod user_like; -pub mod survey_response; \ No newline at end of file +pub mod survey_response; +pub mod general_settings; \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index e668631..216023c 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -3,8 +3,9 @@ use crate::constants::database_tables_names::{USER_ACCESS_TOKENS_TABLE, USERS_TA use crate::data::account_export::AccountExport; use crate::data::api_client::APIClient; use crate::data::error::{ExecError, ResultBoxError}; +use crate::data::general_settings::GeneralSettings; use crate::data::new_account::NewAccount; -use crate::data::user::UserID; +use crate::data::user::{UserID, UserPageStatus}; use crate::data::user_token::UserAccessToken; use crate::helpers::{comments_helper, conversations_helper, database, friends_helper, groups_helper, likes_helper, movies_helper, posts_helper, survey_helper, user_helper}; use crate::helpers::database::{DeleteQuery, InsertQuery, QueryInfo}; @@ -191,6 +192,25 @@ pub fn update_last_activity(user_id: &UserID) -> ResultBoxError { .exec() } + +/// Set new general settings of an account +pub fn set_general(settings: &GeneralSettings) -> ResultBoxError { + database::UpdateInfo::new(USERS_TABLE) + .cond_user_id("ID", &settings.id) + .set_str("prenom", &settings.first_name) + .set_str("nom", &settings.last_name) + .set_legacy_bool("public", settings.page_status != UserPageStatus::PRIVATE) + .set_legacy_bool("pageouverte", settings.page_status == UserPageStatus::OPEN) + .set_legacy_bool("bloquecommentaire", settings.block_comments) + .set_legacy_bool("autoriser_post_amis", settings.allow_posts_from_friends) + .set_legacy_bool("autorise_mail", settings.allow_mails) + .set_legacy_bool("liste_amis_publique", settings.friends_list_public) + .set_opt_str("sous_repertoire", settings.virtual_directory.clone()) + .set_opt_str("site_web", settings.personal_website.clone()) + .set_opt_str("public_note", settings.public_note.clone()) + .exec() +} + /// Export an account's data pub fn export(user_id: &UserID) -> ResultBoxError { let mut data = AccountExport {