From 2b1161c3f0c81ab2ed837e8da1a9a2816d8c7d30 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 26 May 2020 18:45:27 +0200 Subject: [PATCH] Return user account image --- src/api_data/user_info.rs | 36 +++++++++++++++++++++++++----- src/controllers/user_controller.rs | 2 +- src/data/user.rs | 5 +++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/api_data/user_info.rs b/src/api_data/user_info.rs index 5f42b23..068a41e 100644 --- a/src/api_data/user_info.rs +++ b/src/api_data/user_info.rs @@ -4,6 +4,10 @@ use serde::Serialize; use crate::data::user::{User, UserPageStatus, UserID}; +use crate::helpers::friends_helper; +use crate::data::error::ResultBoxError; +use crate::utils::user_data_utils::user_data_url; +use crate::data::user::AccountImageVisibility::{EVERYONE, COMUNIC_USERS}; #[derive(Serialize)] #[allow(non_snake_case)] @@ -20,20 +24,40 @@ pub struct APIUserInfo { impl 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 { + pub fn new(user_id: Option, info: &User) -> ResultBoxError { + Ok(APIUserInfo { userID: info.id, 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.clone().unwrap_or(String::new()), - accountImage: APIUserInfo::get_account_image_url(user_id, info), - } + 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() + pub fn get_account_image_url(user_id: Option, user: &User) -> ResultBoxError { + if !user.has_account_image() { + return Ok(User::default_account_image_url()); + } + + let user_account_image = Ok(user_data_url(user.account_image_path.as_ref().unwrap())); + + if user.account_image_visibility == EVERYONE || user_id == Some(user.id) { + return user_account_image; + } + + if user_id.is_none() { // User is not signed in + return Ok(User::error_account_image_url()); + } + + if user.account_image_visibility == COMUNIC_USERS || + friends_helper::are_friend(user_id.unwrap(), user.id)? { + return user_account_image; + } + + + Ok(User::error_account_image_url()) } } \ No newline at end of file diff --git a/src/controllers/user_controller.rs b/src/controllers/user_controller.rs index cf4f0a2..4a5406c 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(request.user_id_opt(), &user)) + request.set_response(APIUserInfo::new(request.user_id_opt(), &user)?) } \ No newline at end of file diff --git a/src/data/user.rs b/src/data/user.rs index 8024777..411bb34 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -46,4 +46,9 @@ impl User { pub fn error_account_image_url() -> String { user_data_url(crate::constants::ERROR_ACCOUNT_IMAGE) } + + /// Check if this user has an account image or not + pub fn has_account_image(&self) -> bool { + self.account_image_path.is_some() + } } \ No newline at end of file