//! # Conversation information //! //! @author Pierre Hubert use crate::data::group_id::GroupID; use crate::data::user::UserID; #[derive(Copy, Debug, PartialEq, Eq, Clone, Hash)] pub struct ConvID(u64); 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)] pub struct Conversation { pub id: ConvID, pub name: Option, pub color: Option, pub logo: Option, pub creation_time: u64, pub group_id: Option, pub can_everyone_add_members: bool, pub last_activity: u64, pub members: Vec, } 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 { 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, pub name: Option, pub can_everyone_add_members: bool, }