1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-07 11:22:36 +00:00
comunicapiv3/src/controllers/settings_controller.rs

205 lines
6.9 KiB
Rust

//! # Settings controller
//!
//! @author Pierre Hubert
use crate::api_data::account_image_settings_api::AccountImageSettingsAPI;
use crate::api_data::general_settings_api::GeneralSettingsAPI;
use crate::api_data::language_settings_api::LanguageSettingsAPI;
use crate::api_data::res_create_custom_emoji::ResCreateCustomEmoji;
use crate::api_data::security_settings_api::SecuritySettingsAPI;
use crate::constants::SUPPORTED_LANGUAGES;
use crate::controllers::routes::RequestResult;
use crate::data::base_request_handler::BaseRequestHandler;
use crate::data::general_settings::GeneralSettings;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::data::lang_settings::LangSettings;
use crate::data::new_custom_emoji::NewCustomEmoji;
use crate::data::security_settings::{SecurityQuestion, SecuritySettings};
use crate::data::user::{AccountImageVisibility, UserPageStatus};
use crate::helpers::{account_helper, custom_emojies_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!")
}
/// 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))
}
/// 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.")
}
/// 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))
}
/// Set security settings
pub fn set_security(r: &mut HttpRequestHandler) -> RequestResult {
r.need_user_password("password")?;
let new_settings = SecuritySettings {
id: r.user_id()?,
question1: SecurityQuestion::new(
&Option::Some(r.post_content("security_question_1", 0, false)?),
&Option::Some(r.post_content("security_answer_1", 0, false)?),
),
question2: SecurityQuestion::new(
&Option::Some(r.post_content("security_question_2", 0, false)?),
&Option::Some(r.post_content("security_answer_2", 0, false)?),
),
};
account_helper::set_security_settings(&new_settings)?;
r.success("Security settings update.")
}
/// Check user password
pub fn check_password(r: &mut HttpRequestHandler) -> RequestResult {
r.need_user_password("password")?;
r.success("The password is valid.")
}
/// Update user password
pub fn update_password(r: &mut HttpRequestHandler) -> RequestResult {
r.need_user_password("oldPassword")?;
let new_password = r.post_string("newPassword")?;
account_helper::change_password(r.user_id_ref()?, &new_password)?;
r.success("Password updated !")
}
/// Get account image settings
pub fn get_account_image_settings(r: &mut HttpRequestHandler) -> RequestResult {
let user = user_helper::find_user_by_id(r.user_id_ref()?)?;
r.set_response(AccountImageSettingsAPI::new(&user))
}
/// Upload a new account image
pub fn upload_account_image(r: &mut HttpRequestHandler) -> RequestResult {
if !r.has_file("picture") {
return r.bad_request("An error occurred while receiving the image !".to_string());
}
let uri = r.save_post_image("picture", "avatars", 800, 800)?;
account_helper::set_account_image(r.user_id_ref()?, &uri)?;
r.success("Account image updated!")
}
/// Delete user account image
pub fn delete_account_image(r: &mut HttpRequestHandler) -> RequestResult {
account_helper::delete_account_image(r.user_id_ref()?)?;
r.success("Account image deleted!")
}
/// Change account image visibility
pub fn set_account_image_visibility(r: &mut HttpRequestHandler) -> RequestResult {
let level = AccountImageVisibility::from_api(&r.post_string("visibility")?);
account_helper::set_account_image_visibility(r.user_id_ref()?, level)?;
r.success("Account image visibility level updated!")
}
/// Upload a custom emoji
pub fn upload_custom_emoji(r: &mut HttpRequestHandler) -> RequestResult {
let shortcut = r.post_emoji_shortcut("shortcut")?;
if custom_emojies_helper::has_user_similar_shortcut(r.user_id_ref()?, &shortcut)? {
r.forbidden("A custom emoji with the same shortcut is already defined!".to_string())?;
}
let path = r.save_post_image("image", "custom_emojies", 72, 72)?;
let emoji_id = custom_emojies_helper::insert(&NewCustomEmoji {
user_id: r.user_id()?,
shortcut,
path,
})?;
r.set_response(ResCreateCustomEmoji::new(emoji_id))
}
/// Delete custom emoji
pub fn delete_custom_emoji(r: &mut HttpRequestHandler) -> RequestResult {
let emoji = r.post_emoji_id("emojiID")?;
custom_emojies_helper::delete(&emoji)?;
r.success("Emoji deleted.")
}