From cca7f02f8e3939e3b8b396cd5b6a86c12dbe9b3f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 26 May 2020 13:53:24 +0200 Subject: [PATCH] Ready to implement account image visibility --- .idea/dictionaries/pierre.xml | 7 +++++++ src/api_data/user_info.rs | 19 ++++++++++++++----- src/controllers/user_controller.rs | 2 +- src/data/http_request_handler.rs | 5 +++++ src/data/user.rs | 10 ++++++++++ src/helpers/user_helper.rs | 11 ++++++++++- 6 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 .idea/dictionaries/pierre.xml diff --git a/.idea/dictionaries/pierre.xml b/.idea/dictionaries/pierre.xml new file mode 100644 index 0000000..47f01cb --- /dev/null +++ b/.idea/dictionaries/pierre.xml @@ -0,0 +1,7 @@ + + + + comunic + + + \ No newline at end of file diff --git a/src/api_data/user_info.rs b/src/api_data/user_info.rs index 21e3eeb..5f42b23 100644 --- a/src/api_data/user_info.rs +++ b/src/api_data/user_info.rs @@ -3,7 +3,7 @@ //! @author Pierre Hubert use serde::Serialize; -use crate::data::user::{User, UserPageStatus}; +use crate::data::user::{User, UserPageStatus, UserID}; #[derive(Serialize)] #[allow(non_snake_case)] @@ -14,17 +14,26 @@ pub struct APIUserInfo { publicPage: bool, openPage: bool, virtualDirectory: String, + accountImage: String, } impl APIUserInfo { - pub fn new(info: User) -> APIUserInfo { + /// Construct a new API user instance. Note: `user_id` is the ID of the user who makes the + /// requests, not the user whose we return information about! + pub fn new(user_id: Option, info: &User) -> APIUserInfo { APIUserInfo { userID: info.id, - firstName: info.first_name, - lastName: info.last_name, + firstName: info.first_name.to_string(), + lastName: info.last_name.to_string(), publicPage: info.status != UserPageStatus::PRIVATE, openPage: info.status == UserPageStatus::OPEN, - virtualDirectory: info.virtual_directory.unwrap_or("".to_string()) + virtualDirectory: info.virtual_directory.clone().unwrap_or(String::new()), + accountImage: APIUserInfo::get_account_image_url(user_id, info), } } + + /// Get the URL of a specific user account image + pub fn get_account_image_url(user_id: Option, user: &User) -> String { + "do it".to_string() + } } \ No newline at end of file diff --git a/src/controllers/user_controller.rs b/src/controllers/user_controller.rs index 93cae7d..cf4f0a2 100644 --- a/src/controllers/user_controller.rs +++ b/src/controllers/user_controller.rs @@ -21,5 +21,5 @@ pub fn get_single(request: &mut HttpRequestHandler) -> RequestResult { } }; - request.set_response(APIUserInfo::new(user)) + request.set_response(APIUserInfo::new(request.user_id_opt(), &user)) } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 3b90d58..dcf8cd2 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -256,6 +256,11 @@ impl HttpRequestHandler { } } + /// Get a user ID, if available + pub fn user_id_opt(&self) -> Option { + self.curr_user_id + } + /// Get an email included in the request pub fn post_email(&mut self, name: &str) -> ResultBoxError { let mail = self.post_string(name)?; diff --git a/src/data/user.rs b/src/data/user.rs index feb6fc4..c3c411f 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -11,6 +11,14 @@ pub enum UserPageStatus { PRIVATE } +#[derive(Debug, PartialEq)] +#[allow(non_camel_case_types)] +pub enum AccountImageVisibility { + FRIENDS, + COMUNIC_USERS, + EVERYONE +} + #[derive(Debug)] pub struct User { pub id: UserID, @@ -20,4 +28,6 @@ pub struct User { pub last_name: String, pub status: UserPageStatus, pub virtual_directory: Option, + pub account_image_path: Option, + pub account_image_visibility: AccountImageVisibility, } \ No newline at end of file diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index b2c1bd2..49841bb 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -1,5 +1,5 @@ use crate::data::error::ResultBoxError; -use crate::data::user::{User, UserID, UserPageStatus}; +use crate::data::user::{User, UserID, UserPageStatus, AccountImageVisibility}; use crate::helpers::database; use crate::database_structure::USERS_TABLE; @@ -32,6 +32,13 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { UserPageStatus::PRIVATE }; + // Account image visibility + 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!() + }; Ok(User { id: res.get_int64("ID")?, @@ -41,6 +48,8 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { last_name: res.get_str("nom")?, status: page_status, virtual_directory: res.get_optional_str("sous_repertoire")?, + account_image_path: res.get_optional_str("account_image_path")?, + account_image_visibility, }) }) } \ No newline at end of file