2020-06-04 19:01:35 +02:00
|
|
|
//! # Conversation API object
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
use serde::{Serialize, Serializer};
|
2020-07-14 08:07:55 +02:00
|
|
|
|
2020-06-04 19:01:35 +02:00
|
|
|
use crate::api_data::legacy_api_bool::LegacyBool;
|
|
|
|
use crate::data::conversation::Conversation;
|
2021-02-08 18:14:15 +01:00
|
|
|
use crate::helpers::calls_helper;
|
2020-06-04 19:01:35 +02:00
|
|
|
|
|
|
|
/// 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(),
|
2020-06-04 19:01:35 +02:00
|
|
|
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(),
|
2020-06-04 19:01:35 +02:00
|
|
|
canEveryoneAddMembers: conv.can_everyone_add_members,
|
|
|
|
|
2021-02-08 18:14:15 +01:00
|
|
|
can_have_call: calls_helper::can_have_call(conv),
|
|
|
|
can_have_video_call: calls_helper::can_have_video_calls(conv),
|
2020-06-04 19:01:35 +02:00
|
|
|
// TODO : update when call system is implemented
|
2020-07-14 08:07:55 +02:00
|
|
|
has_call_now: false,
|
2020-06-04 19:01:35 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-14 08:07:55 +02:00
|
|
|
|
|
|
|
pub fn for_list(l: &Vec<Conversation>) -> Vec<Self> {
|
|
|
|
l.iter().map(Self::new).collect()
|
|
|
|
}
|
2020-06-04 19:01:35 +02:00
|
|
|
}
|