Add Invitation value object

This commit is contained in:
2024-05-17 09:31:59 +02:00
parent cbe32c250e
commit 0b898dce52
2 changed files with 60 additions and 14 deletions

View File

@@ -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<String>) {}
async fn on_new_name(&self, _name: Option<String>) {}

View File

@@ -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<SpaceId>,
name: RefCell<Option<String>>,
topic: Option<String>,
is_direct: Option<bool>,
state: Option<MatrixRoomState>,
avatar: RefCell<Option<Avatar>>,
invitations: RefCell<HashMap<UserId, Invitation>>,
members: RefCell<HashMap<UserId, RoomMember>>,
spaces: Vec<SpaceId>,
messaging_provider: Option<Rc<dyn RoomMessagingProviderInterface>>,
store: RefCell<Option<Rc<dyn RoomStoreProviderInterface>>>,
}
@@ -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<Avatar> {
@@ -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);