2020-07-14 07:10:10 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
|
2020-05-26 16:02:14 +00:00
|
|
|
use crate::utils::user_data_utils::user_data_url;
|
|
|
|
|
2020-05-24 14:35:54 +00:00
|
|
|
///! User information
|
|
|
|
///!
|
|
|
|
///! @author Pierre Hubert
|
2020-06-25 08:08:34 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct UserID(u64);
|
2020-05-24 14:35:54 +00:00
|
|
|
|
2020-06-25 08:08:34 +00:00
|
|
|
impl UserID {
|
|
|
|
/// Initialize a new user ID object
|
|
|
|
pub fn new(id: u64) -> UserID {
|
|
|
|
UserID(id)
|
|
|
|
}
|
|
|
|
|
2020-06-25 11:39:37 +00:00
|
|
|
/// Create a new invalid user id
|
|
|
|
pub fn invalid() -> UserID {
|
|
|
|
UserID(0)
|
|
|
|
}
|
|
|
|
|
2020-06-25 08:08:34 +00:00
|
|
|
/// Get the current ID stored in this structure
|
|
|
|
pub fn id(&self) -> u64 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the ID currently stored in this structure is valid or not
|
|
|
|
pub fn is_valid(&self) -> bool {
|
|
|
|
self.0 > 0
|
|
|
|
}
|
2020-07-04 16:34:21 +00:00
|
|
|
|
|
|
|
/// Turn this `UserID` object as an instance. An invalid user id will become `None` variant
|
|
|
|
pub fn as_option(&self) -> Option<UserID> {
|
|
|
|
match self.is_valid() {
|
|
|
|
true => Some(self.clone()),
|
|
|
|
false => None,
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 08:08:34 +00:00
|
|
|
}
|
2020-05-24 14:35:54 +00:00
|
|
|
|
2020-07-14 07:10:10 +00:00
|
|
|
impl Hash for UserID {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.0.hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:15:39 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum UserPageStatus {
|
|
|
|
OPEN,
|
|
|
|
PUBLIC,
|
2020-06-25 08:08:34 +00:00
|
|
|
PRIVATE,
|
2020-05-26 11:15:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 11:53:24 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub enum AccountImageVisibility {
|
|
|
|
FRIENDS,
|
|
|
|
COMUNIC_USERS,
|
2020-06-25 08:08:34 +00:00
|
|
|
EVERYONE,
|
2020-05-26 11:53:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 17:17:48 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct User {
|
2020-05-24 14:35:54 +00:00
|
|
|
pub id: UserID,
|
2020-05-23 17:17:48 +00:00
|
|
|
pub email: String,
|
|
|
|
pub password: String,
|
|
|
|
pub first_name: String,
|
|
|
|
pub last_name: String,
|
2020-05-26 11:18:38 +00:00
|
|
|
pub status: UserPageStatus,
|
|
|
|
pub virtual_directory: Option<String>,
|
2020-05-26 11:53:24 +00:00
|
|
|
pub account_image_path: Option<String>,
|
|
|
|
pub account_image_visibility: AccountImageVisibility,
|
2020-06-01 07:19:29 +00:00
|
|
|
pub public_friends_list: bool,
|
|
|
|
pub personal_website: Option<String>,
|
|
|
|
pub public_note: Option<String>,
|
|
|
|
pub block_comments_on_his_page: bool,
|
|
|
|
pub allow_posts_from_friends: bool,
|
|
|
|
pub account_creation_time: u64,
|
2020-07-14 09:15:20 +00:00
|
|
|
pub allow_mails: bool,
|
2020-07-13 11:15:26 +00:00
|
|
|
pub security_question_1: Option<String>,
|
|
|
|
pub security_answer_1: Option<String>,
|
|
|
|
pub security_question_2: Option<String>,
|
|
|
|
pub security_answer_2: Option<String>,
|
2020-05-26 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl User {
|
2020-07-14 09:15:20 +00:00
|
|
|
/// Check if user's page is public
|
|
|
|
pub fn is_page_public(&self) -> bool {
|
|
|
|
!matches!(self.status, UserPageStatus::PRIVATE)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if user's page is open
|
|
|
|
pub fn is_page_open(&self) -> bool {
|
|
|
|
matches!(self.status, UserPageStatus::OPEN)
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:02:14 +00:00
|
|
|
/// Get the URL pointing to the default account image
|
|
|
|
pub fn default_account_image_url() -> String {
|
|
|
|
user_data_url(crate::constants::DEFAULT_ACCOUNT_IMAGE)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the URL pointing to the error account image, when the user is not allowed to see user
|
|
|
|
/// account image
|
|
|
|
pub fn error_account_image_url() -> String {
|
|
|
|
user_data_url(crate::constants::ERROR_ACCOUNT_IMAGE)
|
|
|
|
}
|
2020-05-26 16:45:27 +00:00
|
|
|
|
|
|
|
/// Check if this user has an account image or not
|
|
|
|
pub fn has_account_image(&self) -> bool {
|
|
|
|
self.account_image_path.is_some()
|
|
|
|
}
|
2020-07-13 11:15:26 +00:00
|
|
|
|
|
|
|
/// Check out whether security questions have been defined for this user or not
|
|
|
|
pub fn has_security_questions(&self) -> bool {
|
|
|
|
self.security_question_1.is_some() &&
|
|
|
|
self.security_answer_1.is_some() &&
|
|
|
|
self.security_question_2.is_some() &&
|
|
|
|
self.security_answer_2.is_some()
|
|
|
|
}
|
2020-05-23 17:17:48 +00:00
|
|
|
}
|