feat: Make the relays list optional

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb
2025-05-24 12:30:40 +00:00
parent de68d61c3f
commit ddea5028ee
5 changed files with 47 additions and 25 deletions

View File

@@ -91,7 +91,6 @@ impl CommandRunner for NewArgs {
let user_pubk = options.pubkey().await?; let user_pubk = options.pubkey().await?;
let naddr = utils::naddr_or_file(self.naddr.clone(), &utils::nostr_address_path()?)?; let naddr = utils::naddr_or_file(self.naddr.clone(), &utils::nostr_address_path()?)?;
client.add_relays(&options.relays).await;
client.add_relays(&naddr.relays).await; client.add_relays(&naddr.relays).await;
let relays_list = client.user_relays_list(user_pubk).await?; let relays_list = client.user_relays_list(user_pubk).await?;

View File

@@ -33,7 +33,7 @@ use self::issue::IssueSubcommands;
use self::reply::ReplyArgs; use self::reply::ReplyArgs;
use self::repo::RepoSubcommands; use self::repo::RepoSubcommands;
use super::traits::CommandRunner; use super::traits::CommandRunner;
use crate::error::N34Result; use crate::error::{N34Error, N34Result};
/// The command-line interface options /// The command-line interface options
#[derive(Args, Clone)] #[derive(Args, Clone)]
@@ -48,8 +48,9 @@ pub struct CliOptions {
/// Your Nostr secret key /// Your Nostr secret key
#[arg(short, long)] #[arg(short, long)]
pub secret_key: Option<SecretKey>, pub secret_key: Option<SecretKey>,
/// Where your relays list. And repository relays if not included in naddr /// Fallbacks relay to write and read from it. Multiple relays can be
#[arg(short, long, required = true)] /// passed.
#[arg(short, long)]
pub relays: Vec<RelayUrl>, pub relays: Vec<RelayUrl>,
/// Proof of Work difficulty when creatring events /// Proof of Work difficulty when creatring events
#[arg(long, default_value_t = 0)] #[arg(long, default_value_t = 0)]
@@ -99,6 +100,14 @@ impl CliOptions {
} }
unreachable!("There is no other method until now") unreachable!("There is no other method until now")
} }
/// Returns an error if there are no relays.
pub fn ensure_relays(&self) -> N34Result<()> {
if self.relays.is_empty() {
return Err(N34Error::EmptyRelays);
}
Ok(())
}
} }
impl fmt::Debug for CliOptions { impl fmt::Debug for CliOptions {

View File

@@ -21,7 +21,7 @@ use nostr::{
event::{Event, EventBuilder, EventId, Kind, Tag}, event::{Event, EventBuilder, EventId, Kind, Tag},
filter::Filter, filter::Filter,
nips::{ nips::{
nip01::Metadata, nip01::{Coordinate, Metadata},
nip19::{self, FromBech32, Nip19Coordinate, ToBech32}, nip19::{self, FromBech32, Nip19Coordinate, ToBech32},
}, },
types::RelayUrl, types::RelayUrl,
@@ -112,10 +112,21 @@ pub struct ReplyArgs {
impl CommandRunner for ReplyArgs { impl CommandRunner for ReplyArgs {
async fn run(self, options: CliOptions) -> N34Result<()> { async fn run(self, options: CliOptions) -> N34Result<()> {
let nostr_address_path = utils::nostr_address_path()?;
let client = NostrClient::init(&options).await; let client = NostrClient::init(&options).await;
let user_pubk = options.pubkey().await?; let user_pubk = options.pubkey().await?;
client.add_relays(&options.relays).await; let repo_naddr = if let Some(naddr) = self.naddr {
client.add_relays(&naddr.relays).await;
Some(naddr.coordinate)
} else if fs::exists(&nostr_address_path).is_ok() {
let naddr = utils::naddr_or_file(None, &nostr_address_path)?;
client.add_relays(&naddr.relays).await;
Some(naddr.coordinate)
} else {
None
};
client.add_relays(&self.to.relays).await; client.add_relays(&self.to.relays).await;
let relays_list = client.user_relays_list(user_pubk).await?; let relays_list = client.user_relays_list(user_pubk).await?;
@@ -128,26 +139,10 @@ impl CommandRunner for ReplyArgs {
.ok_or(N34Error::EventNotFound)?; .ok_or(N34Error::EventNotFound)?;
let root = client.find_root(reply_to.clone()).await?; let root = client.find_root(reply_to.clone()).await?;
let repo_naddr = if let Some(naddr) = repo_naddr {
let nostr_address_path = utils::nostr_address_path()?; naddr
let repo_naddr = if let Some(naddr) = self.naddr {
client.add_relays(&naddr.relays).await;
naddr.coordinate
} else if fs::exists(&nostr_address_path).is_ok() {
let naddr = utils::naddr_or_file(None, &nostr_address_path)?;
client.add_relays(&naddr.relays).await;
naddr.coordinate
} else if let Some(ref root_event) = root { } else if let Some(ref root_event) = root {
root_event naddr_from_root(root_event)?
.tags
.coordinates()
.find(|c| c.kind == Kind::GitRepoAnnouncement)
.ok_or_else(|| {
N34Error::InvalidEvent(
"The Git issue/patch does not specify a target repository".to_owned(),
)
})?
.clone()
} else { } else {
return Err(N34Error::NotFoundRepo); return Err(N34Error::NotFoundRepo);
}; };
@@ -247,3 +242,18 @@ async fn quote_reply_to_content(client: &NostrClient, quoted_event: &Event) -> S
quoted_event.content.trim().replace("\n", "\n> ") quoted_event.content.trim().replace("\n", "\n> ")
) )
} }
/// Gets the repository coordinate from a root Nostr event's tags.
/// The event must contain a coordinate tag with GitRepoAnnouncement kind.
fn naddr_from_root(root: &Event) -> N34Result<Coordinate> {
Ok(root
.tags
.coordinates()
.find(|c| c.kind == Kind::GitRepoAnnouncement)
.ok_or_else(|| {
N34Error::InvalidEvent(
"The Git issue/patch does not specify a target repository".to_owned(),
)
})?
.clone())
}

View File

@@ -61,6 +61,8 @@ pub struct AnnounceArgs {
impl CommandRunner for AnnounceArgs { impl CommandRunner for AnnounceArgs {
async fn run(mut self, options: CliOptions) -> N34Result<()> { async fn run(mut self, options: CliOptions) -> N34Result<()> {
options.ensure_relays()?;
let client = NostrClient::init(&options).await; let client = NostrClient::init(&options).await;
let user_pubk = options.pubkey().await?; let user_pubk = options.pubkey().await?;
let relays_list = client.user_relays_list(user_pubk).await?; let relays_list = client.user_relays_list(user_pubk).await?;

View File

@@ -60,6 +60,8 @@ pub enum N34Error {
EmptyNostrAddressFile, EmptyNostrAddressFile,
#[error("Invalid `nostr-address` file content: {0}")] #[error("Invalid `nostr-address` file content: {0}")]
InvalidNostrAddressFileContent(String), InvalidNostrAddressFileContent(String),
#[error("This command requires at least one relay, but none were provided")]
EmptyRelays,
} }
impl N34Error { impl N34Error {