🚧 Add RoomMember value object

This commit is contained in:
2024-05-10 19:56:39 +02:00
parent bfa1539d23
commit c2918fbc78
3 changed files with 95 additions and 2 deletions

View File

@@ -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<Option<Avatar>>,
messaging_provider: Rc<dyn MemberMessagingProviderInterface>,
}
impl RoomMember {
fn new(
id: UserId,
room_id: RoomId,
is_account_user: bool,
messaging_provider: Rc<dyn MemberMessagingProviderInterface>,
) -> 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<dyn MemberMessagingProviderInterface>,
) -> 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<Avatar> {
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()
}
}