🚧 Add Space identity

This commit is contained in:
2024-05-10 19:24:32 +02:00
parent 0190cf9165
commit bfa1539d23
2 changed files with 92 additions and 0 deletions

View File

@@ -3,4 +3,5 @@ pub(crate) mod common;
pub(crate) mod messaging_interface; pub(crate) mod messaging_interface;
pub(crate) mod room; pub(crate) mod room;
pub(crate) mod session; pub(crate) mod session;
pub(crate) mod space;
pub(crate) mod store_interface; pub(crate) mod store_interface;

91
src/domain/model/space.rs Normal file
View File

@@ -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<Option<String>>,
topic: RefCell<Option<String>>,
#[allow(dead_code)]
avatar: RefCell<Option<Avatar>>,
children: RefCell<HashSet<RoomId>>, // We don´t expect to manage nested spaces
messaging_provider: Option<Rc<dyn SpaceMessagingProviderInterface>>,
store: RefCell<Option<Box<dyn SpaceStoreProviderInterface>>>,
}
impl PartialEq for Space {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Space {
pub fn new(id: SpaceId, name: Option<String>, topic: Option<String>) -> 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<dyn SpaceMessagingProviderInterface>) {
self.messaging_provider = Some(provider);
}
pub fn set_store(&self, store: Option<Box<dyn SpaceStoreProviderInterface>>) {
*self.store.borrow_mut() = store;
}
pub fn id(&self) -> &SpaceId {
&self.id
}
pub fn name(&self) -> Option<String> {
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<String>) {
error!("Space::on_new_topic({:?})", topic);
*self.topic.borrow_mut() = topic;
}
async fn on_new_name(&self, name: Option<String>) {
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);
}
}
}