105 lines
2.4 KiB
Rust
105 lines
2.4 KiB
Rust
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
use crate::domain::model::common::Avatar;
|
|
use crate::domain::model::room::Invitation;
|
|
use crate::domain::model::space::SpaceId;
|
|
use crate::domain::model::{
|
|
room::RoomId,
|
|
room_member::RoomMember,
|
|
store_interface::{RoomStoreConsumerInterface, RoomStoreProviderInterface},
|
|
};
|
|
|
|
#[modx::props(id, is_direct, name, topic, spaces)]
|
|
#[modx::store]
|
|
pub struct Store {
|
|
id: RoomId,
|
|
|
|
is_direct: Option<bool>,
|
|
name: Option<String>,
|
|
topic: Option<String>,
|
|
|
|
avatar: Option<Avatar>,
|
|
members: Vec<RoomMember>,
|
|
invitations: Vec<Invitation>,
|
|
is_invited: bool,
|
|
|
|
spaces: Vec<SpaceId>,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Room {
|
|
store: RefCell<Store>,
|
|
|
|
domain: Rc<dyn RoomStoreConsumerInterface>,
|
|
}
|
|
|
|
impl Room {
|
|
pub fn signal(&self) -> Store {
|
|
*self.store.borrow()
|
|
}
|
|
|
|
pub fn from_domain(room: Rc<dyn RoomStoreConsumerInterface>) -> Self {
|
|
let props = StoreProps::new(
|
|
room.id().clone(),
|
|
room.is_direct(),
|
|
room.name(),
|
|
room.topic(),
|
|
room.spaces().clone(),
|
|
);
|
|
|
|
Self {
|
|
store: RefCell::new(Store::new(props)),
|
|
domain: room,
|
|
}
|
|
}
|
|
|
|
pub async fn join(&self) {
|
|
self.domain.join().await;
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub async fn get_avatar(&self) -> Option<Avatar> {
|
|
self.domain.avatar().await
|
|
}
|
|
}
|
|
|
|
impl RoomStoreProviderInterface for Room {
|
|
fn on_new_name(&self, name: Option<String>) {
|
|
let mut store = self.store.borrow_mut();
|
|
store.name.set(name);
|
|
}
|
|
|
|
fn on_new_avatar(&self, avatar: Option<Avatar>) {
|
|
let mut store = self.store.borrow_mut();
|
|
store.avatar.set(avatar);
|
|
}
|
|
|
|
fn on_new_topic(&self, topic: Option<String>) {
|
|
let mut store = self.store.borrow_mut();
|
|
store.topic.set(topic);
|
|
}
|
|
|
|
fn on_new_member(&self, member: RoomMember) {
|
|
let mut store = self.store.borrow_mut();
|
|
|
|
if member.is_account_user() {
|
|
store.is_invited.set(false);
|
|
}
|
|
|
|
store.members.write().push(member);
|
|
}
|
|
|
|
fn on_invitation(&self, invitation: Invitation) {
|
|
let mut store = self.store.borrow_mut();
|
|
|
|
if !store.is_invited() && invitation.is_account_user() {
|
|
store.is_invited.set(true);
|
|
}
|
|
|
|
store.invitations.write().push(invitation);
|
|
}
|
|
}
|