Files
beau-gosse-du-92/src/base.rs

125 lines
2.9 KiB
Rust

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<OwnedMxcUri>,
pub display_name: Option<String>,
pub blurhash: Option<String>,
}
impl UserInfo {
pub fn new(
avatar_url: Option<OwnedMxcUri>,
display_name: Option<String>,
blurhash: Option<String>,
) -> Self {
Self {
avatar_url,
display_name,
blurhash,
}
}
}
#[derive(Clone, Debug)]
pub struct Room {
pub matrix_room: Arc<MatrixRoom>,
pub topic: Option<RefCell<String>>,
pub members: HashMap<OwnedUserId, RoomMember>,
pub is_direct: Option<bool>,
}
impl Room {
pub fn new(
matrix_room: Arc<MatrixRoom>,
topic: Option<RefCell<String>>,
is_direct: Option<bool>,
) -> Self {
Self {
matrix_room,
topic,
members: HashMap::new(),
is_direct,
}
}
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()
}
}
pub type ByIdRooms = HashMap<OwnedRoomId, RefCell<Room>>;
pub type ByIdUserInfos = HashMap<OwnedUserId, UserInfo>;
#[derive(Clone)]
pub struct Store {
pub is_logged: bool,
pub rooms: ByIdRooms,
pub user_infos: ByIdUserInfos,
pub user_id: Option<OwnedUserId>,
}
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<Requester>,
pub store: Store,
}
impl AppSettings {
pub fn new() -> Self {
Self {
requester: None,
store: Store::new(),
}
}
}
pub static APP_SETTINGS: AtomRef<AppSettings> = AtomRef(|_| AppSettings::new());
pub static ROOMS: AtomRef<ByIdRooms> = AtomRef(|_| ByIdRooms::new());