chore(nostr_utils): Move the username logic to the utils

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb
2025-06-09 21:12:17 +00:00
parent 983d2f209d
commit 76b76bc8d6
2 changed files with 23 additions and 25 deletions

View File

@@ -21,10 +21,7 @@ use futures::future;
use nostr::{ use nostr::{
event::{Event, EventBuilder, Kind}, event::{Event, EventBuilder, Kind},
filter::Filter, filter::Filter,
nips::{ nips::nip01::Coordinate,
nip01::{Coordinate, Metadata},
nip19::ToBech32,
},
types::RelayUrl, types::RelayUrl,
}; };
@@ -39,8 +36,6 @@ use crate::{
}, },
}; };
/// Length of a Nostr npub (public key) in characters.
const NPUB_LEN: usize = 63;
/// The max date "9999-01-01 at 00:00 UTC" /// The max date "9999-01-01 at 00:00 UTC"
const MAX_DATE: i64 = 253370764800; const MAX_DATE: i64 = 253370764800;
@@ -192,24 +187,7 @@ impl CommandRunner for ReplyArgs {
/// available, otherwise falls back to a shortened npub string. Dates are /// available, otherwise falls back to a shortened npub string. Dates are
/// formatted in UTC. /// formatted in UTC.
async fn quote_reply_to_content(client: &NostrClient, quoted_event: &Event) -> String { async fn quote_reply_to_content(client: &NostrClient, quoted_event: &Event) -> String {
let author_name = client let author_name = client.get_username(quoted_event.pubkey).await;
.fetch_event(
Filter::new()
.kind(Kind::Metadata)
.author(quoted_event.pubkey),
)
.await
.ok()
.flatten()
.and_then(|e| Metadata::try_from(&e).ok())
.and_then(|m| m.display_name.or(m.name))
.unwrap_or_else(|| {
let pubkey = quoted_event
.pubkey
.to_bech32()
.expect("The error is `Infallible`");
format!("{}...{}", &pubkey[..8], &pubkey[NPUB_LEN - 8..])
});
let fdate = chrono::DateTime::from_timestamp( let fdate = chrono::DateTime::from_timestamp(
quoted_event quoted_event

View File

@@ -26,7 +26,12 @@ use nostr::{
event::{Event, EventId, Kind, Tag, TagStandard, Tags, UnsignedEvent}, event::{Event, EventId, Kind, Tag, TagStandard, Tags, UnsignedEvent},
filter::Filter, filter::Filter,
key::PublicKey, key::PublicKey,
nips::{nip01::Coordinate, nip22, nip34::GitRepositoryAnnouncement}, nips::{
nip01::{Coordinate, Metadata},
nip19::ToBech32,
nip22,
nip34::GitRepositoryAnnouncement,
},
parser::NostrParser, parser::NostrParser,
types::RelayUrl, types::RelayUrl,
}; };
@@ -40,6 +45,8 @@ use crate::{
/// Timeout duration for the clinet. /// Timeout duration for the clinet.
const CLIENT_TIMEOUT: Duration = Duration::from_millis(1500); const CLIENT_TIMEOUT: Duration = Duration::from_millis(1500);
/// Length of a Nostr npub (public key) in characters.
const NPUB_LEN: usize = 63;
/// Parsed content details /// Parsed content details
#[derive(Clone)] #[derive(Clone)]
@@ -222,6 +229,19 @@ impl NostrClient {
.collect() .collect()
} }
pub async fn get_username(&self, user: PublicKey) -> String {
self.fetch_event(Filter::new().kind(Kind::Metadata).author(user))
.await
.ok()
.flatten()
.and_then(|e| Metadata::try_from(&e).ok())
.and_then(|m| m.display_name.or(m.name))
.unwrap_or_else(|| {
let pubkey = user.to_bech32().expect("The error is `Infallible`");
format!("{}...{}", &pubkey[..8], &pubkey[NPUB_LEN - 8..])
})
}
/// Finds the root issue or patch for a given event. If the event is already /// Finds the root issue or patch for a given event. If the event is already
/// a root (issue/patch), returns it directly. For comments, follows /// a root (issue/patch), returns it directly. For comments, follows
/// parent/root references until finding the root or failing. Returns /// parent/root references until finding the root or failing. Returns