From c2918fbc78d578514ab86a35773b519106982e39 Mon Sep 17 00:00:00 2001 From: Adrien Date: Fri, 10 May 2024 19:56:39 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Add=20RoomMember=20value=20objec?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/domain/model/mod.rs | 1 + src/domain/model/room.rs | 6 ++- src/domain/model/room_member.rs | 90 +++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/domain/model/room_member.rs diff --git a/src/domain/model/mod.rs b/src/domain/model/mod.rs index 93fb28e..8a6ead6 100644 --- a/src/domain/model/mod.rs +++ b/src/domain/model/mod.rs @@ -2,6 +2,7 @@ pub(crate) mod account; pub(crate) mod common; pub(crate) mod messaging_interface; pub(crate) mod room; +pub(crate) mod room_member; pub(crate) mod session; pub(crate) mod space; pub(crate) mod store_interface; diff --git a/src/domain/model/room.rs b/src/domain/model/room.rs index a54cf01..d7c6de9 100644 --- a/src/domain/model/room.rs +++ b/src/domain/model/room.rs @@ -114,7 +114,9 @@ impl Room { #[allow(dead_code)] fn add_member(&self, member: RoomMember) { - self.members.borrow_mut().insert(member.id.clone(), member); + self.members + .borrow_mut() + .insert(member.id().clone(), member); } pub async fn get_avatar(&self) -> Option { @@ -153,7 +155,7 @@ impl Room { let mut other_members = Vec::<&RoomMember>::new(); for member in &members { - if member.is_account_user { + if member.is_account_user() { account_member = Some(member); } else { other_members.push(member); diff --git a/src/domain/model/room_member.rs b/src/domain/model/room_member.rs new file mode 100644 index 0000000..8714c74 --- /dev/null +++ b/src/domain/model/room_member.rs @@ -0,0 +1,90 @@ +use std::{ + cell::RefCell, + fmt::{Debug, Formatter}, + rc::Rc, +}; + +use matrix_sdk::{room::RoomMember as MatrixRoomMember, ruma::OwnedRoomId}; +use tracing::error; + +use super::{ + common::{Avatar, UserId}, + messaging_interface::MemberMessagingProviderInterface, + room::RoomId, +}; + +#[derive(Clone)] +pub struct RoomMember { + id: UserId, + room_id: RoomId, + is_account_user: bool, + + #[allow(dead_code)] + avatar: RefCell>, + + messaging_provider: Rc, +} + +impl RoomMember { + fn new( + id: UserId, + room_id: RoomId, + is_account_user: bool, + messaging_provider: Rc, + ) -> Self { + Self { + id, + room_id, + is_account_user, + avatar: RefCell::new(None), + messaging_provider, + } + } + + // TODO: Use a factory instead... + pub async fn from_matrix( + matrix_room_member: &MatrixRoomMember, + room_id: &OwnedRoomId, + messaging_provider: Rc, + ) -> Self { + Self::new( + UserId::from(matrix_room_member.user_id()), + room_id.clone(), + matrix_room_member.is_account_user(), + messaging_provider, + ) + } + + pub fn id(&self) -> &UserId { + &self.id + } + + #[allow(dead_code)] + pub fn room_id(&self) -> &RoomId { + &self.room_id + } + + pub fn is_account_user(&self) -> bool { + self.is_account_user + } + + pub async fn get_avatar(&self) -> Option { + match self + .messaging_provider + .get_avatar(&self.room_id, &self.id) + .await + { + Ok(avatar) => avatar, + Err(err) => { + error!("err={}", err); + None + } + } + } +} + +impl Debug for RoomMember { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + f.debug_struct("RoomMember").field("id", &self.id).finish() + } +}