diff --git a/src/api_data/advanced_group_api.rs b/src/api_data/advanced_group_api.rs index ed43a18..5218072 100644 --- a/src/api_data/advanced_group_api.rs +++ b/src/api_data/advanced_group_api.rs @@ -3,11 +3,12 @@ //! @author Pierre Hubert use serde::Serialize; +use crate::api_data::conversation_api::ConversationAPI; use crate::api_data::group_api::GroupApi; use crate::data::error::ResultBoxError; use crate::data::group::Group; use crate::data::user::UserID; -use crate::helpers::likes_helper; +use crate::helpers::{conversations_helper, groups_helper, likes_helper}; use crate::helpers::likes_helper::LikeType; #[derive(Serialize)] @@ -21,19 +22,28 @@ pub struct AdvancedGroupApi { url: String, number_likes: u64, is_liking: bool, + conversations: Vec, } impl AdvancedGroupApi { /// Construct a new advanced group membership instance pub fn new(g: &Group, user_id: Option) -> ResultBoxError { + let membership = groups_helper::get_membership(&g.id, user_id.clone())?; + + let conversations = conversations_helper::get_list_group(&g.id)? + .into_iter() + .filter(|p| p.min_group_membership_level.as_ref().unwrap() >= &membership.level) + .collect(); + Ok(AdvancedGroupApi { - base_info: GroupApi::new(g, user_id.clone())?, + base_info: GroupApi::new_with_level(g, &membership)?, is_members_list_public: g.is_members_list_public, time_create: g.time_create, description: g.description.clone().unwrap_or("null".to_string()), url: g.url.clone().unwrap_or("null".to_string()), number_likes: likes_helper::count(g.id.id(), LikeType::GROUP)? as u64, is_liking: likes_helper::is_liking(&user_id.unwrap_or(UserID::invalid()), g.id.id(), LikeType::GROUP)?, + conversations: ConversationAPI::for_list(&conversations), }) } } \ No newline at end of file diff --git a/src/api_data/group_api.rs b/src/api_data/group_api.rs index cfa86bc..a5d68a3 100644 --- a/src/api_data/group_api.rs +++ b/src/api_data/group_api.rs @@ -3,8 +3,9 @@ //! @author Pierre Hubert use serde::Serialize; -use crate::data::error::ResultBoxError; +use crate::data::error::Res; use crate::data::group::Group; +use crate::data::group_member::GroupMember; use crate::data::user::UserID; use crate::helpers::groups_helper; @@ -24,9 +25,14 @@ pub struct GroupApi { impl GroupApi { /// Construct a new group membership instance - pub fn new(g: &Group, user_id: Option) -> ResultBoxError { + pub fn new(g: &Group, user_id: Option) -> Res { let membership = groups_helper::get_membership(&g.id, user_id)?; + Self::new_with_level(g, &membership) + } + + /// Construct a new group membership instance + pub fn new_with_level(g: &Group, membership: &GroupMember) -> Res { Ok(GroupApi { id: g.id.id(), name: g.name.clone(),