2020-05-23 19:17:48 +02:00
|
|
|
use crate::data::error::ResultBoxError;
|
2020-05-26 13:53:24 +02:00
|
|
|
use crate::data::user::{User, UserID, UserPageStatus, AccountImageVisibility};
|
2020-05-23 19:17:48 +02:00
|
|
|
use crate::helpers::database;
|
2020-05-26 17:51:11 +02:00
|
|
|
use crate::constants::database_tables_names::USERS_TABLE;
|
2020-05-29 18:26:45 +02:00
|
|
|
use crate::data::user::UserPageStatus::PUBLIC;
|
|
|
|
use crate::helpers::friends_helper::are_friend;
|
2020-05-23 19:17:48 +02:00
|
|
|
|
|
|
|
/// User helper
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
2020-05-25 13:25:51 +02:00
|
|
|
/// Get & return information about a user based on its ID
|
|
|
|
pub fn find_user_by_id(id: UserID) -> ResultBoxError<User> {
|
|
|
|
exec_get_user_query(
|
|
|
|
database::QueryInfo::new(USERS_TABLE).cond_i64("ID", id))
|
|
|
|
}
|
|
|
|
|
2020-05-23 19:17:48 +02:00
|
|
|
/// Get & return information about a user based on his email
|
|
|
|
pub fn find_user_by_email(email: &str) -> ResultBoxError<User> {
|
|
|
|
exec_get_user_query(
|
|
|
|
database::QueryInfo::new(USERS_TABLE).cond("mail", email))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute query & return result
|
2020-05-25 13:25:51 +02:00
|
|
|
fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
|
|
|
|
database::query_row(query, |res| {
|
2020-05-26 13:15:39 +02:00
|
|
|
|
|
|
|
// Page status
|
|
|
|
let page_status = if res.get_int64("pageouverte")? == 1 {
|
|
|
|
UserPageStatus::OPEN
|
|
|
|
} else if res.get_int64("public")? == 1 {
|
|
|
|
UserPageStatus::PUBLIC
|
|
|
|
} else {
|
|
|
|
UserPageStatus::PRIVATE
|
|
|
|
};
|
|
|
|
|
2020-05-26 13:53:24 +02:00
|
|
|
// Account image visibility
|
|
|
|
let account_image_visibility = match res.get_str("account_image_visibility")?.as_ref() {
|
|
|
|
"friends" => AccountImageVisibility::FRIENDS,
|
|
|
|
"comunic_users" => AccountImageVisibility::COMUNIC_USERS,
|
|
|
|
"everyone" => AccountImageVisibility::EVERYONE,
|
|
|
|
_ => unreachable!()
|
|
|
|
};
|
2020-05-26 13:15:39 +02:00
|
|
|
|
2020-05-23 19:17:48 +02:00
|
|
|
Ok(User {
|
|
|
|
id: res.get_int64("ID")?,
|
|
|
|
email: res.get_str("mail")?,
|
|
|
|
password: res.get_str("password")?,
|
|
|
|
first_name: res.get_str("prenom")?,
|
2020-05-25 13:25:51 +02:00
|
|
|
last_name: res.get_str("nom")?,
|
2020-05-26 13:15:39 +02:00
|
|
|
status: page_status,
|
2020-05-26 13:18:38 +02:00
|
|
|
virtual_directory: res.get_optional_str("sous_repertoire")?,
|
2020-05-26 13:53:24 +02:00
|
|
|
account_image_path: res.get_optional_str("account_image_path")?,
|
|
|
|
account_image_visibility,
|
2020-06-01 09:19:29 +02:00
|
|
|
public_friends_list: res.get_legacy_bool("liste_amis_publique")?,
|
|
|
|
personal_website: res.get_optional_str("site_web")?,
|
|
|
|
public_note: res.get_optional_str("public_note")?,
|
|
|
|
block_comments_on_his_page: res.get_legacy_bool("bloquecommentaire")?,
|
|
|
|
allow_posts_from_friends: res.get_legacy_bool("autoriser_post_amis")?,
|
|
|
|
account_creation_time: 10,//TODO : parse date
|
2020-05-23 19:17:48 +02:00
|
|
|
})
|
|
|
|
})
|
2020-05-29 18:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check out whether a given id maps to a user or not
|
|
|
|
pub fn exists(id: UserID) -> ResultBoxError<bool> {
|
|
|
|
Ok(database::QueryInfo::new(USERS_TABLE)
|
|
|
|
.cond_i64("ID", id)
|
|
|
|
.exec_count()? > 0)
|
2020-05-29 18:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a given user can see another user's page
|
|
|
|
pub fn can_see_user_page(user_id: UserID, target_user: UserID) -> ResultBoxError<bool> {
|
|
|
|
if user_id == target_user {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
let visibility = find_user_by_id(target_user)?.status;
|
|
|
|
|
|
|
|
// Open page = OK
|
|
|
|
if visibility == UserPageStatus::OPEN {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user need to be signed in
|
|
|
|
if user_id <= 0 {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public Page = OK for signed in users
|
|
|
|
if visibility == PUBLIC {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the users are friends
|
|
|
|
if !are_friend(user_id, target_user)? {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(true);
|
2020-05-23 19:17:48 +02:00
|
|
|
}
|