1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-02-07 09:47:04 +00:00
comunicapiv3/src/api_data/conversation_api.rs

61 lines
1.8 KiB
Rust
Raw Normal View History

//! # Conversation API object
//!
//! @author Pierre Hubert
use serde::{Serialize, Serializer};
2020-07-14 08:07:55 +02:00
use crate::api_data::legacy_api_bool::LegacyBool;
use crate::data::conversation::Conversation;
/// Special implementation of conversation name (false if none / the name otherwise)
struct ConvName(Option<String>);
impl Serialize for ConvName {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer {
match &self.0 {
None => serializer.serialize_bool(false),
Some(n) => serializer.serialize_str(n)
}
}
}
#[derive(Serialize)]
#[allow(non_snake_case)]
pub struct ConversationAPI {
ID: u64,
ID_owner: u64,
last_active: u64,
name: ConvName,
following: LegacyBool,
saw_last_message: LegacyBool,
members: Vec<u64>,
canEveryoneAddMembers: bool,
can_have_call: bool,
can_have_video_call: bool,
has_call_now: bool,
}
impl ConversationAPI {
/// Construct a new Conversation instance
pub fn new(conv: &Conversation) -> ConversationAPI {
ConversationAPI {
ID: conv.id,
2020-06-25 10:08:34 +02:00
ID_owner: conv.owner_id.id(),
last_active: conv.last_active,
name: ConvName(conv.name.clone()),
following: LegacyBool(conv.following),
saw_last_message: LegacyBool(conv.saw_last_message),
2020-06-25 10:08:34 +02:00
members: conv.members.iter().map(|x| x.id()).collect(),
canEveryoneAddMembers: conv.can_everyone_add_members,
// TODO : update when call system is implemented
can_have_call: false,
can_have_video_call: false,
2020-07-14 08:07:55 +02:00
has_call_now: false,
}
}
2020-07-14 08:07:55 +02:00
pub fn for_list(l: &Vec<Conversation>) -> Vec<Self> {
l.iter().map(Self::new).collect()
}
}