crate::names to take over name rendering functions

This commit is contained in:
Mike Dilger 2023-08-27 10:10:38 +12:00
parent 206a4b1e8a
commit 830e37d7c8
10 changed files with 48 additions and 41 deletions

View File

@ -35,6 +35,7 @@ mod fetcher;
mod filter;
mod globals;
mod media;
mod names;
mod nip05;
mod overlord;
mod people;

35
src/names.rs Normal file
View File

@ -0,0 +1,35 @@
use crate::globals::GLOBALS;
use crate::people::Person;
use nostr_types::{IdHex, PublicKey};
/// A short rendering of a `PublicKey`
pub fn pubkey_short(pk: &PublicKey) -> String {
let npub = pk.as_bech32_string();
format!(
"{}...{}",
&npub.get(0..10).unwrap_or("??????????"),
&npub
.get(npub.len() - 10..npub.len())
.unwrap_or("??????????")
)
}
/// A short rendering of an `IdHex`
pub fn hex_id_short(idhex: &IdHex) -> String {
idhex.as_str()[0..8].to_string()
}
/// A display name for a `Person`
pub fn display_name_from_person(person: &Person) -> String {
match person.display_name() {
Some(name) => name.to_owned(),
None => pubkey_short(&person.pubkey),
}
}
pub fn display_name_from_pubkey_lookup(pubkey: &PublicKey) -> String {
match GLOBALS.storage.read_person(pubkey) {
Ok(Some(person)) => display_name_from_person(&person),
_ => pubkey_short(pubkey),
}
}

View File

@ -193,7 +193,7 @@ pub(super) fn render_plain(ui: &mut Ui, note: &Ref<NoteData>, textspan: &Span, a
}
pub(super) fn render_profile_link(app: &mut GossipUi, ui: &mut Ui, pubkey: &PublicKey) {
let nam = GossipUi::display_name_from_pubkey_lookup(pubkey);
let nam = crate::names::display_name_from_pubkey_lookup(pubkey);
let nam = format!("@{}", nam);
if ui.link(&nam).clicked() {
app.set_page(Page::Person(pubkey.to_owned()));
@ -207,7 +207,7 @@ pub(super) fn render_event_link(
link_to_id: Id,
) {
let idhex: IdHex = link_to_id.into();
let nam = format!("#{}", GossipUi::hex_id_short(&idhex));
let nam = format!("#{}", crate::names::hex_id_short(&idhex));
if ui.link(&nam).clicked() {
app.set_page(Page::Feed(FeedKind::Thread {
id: link_to_id,

View File

@ -291,7 +291,7 @@ fn render_note_inner(
ui.style_mut().override_text_style = Some(TextStyle::Small);
let idhex: IdHex = irt.into();
let nam = format!("▲ #{}", GossipUi::hex_id_short(&idhex));
let nam = format!("▲ #{}", crate::names::hex_id_short(&idhex));
if ui.link(&nam).clicked() {
app.set_page(Page::Feed(FeedKind::Thread {
id: irt,

View File

@ -896,44 +896,13 @@ impl GossipUi {
ui.set_enabled(!relays::is_entry_dialog_active(self));
}
/// A short rendering of a `PublicKey`
pub fn pubkey_short(pk: &PublicKey) -> String {
let npub = pk.as_bech32_string();
format!(
"{}...{}",
&npub.get(0..10).unwrap_or("??????????"),
&npub
.get(npub.len() - 10..npub.len())
.unwrap_or("??????????")
)
}
pub fn hex_id_short(idhex: &IdHex) -> String {
idhex.as_str()[0..8].to_string()
}
/// A display name for a `Person`
pub fn display_name_from_dbperson(dbperson: &Person) -> String {
match dbperson.display_name() {
Some(name) => name.to_owned(),
None => Self::pubkey_short(&dbperson.pubkey),
}
}
pub fn display_name_from_pubkey_lookup(pubkey: &PublicKey) -> String {
match GLOBALS.storage.read_person(pubkey) {
Ok(Some(dbperson)) => Self::display_name_from_dbperson(&dbperson),
_ => Self::pubkey_short(pubkey),
}
}
pub fn render_person_name_line(app: &mut GossipUi, ui: &mut Ui, person: &Person) {
// Let the 'People' manager know that we are interested in displaying this person.
// It will take all actions necessary to make the data eventually available.
GLOBALS.people.person_of_interest(person.pubkey);
ui.horizontal_wrapped(|ui| {
let name = GossipUi::display_name_from_dbperson(person);
let name = crate::names::display_name_from_person(person);
ui.menu_button(&name, |ui| {
let mute_label = if person.muted { "Unmute" } else { "Mute" };

View File

@ -178,7 +178,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra
};
ui.vertical(|ui| {
ui.label(RichText::new(GossipUi::pubkey_short(&person.pubkey)).weak());
ui.label(RichText::new(crate::names::pubkey_short(&person.pubkey)).weak());
GossipUi::render_person_name_line(app, ui, person);
});
});

View File

@ -46,7 +46,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra
};
ui.vertical(|ui| {
ui.label(RichText::new(GossipUi::pubkey_short(&person.pubkey)).weak());
ui.label(RichText::new(crate::names::pubkey_short(&person.pubkey)).weak());
GossipUi::render_person_name_line(app, ui, person);
if ui.button("UNMUTE").clicked() {

View File

@ -55,9 +55,9 @@ fn content(app: &mut GossipUi, ctx: &Context, ui: &mut Ui, pubkey: PublicKey, pe
},
);
ui.vertical(|ui| {
let name = GossipUi::display_name_from_dbperson(&person);
let name = crate::names::display_name_from_person(&person);
ui.heading(name);
ui.label(RichText::new(GossipUi::pubkey_short(&pubkey)).weak());
ui.label(RichText::new(crate::names::pubkey_short(&pubkey)).weak());
GossipUi::render_person_name_line(app, ui, &person);
});
});

View File

@ -41,7 +41,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr
for elem in GLOBALS.relay_picker.pubkey_counts_iter() {
let pk = elem.key();
let count = elem.value();
let name = GossipUi::display_name_from_pubkey_lookup(pk);
let name = crate::names::display_name_from_pubkey_lookup(pk);
ui.label(format!("{}: coverage short by {} relay(s)", name, count));
}
ui.add_space(12.0);

View File

@ -81,7 +81,9 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut Frame, ui:
};
ui.vertical(|ui| {
ui.label(RichText::new(GossipUi::pubkey_short(&person.pubkey)).weak());
ui.label(
RichText::new(crate::names::pubkey_short(&person.pubkey)).weak(),
);
GossipUi::render_person_name_line(app, ui, person);
});
});