1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-08 11:42:35 +00:00
comunicapiv3/src/controllers/settings_controller.rs

96 lines
3.3 KiB
Rust
Raw Normal View History

2020-07-14 09:15:20 +00:00
//! # Settings controller
//!
//! @author Pierre Hubert
use crate::api_data::general_settings_api::GeneralSettingsAPI;
2020-07-14 11:16:52 +00:00
use crate::api_data::language_settings_api::LanguageSettingsAPI;
2021-01-19 16:27:54 +00:00
use crate::api_data::security_settings_api::SecuritySettingsAPI;
2020-07-14 11:33:17 +00:00
use crate::constants::SUPPORTED_LANGUAGES;
2020-07-14 09:15:20 +00:00
use crate::controllers::routes::RequestResult;
2020-07-14 09:36:15 +00:00
use crate::data::general_settings::GeneralSettings;
2020-07-14 09:15:20 +00:00
use crate::data::http_request_handler::HttpRequestHandler;
2020-07-14 11:33:17 +00:00
use crate::data::lang_settings::LangSettings;
2020-07-14 09:36:15 +00:00
use crate::data::user::UserPageStatus;
use crate::helpers::{account_helper, user_helper};
use crate::helpers::virtual_directory_helper::VirtualDirType;
2020-07-14 09:15:20 +00:00
/// 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))
2020-07-14 09:36:15 +00:00
}
/// 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!")
2020-07-14 11:16:52 +00:00
}
/// Get the current language of the user
pub fn get_language(r: &mut HttpRequestHandler) -> RequestResult {
let user = user_helper::find_user_by_id(r.user_id_ref()?)?;
r.set_response(LanguageSettingsAPI::new(&user))
2020-07-14 11:33:17 +00:00
}
/// Set the current language of the user
pub fn set_language(r: &mut HttpRequestHandler) -> RequestResult {
let lang = r.post_string("lang")?;
if !SUPPORTED_LANGUAGES.contains(&lang.as_str()) {
r.forbidden("Language not supported!".to_string())?;
}
let new_settings = LangSettings {
id: r.user_id()?,
lang,
};
account_helper::set_language_settings(&new_settings)?;
r.success("Language settings updated.")
2021-01-19 16:27:54 +00:00
}
/// Get security settings
pub fn get_security(r: &mut HttpRequestHandler) -> RequestResult {
r.need_user_password("password")?;
let user = user_helper::find_user_by_id(r.user_id_ref()?)?;
r.set_response(SecuritySettingsAPI::new(&user))
2020-07-14 09:15:20 +00:00
}