1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 23:39:22 +00:00

Check if a user is liking a page or not

This commit is contained in:
Pierre HUBERT 2020-06-01 11:12:05 +02:00
parent cef5838ef4
commit 637408c626
2 changed files with 23 additions and 4 deletions

View File

@ -39,6 +39,7 @@ struct APIAdvancedInfo {
backgroundImage: String, backgroundImage: String,
number_friends: usize, number_friends: usize,
pageLikes: usize, pageLikes: usize,
user_page_like: bool,
} }
impl APIUserInfo { impl APIUserInfo {
@ -64,13 +65,16 @@ impl APIUserInfo {
/// Get advanced user information /// Get advanced user information
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)?;
let signed_in = user_id.is_some();
// Check if we can return the number of friends of the user // 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 { let number_friends = if info.public_friends_list || user_id.unwrap_or(0) == info.id {
friends_helper::count_friends(info.id)? friends_helper::count_friends(info.id)?
} else { } else { 0 };
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 // Set advanced user information
user.advanced_info = Some(APIAdvancedInfo { user.advanced_info = Some(APIAdvancedInfo {
@ -83,6 +87,7 @@ impl APIUserInfo {
backgroundImage: background_image_helper::get_url(info.id), backgroundImage: background_image_helper::get_url(info.id),
number_friends, number_friends,
pageLikes: likes_helper::count(info.id as u64, LikeType::USER)?, pageLikes: likes_helper::count(info.id as u64, LikeType::USER)?,
user_page_like: likes_page,
}); });
Ok(user) Ok(user)

View File

@ -5,6 +5,7 @@
use crate::data::error::ResultBoxError; use crate::data::error::ResultBoxError;
use crate::helpers::database::QueryInfo; use crate::helpers::database::QueryInfo;
use crate::constants::database_tables_names::LIKES_TABLE; use crate::constants::database_tables_names::LIKES_TABLE;
use crate::data::user::UserID;
pub enum LikeType { pub enum LikeType {
USER, USER,
@ -33,3 +34,16 @@ pub fn count(id: u64, kind: LikeType) -> ResultBoxError<usize> {
.cond("type", kind.to_db_type().as_ref()) .cond("type", kind.to_db_type().as_ref())
.exec_count() .exec_count()
} }
/// Check if a user likes an element or not
pub fn is_liking(user_id: UserID, id: u64, kind: LikeType) -> ResultBoxError<bool> {
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)
}