diff --git a/src/domain/model/mod.rs b/src/domain/model/mod.rs index 9eca3df..93fb28e 100644 --- a/src/domain/model/mod.rs +++ b/src/domain/model/mod.rs @@ -3,4 +3,5 @@ pub(crate) mod common; pub(crate) mod messaging_interface; pub(crate) mod room; pub(crate) mod session; +pub(crate) mod space; pub(crate) mod store_interface; diff --git a/src/domain/model/space.rs b/src/domain/model/space.rs new file mode 100644 index 0000000..034fac5 --- /dev/null +++ b/src/domain/model/space.rs @@ -0,0 +1,91 @@ +use std::{cell::RefCell, collections::HashSet, rc::Rc}; + +use async_trait::async_trait; +use matrix_sdk::ruma::OwnedRoomId; +use tracing::error; + +use super::{ + common::Avatar, + messaging_interface::{SpaceMessagingConsumerInterface, SpaceMessagingProviderInterface}, + room::RoomId, + store_interface::SpaceStoreProviderInterface, +}; + +pub type SpaceId = OwnedRoomId; + +// TODO: Add membership? +pub struct Space { + id: SpaceId, + + name: RefCell>, + topic: RefCell>, + + #[allow(dead_code)] + avatar: RefCell>, + + children: RefCell>, // We don“t expect to manage nested spaces + + messaging_provider: Option>, + store: RefCell>>, +} + +impl PartialEq for Space { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + +impl Space { + pub fn new(id: SpaceId, name: Option, topic: Option) -> Self { + Self { + id, + + name: RefCell::new(name), + topic: RefCell::new(topic), + + #[allow(dead_code)] + avatar: RefCell::new(None), + + children: RefCell::new(HashSet::new()), + + messaging_provider: None, + store: RefCell::new(None), + } + } + + pub fn set_messaging_provider(&mut self, provider: Rc) { + self.messaging_provider = Some(provider); + } + + pub fn set_store(&self, store: Option>) { + *self.store.borrow_mut() = store; + } + + pub fn id(&self) -> &SpaceId { + &self.id + } + + pub fn name(&self) -> Option { + self.name.borrow().clone() + } +} + +#[async_trait(?Send)] +impl SpaceMessagingConsumerInterface for Space { + async fn on_child(&self, room_id: RoomId) { + error!("Space::on_child({room_id})"); + self.children.borrow_mut().insert(room_id); + } + async fn on_new_topic(&self, topic: Option) { + error!("Space::on_new_topic({:?})", topic); + *self.topic.borrow_mut() = topic; + } + async fn on_new_name(&self, name: Option) { + error!("Space::on_new_name({:?})", name); + self.name.borrow_mut().clone_from(&name); + + if let Some(store) = self.store.borrow_mut().as_mut() { + store.set_name(name); + } + } +}