From 948276eb65b1a27119e70cbba0b653205fc9f40a Mon Sep 17 00:00:00 2001 From: kieran Date: Thu, 17 Oct 2024 22:31:12 +0100 Subject: [PATCH] fix: profile loading --- src/widgets/avatar.rs | 10 ++++++++++ src/widgets/chat_message.rs | 8 +++++--- src/widgets/profile.rs | 11 +++++------ src/widgets/stream_tile.rs | 4 +++- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/widgets/avatar.rs b/src/widgets/avatar.rs index f651591..e48305c 100644 --- a/src/widgets/avatar.rs +++ b/src/widgets/avatar.rs @@ -1,3 +1,4 @@ +use crate::route::RouteServices; use crate::services::image_cache::ImageCache; use crate::services::ndb_wrapper::SubWrapper; use egui::{Color32, Image, Pos2, Response, Rounding, Sense, Ui, Vec2, Widget}; @@ -36,6 +37,15 @@ impl<'a> Avatar<'a> { } } + pub fn pubkey(pk: &[u8; 32], svc: &'a RouteServices<'a>) -> Self { + let (p, sub) = svc.ndb.fetch_profile(svc.tx, pk); + Self { + image: p.and_then(|p| p.picture().and_then(|p| Some(svc.img_cache.load(p)))), + sub, + size: None, + } + } + pub fn size(mut self, size: f32) -> Self { self.size = Some(size); self diff --git a/src/widgets/chat_message.rs b/src/widgets/chat_message.rs index e5149c2..ca74db2 100644 --- a/src/widgets/chat_message.rs +++ b/src/widgets/chat_message.rs @@ -27,8 +27,10 @@ impl<'a> Widget for ChatMessage<'a> { let mut job = LayoutJob::default(); let is_host = self.stream.host().eq(self.ev.pubkey()); - let name = self - .profile.0 + let profile = self.services.ndb.get_profile_by_pubkey(self.services.tx, self.ev.pubkey()) + .map_or(None, |p| p.record().profile()); + + let name = profile .map_or("Nostrich", |f| f.name().map_or("Nostrich", |f| f)); let name_color = if is_host { @@ -46,7 +48,7 @@ impl<'a> Widget for ChatMessage<'a> { format.color = Color32::WHITE; job.append(self.ev.content(), 5.0, format.clone()); - ui.add(Avatar::from_profile(self.profile.0, self.services.img_cache).size(24.)); + ui.add(Avatar::from_profile(profile ,self.services.img_cache).size(24.)); ui.add(Label::new(job) .wrap_mode(TextWrapMode::Wrap) ); diff --git a/src/widgets/profile.rs b/src/widgets/profile.rs index a027a55..f6f59c9 100644 --- a/src/widgets/profile.rs +++ b/src/widgets/profile.rs @@ -1,28 +1,27 @@ use crate::route::RouteServices; +use crate::services::image_cache::ImageCache; use crate::services::ndb_wrapper::SubWrapper; use crate::widgets::Avatar; -use egui::{Color32, Image, Label, Response, RichText, TextWrapMode, Ui, Widget}; +use egui::{Color32, Label, Response, RichText, TextWrapMode, Ui, Widget}; use nostrdb::NdbProfile; pub struct Profile<'a> { size: f32, pubkey: &'a [u8; 32], profile: Option>, - profile_image: Option>, sub: Option, + img_cache: &'a ImageCache, } impl<'a> Profile<'a> { pub fn new(pubkey: &'a [u8; 32], services: &'a RouteServices<'a>) -> Self { let (p, sub) = services.ndb.fetch_profile(services.tx, pubkey); - let img = p - .map_or(None, |f| f.picture().map(|f| services.img_cache.load(f))); Self { pubkey, size: 40., profile: p, - profile_image: img, + img_cache: services.img_cache, sub, } } @@ -37,7 +36,7 @@ impl<'a> Widget for Profile<'a> { ui.horizontal(|ui| { ui.spacing_mut().item_spacing.x = 8.; - ui.add(Avatar::new_optional(self.profile_image).size(self.size)); + ui.add(Avatar::from_profile(self.profile, self.img_cache).size(self.size)); let name = self .profile diff --git a/src/widgets/stream_tile.rs b/src/widgets/stream_tile.rs index b682a44..5d08896 100644 --- a/src/widgets/stream_tile.rs +++ b/src/widgets/stream_tile.rs @@ -37,6 +37,8 @@ impl Widget for StreamEvent<'_> { ui.style_mut().spacing.item_spacing = Vec2::new(12., 16.); let host = self.event.host(); + let (host_profile, _sub) = self.services.ndb.fetch_profile(self.services.tx, host); + let w = ui.available_width(); let h = (w / 16.0) * 9.0; let img_size = Vec2::new(w, h); @@ -56,7 +58,7 @@ impl Widget for StreamEvent<'_> { }); } ui.horizontal(|ui| { - ui.add(Avatar::from_profile(None, self.services.img_cache).size(40.)); + ui.add(Avatar::from_profile(host_profile, self.services.img_cache).size(40.)); let title = RichText::new(self.event.title().unwrap_or("Untitled")) .size(16.) .color(Color32::WHITE);