2020-05-25 13:25:51 +02:00
|
|
|
//! API representation of user information
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
use serde::Serialize;
|
|
|
|
|
2020-05-26 13:53:24 +02:00
|
|
|
use crate::data::user::{User, UserPageStatus, UserID};
|
2020-05-26 18:45:27 +02:00
|
|
|
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};
|
2020-05-25 13:25:51 +02:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub struct APIUserInfo {
|
|
|
|
userID: i64,
|
|
|
|
firstName: String,
|
|
|
|
lastName: String,
|
2020-05-26 13:15:39 +02:00
|
|
|
publicPage: bool,
|
|
|
|
openPage: bool,
|
2020-05-26 13:18:38 +02:00
|
|
|
virtualDirectory: String,
|
2020-05-26 13:53:24 +02:00
|
|
|
accountImage: String,
|
2020-05-25 13:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl APIUserInfo {
|
2020-05-26 13:53:24 +02:00
|
|
|
/// 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!
|
2020-05-26 18:45:27 +02:00
|
|
|
pub fn new(user_id: Option<UserID>, info: &User) -> ResultBoxError<APIUserInfo> {
|
|
|
|
Ok(APIUserInfo {
|
2020-05-25 13:25:51 +02:00
|
|
|
userID: info.id,
|
2020-05-26 13:53:24 +02:00
|
|
|
firstName: info.first_name.to_string(),
|
|
|
|
lastName: info.last_name.to_string(),
|
2020-05-26 13:15:39 +02:00
|
|
|
publicPage: info.status != UserPageStatus::PRIVATE,
|
|
|
|
openPage: info.status == UserPageStatus::OPEN,
|
2020-05-26 13:53:24 +02:00
|
|
|
virtualDirectory: info.virtual_directory.clone().unwrap_or(String::new()),
|
2020-05-26 18:45:27 +02:00
|
|
|
accountImage: APIUserInfo::get_account_image_url(user_id, info)?,
|
|
|
|
})
|
2020-05-25 13:25:51 +02:00
|
|
|
}
|
2020-05-26 13:53:24 +02:00
|
|
|
|
|
|
|
/// Get the URL of a specific user account image
|
2020-05-26 18:45:27 +02:00
|
|
|
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())
|
2020-05-26 13:53:24 +02:00
|
|
|
}
|
2020-05-25 13:25:51 +02:00
|
|
|
}
|