use crate::constants::database_tables_names::USERS_TABLE; use crate::data::error::ResultBoxError; use crate::data::user::{AccountImageVisibility, User, UserID, UserPageStatus}; use crate::data::user::UserPageStatus::PUBLIC; use crate::helpers::{database, friends_helper}; use crate::helpers::friends_helper::are_friend; /// User helper /// /// @author Pierre Hubert /// Get & return information about a user based on its ID pub fn find_user_by_id(id: &UserID) -> ResultBoxError { exec_get_user_query( database::QueryInfo::new(USERS_TABLE).cond_user_id("ID", id)) } /// Get & return information about a user based on his email pub fn find_user_by_email(email: &str) -> ResultBoxError { 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 { exec_get_user_query( database::QueryInfo::new(USERS_TABLE).cond("sous_repertoire", dir)) } /// Execute query & return result fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { database::query_row(query, |res| { // 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!() }; Ok(User { id: res.get_user_id("ID")?, email: res.get_str("mail")?, password: res.get_str("password")?, first_name: res.get_str("prenom")?, last_name: res.get_str("nom")?, status: page_status, virtual_directory: res.get_optional_str("sous_repertoire")?, account_image_path: res.get_optional_str("account_image_path")?, account_image_visibility, 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: res.get_date_as_time("date_creation")?, }) }) } /// Check out whether a given id maps to a user or not pub fn exists(id: &UserID) -> ResultBoxError { Ok(database::QueryInfo::new(USERS_TABLE) .cond_user_id("ID", id) .exec_count()? > 0) } /// Check if a given user can see another user's page pub fn can_see_user_page(user_id: &UserID, target_user: &UserID) -> ResultBoxError { 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.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 pub fn allow_posts_on_his_page(user_id: &UserID) -> ResultBoxError { Ok(find_user_by_id(user_id)?.allow_posts_from_friends) } /// Check out whether a user has blocked comments on his / her page pub fn allow_comments(user_id: &UserID) -> ResultBoxError { Ok(!find_user_by_id(user_id)?.block_comments_on_his_page) } /// Check out whether the friends list of a user is public or not pub fn is_user_friends_list_public(user_id: &UserID) -> ResultBoxError { Ok(find_user_by_id(user_id)?.public_friends_list) } /// Check out if a user can create posts on another user page pub fn can_create_posts(user_id: &UserID, target_id: &UserID) -> ResultBoxError { // Login required 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) } /// Search for user in the database pub fn search_user(query: &str, limit: u64) -> ResultBoxError> { 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")) }