2020-06-04 17:01:35 +00:00
|
|
|
//! # Conversation information
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
2021-03-04 17:51:52 +00:00
|
|
|
use crate::data::group_id::GroupID;
|
2020-06-04 17:01:35 +00:00
|
|
|
use crate::data::user::UserID;
|
|
|
|
|
2021-03-04 17:51:52 +00:00
|
|
|
#[derive(Copy, Debug, PartialEq, Eq, Clone, Hash)]
|
|
|
|
pub struct ConvID(u64);
|
2021-02-06 07:55:14 +00:00
|
|
|
|
2021-03-04 17:51:52 +00:00
|
|
|
impl ConvID {
|
|
|
|
pub fn new(id: u64) -> Self {
|
|
|
|
ConvID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn id(&self) -> u64 {
|
|
|
|
self.0.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ConversationMember {
|
|
|
|
pub member_id: u64,
|
|
|
|
pub conv_id: ConvID,
|
|
|
|
pub user_id: UserID,
|
|
|
|
pub added_on: u64,
|
|
|
|
pub following: bool,
|
|
|
|
pub is_admin: bool,
|
|
|
|
pub last_message_seen: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-06-04 17:01:35 +00:00
|
|
|
pub struct Conversation {
|
2021-02-06 07:55:14 +00:00
|
|
|
pub id: ConvID,
|
2020-06-04 17:01:35 +00:00
|
|
|
pub name: Option<String>,
|
2021-03-04 17:51:52 +00:00
|
|
|
pub color: Option<String>,
|
|
|
|
pub background: Option<String>,
|
|
|
|
pub creation_time: u64,
|
|
|
|
pub group_id: Option<GroupID>,
|
2020-06-04 17:01:35 +00:00
|
|
|
pub can_everyone_add_members: bool,
|
2021-03-04 17:51:52 +00:00
|
|
|
pub last_activity: u64,
|
|
|
|
pub members: Vec<ConversationMember>,
|
|
|
|
}
|
2020-06-04 17:01:35 +00:00
|
|
|
|
2021-03-04 17:51:52 +00:00
|
|
|
impl PartialEq for Conversation {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id.eq(&other.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Conversation {}
|
|
|
|
|
|
|
|
impl Conversation {
|
|
|
|
/// Get the IDs of the members in the conversation
|
|
|
|
pub fn members_ids(&self) -> Vec<UserID> {
|
|
|
|
self.members.iter().map(|m| m.user_id.clone()).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check out whether this conversation is managed or not
|
|
|
|
pub fn is_managed(&self) -> bool {
|
|
|
|
self.group_id.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check out whether a given user is an admin of a conversation or not
|
|
|
|
pub fn is_admin(&self, user_id: &UserID) -> bool {
|
|
|
|
self
|
|
|
|
.members
|
|
|
|
.iter()
|
|
|
|
.any(|m| m.user_id == user_id && m.is_admin)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check out whether a user is the last administrator of a conversation or not
|
|
|
|
pub fn is_last_admin(&self, user_id: &UserID) -> bool {
|
|
|
|
let admins: Vec<&ConversationMember> = self.members
|
|
|
|
.iter()
|
|
|
|
.filter(|m| m.is_admin)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
admins.len() == 1 && admins[0].user_id == user_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Structure used to update the list of members of the conversation
|
|
|
|
pub struct ConversationMemberSetting {
|
|
|
|
pub user_id: UserID,
|
|
|
|
pub set_admin: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Structure used to update conversation settings
|
|
|
|
pub struct NewConversationSettings {
|
|
|
|
pub conv_id: ConvID,
|
|
|
|
pub color: Option<String>,
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub can_everyone_add_members: bool,
|
2020-06-04 17:01:35 +00:00
|
|
|
}
|