diff --git a/src/api_data/user_info.rs b/src/api_data/user_info.rs index 06b83f5..8bb85a8 100644 --- a/src/api_data/user_info.rs +++ b/src/api_data/user_info.rs @@ -36,6 +36,7 @@ struct APIAdvancedInfo { allowPostFromFriendOnHisPage: bool, account_creation_time: u64, backgroundImage: String, + number_friends: usize, } impl APIUserInfo { @@ -62,6 +63,13 @@ impl APIUserInfo { pub fn new_advanced_info(user_id: Option, info: &User) -> ResultBoxError { 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 user.advanced_info = Some(APIAdvancedInfo { friend_list_public: info.public_friends_list, @@ -71,6 +79,7 @@ impl APIUserInfo { allowPostFromFriendOnHisPage: info.allow_posts_from_friends, account_creation_time: info.account_creation_time, backgroundImage: background_image_helper::get_url(info.id), + number_friends, }); Ok(user) diff --git a/src/helpers/friends_helper.rs b/src/helpers/friends_helper.rs index 706f875..dd9827d 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -15,4 +15,12 @@ pub fn are_friend(user_one: UserID, user_two: UserID) -> ResultBoxError { .cond_i64("ID_personne", user_one) .cond_i64("ID_amis", user_two) .cond_i64("actif", 1))? > 0) +} + +/// Count the number of friends of a user +pub fn count_friends(user_id: UserID) -> ResultBoxError { + QueryInfo::new(FRIENDS_TABLE) + .cond_i64("ID_amis", user_id) + .cond_u32("actif", 1) + .exec_count() } \ No newline at end of file