From 364a98634fb4be1784217d0a1ec2ce776c145256 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 19 Jan 2021 18:14:17 +0100 Subject: [PATCH] Can get account image settings --- src/api_data/account_image_settings_api.rs | 29 ++++++++++++++++++++++ src/api_data/mod.rs | 3 ++- src/controllers/routes.rs | 1 + src/controllers/settings_controller.rs | 8 ++++++ src/data/user.rs | 10 ++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/api_data/account_image_settings_api.rs diff --git a/src/api_data/account_image_settings_api.rs b/src/api_data/account_image_settings_api.rs new file mode 100644 index 0000000..89609d1 --- /dev/null +++ b/src/api_data/account_image_settings_api.rs @@ -0,0 +1,29 @@ +//! # Account image settings API + +use serde::Serialize; + +use crate::constants::DEFAULT_ACCOUNT_IMAGE; +use crate::data::user::User; +use crate::utils::user_data_utils::user_data_url; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct AccountImageSettingsAPI +{ + has_image: bool, + image_url: String, + visibility: String, +} + +impl AccountImageSettingsAPI { + pub fn new(user: &User) -> AccountImageSettingsAPI + { + AccountImageSettingsAPI + { + has_image: user.has_account_image(), + image_url: user_data_url(user.account_image_path + .as_ref().unwrap_or(&DEFAULT_ACCOUNT_IMAGE.to_string())), + visibility: user.account_image_visibility.to_api(), + } + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 490dca5..ed1e4f7 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -53,4 +53,5 @@ pub mod survey_response_api; pub mod entities_constructor; pub mod general_settings_api; pub mod language_settings_api; -pub mod security_settings_api; \ No newline at end of file +pub mod security_settings_api; +pub mod account_image_settings_api; \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index eaafbb2..51fe7e7 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -102,6 +102,7 @@ pub fn get_routes() -> Vec { Route::post("/settings/set_security", Box::new(settings_controller::set_security)), Route::post("/settings/check_password", Box::new(settings_controller::check_password)), Route::post("/settings/update_password", Box::new(settings_controller::update_password)), + Route::post("/settings/get_account_image", Box::new(settings_controller::get_account_image_settings)), // Friends controller diff --git a/src/controllers/settings_controller.rs b/src/controllers/settings_controller.rs index b1e31d9..1fdda4b 100644 --- a/src/controllers/settings_controller.rs +++ b/src/controllers/settings_controller.rs @@ -14,6 +14,7 @@ use crate::data::security_settings::{SecurityQuestion, SecuritySettings}; use crate::data::user::UserPageStatus; use crate::helpers::{account_helper, user_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; +use crate::api_data::account_image_settings_api::AccountImageSettingsAPI; /// Get the general settings of the user pub fn get_general(r: &mut HttpRequestHandler) -> RequestResult { @@ -134,4 +135,11 @@ pub fn update_password(r: &mut HttpRequestHandler) -> RequestResult { 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)) } \ No newline at end of file diff --git a/src/data/user.rs b/src/data/user.rs index e66c200..d293613 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -59,6 +59,16 @@ pub enum AccountImageVisibility { EVERYONE, } +impl AccountImageVisibility { + pub fn to_api(&self) -> String { + match self { + AccountImageVisibility::FRIENDS => "friends".to_string(), + AccountImageVisibility::COMUNIC_USERS => "public".to_string(), + AccountImageVisibility::EVERYONE => "open".to_string(), + } + } +} + #[derive(Debug)] pub struct User { pub id: UserID,