diff --git a/crates/client/src/accounts.rs b/crates/client/src/accounts.rs index 428a3f1..b84b101 100644 --- a/crates/client/src/accounts.rs +++ b/crates/client/src/accounts.rs @@ -1,12 +1,16 @@ +use std::sync::Arc; + use common::model::{Id, User}; +use gpui_kit::{Image, ImageFormat}; -use crate::{read_config, write_config}; +use crate::{read_config, read_config_bytes, remove_config, write_config}; -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone)] pub struct Account { pub user_id: Id, pub username: String, pub token: String, + pub picture: Option>, } #[derive(Debug, Clone, Default)] @@ -33,6 +37,7 @@ impl Accounts { user_id: user.id, username: user.username.clone(), token, + picture: picture(&self.server, user.id), }); self.active = Some(user.id); save(self); @@ -58,7 +63,24 @@ impl Accounts { save(self); } + pub fn set_picture(&mut self, user_id: Id, bytes: Option>) { + let name = picture_file(&self.server, user_id); + match &bytes { + Some(bytes) => write_config(&name, bytes), + None => remove_config(&name), + } + if let Some(account) = self + .list + .iter_mut() + .find(|account| account.user_id == user_id) + { + account.picture = + bytes.map(|bytes| Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes))); + } + } + pub fn forget(&mut self, user_id: Id) { + remove_config(&picture_file(&self.server, user_id)); self.remove(user_id); if self.active == Some(user_id) { self.active = None; @@ -85,6 +107,15 @@ fn file(server: &str) -> String { format!("accounts-{server}") } +fn picture_file(server: &str, user_id: Id) -> String { + format!("{}-{user_id}.jpg", file(server)) +} + +fn picture(server: &str, user_id: Id) -> Option> { + read_config_bytes(&picture_file(server, user_id)) + .map(|bytes| Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes))) +} + pub fn load(server: &str) -> Accounts { let mut accounts = Accounts { server: server.to_string(), @@ -103,6 +134,7 @@ pub fn load(server: &str) -> Accounts { user_id, username: username.to_string(), token: token.to_string(), + picture: picture(server, user_id), }); } } diff --git a/crates/client/src/gui.rs b/crates/client/src/gui.rs index a908f12..034f74b 100644 --- a/crates/client/src/gui.rs +++ b/crates/client/src/gui.rs @@ -947,6 +947,13 @@ impl Yumush { yumush.chat.avatar_requests.remove(&avatar_id); if let Ok(bytes) = result { style::picture(avatar_id, &bytes); + if let Some(me) = yumush + .user + .as_ref() + .filter(|me| me.avatar_id == Some(avatar_id)) + { + yumush.accounts.set_picture(me.id, Some(bytes.clone())); + } yumush.chat.avatars.insert( avatar_id, Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes)), @@ -959,6 +966,14 @@ impl Yumush { } pub fn request_user_avatar(&mut self, user: &User, cx: &mut Context) { + if user.avatar_id.is_none() + && self + .accounts + .get(user.id) + .is_some_and(|account| account.picture.is_some()) + { + self.accounts.set_picture(user.id, None); + } if let Some(avatar_id) = user.avatar_id { self.request_avatar(avatar_id, cx); } diff --git a/crates/client/src/gui/auth.rs b/crates/client/src/gui/auth.rs index 97630b6..3202acc 100644 --- a/crates/client/src/gui/auth.rs +++ b/crates/client/src/gui/auth.rs @@ -293,8 +293,14 @@ impl Yumush { .items_center() .gap(px(space::SM)) .rounded(px(radius::MD)) - .child( - badge( + .child(match &account.picture { + Some(picture) => div() + .size(px(size::CHIP)) + .flex_shrink_0() + .rounded_full() + .overflow_hidden() + .child(gpui_kit::img(picture.clone()).size_full()), + None => badge( palette, account.user_id, &account.username, @@ -302,7 +308,7 @@ impl Yumush { text(text::CAPTION), ) .rounded_full(), - ) + }) .child(div().flex_1().truncate().child(account.username.clone())) .child( clickable( diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index 18a7d6a..a0a8f15 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -35,7 +35,17 @@ pub fn read_config(name: &str) -> Option { fs::read_to_string(config_file(name)?).ok() } -pub fn write_config(name: &str, content: &str) { +pub fn read_config_bytes(name: &str) -> Option> { + fs::read(config_file(name)?).ok() +} + +pub fn remove_config(name: &str) { + if let Some(path) = config_file(name) { + let _ = fs::remove_file(path); + } +} + +pub fn write_config(name: &str, content: impl AsRef<[u8]>) { let Some(path) = config_file(name) else { return; }; @@ -47,7 +57,7 @@ pub fn write_config(name: &str, content: &str) { options.write(true).create(true).truncate(true); #[cfg(unix)] std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); - options.open(&path)?.write_all(content.as_bytes()) + options.open(&path)?.write_all(content.as_ref()) }); if let Err(error) = result {