chore: Send the repo announce to the author relays list
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
@@ -15,21 +15,20 @@
|
|||||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
use convert_case::{Case, Casing};
|
|
||||||
use nostr::{
|
use nostr::{
|
||||||
event::{EventBuilder, Kind, Tag},
|
event::{EventBuilder, Kind},
|
||||||
key::PublicKey,
|
key::PublicKey,
|
||||||
nips::{
|
nips::{
|
||||||
nip01::Coordinate,
|
nip01::Coordinate,
|
||||||
nip19::{Nip19Coordinate, Nip19Event, ToBech32},
|
nip19::{Nip19Coordinate, Nip19Event, ToBech32},
|
||||||
nip34::GitRepositoryAnnouncement,
|
nip65::{self, RelayMetadata},
|
||||||
},
|
},
|
||||||
types::Url,
|
types::Url,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cli::{CliOptions, CommandRunner},
|
cli::{CliOptions, CommandRunner},
|
||||||
error::{N34Error, N34Result},
|
error::N34Result,
|
||||||
nostr_utils::{NostrClient, traits::NewGitRepositoryAnnouncement},
|
nostr_utils::{NostrClient, traits::NewGitRepositoryAnnouncement},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -63,28 +62,43 @@ pub struct AnnounceArgs {
|
|||||||
impl CommandRunner for AnnounceArgs {
|
impl CommandRunner for AnnounceArgs {
|
||||||
async fn run(self, options: CliOptions) -> N34Result<()> {
|
async fn run(self, options: CliOptions) -> N34Result<()> {
|
||||||
let client = NostrClient::init(&options).await;
|
let client = NostrClient::init(&options).await;
|
||||||
|
let user_pubk = options.pubkey().await;
|
||||||
let naddr = Nip19Coordinate::new(
|
let naddr = Nip19Coordinate::new(
|
||||||
Coordinate::new(Kind::GitRepoAnnouncement, options.pubkey().await),
|
Coordinate::new(Kind::GitRepoAnnouncement, user_pubk),
|
||||||
options.relays.iter().take(3),
|
options.relays.iter().take(3),
|
||||||
)
|
)
|
||||||
.expect("Valid relays");
|
.expect("Valid relays");
|
||||||
|
let relays_list = client.user_relays_list(user_pubk).await?;
|
||||||
|
|
||||||
let mut maintainers = vec![naddr.public_key];
|
let mut write_relays = options.relays.clone();
|
||||||
|
let mut maintainers = vec![user_pubk];
|
||||||
maintainers.extend(self.maintainers);
|
maintainers.extend(self.maintainers);
|
||||||
|
|
||||||
let event_builder = EventBuilder::new_git_repo(
|
if let Some(event) = relays_list.clone() {
|
||||||
|
write_relays.extend(
|
||||||
|
nip65::extract_owned_relay_list(event)
|
||||||
|
.filter_map(|(r, m)| m.is_none_or(|m| m == RelayMetadata::Write).then_some(r)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let event = EventBuilder::new_git_repo(
|
||||||
self.repo_id,
|
self.repo_id,
|
||||||
self.name,
|
self.name,
|
||||||
self.description,
|
self.description,
|
||||||
self.web,
|
self.web,
|
||||||
self.clone,
|
self.clone,
|
||||||
options.relays.clone(),
|
options.relays,
|
||||||
maintainers,
|
maintainers,
|
||||||
self.labels,
|
self.labels,
|
||||||
)?;
|
)?
|
||||||
|
.build(user_pubk);
|
||||||
|
let nevent = Nip19Event::new(event.id.expect("There is an id"))
|
||||||
|
.relays(write_relays.iter().take(3).cloned())
|
||||||
|
.to_bech32()?;
|
||||||
|
|
||||||
|
|
||||||
let result = client
|
let result = client
|
||||||
.send_builder_to(event_builder, &options.relays)
|
.send_event_to(event, relays_list.as_ref(), write_relays)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
for relay in &result.success {
|
for relay in &result.success {
|
||||||
@@ -94,12 +108,7 @@ impl CommandRunner for AnnounceArgs {
|
|||||||
tracing::warn!(relay = %relay, reason = %reason, "Failed to send event");
|
tracing::warn!(relay = %relay, reason = %reason, "Failed to send event");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!(
|
println!("Event: {nevent}",);
|
||||||
"Event: {}",
|
|
||||||
Nip19Event::new(result.val)
|
|
||||||
.relays(options.relays.into_iter().take(3))
|
|
||||||
.to_bech32()?
|
|
||||||
);
|
|
||||||
println!("Repo Address: {}", naddr.to_bech32()?);
|
println!("Repo Address: {}", naddr.to_bech32()?);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ pub enum N34Error {
|
|||||||
InvalidRepoId,
|
InvalidRepoId,
|
||||||
#[error("Bech32 error: {0}")]
|
#[error("Bech32 error: {0}")]
|
||||||
Bech32(#[from] nostr::nips::nip19::Error),
|
Bech32(#[from] nostr::nips::nip19::Error),
|
||||||
|
#[error("Event error: {0}")]
|
||||||
|
Event(#[from] nostr::event::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl N34Error {
|
impl N34Error {
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ pub mod utils;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use nostr::{
|
use nostr::{
|
||||||
event::{EventBuilder, EventId, Kind},
|
event::{Event, EventId, Kind, UnsignedEvent},
|
||||||
filter::Filter,
|
filter::Filter,
|
||||||
key::Keys,
|
key::{Keys, PublicKey},
|
||||||
nips::{nip19::Nip19Coordinate, nip34::GitRepositoryAnnouncement},
|
nips::{nip19::Nip19Coordinate, nip34::GitRepositoryAnnouncement},
|
||||||
types::RelayUrl,
|
types::RelayUrl,
|
||||||
};
|
};
|
||||||
@@ -35,6 +35,9 @@ use crate::{
|
|||||||
error::{N34Error, N34Result},
|
error::{N34Error, N34Result},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Timeout duration for the clinet.
|
||||||
|
const CLIENT_TIMEOUT: Duration = Duration::from_millis(1500);
|
||||||
|
|
||||||
/// A client for interacting with the Nostr relays
|
/// A client for interacting with the Nostr relays
|
||||||
pub struct NostrClient {
|
pub struct NostrClient {
|
||||||
/// The underlying Nostr client implementation
|
/// The underlying Nostr client implementation
|
||||||
@@ -73,28 +76,30 @@ impl NostrClient {
|
|||||||
.add_read_relay(relay)
|
.add_read_relay(relay)
|
||||||
.await
|
.await
|
||||||
.expect("It's a valid relay url");
|
.expect("It's a valid relay url");
|
||||||
if let Err(err) = self
|
if let Err(err) = self.client.try_connect_relay(relay, CLIENT_TIMEOUT).await {
|
||||||
.client
|
|
||||||
.try_connect_relay(relay, Duration::from_millis(1500))
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
tracing::error!("Failed to connect to relay '{relay}': {err}");
|
tracing::error!("Failed to connect to relay '{relay}': {err}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends an event builder to the specified relays.
|
/// Sends an event to the specified relays.
|
||||||
pub async fn send_builder_to(
|
pub async fn send_event_to(
|
||||||
&self,
|
&self,
|
||||||
builder: EventBuilder,
|
event: UnsignedEvent,
|
||||||
relays: &[RelayUrl],
|
relays_list: Option<&Event>,
|
||||||
|
mut relays: Vec<RelayUrl>,
|
||||||
) -> N34Result<Output<EventId>> {
|
) -> N34Result<Output<EventId>> {
|
||||||
for relay in relays {
|
relays.sort_unstable();
|
||||||
|
relays.dedup();
|
||||||
|
for relay in &relays {
|
||||||
let _ = self.client.add_write_relay(relay).await;
|
let _ = self.client.add_write_relay(relay).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(event) = relays_list {
|
||||||
|
let _ = self.client.send_event_to(&relays, event).await;
|
||||||
|
}
|
||||||
self.client
|
self.client
|
||||||
.send_event_builder_to(relays, builder)
|
.send_event_to(relays, &event.sign(&self.client.signer().await?).await?)
|
||||||
.await
|
.await
|
||||||
.map_err(N34Error::from)
|
.map_err(N34Error::from)
|
||||||
}
|
}
|
||||||
@@ -110,7 +115,7 @@ impl NostrClient {
|
|||||||
.identifier(&repo_naddr.identifier);
|
.identifier(&repo_naddr.identifier);
|
||||||
let events = self
|
let events = self
|
||||||
.client
|
.client
|
||||||
.fetch_events(filter, Duration::from_secs(1))
|
.fetch_events(filter, CLIENT_TIMEOUT)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| N34Error::NotFoundRepo)?;
|
.map_err(|_| N34Error::NotFoundRepo)?;
|
||||||
|
|
||||||
@@ -120,4 +125,17 @@ impl NostrClient {
|
|||||||
&repo_naddr.identifier,
|
&repo_naddr.identifier,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetches the relay list (kind 10002) for the given user. Returns None if
|
||||||
|
/// no relays are found.
|
||||||
|
pub async fn user_relays_list(&self, user: PublicKey) -> N34Result<Option<Event>> {
|
||||||
|
Ok(self
|
||||||
|
.client
|
||||||
|
.fetch_events(
|
||||||
|
Filter::new().author(user).kind(Kind::RelayList),
|
||||||
|
CLIENT_TIMEOUT,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
.first_owned())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user