use std::{cell::RefCell, collections::HashMap, sync::Arc}; use fermi::*; use matrix_sdk::room::Room as MatrixRoom; use matrix_sdk::{ room::RoomMember, ruma::{OwnedMxcUri, OwnedRoomId, OwnedUserId}, }; use crate::matrix_interface::requester::Requester; #[derive(Clone, Debug)] pub struct UserInfo { pub avatar_url: Option, pub display_name: Option, pub blurhash: Option, } impl UserInfo { pub fn new( avatar_url: Option, display_name: Option, blurhash: Option, ) -> Self { Self { avatar_url, display_name, blurhash, } } } #[derive(Clone, Debug)] pub struct Room { pub matrix_room: Arc, pub topic: Option>, pub members: HashMap, pub is_direct: Option, } impl Room { pub fn new( matrix_room: Arc, topic: Option>, is_direct: Option, ) -> Self { Self { matrix_room, topic, members: HashMap::new(), is_direct, } } pub fn name(&self) -> Option { 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() } } pub type ByIdRooms = HashMap>; pub type ByIdUserInfos = HashMap; #[derive(Clone)] pub struct Store { pub is_logged: bool, pub rooms: ByIdRooms, pub user_infos: ByIdUserInfos, pub user_id: Option, } impl Store { pub fn new() -> Self { Self { is_logged: false, rooms: HashMap::new(), user_infos: HashMap::new(), user_id: None, } } } impl PartialEq for Store { fn eq(&self, other: &Self) -> bool { self.is_logged == other.is_logged && self.user_id == other.user_id && self.user_infos.len() == other.user_infos.len() && self .user_infos .keys() .all(|k| other.user_infos.contains_key(k)) && self.rooms.len() == other.rooms.len() && self.rooms.keys().all(|k| other.rooms.contains_key(k)) } } impl Eq for Store {} pub struct AppSettings { pub requester: Option, pub store: Store, } impl AppSettings { pub fn new() -> Self { Self { requester: None, store: Store::new(), } } } pub static APP_SETTINGS: AtomRef = AtomRef(|_| AppSettings::new()); pub static ROOMS: AtomRef = AtomRef(|_| ByIdRooms::new());