2020-05-26 16:25:33 +00:00
|
|
|
//! # Friends helper
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
|
|
|
|
|
|
|
use crate::data::user::UserID;
|
|
|
|
use crate::data::error::ResultBoxError;
|
|
|
|
use crate::helpers::database;
|
|
|
|
use crate::constants::database_tables_names::FRIENDS_TABLE;
|
|
|
|
use crate::helpers::database::QueryInfo;
|
|
|
|
|
|
|
|
/// Check out whether two users are friend or not
|
|
|
|
pub fn are_friend(user_one: UserID, user_two: UserID) -> ResultBoxError<bool> {
|
|
|
|
Ok(database::count(QueryInfo::new(FRIENDS_TABLE)
|
|
|
|
.cond_i64("ID_personne", user_one)
|
|
|
|
.cond_i64("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
|
|
|
|
pub fn count_friends(user_id: UserID) -> ResultBoxError<usize> {
|
|
|
|
QueryInfo::new(FRIENDS_TABLE)
|
|
|
|
.cond_i64("ID_amis", user_id)
|
|
|
|
.cond_u32("actif", 1)
|
|
|
|
.exec_count()
|
2020-06-01 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a user can create posts on another friend's page
|
|
|
|
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-05-26 16:25:33 +00:00
|
|
|
}
|