fix: profile loading

This commit is contained in:
kieran 2024-10-17 22:31:12 +01:00
parent 117968bd17
commit 948276eb65
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
4 changed files with 23 additions and 10 deletions

View File

@ -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

View File

@ -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)
);

View File

@ -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<NdbProfile<'a>>,
profile_image: Option<Image<'a>>,
sub: Option<SubWrapper>,
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

View File

@ -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);