1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 23:39:22 +00:00
comunicapiv3/src/helpers/user_helper.rs

158 lines
5.2 KiB
Rust
Raw Normal View History

2020-05-26 15:51:11 +00:00
use crate::constants::database_tables_names::USERS_TABLE;
2020-06-23 13:06:26 +00:00
use crate::data::error::ResultBoxError;
use crate::data::user::{AccountImageVisibility, User, UserID, UserPageStatus};
use crate::data::user::UserPageStatus::PUBLIC;
2020-06-23 13:06:26 +00:00
use crate::helpers::{database, friends_helper};
use crate::helpers::friends_helper::are_friend;
2020-05-23 17:17:48 +00:00
/// User helper
///
/// @author Pierre Hubert
2020-05-25 11:25:51 +00:00
/// Get & return information about a user based on its ID
2020-06-25 08:08:34 +00:00
pub fn find_user_by_id(id: &UserID) -> ResultBoxError<User> {
2020-05-25 11:25:51 +00:00
exec_get_user_query(
2020-06-25 08:08:34 +00:00
database::QueryInfo::new(USERS_TABLE).cond_user_id("ID", id))
2020-05-25 11:25:51 +00:00
}
2020-05-23 17:17:48 +00: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))
}
/// Get & return information about a user based on his virtual directory
pub fn find_user_by_virtual_directory(dir: &str) -> ResultBoxError<User> {
exec_get_user_query(
database::QueryInfo::new(USERS_TABLE).cond("sous_repertoire", dir))
}
2020-05-23 17:17:48 +00:00
/// Execute query & return result
2020-05-25 11:25:51 +00:00
fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
database::query_row(query, |res| {
2020-05-26 11:15:39 +00: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
};
// 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 11:15:39 +00:00
2020-05-23 17:17:48 +00:00
Ok(User {
2020-06-25 08:08:34 +00:00
id: res.get_user_id("ID")?,
2020-05-23 17:17:48 +00:00
email: res.get_str("mail")?,
password: res.get_str("password")?,
first_name: res.get_str("prenom")?,
2020-05-25 11:25:51 +00:00
last_name: res.get_str("nom")?,
2020-05-26 11:15:39 +00:00
status: page_status,
2020-05-26 11:18:38 +00:00
virtual_directory: res.get_optional_str("sous_repertoire")?,
account_image_path: res.get_optional_str("account_image_path")?,
account_image_visibility,
2020-06-01 07:19:29 +00: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")?,
2020-06-23 13:06:26 +00:00
account_creation_time: res.get_date_as_time("date_creation")?,
2020-05-23 17:17:48 +00:00
})
})
}
/// Check out whether a given id maps to a user or not
2020-06-25 08:08:34 +00:00
pub fn exists(id: &UserID) -> ResultBoxError<bool> {
Ok(database::QueryInfo::new(USERS_TABLE)
2020-06-25 08:08:34 +00:00
.cond_user_id("ID", id)
.exec_count()? > 0)
}
/// Check if a given user can see another user's page
2020-06-25 08:08:34 +00:00
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
2020-06-23 13:06:26 +00:00
if visibility == UserPageStatus::OPEN {
return Ok(true);
}
// The user need to be signed in
2020-06-25 08:08:34 +00:00
if user_id.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);
}
/// Check out whether a user allow posts on his page or not
2020-06-25 08:08:34 +00:00
pub fn allow_posts_on_his_page(user_id: &UserID) -> ResultBoxError<bool> {
Ok(find_user_by_id(user_id)?.allow_posts_from_friends)
}
/// Check out if a user can create posts on another user page
2020-06-25 08:08:34 +00:00
pub fn can_create_posts(user_id: &UserID, target_id: &UserID) -> ResultBoxError<bool> {
// Login required
2020-06-25 08:08:34 +00:00
if !user_id.is_valid() {
return Ok(false);
}
// A user can always create posts on his page
if user_id == target_id {
return Ok(true);
}
// User must be able to see the page
if !can_see_user_page(user_id, target_id)? {
return Ok(false);
}
// Check if user allow posts on his page
if !allow_posts_on_his_page(target_id)? {
return Ok(false);
}
// Check if the friendship of the user allows him to create posts
if !friends_helper::can_post_texts(user_id, target_id)? {
return Ok(false);
}
Ok(true)
2020-06-23 13:06:26 +00:00
}
/// Search for user in the database
pub fn search_user(query: &str, limit: u64) -> ResultBoxError<Vec<UserID>> {
let query = format!("%{}%", query.replace(" ", "%"));
database::QueryInfo::new(USERS_TABLE)
.add_field("ID")
.set_custom_where("(nom LIKE ?) || (prenom LIKE ?) || (CONCAT(prenom, '%', nom) LIKE ?) || (CONCAT(nom, '%', prenom) LIKE ?)")
.add_custom_where_argument_str(&query)
.add_custom_where_argument_str(&query)
.add_custom_where_argument_str(&query)
.add_custom_where_argument_str(&query)
.set_limit(limit)
.exec(|row| row.get_user_id("ID"))
2020-05-23 17:17:48 +00:00
}