summaryrefslogtreecommitdiffstats
path: root/src/user.rs
blob: d92ba4f9f6b5a6937ad326da1205eb88b7a1daf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::ops::{Deref, DerefMut};

use filamento::user::{User, UserStoreFields};
use jid::BareJID;
use leptos_fetch::QueryClient;
use reactive_stores::{ArcStore, Store};
use leptos::prelude::*;

use crate::{client::Client, roster::{Roster, RosterStoreFields}};

async fn get_user(jid: BareJID) -> ArcStore<User> {
    let client: Client = use_context().expect("no client in context");
    ArcStore::new(client.get_user(jid).await.unwrap())
}

#[derive(Clone)]
pub struct MacawUser {
    pub user: ArcStore<User>,
}

impl MacawUser {
    pub fn got_user(user: User) -> Self {
        // let avatar = fetch_avatar(&user);
        // user.avatar = avatar;
        let query_client: QueryClient = expect_context();

        let jid = user.jid.clone();
        let user_store = query_client.subscribe_value_local(get_user, move || jid.clone());
        if let Some(user_store) = user_store.get() {
            user_store.set(user);
            Self { user: user_store }
        } else {
            let jid = user.jid.clone();
            let user_store = ArcStore::new(user);
            query_client.set_query_local(get_user, jid, user_store.clone());
            Self {
                user: user_store,
            }
        }
    }
}

impl Deref for MacawUser {
    type Target = 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 fetch_avatar(user: &User) -> Option<String> {
    if let Some(avatar) = &user.avatar {
        let client = use_context::<Client>().expect("client not in context");
        client.file_store.get_src(avatar).await
    } else {
        None
    }
}

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()
        }
        // avatar.clone()
        // 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()
    }
}