1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-30 22:13:01 +00:00

Return the number of friends of a user

This commit is contained in:
Pierre HUBERT 2020-06-01 10:00:58 +02:00
parent 9d4b60f0ff
commit a5dab7e312
2 changed files with 17 additions and 0 deletions

View File

@ -36,6 +36,7 @@ struct APIAdvancedInfo {
allowPostFromFriendOnHisPage: bool, allowPostFromFriendOnHisPage: bool,
account_creation_time: u64, account_creation_time: u64,
backgroundImage: String, backgroundImage: String,
number_friends: usize,
} }
impl APIUserInfo { impl APIUserInfo {
@ -62,6 +63,13 @@ impl APIUserInfo {
pub fn new_advanced_info(user_id: Option<UserID>, info: &User) -> ResultBoxError<APIUserInfo> { pub fn new_advanced_info(user_id: Option<UserID>, info: &User) -> ResultBoxError<APIUserInfo> {
let mut user = APIUserInfo::new(user_id, info)?; let mut user = APIUserInfo::new(user_id, info)?;
// Check if we can return the number of friends of the user
let number_friends = if info.public_friends_list || user_id.unwrap_or(0) == info.id {
friends_helper::count_friends(info.id)?
} else {
0
};
// Set advanced user information // Set advanced user information
user.advanced_info = Some(APIAdvancedInfo { user.advanced_info = Some(APIAdvancedInfo {
friend_list_public: info.public_friends_list, friend_list_public: info.public_friends_list,
@ -71,6 +79,7 @@ impl APIUserInfo {
allowPostFromFriendOnHisPage: info.allow_posts_from_friends, allowPostFromFriendOnHisPage: info.allow_posts_from_friends,
account_creation_time: info.account_creation_time, account_creation_time: info.account_creation_time,
backgroundImage: background_image_helper::get_url(info.id), backgroundImage: background_image_helper::get_url(info.id),
number_friends,
}); });
Ok(user) Ok(user)

View File

@ -15,4 +15,12 @@ pub fn are_friend(user_one: UserID, user_two: UserID) -> ResultBoxError<bool> {
.cond_i64("ID_personne", user_one) .cond_i64("ID_personne", user_one)
.cond_i64("ID_amis", user_two) .cond_i64("ID_amis", user_two)
.cond_i64("actif", 1))? > 0) .cond_i64("actif", 1))? > 0)
}
/// 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()
} }