diff --git a/src/api_data/user_info.rs b/src/api_data/user_info.rs index f04bf14..783d26c 100644 --- a/src/api_data/user_info.rs +++ b/src/api_data/user_info.rs @@ -39,6 +39,7 @@ struct APIAdvancedInfo { backgroundImage: String, number_friends: usize, pageLikes: usize, + user_page_like: bool, } impl APIUserInfo { @@ -64,13 +65,16 @@ impl APIUserInfo { /// Get advanced user information pub fn new_advanced_info(user_id: Option, info: &User) -> ResultBoxError { let mut user = APIUserInfo::new(user_id, info)?; + let signed_in = user_id.is_some(); // 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 - }; + friends_helper::count_friends(info.id)? + } else { 0 }; + + let likes_page = if signed_in { + likes_helper::is_liking(user_id.unwrap(), info.id as u64, LikeType::USER)? + } else { false }; // Set advanced user information user.advanced_info = Some(APIAdvancedInfo { @@ -83,6 +87,7 @@ impl APIUserInfo { backgroundImage: background_image_helper::get_url(info.id), number_friends, pageLikes: likes_helper::count(info.id as u64, LikeType::USER)?, + user_page_like: likes_page, }); Ok(user) diff --git a/src/helpers/likes_helper.rs b/src/helpers/likes_helper.rs index 3ab04ec..f968a9e 100644 --- a/src/helpers/likes_helper.rs +++ b/src/helpers/likes_helper.rs @@ -5,6 +5,7 @@ use crate::data::error::ResultBoxError; use crate::helpers::database::QueryInfo; use crate::constants::database_tables_names::LIKES_TABLE; +use crate::data::user::UserID; pub enum LikeType { USER, @@ -32,4 +33,17 @@ pub fn count(id: u64, kind: LikeType) -> ResultBoxError { .cond_u64("ID_type", id) .cond("type", kind.to_db_type().as_ref()) .exec_count() +} + +/// Check if a user likes an element or not +pub fn is_liking(user_id: UserID, id: u64, kind: LikeType) -> ResultBoxError { + if user_id == 0 { + return Ok(false); + } + + Ok(QueryInfo::new(LIKES_TABLE) + .cond_u64("ID_type", id) + .cond_i64("ID_personne", user_id) + .cond("type", kind.to_db_type().as_ref()) + .exec_count()? > 0) } \ No newline at end of file