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

189 lines
6.5 KiB
Rust
Raw Normal View History

2020-05-26 15:51:11 +00:00
use crate::constants::database_tables_names::USERS_TABLE;
2021-02-14 15:45:00 +00:00
use crate::data::error::{Res, ResultBoxError};
2020-06-23 13:06:26 +00:00
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> {
2021-02-14 15:45:00 +00:00
database::QueryInfo::new(USERS_TABLE)
.cond_user_id("ID", id)
.query_row(db_to_user)
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> {
2021-02-14 15:45:00 +00:00
database::QueryInfo::new(USERS_TABLE)
.cond("mail", email)
.query_row(db_to_user)
2020-05-23 17:17:48 +00:00
}
/// Get & return information about a user based on his virtual directory
pub fn find_user_by_virtual_directory(dir: &str) -> ResultBoxError<User> {
2021-02-14 15:45:00 +00:00
database::QueryInfo::new(USERS_TABLE)
.cond("sous_repertoire", dir)
.query_row(db_to_user)
}
/// Get the entire list of Comunic users
pub fn get_all_users() -> Res<Vec<User>> {
database::QueryInfo::new(USERS_TABLE)
.exec(db_to_user)
}
2020-05-23 17:17:48 +00:00
/// Execute query & return result
2021-02-14 15:45:00 +00:00
fn db_to_user(res: &database::RowResult) -> ResultBoxError<User> {
// 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,
};
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,
last_activity: res.get_u64("last_activity")?,
2021-02-14 15:45:00 +00:00
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")?,
allow_mails: res.get_legacy_bool("autorise_mail")?,
lang: res.get_str("lang")?,
security_question_1: res.get_optional_str("question1")?,
security_answer_1: res.get_optional_str("reponse1")?,
security_question_2: res.get_optional_str("question2")?,
security_answer_2: res.get_optional_str("reponse2")?,
2021-02-15 16:38:25 +00:00
delete_account_after: res.get_optional_positive_u64("delete_account_after")?,
delete_notifications_after: res.get_optional_positive_u64("delete_notifications_after")?,
delete_comments_after: res.get_optional_positive_u64("delete_comments_after")?,
delete_posts_after: res.get_optional_positive_u64("delete_posts_after")?,
delete_conversation_messages_after: res.get_optional_positive_u64("delete_conversation_messages_after")?,
delete_likes_after: res.get_optional_positive_u64("delete_likes_after")?,
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)
}
2020-07-05 17:32:28 +00:00
/// Check out whether a user has blocked comments on his / her page
pub fn allow_comments(user_id: &UserID) -> ResultBoxError<bool> {
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<bool> {
Ok(find_user_by_id(user_id)?.public_friends_list)
}
/// 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
}