✨ Add Invitation value object
This commit is contained in:
@@ -5,8 +5,8 @@ use tokio::sync::broadcast::Receiver;
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
common::{Avatar, UserId},
|
common::{Avatar, UserId},
|
||||||
room::{Room, RoomId},
|
|
||||||
room_member::RoomMember,
|
room_member::RoomMember,
|
||||||
|
room::{Invitation, Room, RoomId},
|
||||||
space::Space,
|
space::Space,
|
||||||
};
|
};
|
||||||
use crate::infrastructure::messaging::matrix::account_event::AccountEvent;
|
use crate::infrastructure::messaging::matrix::account_event::AccountEvent;
|
||||||
@@ -31,7 +31,7 @@ pub trait AccountMessagingProviderInterface {
|
|||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
pub trait RoomMessagingConsumerInterface {
|
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_topic(&self, _topic: Option<String>) {}
|
||||||
async fn on_new_name(&self, _name: Option<String>) {}
|
async fn on_new_name(&self, _name: Option<String>) {}
|
||||||
|
@@ -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 async_trait::async_trait;
|
||||||
use futures::future::{join, join_all};
|
use futures::future::{join, join_all};
|
||||||
@@ -11,26 +16,58 @@ use super::{
|
|||||||
messaging_interface::{RoomMessagingConsumerInterface, RoomMessagingProviderInterface},
|
messaging_interface::{RoomMessagingConsumerInterface, RoomMessagingProviderInterface},
|
||||||
room_member::RoomMember,
|
room_member::RoomMember,
|
||||||
space::SpaceId,
|
space::SpaceId,
|
||||||
store_interface::RoomStoreProviderInterface,
|
store_interface::{RoomStoreConsumerInterface, RoomStoreProviderInterface},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::infrastructure::services::mozaik_builder::create_mozaik;
|
use crate::infrastructure::services::mozaik_builder::create_mozaik;
|
||||||
|
|
||||||
pub type RoomId = OwnedRoomId;
|
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 {
|
pub struct Room {
|
||||||
id: RoomId,
|
id: RoomId,
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
spaces: Vec<SpaceId>,
|
|
||||||
|
|
||||||
name: RefCell<Option<String>>,
|
name: RefCell<Option<String>>,
|
||||||
topic: Option<String>,
|
topic: Option<String>,
|
||||||
is_direct: Option<bool>,
|
is_direct: Option<bool>,
|
||||||
state: Option<MatrixRoomState>,
|
state: Option<MatrixRoomState>,
|
||||||
avatar: RefCell<Option<Avatar>>,
|
avatar: RefCell<Option<Avatar>>,
|
||||||
|
|
||||||
|
invitations: RefCell<HashMap<UserId, Invitation>>,
|
||||||
members: RefCell<HashMap<UserId, RoomMember>>,
|
members: RefCell<HashMap<UserId, RoomMember>>,
|
||||||
|
|
||||||
|
spaces: Vec<SpaceId>,
|
||||||
|
|
||||||
messaging_provider: Option<Rc<dyn RoomMessagingProviderInterface>>,
|
messaging_provider: Option<Rc<dyn RoomMessagingProviderInterface>>,
|
||||||
store: RefCell<Option<Rc<dyn RoomStoreProviderInterface>>>,
|
store: RefCell<Option<Rc<dyn RoomStoreProviderInterface>>>,
|
||||||
}
|
}
|
||||||
@@ -53,14 +90,17 @@ impl Room {
|
|||||||
Self {
|
Self {
|
||||||
id,
|
id,
|
||||||
|
|
||||||
spaces,
|
|
||||||
name: RefCell::new(name),
|
name: RefCell::new(name),
|
||||||
topic,
|
topic,
|
||||||
is_direct,
|
is_direct,
|
||||||
state,
|
state,
|
||||||
avatar: RefCell::new(None),
|
avatar: RefCell::new(None),
|
||||||
|
|
||||||
|
invitations: RefCell::new(HashMap::new()),
|
||||||
members: RefCell::new(HashMap::new()),
|
members: RefCell::new(HashMap::new()),
|
||||||
|
|
||||||
|
spaces,
|
||||||
|
|
||||||
messaging_provider: None,
|
messaging_provider: None,
|
||||||
store: RefCell::new(None),
|
store: RefCell::new(None),
|
||||||
}
|
}
|
||||||
@@ -110,11 +150,17 @@ impl Room {
|
|||||||
self.state.map(|state| state == MatrixRoomState::Invited)
|
self.state.map(|state| state == MatrixRoomState::Invited)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
fn add_invitation(&self, invitation: Invitation) {
|
||||||
fn add_member(&self, member: RoomMember) {
|
self.members.borrow_mut().remove(&invitation.invitee_id);
|
||||||
self.members
|
|
||||||
|
self.invitations
|
||||||
.borrow_mut()
|
.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> {
|
pub async fn get_avatar(&self) -> Option<Avatar> {
|
||||||
@@ -194,8 +240,8 @@ impl Room {
|
|||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
impl RoomMessagingConsumerInterface for Room {
|
impl RoomMessagingConsumerInterface for Room {
|
||||||
async fn on_invitation(&self) {
|
async fn on_invitation(&self, invitation: Invitation) {
|
||||||
trace!("Room::on_invitation");
|
self.add_invitation(invitation);
|
||||||
}
|
}
|
||||||
async fn on_membership(&self, member: RoomMember) {
|
async fn on_membership(&self, member: RoomMember) {
|
||||||
trace!("Room::on_membership({:?})", member);
|
trace!("Room::on_membership({:?})", member);
|
||||||
|
Reference in New Issue
Block a user