From 76b76bc8d696b25d012d80599b1b99e494e5910d Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Mon, 9 Jun 2025 21:12:17 +0000
Subject: [PATCH] chore(nostr_utils): Move the username logic to the utils
Signed-off-by: Awiteb
---
src/cli/commands/reply.rs | 26 ++------------------------
src/nostr_utils/mod.rs | 22 +++++++++++++++++++++-
2 files changed, 23 insertions(+), 25 deletions(-)
diff --git a/src/cli/commands/reply.rs b/src/cli/commands/reply.rs
index c30c227..6e9deab 100644
--- a/src/cli/commands/reply.rs
+++ b/src/cli/commands/reply.rs
@@ -21,10 +21,7 @@ use futures::future;
use nostr::{
event::{Event, EventBuilder, Kind},
filter::Filter,
- nips::{
- nip01::{Coordinate, Metadata},
- nip19::ToBech32,
- },
+ nips::nip01::Coordinate,
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"
const MAX_DATE: i64 = 253370764800;
@@ -192,24 +187,7 @@ impl CommandRunner for ReplyArgs {
/// available, otherwise falls back to a shortened npub string. Dates are
/// formatted in UTC.
async fn quote_reply_to_content(client: &NostrClient, quoted_event: &Event) -> String {
- let author_name = client
- .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 author_name = client.get_username(quoted_event.pubkey).await;
let fdate = chrono::DateTime::from_timestamp(
quoted_event
diff --git a/src/nostr_utils/mod.rs b/src/nostr_utils/mod.rs
index d5cb47d..65dbfc6 100644
--- a/src/nostr_utils/mod.rs
+++ b/src/nostr_utils/mod.rs
@@ -26,7 +26,12 @@ use nostr::{
event::{Event, EventId, Kind, Tag, TagStandard, Tags, UnsignedEvent},
filter::Filter,
key::PublicKey,
- nips::{nip01::Coordinate, nip22, nip34::GitRepositoryAnnouncement},
+ nips::{
+ nip01::{Coordinate, Metadata},
+ nip19::ToBech32,
+ nip22,
+ nip34::GitRepositoryAnnouncement,
+ },
parser::NostrParser,
types::RelayUrl,
};
@@ -40,6 +45,8 @@ use crate::{
/// Timeout duration for the clinet.
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
#[derive(Clone)]
@@ -222,6 +229,19 @@ impl NostrClient {
.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
/// a root (issue/patch), returns it directly. For comments, follows
/// parent/root references until finding the root or failing. Returns