From 1fd3a8e6d982351de3b3dc8ca875f4a5ae1f68b4 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 19 Jan 2021 18:48:56 +0100 Subject: [PATCH] Can update account image visibility --- src/controllers/routes.rs | 1 + src/controllers/settings_controller.rs | 11 ++++++++++- src/data/user.rs | 16 ++++++++++++++++ src/helpers/account_helper.rs | 12 ++++++++++-- src/helpers/user_helper.rs | 3 +-- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 9668cf1..108a178 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -105,6 +105,7 @@ pub fn get_routes() -> Vec { Route::post("/settings/get_account_image", Box::new(settings_controller::get_account_image_settings)), Route::post("/settings/upload_account_image", Box::new(settings_controller::upload_account_image)), Route::post("/settings/delete_account_image", Box::new(settings_controller::delete_account_image)), + Route::post("/settings/set_account_image_visibility", Box::new(settings_controller::set_account_image_visibility)), // Friends controller diff --git a/src/controllers/settings_controller.rs b/src/controllers/settings_controller.rs index 8df34a2..ac1c820 100644 --- a/src/controllers/settings_controller.rs +++ b/src/controllers/settings_controller.rs @@ -12,7 +12,7 @@ use crate::data::general_settings::GeneralSettings; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::lang_settings::LangSettings; use crate::data::security_settings::{SecurityQuestion, SecuritySettings}; -use crate::data::user::UserPageStatus; +use crate::data::user::{AccountImageVisibility, UserPageStatus}; use crate::helpers::{account_helper, user_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; @@ -162,4 +162,13 @@ 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("Image image visibility level updated!") } \ No newline at end of file diff --git a/src/data/user.rs b/src/data/user.rs index d293613..5cbce0a 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -67,6 +67,22 @@ impl AccountImageVisibility { AccountImageVisibility::EVERYONE => "open".to_string(), } } + + pub fn from_api(s: &str) -> Self { + match s { + "friends" => AccountImageVisibility::FRIENDS, + "public" => AccountImageVisibility::COMUNIC_USERS, + "open" | _ => AccountImageVisibility::EVERYONE, + } + } + + pub fn to_db(&self) -> String { + match self { + AccountImageVisibility::FRIENDS => "friends".to_string(), + AccountImageVisibility::COMUNIC_USERS => "comunic_users".to_string(), + AccountImageVisibility::EVERYONE => "everyone".to_string(), + } + } } #[derive(Debug)] diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 2e1cbae..d029184 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -6,13 +6,13 @@ use crate::data::error::{ExecError, ResultBoxError}; use crate::data::general_settings::GeneralSettings; use crate::data::lang_settings::LangSettings; use crate::data::new_account::NewAccount; -use crate::data::user::{UserID, UserPageStatus}; +use crate::data::security_settings::SecuritySettings; +use crate::data::user::{AccountImageVisibility, 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}; use crate::utils::crypt_utils::{crypt_pass, rand_str}; use crate::utils::date_utils::{mysql_date, time}; -use crate::data::security_settings::SecuritySettings; use crate::utils::user_data_utils::user_data_path; /// Account helper @@ -266,6 +266,14 @@ pub fn set_account_image(user_id: &UserID, uri: &String) -> ResultBoxError { .exec() } +/// Set account image visibility level +pub fn set_account_image_visibility(user_id: &UserID, level: AccountImageVisibility) -> ResultBoxError { + database::UpdateInfo::new(USERS_TABLE) + .cond_user_id("ID", user_id) + .set_str("account_image_visibility", &level.to_db()) + .exec() +} + /// Export an account's data pub fn export(user_id: &UserID) -> ResultBoxError { let mut data = AccountExport { diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index dd32c8a..9519b7d 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -44,8 +44,7 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { let account_image_visibility = match res.get_str("account_image_visibility")?.as_ref() { "friends" => AccountImageVisibility::FRIENDS, "comunic_users" => AccountImageVisibility::COMUNIC_USERS, - "everyone" => AccountImageVisibility::EVERYONE, - _ => unreachable!() + "everyone" | _ => AccountImageVisibility::EVERYONE, }; Ok(User {