summaryrefslogtreecommitdiffstats
path: root/src/user.rs
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@bunny.garden>2025-06-01 16:10:26 +0100
committerLibravatar cel 🌸 <cel@bunny.garden>2025-06-01 17:27:40 +0100
commit6ee4190a26f32bfa953302ee363ad3bb6c384ebb (patch)
tree2c3182c29d5780a0ad9c9770b5e546312bea49b4 /src/user.rs
parentf76c80c1d23177ab00c81240ee3a75d3bcda0e3b (diff)
downloadmacaw-web-6ee4190a26f32bfa953302ee363ad3bb6c384ebb.tar.gz
macaw-web-6ee4190a26f32bfa953302ee363ad3bb6c384ebb.tar.bz2
macaw-web-6ee4190a26f32bfa953302ee363ad3bb6c384ebb.zip
refactor: reorganise code
Diffstat (limited to 'src/user.rs')
-rw-r--r--src/user.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/user.rs b/src/user.rs
new file mode 100644
index 0000000..f55c0dd
--- /dev/null
+++ b/src/user.rs
@@ -0,0 +1,78 @@
+use std::ops::{Deref, DerefMut};
+
+use filamento::user::{User, UserStoreFields};
+use jid::BareJID;
+use reactive_stores::{ArcStore, Store};
+use leptos::prelude::*;
+
+use crate::{client::Client, roster::{Roster, RosterStoreFields}, state_store::{StateListener, StateStore}};
+
+#[derive(Clone)]
+pub struct MacawUser {
+ pub user: StateListener<BareJID, ArcStore<User>>,
+}
+
+impl MacawUser {
+ pub fn got_user(user: User) -> Self {
+
+ let user_state_store: StateStore<BareJID, ArcStore<User>> =
+ use_context().expect("no user state store");
+ let user = user_state_store.store(user.jid.clone(), ArcStore::new(user));
+ Self { user }
+ }
+}
+
+impl Deref for MacawUser {
+ type Target = StateListener<BareJID, ArcStore<User>>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.user
+ }
+}
+
+impl DerefMut for MacawUser {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.user
+ }
+}
+
+pub const NO_AVATAR: &str = "/assets/no-avatar.png";
+
+pub async fn get_avatar(user: Store<User>) -> String {
+ if let Some(avatar) = &user.read().avatar {
+ let client = use_context::<Client>().expect("client not in context");
+ if let Some(data) = client.file_store.get_src(avatar).await {
+ data
+ } else {
+ NO_AVATAR.to_string()
+ }
+ // TODO: enable avatar fetching
+ // format!("/files/{}", avatar)
+ } else {
+ NO_AVATAR.to_string()
+ }
+}
+
+pub fn get_name(user: Store<User>, note_to_self: bool) -> String {
+ let roster: Store<Roster> = use_context().expect("no roster in context");
+ if note_to_self {
+ let client: Client = use_context().expect("no client in context");
+ if *client.jid == *user.jid().read() {
+ return "Note to self".to_string()
+ }
+ }
+ if let Some(name) = roster
+ .contacts()
+ .read()
+ .get(&user.read().jid)
+ .map(|contact| contact.read().name.clone())
+ .unwrap_or_default()
+ {
+ name.to_string()
+ } else if let Some(nick) = &user.read().nick {
+ nick.to_string()
+ } else {
+ user.read().jid.to_string()
+ }
+}
+