2020-06-04 19:01:35 +02:00
|
|
|
//! # Conversation API object
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
2021-03-04 18:51:52 +01:00
|
|
|
use serde::Serialize;
|
2020-07-14 08:07:55 +02:00
|
|
|
|
2021-02-10 18:14:17 +01:00
|
|
|
use crate::controllers::calls_controller;
|
2021-03-04 18:51:52 +01:00
|
|
|
use crate::data::conversation::{Conversation, ConversationMember};
|
2021-02-08 18:14:15 +01:00
|
|
|
use crate::helpers::calls_helper;
|
2021-03-07 15:06:44 +01:00
|
|
|
use crate::utils::user_data_utils::user_data_url;
|
2020-06-04 19:01:35 +02:00
|
|
|
|
2021-03-04 18:51:52 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct ConversationMembersAPI {
|
|
|
|
user_id: u64,
|
|
|
|
last_message_seen: u64,
|
2021-03-05 17:07:39 +01:00
|
|
|
last_access: u64,
|
2021-03-04 18:51:52 +01:00
|
|
|
following: bool,
|
|
|
|
is_admin: bool,
|
2020-06-04 19:01:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct ConversationAPI {
|
2021-03-04 18:51:52 +01:00
|
|
|
id: u64,
|
|
|
|
last_activity: u64,
|
|
|
|
name: Option<String>,
|
|
|
|
color: Option<String>,
|
2021-03-05 14:20:50 +01:00
|
|
|
logo: Option<String>,
|
2021-03-04 18:51:52 +01:00
|
|
|
group_id: Option<u64>,
|
|
|
|
members: Vec<ConversationMembersAPI>,
|
|
|
|
can_everyone_add_members: bool,
|
2020-06-04 19:01:35 +02:00
|
|
|
can_have_call: bool,
|
|
|
|
can_have_video_call: bool,
|
|
|
|
has_call_now: bool,
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:51:52 +01:00
|
|
|
impl ConversationMembersAPI {
|
|
|
|
pub fn new(m: &ConversationMember) -> Self {
|
|
|
|
Self {
|
|
|
|
user_id: m.user_id.id(),
|
|
|
|
last_message_seen: m.last_message_seen,
|
|
|
|
following: m.following,
|
|
|
|
is_admin: m.is_admin,
|
2021-03-05 17:07:39 +01:00
|
|
|
last_access: m.last_access,
|
2021-03-04 18:51:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 19:01:35 +02:00
|
|
|
impl ConversationAPI {
|
|
|
|
/// Construct a new Conversation instance
|
|
|
|
pub fn new(conv: &Conversation) -> ConversationAPI {
|
|
|
|
ConversationAPI {
|
2021-03-04 18:51:52 +01:00
|
|
|
id: conv.id.id(),
|
|
|
|
last_activity: conv.last_activity,
|
|
|
|
name: conv.name.clone(),
|
|
|
|
members: conv.members.iter().map(ConversationMembersAPI::new).collect(),
|
|
|
|
can_everyone_add_members: conv.can_everyone_add_members,
|
|
|
|
color: conv.color.clone(),
|
2021-03-07 15:06:44 +01:00
|
|
|
logo: conv.logo.as_ref().map(|s| user_data_url(s)),
|
2021-03-04 18:51:52 +01:00
|
|
|
group_id: conv.group_id.as_ref().map(|i| i.id()),
|
2020-06-04 19:01:35 +02:00
|
|
|
|
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),
|
2021-02-10 18:14:17 +01:00
|
|
|
has_call_now: calls_controller::is_conversation_having_call(&conv.id),
|
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
|
|
|
}
|