From 0b898dce52f6f9650561f02b42789d0bef8fcbc4 Mon Sep 17 00:00:00 2001 From: Adrien Date: Fri, 17 May 2024 09:31:59 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Invitation=20value=20object?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/domain/model/messaging_interface.rs | 4 +- src/domain/model/room.rs | 70 ++++++++++++++++++++----- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/domain/model/messaging_interface.rs b/src/domain/model/messaging_interface.rs index 9a1fb60..7e70b51 100644 --- a/src/domain/model/messaging_interface.rs +++ b/src/domain/model/messaging_interface.rs @@ -5,8 +5,8 @@ use tokio::sync::broadcast::Receiver; use super::{ common::{Avatar, UserId}, - room::{Room, RoomId}, room_member::RoomMember, + room::{Invitation, Room, RoomId}, space::Space, }; use crate::infrastructure::messaging::matrix::account_event::AccountEvent; @@ -31,7 +31,7 @@ pub trait AccountMessagingProviderInterface { #[async_trait(?Send)] pub trait RoomMessagingConsumerInterface { - async fn on_invitation(&self) {} + async fn on_invitation(&self, _invitation: Invitation) {} async fn on_new_topic(&self, _topic: Option) {} async fn on_new_name(&self, _name: Option) {} diff --git a/src/domain/model/room.rs b/src/domain/model/room.rs index 93d0da5..23d100a 100644 --- a/src/domain/model/room.rs +++ b/src/domain/model/room.rs @@ -1,4 +1,9 @@ -use std::{cell::RefCell, collections::HashMap, rc::Rc}; +use std::{ + cell::RefCell, + collections::HashMap, + fmt::{Debug, Formatter}, + rc::Rc, +}; use async_trait::async_trait; use futures::future::{join, join_all}; @@ -11,26 +16,58 @@ use super::{ messaging_interface::{RoomMessagingConsumerInterface, RoomMessagingProviderInterface}, room_member::RoomMember, space::SpaceId, - store_interface::RoomStoreProviderInterface, + store_interface::{RoomStoreConsumerInterface, RoomStoreProviderInterface}, }; use crate::infrastructure::services::mozaik_builder::create_mozaik; pub type RoomId = OwnedRoomId; +#[derive(PartialEq, Clone)] +pub struct Invitation { + invitee_id: UserId, + sender_id: UserId, + is_account_user: bool, +} + +impl Invitation { + pub fn new(invitee_id: UserId, sender_id: UserId, is_account_user: bool) -> Self { + Self { + invitee_id, + sender_id, + is_account_user, + } + } + + pub fn is_account_user(&self) -> bool { + self.is_account_user + } +} + +impl Debug for Invitation { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + f.debug_tuple("Invitation") + .field(&self.invitee_id) + .field(&self.sender_id) + .field(&self.is_account_user) + .finish() + } +} + pub struct Room { id: RoomId, - #[allow(dead_code)] - spaces: Vec, - name: RefCell>, topic: Option, is_direct: Option, state: Option, avatar: RefCell>, + + invitations: RefCell>, members: RefCell>, + spaces: Vec, + messaging_provider: Option>, store: RefCell>>, } @@ -53,14 +90,17 @@ impl Room { Self { id, - spaces, name: RefCell::new(name), topic, is_direct, state, avatar: RefCell::new(None), + + invitations: RefCell::new(HashMap::new()), members: RefCell::new(HashMap::new()), + spaces, + messaging_provider: None, store: RefCell::new(None), } @@ -110,11 +150,17 @@ impl Room { self.state.map(|state| state == MatrixRoomState::Invited) } - #[allow(dead_code)] - fn add_member(&self, member: RoomMember) { - self.members + fn add_invitation(&self, invitation: Invitation) { + self.members.borrow_mut().remove(&invitation.invitee_id); + + self.invitations .borrow_mut() - .insert(member.id().clone(), member); + .insert(invitation.invitee_id.clone(), invitation.clone()); + + if let Some(store) = self.store.borrow().as_ref() { + store.on_invitation(invitation); + } + } } pub async fn get_avatar(&self) -> Option { @@ -194,8 +240,8 @@ impl Room { #[async_trait(?Send)] impl RoomMessagingConsumerInterface for Room { - async fn on_invitation(&self) { - trace!("Room::on_invitation"); + async fn on_invitation(&self, invitation: Invitation) { + self.add_invitation(invitation); } async fn on_membership(&self, member: RoomMember) { trace!("Room::on_membership({:?})", member);