1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-10-10 21:34:42 +00:00

Can get group's posts

This commit is contained in:
2020-07-06 09:20:31 +02:00
parent bb76513d02
commit fca15e15e1
3 changed files with 58 additions and 0 deletions

View File

@@ -2,8 +2,11 @@
//!
//! @author Pierre Hubert
use crate::constants::database_tables_names::POSTS_TABLE;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::group_id::GroupID;
use crate::data::group_member::GroupMembershipLevel;
use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink};
use crate::data::post::PostKind::{POST_KIND_COUNTDOWN, POST_KIND_IMAGE, POST_KIND_MOVIE, POST_KIND_PDF, POST_KIND_SURVEY, POST_KIND_WEBLINK, POST_KIND_YOUTUBE};
@@ -70,6 +73,11 @@ impl PostsQuery {
pub fn get_user(self, user_id: &UserID) -> ResultBoxError<Vec<Post>> {
get_user(&self, user_id)
}
/// Get the posts of a group
pub fn get_group(self, group_id: &GroupID) -> ResultBoxError<Vec<Post>> {
get_group(&self, group_id)
}
}
/// Get the posts of `target_id`
@@ -123,6 +131,40 @@ fn get_user(query: &PostsQuery, target_id: &UserID) -> ResultBoxError<Vec<Post>>
.exec(db_to_post)
}
/// Get the list of posts of a group
fn get_group(query: &PostsQuery, group_id: &GroupID) -> ResultBoxError<Vec<Post>> {
let membership = groups_helper::get_membership_level(group_id, query.user_id.clone())?;
let can_see_all_posts = membership <= GroupMembershipLevel::MEMBER;
let visibility_level = match can_see_all_posts {
true => PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS,
false => PostVisibilityLevel::VISIBILITY_FRIENDS
};
// Prepare request
let mut db_query = database::QueryInfo::new(POSTS_TABLE);
let mut custom_where = String::new();
// =============== VISIBILITY CONDITION ================
custom_where.push_str("(niveau_visibilite <= ?)");
db_query = db_query.add_custom_where_argument_u32(visibility_level.to_db());
// ============== /VISIBILITY 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
.cond_group_id("group_id", group_id)
.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() {