use std::{cell::RefCell, collections::HashSet, rc::Rc}; use async_trait::async_trait; use matrix_sdk::ruma::OwnedRoomId; use tracing::{instrument, trace}; use super::{ common::Avatar, messaging_interface::{SpaceMessagingConsumerInterface, SpaceMessagingProviderInterface}, room::RoomId, store_interface::{SpaceStoreConsumerInterface, 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: Rc) { *self.store.borrow_mut() = Some(store); } pub fn id(&self) -> &SpaceId { &self.id } #[allow(dead_code)] pub fn name(&self) -> Option { self.name.borrow().clone() } } #[async_trait(?Send)] impl SpaceMessagingConsumerInterface for Space { #[instrument(name = "Space", skip_all)] async fn on_child(&self, room_id: RoomId) { trace!("on_child"); self.children.borrow_mut().insert(room_id); } #[instrument(name = "Space", skip_all)] async fn on_new_topic(&self, topic: Option) { trace!("on_new_topic"); *self.topic.borrow_mut() = topic; } #[instrument(name = "Space", skip_all)] async fn on_new_name(&self, name: Option) { trace!("on_new_name"); self.name.borrow_mut().clone_from(&name); if let Some(store) = self.store.borrow().as_ref() { store.set_name(name); } } } impl SpaceStoreConsumerInterface for Space { fn id(&self) -> &SpaceId { &self.id } fn name(&self) -> Option { self.name.borrow().clone() } }