summaryrefslogtreecommitdiffstats
path: root/src/user.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/user.rs45
1 files changed, 35 insertions, 10 deletions
diff --git a/src/user.rs b/src/user.rs
index e62ebea..fb722c6 100644
--- a/src/user.rs
+++ b/src/user.rs
@@ -12,11 +12,16 @@ pub struct MacawUser {
pub user: ArenaItem<ArcMacawUser>,
// TODO: just store avatar src in user
// pub avatar: String,
+ // pub avatar: RwSignal<String>,
}
impl MacawUser {
pub fn get(&self) -> ArcStore<User> {
- self.try_get_value().unwrap().get()
+ self.try_get_value().unwrap().get().0
+ }
+
+ pub fn avatar(&self) -> ArcRwSignal<String> {
+ self.try_get_value().unwrap().get().1
}
}
@@ -38,6 +43,7 @@ impl From<ArcMacawUser> for MacawUser {
fn from(value: ArcMacawUser) -> Self {
Self {
user: ArenaItem::new_with_storage(value),
+ // avatar: value.avatar.into(),
}
}
}
@@ -50,21 +56,29 @@ impl From<MacawUser> for ArcMacawUser {
#[derive(Clone)]
pub struct ArcMacawUser {
- pub user: StateListener<BareJID, ArcStore<User>>,
+ pub user: StateListener<BareJID, (ArcStore<User>, ArcRwSignal<String>)>,
}
impl ArcMacawUser {
- pub fn got_user(user: User) -> Self {
-
- let user_state_store: StateStore<BareJID, ArcStore<User>> =
+ pub async fn got_user(user: User) -> Self {
+ let user_state_store: StateStore<BareJID, (ArcStore<User>, ArcRwSignal<String>)> =
use_context().expect("no user state store");
- let user = user_state_store.store(user.jid.clone(), ArcStore::new(user));
- Self { user }
+ let old_user = user_state_store.get_listener(user.jid.clone());
+ let user = if let Some(old_user) = old_user {
+ old_user.update(|(old_user, _avatar)| { old_user.set(user); });
+ old_user
+ } else {
+ let avatar = fetch_avatar(user.avatar.as_deref()).await;
+ let avatar = ArcRwSignal::new(avatar);
+ user_state_store.store(user.jid.clone(), (ArcStore::new(user), avatar))
+ };
+ let user = ArcMacawUser { user };
+ user
}
}
impl Deref for ArcMacawUser {
- type Target = StateListener<BareJID, ArcStore<User>>;
+ type Target = StateListener<BareJID, (ArcStore<User>, ArcRwSignal<String>)>;
fn deref(&self) -> &Self::Target {
&self.user
@@ -79,6 +93,19 @@ impl DerefMut for ArcMacawUser {
pub const NO_AVATAR: &str = "/assets/no-avatar.png";
+pub async fn fetch_avatar(id: Option<&str>) -> String {
+ if let Some(avatar) = id {
+ 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()
+ }
+ } else {
+ NO_AVATAR.to_string()
+ }
+}
+
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");
@@ -87,8 +114,6 @@ pub async fn get_avatar(user: Store<User>) -> String {
} else {
NO_AVATAR.to_string()
}
- // TODO: enable avatar fetching
- // format!("/files/{}", avatar)
} else {
NO_AVATAR.to_string()
}