mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Count the number of likes of a user
This commit is contained in:
		@@ -4,11 +4,12 @@
 | 
			
		||||
use serde::Serialize;
 | 
			
		||||
 | 
			
		||||
use crate::data::user::{User, UserPageStatus, UserID};
 | 
			
		||||
use crate::helpers::{friends_helper, custom_emojies_helper, background_image_helper};
 | 
			
		||||
use crate::helpers::{friends_helper, custom_emojies_helper, background_image_helper, likes_helper};
 | 
			
		||||
use crate::data::error::ResultBoxError;
 | 
			
		||||
use crate::utils::user_data_utils::user_data_url;
 | 
			
		||||
use crate::data::user::AccountImageVisibility::{EVERYONE, COMUNIC_USERS};
 | 
			
		||||
use crate::api_data::custom_emoji::CustomEmojiAPI;
 | 
			
		||||
use crate::helpers::likes_helper::LikeType;
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize)]
 | 
			
		||||
#[allow(non_snake_case)]
 | 
			
		||||
@@ -37,6 +38,7 @@ struct APIAdvancedInfo {
 | 
			
		||||
    account_creation_time: u64,
 | 
			
		||||
    backgroundImage: String,
 | 
			
		||||
    number_friends: usize,
 | 
			
		||||
    pageLikes: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl APIUserInfo {
 | 
			
		||||
@@ -80,6 +82,7 @@ impl APIUserInfo {
 | 
			
		||||
            account_creation_time: info.account_creation_time,
 | 
			
		||||
            backgroundImage: background_image_helper::get_url(info.id),
 | 
			
		||||
            number_friends,
 | 
			
		||||
            pageLikes: likes_helper::count(info.id as u64, LikeType::USER)?,
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        Ok(user)
 | 
			
		||||
 
 | 
			
		||||
@@ -20,6 +20,9 @@ pub mod database_tables_names {
 | 
			
		||||
 | 
			
		||||
    /// Custom emojis table
 | 
			
		||||
    pub const EMOJIS_TABLE: &str = "comunic_custom_emojis";
 | 
			
		||||
 | 
			
		||||
    /// Likes table
 | 
			
		||||
    pub const LIKES_TABLE: &str = "aime";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// The account image to show for user who do not have any
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										35
									
								
								src/helpers/likes_helper.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/helpers/likes_helper.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
//! # Likes helper
 | 
			
		||||
//!
 | 
			
		||||
//! Module dedicated to likes management
 | 
			
		||||
 | 
			
		||||
use crate::data::error::ResultBoxError;
 | 
			
		||||
use crate::helpers::database::QueryInfo;
 | 
			
		||||
use crate::constants::database_tables_names::LIKES_TABLE;
 | 
			
		||||
 | 
			
		||||
pub enum LikeType {
 | 
			
		||||
    USER,
 | 
			
		||||
    POST,
 | 
			
		||||
    COMMENT,
 | 
			
		||||
    GROUP
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl LikeType {
 | 
			
		||||
 | 
			
		||||
    /// Get matching database type
 | 
			
		||||
    pub fn to_db_type(&self) -> String {
 | 
			
		||||
        match self {
 | 
			
		||||
            LikeType::USER => "page".to_string(),
 | 
			
		||||
            LikeType::POST => "texte".to_string(),
 | 
			
		||||
            LikeType::COMMENT => "commentaire".to_string(),
 | 
			
		||||
            LikeType::GROUP => "group".to_string(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Count the number of likes over an element
 | 
			
		||||
pub fn count(id: u64, kind: LikeType) -> ResultBoxError<usize> {
 | 
			
		||||
    QueryInfo::new(LIKES_TABLE)
 | 
			
		||||
        .cond_u64("ID_type", id)
 | 
			
		||||
        .cond("type", kind.to_db_type().as_ref())
 | 
			
		||||
        .exec_count()
 | 
			
		||||
}
 | 
			
		||||
@@ -6,3 +6,4 @@ pub mod user_helper;
 | 
			
		||||
pub mod friends_helper;
 | 
			
		||||
pub mod custom_emojies_helper;
 | 
			
		||||
pub mod background_image_helper;
 | 
			
		||||
pub mod likes_helper;
 | 
			
		||||
		Reference in New Issue
	
	Block a user