1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-25 23:09:22 +00:00

Return user account image

This commit is contained in:
Pierre HUBERT 2020-05-26 18:45:27 +02:00
parent 11865c2bb4
commit 2b1161c3f0
3 changed files with 36 additions and 7 deletions

View File

@ -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<UserID>, info: &User) -> APIUserInfo {
APIUserInfo {
pub fn new(user_id: Option<UserID>, info: &User) -> ResultBoxError<APIUserInfo> {
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<UserID>, user: &User) -> String {
"do it".to_string()
pub fn get_account_image_url(user_id: Option<UserID>, user: &User) -> ResultBoxError<String> {
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())
}
}

View File

@ -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)?)
}

View File

@ -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()
}
}