From f670859b92d525874fd621452080c8479964ac6a Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Tue, 27 May 2025 12:35:26 +0000
Subject: [PATCH] chore: Remove the vector parameter from
`add_{read,write}_relays`
Signed-off-by: Awiteb
---
src/cli/commands/issue/new.rs | 11 ++++++-----
src/cli/commands/reply.rs | 19 +++++++++----------
src/cli/commands/repo/announce.rs | 9 ++++++---
src/nostr_utils/mod.rs | 19 +++++++------------
src/nostr_utils/utils.rs | 12 ++++++++----
5 files changed, 36 insertions(+), 34 deletions(-)
diff --git a/src/cli/commands/issue/new.rs b/src/cli/commands/issue/new.rs
index 7405c65..4120f5a 100644
--- a/src/cli/commands/issue/new.rs
+++ b/src/cli/commands/issue/new.rs
@@ -103,14 +103,15 @@ impl CommandRunner for NewArgs {
client.add_relays(&naddrs.extract_relays()).await;
let relays_list = client.user_relays_list(user_pubk).await?;
- let mut write_relays =
- utils::add_write_relays(options.relays.clone(), relays_list.as_ref());
- write_relays.extend(
+ let mut write_relays = [
+ options.relays,
+ utils::add_write_relays(relays_list.as_ref()),
client
.fetch_repos(&naddrs.into_coordinates())
.await?
.extract_relays(),
- );
+ ]
+ .concat();
let (subject, content) = self.issue_content()?;
let content_details = client.parse_content(&content).await;
@@ -121,7 +122,7 @@ impl CommandRunner for NewArgs {
future::join_all(
naddrs_iter
.clone()
- .map(|c| client.read_relays_from_user(Vec::new(), c.public_key)),
+ .map(|c| client.read_relays_from_user(c.public_key)),
)
.await
.into_iter()
diff --git a/src/cli/commands/reply.rs b/src/cli/commands/reply.rs
index 2238585..3c2f38b 100644
--- a/src/cli/commands/reply.rs
+++ b/src/cli/commands/reply.rs
@@ -136,8 +136,11 @@ impl CommandRunner for ReplyArgs {
client.add_relays(&self.to.relays).await;
let relays_list = client.user_relays_list(user_pubk).await?;
- let mut write_relays =
- utils::add_write_relays(options.relays.clone(), relays_list.as_ref());
+ let mut write_relays = [
+ options.relays,
+ utils::add_write_relays(relays_list.as_ref()),
+ ]
+ .concat();
let reply_to = client
.fetch_event(Filter::new().id(self.to.event_id))
@@ -161,20 +164,16 @@ impl CommandRunner for ReplyArgs {
future::join_all(
repos_coordinate
.iter()
- .map(|c| client.read_relays_from_user(Vec::new(), c.public_key)),
+ .map(|c| client.read_relays_from_user(c.public_key)),
)
.await
.into_iter()
.flatten(),
);
- write_relays = client
- .read_relays_from_user(write_relays, reply_to.pubkey)
- .await;
+ write_relays.extend(client.read_relays_from_user(reply_to.pubkey).await);
if let Some(root_event) = &root {
- write_relays = client
- .read_relays_from_user(write_relays, root_event.pubkey)
- .await;
+ write_relays.extend(client.read_relays_from_user(root_event.pubkey).await);
}
let quoted_content = if self.quote_to {
@@ -199,7 +198,7 @@ impl CommandRunner for ReplyArgs {
.build(user_pubk);
let event_id = event.id.expect("There is an id");
- let author_read_relays = utils::add_read_relays(Vec::new(), relays_list.as_ref());
+ let author_read_relays = utils::add_read_relays(relays_list.as_ref());
tracing::trace!(relays = ?write_relays, "Write relays list");
let (success, ..) = futures::join!(
diff --git a/src/cli/commands/repo/announce.rs b/src/cli/commands/repo/announce.rs
index 2789c57..152e6cd 100644
--- a/src/cli/commands/repo/announce.rs
+++ b/src/cli/commands/repo/announce.rs
@@ -84,8 +84,11 @@ impl CommandRunner for AnnounceArgs {
let client = NostrClient::init(&options).await;
let user_pubk = options.pubkey().await?;
let relays_list = client.user_relays_list(user_pubk).await?;
- let mut write_relays =
- utils::add_write_relays(options.relays.clone(), relays_list.as_ref());
+ let mut write_relays = [
+ options.relays.clone(),
+ utils::add_write_relays(relays_list.as_ref()),
+ ]
+ .concat();
if !self.maintainers.contains(&user_pubk) {
self.maintainers.insert(0, user_pubk);
@@ -96,7 +99,7 @@ impl CommandRunner for AnnounceArgs {
future::join_all(
self.maintainers
.iter()
- .map(|pkey| client.read_relays_from_user(Vec::new(), *pkey)),
+ .map(|pkey| client.read_relays_from_user(*pkey)),
)
.await
.into_iter()
diff --git a/src/nostr_utils/mod.rs b/src/nostr_utils/mod.rs
index a25a6b6..134f6cd 100644
--- a/src/nostr_utils/mod.rs
+++ b/src/nostr_utils/mod.rs
@@ -82,6 +82,7 @@ impl ContentDetails {
tags.extend(self.hashtags.into_iter().map(Tag::hashtag));
tags.extend(self.p_tagged.into_iter().map(Tag::public_key));
tags.extend(self.quotes.into_iter().map(|(event_id, relay_url)| {
+ // TODO: Add the author public key if we know it
Tag::from_standardized(TagStandard::Quote {
event_id,
relay_url,
@@ -281,16 +282,10 @@ impl NostrClient {
.map(|e| e.pubkey))
}
- /// Adds read relays from the user to the given vector of relays.
- pub async fn read_relays_from_user(
- &self,
- vector: Vec,
- user: PublicKey,
- ) -> Vec {
- utils::add_read_relays(
- vector,
- self.user_relays_list(user).await.ok().flatten().as_ref(),
- )
+ /// Returns the read relays of the given user if found, otherwise empty
+ /// vector
+ pub async fn read_relays_from_user(&self, user: PublicKey) -> Vec {
+ utils::add_read_relays(self.user_relays_list(user).await.ok().flatten().as_ref())
}
/// Parse the given content and returns the details that inside it
@@ -313,14 +308,14 @@ impl NostrClient {
for (user, relays) in &p_tagged_users {
self.add_relays(relays).await;
- write_relays = self.read_relays_from_user(write_relays, *user).await
+ write_relays.extend(self.read_relays_from_user(*user).await);
}
for (event_id, relays) in "es {
self.add_relays(relays).await;
// Add the event author to the p-tagged users
if let Ok(Some(author)) = self.event_author(*event_id).await {
p_tagged_users.push((author, Vec::new()));
- write_relays = self.read_relays_from_user(write_relays, author).await;
+ write_relays.extend(self.read_relays_from_user(author).await);
}
}
diff --git a/src/nostr_utils/utils.rs b/src/nostr_utils/utils.rs
index 485bacd..42abbf4 100644
--- a/src/nostr_utils/utils.rs
+++ b/src/nostr_utils/utils.rs
@@ -140,8 +140,10 @@ pub fn repo_naddr(
.map_err(N34Error::from)
}
-/// Extends the given vector with write relays found in the event (if any).
-pub fn add_write_relays(mut vector: Vec, event: Option<&Event>) -> Vec {
+/// Extracts write relay URLs from an event if present, otherwise returns an
+/// empty vector.
+pub fn add_write_relays(event: Option<&Event>) -> Vec {
+ let mut vector = Vec::new();
if let Some(event) = event {
vector.extend(
nip65::extract_owned_relay_list(event.clone())
@@ -151,8 +153,10 @@ pub fn add_write_relays(mut vector: Vec, event: Option<&Event>) -> Vec
vector
}
-/// Extends the given vector with read relays found in the event (if any).
-pub fn add_read_relays(mut vector: Vec, event: Option<&Event>) -> Vec {
+/// Extracts read relay URLs from an event if present, otherwise returns an
+/// empty vector.
+pub fn add_read_relays(event: Option<&Event>) -> Vec {
+ let mut vector = Vec::new();
if let Some(event) = event {
vector.extend(
nip65::extract_owned_relay_list(event.clone())