refactor DisplayName -> NostrName

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-01-01 21:03:11 -05:00
parent df82e08041
commit a7cfe9bd37
7 changed files with 88 additions and 83 deletions

View File

@@ -42,6 +42,6 @@ pub mod storage;
pub use app::Damus;
pub use error::Error;
pub use profile::DisplayName;
pub use profile::NostrName;
pub type Result<T> = std::result::Result<T, error::Error>;

View File

@@ -9,20 +9,28 @@ use crate::{
timeline::{copy_notes_into_timeline, PubkeySource, Timeline, TimelineKind, TimelineTab},
};
pub enum DisplayName<'a> {
One(&'a str),
Both {
username: &'a str,
display_name: &'a str,
},
pub struct NostrName<'a> {
pub username: Option<&'a str>,
pub display_name: Option<&'a str>,
pub nip05: Option<&'a str>,
}
impl<'a> DisplayName<'a> {
pub fn username(&self) -> &'a str {
match self {
Self::One(n) => n,
Self::Both { username, .. } => username,
impl<'a> NostrName<'a> {
pub fn name(&self) -> &'a str {
if let Some(name) = self.username {
name
} else if let Some(name) = self.display_name {
name
} else {
self.nip05.unwrap_or("??")
}
}
pub fn unknown() -> Self {
Self {
username: None,
display_name: None,
nip05: None,
}
}
}
@@ -31,19 +39,35 @@ fn is_empty(s: &str) -> bool {
s.chars().all(|c| c.is_whitespace())
}
pub fn get_profile_name<'a>(record: &ProfileRecord<'a>) -> Option<DisplayName<'a>> {
let profile = record.record().profile()?;
pub fn get_display_name<'a>(record: Option<&ProfileRecord<'a>>) -> NostrName<'a> {
if let Some(record) = record {
if let Some(profile) = record.record().profile() {
let display_name = profile.display_name().filter(|n| !is_empty(n));
let name = profile.name().filter(|n| !is_empty(n));
let username = profile.name().filter(|n| !is_empty(n));
let nip05 = if let Some(raw_nip05) = profile.nip05() {
if let Some(at_pos) = raw_nip05.find('@') {
if raw_nip05.starts_with('_') {
raw_nip05.get(at_pos + 1..)
} else {
Some(raw_nip05)
}
} else {
None
}
} else {
None
};
match (display_name, name) {
(None, None) => None,
(Some(disp), None) => Some(DisplayName::One(disp)),
(None, Some(username)) => Some(DisplayName::One(username)),
(Some(display_name), Some(username)) => Some(DisplayName::Both {
display_name,
NostrName {
username,
}),
display_name,
nip05,
}
} else {
NostrName::unknown()
}
} else {
NostrName::unknown()
}
}

View File

