♻️ Add Room domain entity

This commit is contained in:
2024-04-10 13:39:45 +02:00
parent a7bccfa779
commit c580fba315
8 changed files with 447 additions and 339 deletions

View File

@@ -1 +1,2 @@
pub(crate) mod room;
pub(crate) mod session;

145
src/domain/model/room.rs Normal file
View File

@@ -0,0 +1,145 @@
use std::cell::RefCell;
use std::{collections::HashMap, sync::Arc};
use matrix_sdk::ruma::OwnedRoomId;
use matrix_sdk::{Room as MatrixRoom, RoomState as MatrixRoomState};
use tracing::error;
pub(crate) type RoomId = OwnedRoomId;
#[derive(Clone, Debug)]
pub(crate) struct Room {
id: RoomId,
name: Option<String>,
topic: Option<String>,
is_direct: Option<bool>,
state: Option<MatrixRoomState>,
}
impl Room {
fn new(
id: RoomId,
name: Option<String>,
topic: Option<String>,
is_direct: Option<bool>,
state: Option<MatrixRoomState>,
) -> Self {
Self {
id,
name,
topic,
is_direct,
state,
}
}
// TODO: Use a factory instead...
pub async fn from_matrix_room(matrix_room: &MatrixRoom) -> Self {
// let room_topic = matrix_room.topic().map(RefCell::new);
let id = RoomId::from(matrix_room.room_id());
let name = matrix_room.name();
let room_topic = matrix_room.topic();
let is_direct = match matrix_room.is_direct().await {
Ok(is_direct) => Some(is_direct),
Err(err) => {
error!("Unable to know if the room \"{id}\" is direct: {err}");
None
}
};
let state = Some(matrix_room.state());
Self::new(id, name, room_topic, is_direct, state)
// room.timeline.subscribe().await
// Arc::new(matrix_room.to_owned()),
}
pub fn id(&self) -> &OwnedRoomId {
&self.id
}
pub fn name(&self) -> &Option<String> {
&self.name
}
pub fn topic(&self) -> &Option<String> {
&self.topic
}
pub fn set_topic(&mut self, topic: Option<String>) {
self.topic = topic;
}
pub fn is_direct(&self) -> &Option<bool> {
&self.is_direct
}
pub fn state(&self) -> &Option<MatrixRoomState> {
&self.state
}
pub fn is_invited(&self) -> Option<bool> {
match self.state {
Some(state) => Some(state == MatrixRoomState::Invited),
None => None,
}
}
}
pub type ByIdRooms = HashMap<OwnedRoomId, RefCell<Room>>;
// pub type ByIdRooms = HashMap<OwnedRoomId, RefCell<Room>>;
// #[derive(Clone)]
// pub struct Room {
// // pub matrix_room: Arc<MatrixRoom>,
// pub topic: Option<RefCell<String>>,
// pub members: HashMap<OwnedUserId, RoomMember>,
// pub is_direct: Option<bool>,
// // pub timeline: Arc<Timeline>,
// }
// impl Room {
// pub async fn new(
// matrix_room: Arc<MatrixRoom>,
// topic: Option<RefCell<String>>,
// is_direct: Option<bool>,
// ) -> Self {
// // TODO: Filter events
// // let timeline = Arc::new(matrix_room.timeline_builder().build().await.ok().unwrap());
// Self {
// matrix_room,
// topic,
// members: HashMap::new(),
// is_direct,
// // timeline,
// }
// }
// pub async fn from_matrix_room(matrix_room: &MatrixRoom) -> Self {
// let room_topic = matrix_room.topic().map(RefCell::new);
// Self::new(
// Arc::new(matrix_room.to_owned()),
// room_topic,
// matrix_room.is_direct().await.ok(),
// )
// .await
// // room.timeline.subscribe().await
// }
// pub fn name(&self) -> Option<String> {
// self.matrix_room.name()
// }
// pub fn id(&self) -> OwnedRoomId {
// OwnedRoomId::from(self.matrix_room.room_id())
// }
// }
// impl PartialEq for Room {
// fn eq(&self, other: &Self) -> bool {
// // TODO: Look for a better way to compare Matrix rooms
// self.matrix_room.room_id() == other.matrix_room.room_id()
// }
// }