//! # Settings controller //! //! @author Pierre Hubert 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::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") } /// Check the availability of a virtual directory pub fn check_virtual_directory(r: &mut HttpRequestHandler) -> RequestResult { // We can do this check just by getting it, there is no need to store it in a variable r.post_checked_virtual_directory_opt("directory", r.user_id()?.id(), VirtualDirType::USER)?; r.success("The directory is available!") }