1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-27 22:18:51 +00:00

Can set general settings

This commit is contained in:
Pierre HUBERT 2020-07-14 11:36:15 +02:00
parent dcd8b07b60
commit 1850ca0626
6 changed files with 82 additions and 6 deletions

View File

@ -94,6 +94,7 @@ pub fn get_routes() -> Vec<Route> {
// Settings controller
Route::post("/settings/get_general", Box::new(settings_controller::get_general)),
Route::post("/settings/set_general", Box::new(settings_controller::set_general)),
// Friends controller
Route::post("/friends/getList", Box::new(friends_controller::get_list)),

View File

@ -4,12 +4,47 @@
use crate::api_data::general_settings_api::GeneralSettingsAPI;
use crate::controllers::routes::RequestResult;
use crate::data::general_settings::GeneralSettings;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::user_helper;
use crate::data::user::UserPageStatus;
use crate::helpers::{account_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")
}

View File

@ -0,0 +1,19 @@
//! # General settings
//!
//! @author Pierre Hubert
use crate::data::user::{UserID, UserPageStatus};
pub struct GeneralSettings {
pub id: UserID,
pub first_name: String,
pub last_name: String,
pub page_status: UserPageStatus,
pub block_comments: bool,
pub allow_posts_from_friends: bool,
pub friends_list_public: bool,
pub personal_website: Option<String>,
pub virtual_directory: Option<String>,
pub allow_mails: bool,
pub public_note: Option<String>,
}

View File

@ -10,6 +10,7 @@ use image::{GenericImageView, ImageFormat};
use serde::Serialize;
use crate::api_data::http_error::HttpError;
use crate::constants::PASSWORD_MIN_LENGTH;
use crate::controllers::routes::RequestResult;
use crate::data::api_client::APIClient;
use crate::data::comment::Comment;
@ -25,7 +26,6 @@ use crate::utils::pdf_utils::is_valid_pdf;
use crate::utils::string_utils::{check_string_before_insert, check_url, remove_html_nodes};
use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path};
use crate::utils::virtual_directories_utils::check_virtual_directory;
use crate::constants::PASSWORD_MIN_LENGTH;
/// Http request handler
///
@ -654,7 +654,7 @@ impl HttpRequestHandler {
self.forbidden("Please do not include inline images!".to_string())?;
}
if !check_string_before_insert(&content) {
if min_len > 0 && required && !check_string_before_insert(&content) {
self.forbidden(format!("The content inside {} was rejected!", name))?;
}
@ -662,7 +662,7 @@ impl HttpRequestHandler {
}
/// Check the password of the current user
pub fn need_user_password(&mut self, field: &str) ->ResultBoxError {
pub fn need_user_password(&mut self, field: &str) -> ResultBoxError {
let password = self.post_string_opt(field, PASSWORD_MIN_LENGTH, true)?;
if !account_helper::check_user_password(self.user_id_ref()?, &password)? {

View File

@ -29,4 +29,5 @@ pub mod user_membership;
pub mod new_account;
pub mod account_export;
pub mod user_like;
pub mod survey_response;
pub mod survey_response;
pub mod general_settings;

View File

@ -3,8 +3,9 @@ use crate::constants::database_tables_names::{USER_ACCESS_TOKENS_TABLE, USERS_TA
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::new_account::NewAccount;
use crate::data::user::UserID;
use crate::data::user::{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};
@ -191,6 +192,25 @@ pub fn update_last_activity(user_id: &UserID) -> ResultBoxError {
.exec()
}
/// Set new general settings of an account
pub fn set_general(settings: &GeneralSettings) -> ResultBoxError {
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", &settings.id)
.set_str("prenom", &settings.first_name)
.set_str("nom", &settings.last_name)
.set_legacy_bool("public", settings.page_status != UserPageStatus::PRIVATE)
.set_legacy_bool("pageouverte", settings.page_status == UserPageStatus::OPEN)
.set_legacy_bool("bloquecommentaire", settings.block_comments)
.set_legacy_bool("autoriser_post_amis", settings.allow_posts_from_friends)
.set_legacy_bool("autorise_mail", settings.allow_mails)
.set_legacy_bool("liste_amis_publique", settings.friends_list_public)
.set_opt_str("sous_repertoire", settings.virtual_directory.clone())
.set_opt_str("site_web", settings.personal_website.clone())
.set_opt_str("public_note", settings.public_note.clone())
.exec()
}
/// Export an account's data
pub fn export(user_id: &UserID) -> ResultBoxError<AccountExport> {
let mut data = AccountExport {