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-06-01 09:49:48 +02:00
|
|
|
use crate::helpers::{friends_helper, custom_emojies_helper, background_image_helper};
|
2020-05-26 18:45:27 +02:00
|
|
|
use crate::data::error::ResultBoxError;
|
|
|
|
use crate::utils::user_data_utils::user_data_url;
|
|
|
|
use crate::data::user::AccountImageVisibility::{EVERYONE, COMUNIC_USERS};
|
2020-05-26 19:45:38 +02:00
|
|
|
use crate::api_data::custom_emoji::CustomEmojiAPI;
|
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-26 19:45:38 +02:00
|
|
|
customEmojis: Vec<CustomEmojiAPI>,
|
2020-06-01 09:19:29 +02:00
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
advanced_info: Option<APIAdvancedInfo>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct APIAdvancedInfo {
|
|
|
|
friend_list_public: bool,
|
|
|
|
personnalWebsite: String,
|
|
|
|
publicNote: String,
|
|
|
|
noCommentOnHisPage: bool,
|
|
|
|
allowPostFromFriendOnHisPage: bool,
|
2020-06-01 09:49:48 +02:00
|
|
|
account_creation_time: u64,
|
|
|
|
backgroundImage: String,
|
2020-06-01 10:00:58 +02:00
|
|
|
number_friends: usize,
|
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-26 19:45:38 +02:00
|
|
|
customEmojis: custom_emojies_helper::get_list_user(info.id)?
|
|
|
|
.iter()
|
|
|
|
.map(|f| CustomEmojiAPI::new(f))
|
|
|
|
.collect(),
|
2020-06-01 09:19:29 +02:00
|
|
|
advanced_info: None,
|
2020-05-26 18:45:27 +02:00
|
|
|
})
|
2020-05-25 13:25:51 +02:00
|
|
|
}
|
2020-05-26 13:53:24 +02:00
|
|
|
|
2020-06-01 09:19:29 +02:00
|
|
|
/// Get advanced user information
|
|
|
|
pub fn new_advanced_info(user_id: Option<UserID>, info: &User) -> ResultBoxError<APIUserInfo> {
|
|
|
|
let mut user = APIUserInfo::new(user_id, info)?;
|
|
|
|
|
2020-06-01 10:00:58 +02:00
|
|
|
// Check if we can return the number of friends of the user
|
|
|
|
let number_friends = if info.public_friends_list || user_id.unwrap_or(0) == info.id {
|
|
|
|
friends_helper::count_friends(info.id)?
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2020-06-01 09:19:29 +02:00
|
|
|
// Set advanced user information
|
|
|
|
user.advanced_info = Some(APIAdvancedInfo {
|
|
|
|
friend_list_public: info.public_friends_list,
|
|
|
|
personnalWebsite: info.personal_website.clone().unwrap_or(String::new()),
|
|
|
|
publicNote: info.public_note.clone().unwrap_or(String::new()),
|
|
|
|
noCommentOnHisPage: info.block_comments_on_his_page,
|
|
|
|
allowPostFromFriendOnHisPage: info.allow_posts_from_friends,
|
2020-06-01 09:49:48 +02:00
|
|
|
account_creation_time: info.account_creation_time,
|
|
|
|
backgroundImage: background_image_helper::get_url(info.id),
|
2020-06-01 10:00:58 +02:00
|
|
|
number_friends,
|
2020-06-01 09:19:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|