mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 16:35:17 +00:00
Can get the list of memberships of a user
This commit is contained in:
@ -40,4 +40,6 @@ pub mod posts_targets_api;
|
||||
pub mod res_create_comment;
|
||||
pub mod res_number_unread_notifications;
|
||||
pub mod res_count_all_unreads;
|
||||
pub mod notification_api;
|
||||
pub mod notification_api;
|
||||
pub mod user_membership_api;
|
||||
mod type_container_api;
|
@ -2,24 +2,11 @@
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use serde::{Serialize, Serializer};
|
||||
use serde::ser::SerializeMap;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::api_data::type_container_api::TypeContainerAPI;
|
||||
use crate::data::notification::Notification;
|
||||
|
||||
struct TypeContainer {
|
||||
t: String,
|
||||
}
|
||||
|
||||
impl Serialize for TypeContainer {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
|
||||
S: Serializer {
|
||||
let mut map = serializer.serialize_map(Some(1))?;
|
||||
map.serialize_entry("type", &self.t)?;
|
||||
map.end()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct NotificationAPI {
|
||||
id: u64,
|
||||
@ -30,7 +17,7 @@ pub struct NotificationAPI {
|
||||
on_elem_id: u64,
|
||||
on_elem_type: String,
|
||||
#[serde(flatten)]
|
||||
type_container: TypeContainer,
|
||||
type_container: TypeContainerAPI,
|
||||
event_visibility: String,
|
||||
from_container_id: Option<u64>,
|
||||
from_container_type: Option<String>,
|
||||
@ -46,7 +33,7 @@ impl NotificationAPI {
|
||||
dest_user_id: n.dest_user_id.id(),
|
||||
on_elem_id: n.on_elem_id,
|
||||
on_elem_type: n.on_elem_type.to_api(),
|
||||
type_container: TypeContainer { t: n.kind.to_api() },
|
||||
type_container: TypeContainerAPI::new(n.kind.to_api()),
|
||||
event_visibility: n.visibility.to_api(),
|
||||
from_container_id: n.container_id,
|
||||
from_container_type: n.container_type.as_ref().map(|f| f.to_api()),
|
||||
|
24
src/api_data/type_container_api.rs
Normal file
24
src/api_data/type_container_api.rs
Normal file
@ -0,0 +1,24 @@
|
||||
//! Use this structure for all structures that needs a "type" field
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
use serde::{Serialize, Serializer};
|
||||
use serde::ser::SerializeMap;
|
||||
|
||||
pub struct TypeContainerAPI {
|
||||
t: String,
|
||||
}
|
||||
|
||||
impl TypeContainerAPI {
|
||||
pub fn new(t: String) -> TypeContainerAPI {
|
||||
TypeContainerAPI { t }
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for TypeContainerAPI {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
|
||||
S: Serializer {
|
||||
let mut map = serializer.serialize_map(Some(1))?;
|
||||
map.serialize_entry("type", &self.t)?;
|
||||
map.end()
|
||||
}
|
||||
}
|
54
src/api_data/user_membership_api.rs
Normal file
54
src/api_data/user_membership_api.rs
Normal file
@ -0,0 +1,54 @@
|
||||
//! # User membership API entry
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::api_data::conversation_api::ConversationAPI;
|
||||
use crate::api_data::friend_api::FriendAPI;
|
||||
use crate::api_data::type_container_api::TypeContainerAPI;
|
||||
use crate::data::user_membership::UserMembership;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct UserMembershipAPI {
|
||||
#[serde(flatten)]
|
||||
membership_type: TypeContainerAPI,
|
||||
id: Option<u64>,
|
||||
friend: Option<FriendAPI>,
|
||||
last_activity: Option<u64>,
|
||||
conv: Option<ConversationAPI>,
|
||||
}
|
||||
|
||||
impl UserMembershipAPI {
|
||||
pub fn new(m: &UserMembership) -> UserMembershipAPI {
|
||||
match &m {
|
||||
UserMembership::Group(group_id, last_activity) => UserMembershipAPI {
|
||||
membership_type: TypeContainerAPI::new("group".to_string()),
|
||||
id: Some(group_id.id()),
|
||||
friend: None,
|
||||
last_activity: Some(last_activity.clone()),
|
||||
conv: None,
|
||||
},
|
||||
|
||||
UserMembership::Friend(friend) => UserMembershipAPI {
|
||||
membership_type: TypeContainerAPI::new("friend".to_string()),
|
||||
id: None,
|
||||
friend: Some(FriendAPI::new(friend)),
|
||||
last_activity: None,
|
||||
conv: None,
|
||||
},
|
||||
|
||||
UserMembership::Conversation(conversation) => UserMembershipAPI {
|
||||
membership_type: TypeContainerAPI::new("conversation".to_string()),
|
||||
id: None,
|
||||
friend: None,
|
||||
last_activity: None,
|
||||
conv: Some(ConversationAPI::new(conversation)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_list(l: &Vec<UserMembership>) -> Vec<UserMembershipAPI> {
|
||||
l.iter().map(Self::new).collect()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user