From 66ad11171943970380e377643d88af29fb36e7d2 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 14 Jul 2020 13:33:17 +0200 Subject: [PATCH] Can set language settings --- src/constants.rs | 5 ++++- src/controllers/routes.rs | 1 + src/controllers/settings_controller.rs | 20 ++++++++++++++++++++ src/data/lang_settings.rs | 10 ++++++++++ src/data/mod.rs | 3 ++- src/helpers/account_helper.rs | 9 +++++++++ 6 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/data/lang_settings.rs diff --git a/src/constants.rs b/src/constants.rs index 53d7f54..5b10be6 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -91,4 +91,7 @@ pub const PASSWORD_RESET_TOKEN_LENGTH: usize = 255; pub const PASSWORD_RESET_TOKEN_LIFETIME: u64 = 60 * 60 * 6; /// Minimum password length -pub const PASSWORD_MIN_LENGTH: usize = 3; \ No newline at end of file +pub const PASSWORD_MIN_LENGTH: usize = 3; + +/// Supported languages (for ComunicWeb) +pub const SUPPORTED_LANGUAGES: &'static [&'static str] = &["en", "fr"]; \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 76e4ee0..05038d5 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -97,6 +97,7 @@ pub fn get_routes() -> Vec { Route::post("/settings/set_general", Box::new(settings_controller::set_general)), Route::post("/settings/check_user_directory_availability", Box::new(settings_controller::check_virtual_directory)), Route::post("/settings/get_language", Box::new(settings_controller::get_language)), + Route::post("/settings/set_language", Box::new(settings_controller::set_language)), // Friends controller Route::post("/friends/getList", Box::new(friends_controller::get_list)), diff --git a/src/controllers/settings_controller.rs b/src/controllers/settings_controller.rs index 7f6dfef..baf7a65 100644 --- a/src/controllers/settings_controller.rs +++ b/src/controllers/settings_controller.rs @@ -4,9 +4,11 @@ use crate::api_data::general_settings_api::GeneralSettingsAPI; use crate::api_data::language_settings_api::LanguageSettingsAPI; +use crate::constants::SUPPORTED_LANGUAGES; use crate::controllers::routes::RequestResult; use crate::data::general_settings::GeneralSettings; use crate::data::http_request_handler::HttpRequestHandler; +use crate::data::lang_settings::LangSettings; use crate::data::user::UserPageStatus; use crate::helpers::{account_helper, user_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; @@ -63,4 +65,22 @@ 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.") } \ No newline at end of file diff --git a/src/data/lang_settings.rs b/src/data/lang_settings.rs new file mode 100644 index 0000000..8548d64 --- /dev/null +++ b/src/data/lang_settings.rs @@ -0,0 +1,10 @@ +//! # Language settings +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; + +pub struct LangSettings { + pub id: UserID, + pub lang: String, +} \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 379a0dc..00bf773 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -30,4 +30,5 @@ pub mod new_account; pub mod account_export; pub mod user_like; pub mod survey_response; -pub mod general_settings; \ No newline at end of file +pub mod general_settings; +pub mod lang_settings; \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 216023c..7c9e749 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -4,6 +4,7 @@ use crate::data::account_export::AccountExport; use crate::data::api_client::APIClient; 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::user_token::UserAccessToken; @@ -211,6 +212,14 @@ pub fn set_general(settings: &GeneralSettings) -> ResultBoxError { .exec() } +/// Set new language settings +pub fn set_language_settings(settings: &LangSettings) -> ResultBoxError { + database::UpdateInfo::new(USERS_TABLE) + .cond_user_id("ID", &settings.id) + .set_str("lang", &settings.lang) + .exec() +} + /// Export an account's data pub fn export(user_id: &UserID) -> ResultBoxError { let mut data = AccountExport {