summaryrefslogtreecommitdiffstats
path: root/src/chat.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/chat.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/chat.rs b/src/chat.rs
new file mode 100644
index 0000000..e40119f
--- /dev/null
+++ b/src/chat.rs
@@ -0,0 +1,90 @@
+// SPDX-FileCopyrightText: 2025 cel <cel@bunny.garden>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+use std::ops::{Deref, DerefMut};
+
+use filamento::{chat::Chat, user::User};
+use jid::BareJID;
+use leptos::prelude::*;
+use reactive_stores::ArcStore;
+
+use crate::{
+ state_store::{StateListener, StateStore},
+ user::{ArcMacawUser, MacawUser},
+};
+
+#[derive(Clone, Copy)]
+pub struct MacawChat {
+ pub chat: ArenaItem<StateListener<BareJID, ArcStore<Chat>>>,
+ pub user: MacawUser,
+ // user: StateListener<BareJID, ArcStore<User>>,
+}
+
+impl MacawChat {
+ pub fn get(&self) -> ArcStore<Chat> {
+ self.try_get_value().unwrap().get()
+ }
+}
+
+impl Deref for MacawChat {
+ type Target = ArenaItem<StateListener<BareJID, ArcStore<Chat>>>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.chat
+ }
+}
+
+impl DerefMut for MacawChat {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.chat
+ }
+}
+
+impl From<ArcMacawChat> for MacawChat {
+ fn from(value: ArcMacawChat) -> Self {
+ Self {
+ chat: ArenaItem::new_with_storage(value.chat),
+ user: value.user.into(),
+ }
+ }
+}
+
+impl From<MacawChat> for ArcMacawChat {
+ fn from(value: MacawChat) -> Self {
+ Self {
+ chat: value.chat.try_get_value().unwrap(),
+ user: value.user.into(),
+ }
+ }
+}
+
+#[derive(Clone)]
+pub struct ArcMacawChat {
+ pub chat: StateListener<BareJID, ArcStore<Chat>>,
+ pub user: ArcMacawUser,
+}
+
+impl ArcMacawChat {
+ pub async fn got_chat_and_user(chat: Chat, user: User) -> Self {
+ let chat_state_store: StateStore<BareJID, ArcStore<Chat>> =
+ use_context().expect("no chat state store");
+ let chat = chat_state_store.store(chat.correspondent.clone(), ArcStore::new(chat));
+ let user = ArcMacawUser::got_user(user).await;
+ Self { chat, user }
+ }
+}
+
+impl Deref for ArcMacawChat {
+ type Target = StateListener<BareJID, ArcStore<Chat>>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.chat
+ }
+}
+
+impl DerefMut for ArcMacawChat {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.chat
+ }
+}