diff --git a/src/api_data/general_settings_api.rs b/src/api_data/general_settings_api.rs new file mode 100644 index 0000000..65badeb --- /dev/null +++ b/src/api_data/general_settings_api.rs @@ -0,0 +1,45 @@ +//! # General settings API +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::data::user::User; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct GeneralSettingsAPI { + id: u64, + email: String, + firstName: String, + lastName: String, + is_public: bool, + is_open: bool, + allow_comments: bool, + allow_posts_from_friends: bool, + allow_comunic_mails: bool, + public_friends_list: bool, + virtual_directory: String, + personnal_website: String, + publicNote: String, +} + +impl GeneralSettingsAPI { + pub fn new(user: &User) -> GeneralSettingsAPI { + GeneralSettingsAPI { + id: user.id.id(), + email: user.email.clone(), + firstName: user.first_name.clone(), + lastName: user.last_name.clone(), + is_public: user.is_page_public(), + is_open: user.is_page_open(), + allow_comments: !user.block_comments_on_his_page, + allow_posts_from_friends: user.allow_posts_from_friends, + allow_comunic_mails: user.allow_mails, + public_friends_list: user.public_friends_list, + virtual_directory: user.virtual_directory.clone().unwrap_or(String::new()), + personnal_website: user.personal_website.clone().unwrap_or(String::new()), + publicNote: user.public_note.clone().unwrap_or(String::new()), + } + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index fc7039d..a84f2d7 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -50,4 +50,5 @@ pub mod res_check_security_answers; pub mod account_export_api; pub mod user_like_api; pub mod survey_response_api; -pub mod entities_constructor; \ No newline at end of file +pub mod entities_constructor; +pub mod general_settings_api; \ No newline at end of file diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index 3685e89..168302d 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -4,6 +4,7 @@ pub mod server; pub mod server_controller; pub mod account_controller; pub mod user_controller; +pub mod settings_controller; pub mod friends_controller; pub mod conversations_controller; pub mod search_controller; diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 8b9fba9..19bbee9 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -1,6 +1,6 @@ use std::error::Error; -use crate::controllers::{account_controller, calls_controller, comments_controller, conversations_controller, friends_controller, groups_controller, likes_controller, movies_controller, notifications_controller, posts_controller, search_controller, server_controller, surveys_controller, user_controller, virtual_directory_controller, web_app_controller}; +use crate::controllers::{account_controller, calls_controller, comments_controller, conversations_controller, friends_controller, groups_controller, likes_controller, movies_controller, notifications_controller, posts_controller, search_controller, server_controller, settings_controller, surveys_controller, user_controller, virtual_directory_controller, web_app_controller}; use crate::controllers::routes::Method::{GET, POST}; use crate::data::http_request_handler::HttpRequestHandler; @@ -92,6 +92,9 @@ pub fn get_routes() -> Vec { Route::post_without_login("/user/getAdvancedUserInfo", Box::new(user_controller::get_advanced_info)), Route::post_without_login("/user/getAdvancedUserInfos", Box::new(user_controller::get_advanced_info)), + // Settings controller + Route::post("/settings/get_general", Box::new(settings_controller::get_general)), + // Friends controller Route::post("/friends/getList", Box::new(friends_controller::get_list)), Route::post("/friends/get_single_infos", Box::new(friends_controller::get_single_friendship_info)), diff --git a/src/controllers/settings_controller.rs b/src/controllers/settings_controller.rs new file mode 100644 index 0000000..af16425 --- /dev/null +++ b/src/controllers/settings_controller.rs @@ -0,0 +1,15 @@ +//! # Settings controller +//! +//! @author Pierre Hubert + +use crate::api_data::general_settings_api::GeneralSettingsAPI; +use crate::controllers::routes::RequestResult; +use crate::data::http_request_handler::HttpRequestHandler; +use crate::helpers::user_helper; + +/// 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)) +} \ No newline at end of file diff --git a/src/data/user.rs b/src/data/user.rs index 882a220..7dc0cc5 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -76,6 +76,7 @@ pub struct User { pub block_comments_on_his_page: bool, pub allow_posts_from_friends: bool, pub account_creation_time: u64, + pub allow_mails: bool, pub security_question_1: Option, pub security_answer_1: Option, pub security_question_2: Option, @@ -83,6 +84,16 @@ pub struct User { } impl User { + /// Check if user's page is public + pub fn is_page_public(&self) -> bool { + !matches!(self.status, UserPageStatus::PRIVATE) + } + + /// Check if user's page is open + pub fn is_page_open(&self) -> bool { + matches!(self.status, UserPageStatus::OPEN) + } + /// Get the URL pointing to the default account image pub fn default_account_image_url() -> String { user_data_url(crate::constants::DEFAULT_ACCOUNT_IMAGE) diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index ba3b266..b3d9e25 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -64,6 +64,7 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { block_comments_on_his_page: res.get_legacy_bool("bloquecommentaire")?, allow_posts_from_friends: res.get_legacy_bool("autoriser_post_amis")?, account_creation_time: res.get_date_as_time("date_creation")?, + allow_mails: res.get_legacy_bool("autorise_mail")?, security_question_1: res.get_optional_str("question1")?, security_answer_1: res.get_optional_str("reponse1")?, security_question_2: res.get_optional_str("question2")?,