1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 15:29:21 +00:00
comunicapiv3/src/helpers/friends_helper.rs

63 lines
2.2 KiB
Rust
Raw Normal View History

//! # Friends helper
//!
//! @author Pierre Hubert
2020-06-29 13:45:26 +00:00
use crate::constants::database_tables_names::{FRIENDS_TABLE, USERS_TABLE};
use crate::data::error::ResultBoxError;
2020-06-29 13:45:26 +00:00
use crate::data::friend::Friend;
use crate::data::user::UserID;
use crate::helpers::database;
use crate::helpers::database::QueryInfo;
2020-06-29 13:45:26 +00:00
/// Get the list of friends of a user
pub fn get_list(user_id: &UserID) -> ResultBoxError<Vec<Friend>> {
database::QueryInfo::new(FRIENDS_TABLE)
.alias("f")
.cond_user_id("ID_personne", user_id)
.join(USERS_TABLE, "u", "f.ID_amis = u.ID")
.add_field("u.last_activity")
.add_field("f.ID_amis")
.add_field("f.actif")
.add_field("f.abonnement")
.add_field("f.autoriser_post_page")
.set_order("u.last_activity DESC")
.exec(db_to_friend)
}
/// Check out whether two users are friend or not
2020-06-25 08:08:34 +00:00
pub fn are_friend(user_one: &UserID, user_two: &UserID) -> ResultBoxError<bool> {
Ok(database::count(QueryInfo::new(FRIENDS_TABLE)
2020-06-25 08:08:34 +00:00
.cond_user_id("ID_personne", user_one)
.cond_user_id("ID_amis", user_two)
.cond_i64("actif", 1))? > 0)
2020-06-01 08:00:58 +00:00
}
/// Count the number of friends of a user
2020-06-25 08:08:34 +00:00
pub fn count_friends(user_id: &UserID) -> ResultBoxError<usize> {
2020-06-01 08:00:58 +00:00
QueryInfo::new(FRIENDS_TABLE)
2020-06-25 08:08:34 +00:00
.cond_user_id("ID_amis", user_id)
2020-06-01 08:00:58 +00:00
.cond_u32("actif", 1)
.exec_count()
}
/// Check if a user can create posts on another friend's page
2020-06-25 08:08:34 +00:00
pub fn can_post_texts(user_id: &UserID, target_user: &UserID) -> ResultBoxError<bool> {
QueryInfo::new(FRIENDS_TABLE)
.cond_user_id("ID_personne", target_user)
.cond_user_id("ID_amis", user_id)
.add_field("autoriser_post_page")
.query_row(|res| res.get_legacy_bool("autoriser_post_page"))
.or(Ok(false))
2020-06-29 13:45:26 +00:00
}
/// Turn a database entry into a Friend structure
fn db_to_friend(row: &database::RowResult) -> ResultBoxError<Friend> {
Ok(Friend {
friend_id: row.get_user_id("ID_amis")?,
accepted: row.get_legacy_bool("actif")?,
following: row.get_legacy_bool("abonnement")?,
last_activity_time: row.get_u64("last_activity")?,
can_post_texts: row.get_legacy_bool("autoriser_post_page")?,
})
}