mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Can get the latest posts
This commit is contained in:
		@@ -201,6 +201,12 @@ impl QueryInfo {
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Add a custom Group ID WHERE value
 | 
			
		||||
    pub fn add_custom_where_argument_group_id(mut self, val: &GroupID) -> QueryInfo {
 | 
			
		||||
        self.custom_where_ars.push(mysql::Value::UInt(val.id()));
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Add a custom string WHERE value
 | 
			
		||||
    pub fn add_custom_where_argument_str(mut self, val: &str) -> QueryInfo {
 | 
			
		||||
        self.custom_where_ars.push(mysql::Value::from(val));
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,8 @@ use crate::helpers::database::QueryInfo;
 | 
			
		||||
 | 
			
		||||
/// Structure used to get a friend information
 | 
			
		||||
pub struct GetFriendsQuery {
 | 
			
		||||
    only_accepted: bool,
 | 
			
		||||
    only_following: bool,
 | 
			
		||||
    target_user: UserID,
 | 
			
		||||
    friend_id: Option<UserID>,
 | 
			
		||||
}
 | 
			
		||||
@@ -22,11 +24,23 @@ impl GetFriendsQuery {
 | 
			
		||||
    /// Construct a new request
 | 
			
		||||
    pub fn new(target_user: &UserID) -> GetFriendsQuery {
 | 
			
		||||
        GetFriendsQuery {
 | 
			
		||||
            only_accepted: false,
 | 
			
		||||
            only_following: false,
 | 
			
		||||
            target_user: target_user.clone(),
 | 
			
		||||
            friend_id: None,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn set_only_accepted(mut self, accepted: bool) -> GetFriendsQuery {
 | 
			
		||||
        self.only_accepted = accepted;
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn set_only_following(mut self, following: bool) -> GetFriendsQuery {
 | 
			
		||||
        self.only_following = following;
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get the list of friends
 | 
			
		||||
    pub fn exec(self) -> ResultBoxError<Vec<Friend>> {
 | 
			
		||||
        get_list(&self)
 | 
			
		||||
@@ -58,6 +72,14 @@ fn get_list(friend_query: &GetFriendsQuery) -> ResultBoxError<Vec<Friend>> {
 | 
			
		||||
        query = query.cond_user_id("ID_amis", friend_id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if friend_query.only_accepted {
 | 
			
		||||
        query = query.cond_legacy_bool("f.actif", true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if friend_query.only_following {
 | 
			
		||||
        query = query.cond_legacy_bool("f.abonnement", true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    query.exec(db_to_friend)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -152,10 +152,10 @@ pub fn set_following(g: &GroupID, u: &UserID, follow: bool) -> ResultBoxError {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get the list of groups of a user
 | 
			
		||||
pub fn get_list_user(user_id: UserID, only_followed: bool) -> ResultBoxError<Vec<GroupID>> {
 | 
			
		||||
pub fn get_list_user(user_id: &UserID, only_followed: bool) -> ResultBoxError<Vec<GroupID>> {
 | 
			
		||||
    let mut query = database::QueryInfo::new(GROUPS_MEMBERS_TABLE)
 | 
			
		||||
        .add_field("groups_id")
 | 
			
		||||
        .cond_user_id("user_id", &user_id);
 | 
			
		||||
        .cond_user_id("user_id", user_id);
 | 
			
		||||
 | 
			
		||||
    if only_followed {
 | 
			
		||||
        query = query.cond_legacy_bool("following", true);
 | 
			
		||||
 
 | 
			
		||||
@@ -78,6 +78,11 @@ impl PostsQuery {
 | 
			
		||||
    pub fn get_group(self, group_id: &GroupID) -> ResultBoxError<Vec<Post>> {
 | 
			
		||||
        get_group(&self, group_id)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get the latest posts for a user
 | 
			
		||||
    pub fn get_latest(self, include_groups_posts: bool) -> ResultBoxError<Vec<Post>> {
 | 
			
		||||
        get_latest(&self, include_groups_posts)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get the posts of `target_id`
 | 
			
		||||
@@ -165,6 +170,66 @@ fn get_group(query: &PostsQuery, group_id: &GroupID) -> ResultBoxError<Vec<Post>
 | 
			
		||||
        .exec(db_to_post)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get the latest posts of a user
 | 
			
		||||
pub fn get_latest(query: &PostsQuery, include_group_posts: bool) -> ResultBoxError<Vec<Post>> {
 | 
			
		||||
    let user_id = query.user_id.as_ref().ok_or(ExecError::new("Can not get latest posts of no user!"))?;
 | 
			
		||||
 | 
			
		||||
    let visibility_level = PostVisibilityLevel::VISIBILITY_FRIENDS;
 | 
			
		||||
 | 
			
		||||
    // Get the list of friends of the user
 | 
			
		||||
    let friends_list = friends_helper::GetFriendsQuery::new(user_id)
 | 
			
		||||
        .set_only_accepted(true)
 | 
			
		||||
        .set_only_following(true)
 | 
			
		||||
        .exec()?;
 | 
			
		||||
 | 
			
		||||
    // Prepare the request
 | 
			
		||||
    let mut db_query = database::QueryInfo::new(POSTS_TABLE);
 | 
			
		||||
    let mut custom_where = String::new();
 | 
			
		||||
 | 
			
		||||
    // ================== MEMBERSHIP CONDITION ======================
 | 
			
		||||
    custom_where.push_str("(");
 | 
			
		||||
 | 
			
		||||
    // ========== FRIENDS POSTS ===========
 | 
			
		||||
    custom_where.push_str("(group_id = 0 AND niveau_visibilite <= ? AND (ID_personne = ?");
 | 
			
		||||
    db_query = db_query.add_custom_where_argument_u32(visibility_level.to_db());
 | 
			
		||||
    db_query = db_query.add_custom_where_argument_user_id(&user_id);
 | 
			
		||||
 | 
			
		||||
    for f in &friends_list {
 | 
			
		||||
        custom_where.push_str(" OR ID_personne = ?");
 | 
			
		||||
        db_query = db_query.add_custom_where_argument_user_id(&f.friend_id);
 | 
			
		||||
    };
 | 
			
		||||
    custom_where.push_str("))");
 | 
			
		||||
    // ========== /FRIENDS POSTS ==========
 | 
			
		||||
 | 
			
		||||
    // =========== GROUPS POSTS ===========
 | 
			
		||||
    if include_group_posts {
 | 
			
		||||
        let groups = groups_helper::get_list_user(user_id, true)?;
 | 
			
		||||
 | 
			
		||||
        for g in &groups {
 | 
			
		||||
            custom_where.push_str(" OR group_id = ?");
 | 
			
		||||
            db_query = db_query.add_custom_where_argument_group_id(g);
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
    // ========== /GROUPS POSTS ===========
 | 
			
		||||
 | 
			
		||||
    custom_where.push_str(")");
 | 
			
		||||
    // ================== /MEMBERSHIP CONDITION =====================
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // ======================= START POINT ==========================
 | 
			
		||||
    if query.start_from > 0 {
 | 
			
		||||
        custom_where.push_str("AND ID <= ?");
 | 
			
		||||
        db_query = db_query.add_custom_where_argument_u64(query.start_from);
 | 
			
		||||
    }
 | 
			
		||||
    // ====================== /START POINT ==========================
 | 
			
		||||
 | 
			
		||||
    db_query
 | 
			
		||||
        .set_custom_where(&custom_where)
 | 
			
		||||
        .set_order("ID DESC")
 | 
			
		||||
        .set_limit(query.limit)
 | 
			
		||||
        .exec(db_to_post)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get the access level of a user over a post
 | 
			
		||||
pub fn get_access_level(p: &Post, user_id: &Option<UserID>) -> ResultBoxError<PostAccessLevel> {
 | 
			
		||||
    if user_id == &p.user_id.as_option() {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user