mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 00:45:18 +00:00
Can get the list of friends
This commit is contained in:
@ -3,12 +3,28 @@
|
||||
//! @author Pierre Hubert
|
||||
|
||||
|
||||
use crate::data::user::UserID;
|
||||
use crate::constants::database_tables_names::{FRIENDS_TABLE, USERS_TABLE};
|
||||
use crate::data::error::ResultBoxError;
|
||||
use crate::data::friend::Friend;
|
||||
use crate::data::user::UserID;
|
||||
use crate::helpers::database;
|
||||
use crate::constants::database_tables_names::FRIENDS_TABLE;
|
||||
use crate::helpers::database::QueryInfo;
|
||||
|
||||
/// 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
|
||||
pub fn are_friend(user_one: &UserID, user_two: &UserID) -> ResultBoxError<bool> {
|
||||
Ok(database::count(QueryInfo::new(FRIENDS_TABLE)
|
||||
@ -33,4 +49,15 @@ pub fn can_post_texts(user_id: &UserID, target_user: &UserID) -> ResultBoxError<
|
||||
.add_field("autoriser_post_page")
|
||||
.query_row(|res| res.get_legacy_bool("autoriser_post_page"))
|
||||
.or(Ok(false))
|
||||
}
|
||||
|
||||
/// 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")?,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user