@@ -241,10 +241,9 @@ impl<'a> TitleNeedsDb<'a> {
let pubkey = pubkey_source.to_pubkey(deck_author);
let profile = ndb.get_profile_by_pubkey(txn, pubkey);
let m_name = profile
.ok()
.as_ref()
.and_then(|p| crate::profile::get_profile_name(p))
.map(|display_name| display_name.username());
.ok()
.map(|p| crate::profile::get_display_name(Some(p)).name());
m_name.unwrap_or("Profile")
} else {

View File

@@ -1,5 +1,5 @@
use crate::actionbar::NoteAction;
use crate::ui;
use crate::{actionbar::NoteAction, profile::get_display_name};
use egui::Sense;
use enostr::Pubkey;
use nostrdb::{Ndb, Transaction};
@@ -79,12 +79,7 @@ fn mention_ui(
ui.horizontal(|ui| {
let profile = ndb.get_profile_by_pubkey(txn, pk).ok();
let name: String =
if let Some(name) = profile.as_ref().and_then(crate::profile::get_profile_name) {
format!("@{}", name.username())
} else {
"@???".to_string()
};
let name: String = format!("@{}", get_display_name(profile.as_ref()).name());
let resp = ui.add(
egui::Label::new(egui::RichText::new(name).color(link_color).size(size))

View File

@@ -16,6 +16,7 @@ pub use reply_description::reply_desc;
use crate::{
actionbar::NoteAction,
profile::get_display_name,
ui::{self, View},
};
@@ -25,7 +26,7 @@ use enostr::{NoteId, Pubkey};
use nostrdb::{Ndb, Note, NoteKey, Transaction};
use notedeck::{CachedNote, ImageCache, NoteCache, NotedeckTextStyle};
use super::profile::{get_display_name, preview::one_line_display_name_widget};
use super::profile::preview::one_line_display_name_widget;
pub struct NoteView<'a> {
ndb: &'a Ndb,

View File

@@ -1,9 +1,10 @@
pub mod picture;
pub mod preview;
use crate::profile::get_display_name;
use crate::ui::note::NoteOptions;
use crate::{colors, images};
use crate::{notes_holder::NotesHolder, DisplayName};
use crate::{notes_holder::NotesHolder, NostrName};
use egui::load::TexturePoll;
use egui::{Label, RichText, ScrollArea, Sense};
use enostr::Pubkey;
@@ -131,43 +132,42 @@ impl<'a> ProfileView<'a> {
}
}
fn display_name_widget(
display_name: DisplayName<'_>,
add_placeholder_space: bool,
) -> impl egui::Widget + '_ {
move |ui: &mut egui::Ui| match display_name {
DisplayName::One(n) => {
let name_response = ui.add(
Label::new(RichText::new(n).text_style(NotedeckTextStyle::Heading3.text_style()))
.selectable(false),
);
if add_placeholder_space {
ui.add_space(16.0);
}
name_response
}
DisplayName::Both {
display_name,
username,
} => {
fn display_name_widget(name: NostrName<'_>, add_placeholder_space: bool) -> impl egui::Widget + '_ {
move |ui: &mut egui::Ui| -> egui::Response {
let disp_resp = name.display_name.map(|disp_name| {
ui.add(
Label::new(
RichText::new(display_name)
.text_style(NotedeckTextStyle::Heading3.text_style()),
RichText::new(disp_name).text_style(NotedeckTextStyle::Heading3.text_style()),
)
.selectable(false),
);
)
});
let username_resp = name.username.map(|username| {
ui.add(
Label::new(
RichText::new(format!("@{}", username))
.size(12.0)
.size(16.0)
.color(colors::MID_GRAY),
)
.selectable(false),
)
});
let resp = if let Some(disp_resp) = disp_resp {
if let Some(username_resp) = username_resp {
username_resp
} else {
disp_resp
}
} else {
ui.add(Label::new(RichText::new(name.name())))
};
if add_placeholder_space {
ui.add_space(16.0);
}
resp
}
}
@@ -179,14 +179,6 @@ pub fn get_profile_url<'a>(profile: Option<&ProfileRecord<'a>>) -> &'a str {
}
}
pub fn get_display_name<'a>(profile: Option<&ProfileRecord<'a>>) -> DisplayName<'a> {
if let Some(name) = profile.and_then(|p| crate::profile::get_profile_name(p)) {
name
} else {
DisplayName::One("??")
}
}
fn about_section_widget<'a, 'b>(profile: &'b ProfileRecord<'a>) -> impl egui::Widget + 'b
where
'b: 'a,

View File

@@ -1,9 +1,8 @@
use crate::ui::ProfilePic;
use crate::DisplayName;
use crate::NostrName;
use egui::{Frame, Label, RichText, Widget};
use egui_extras::Size;
use enostr::{NoteId, Pubkey};
use nostrdb::{Ndb, ProfileRecord, Transaction};
use nostrdb::ProfileRecord;
use notedeck::{ImageCache, NotedeckTextStyle, UserAccount};
@@ -175,22 +174,17 @@ pub fn get_account_url<'a>(
pub fn one_line_display_name_widget<'a>(
visuals: &egui::Visuals,
display_name: DisplayName<'a>,
display_name: NostrName<'a>,
style: NotedeckTextStyle,
) -> impl egui::Widget + 'a {
let text_style = style.text_style();
let color = visuals.noninteractive().fg_stroke.color;
move |ui: &mut egui::Ui| match display_name {
DisplayName::One(n) => ui.label(RichText::new(n).text_style(text_style).color(color)),
DisplayName::Both {
display_name,
username: _,
} => ui.label(
RichText::new(display_name)
move |ui: &mut egui::Ui| -> egui::Response {
ui.label(
RichText::new(display_name.name())
.text_style(text_style)
.color(color),
),
)
}
}