1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-27 05:58:51 +00:00

Returns the list of conversations for a group in the API

This commit is contained in:
Pierre HUBERT 2021-04-05 14:26:50 +02:00
parent fbc1a59829
commit 16ba9486f7
2 changed files with 20 additions and 4 deletions

View File

@ -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<ConversationAPI>,
}
impl AdvancedGroupApi {
/// Construct a new advanced group membership instance
pub fn new(g: &Group, user_id: Option<UserID>) -> ResultBoxError<AdvancedGroupApi> {
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),
})
}
}

View File

@ -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<UserID>) -> ResultBoxError<GroupApi> {
pub fn new(g: &Group, user_id: Option<UserID>) -> Res<GroupApi> {
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<GroupApi> {
Ok(GroupApi {
id: g.id.id(),
name: g.name.clone(),