|
| 1 | +use crate::error::RelayClientError; |
| 2 | +use nostr::prelude::*; |
| 3 | +use nostr_sdk::pool::Output; |
| 4 | +use nostr_sdk::prelude::Events; |
| 5 | +use nostr_sdk::{Client, Relay, SubscribeAutoCloseOptions}; |
| 6 | +use std::collections::HashMap; |
| 7 | +use std::fmt::Debug; |
| 8 | +use std::sync::Arc; |
| 9 | +use std::time::Duration; |
| 10 | +use tracing::instrument; |
| 11 | + |
| 12 | +#[derive(Debug)] |
| 13 | +pub struct RelayClient { |
| 14 | + client: Client, |
| 15 | + timeout: Duration, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Debug)] |
| 19 | +pub struct ClientConfig { |
| 20 | + pub timeout: Duration, |
| 21 | +} |
| 22 | + |
| 23 | +impl RelayClient { |
| 24 | + #[instrument(skip_all, level = "debug", err)] |
| 25 | + pub async fn connect( |
| 26 | + relay_urls: impl IntoIterator<Item = impl TryIntoUrl>, |
| 27 | + keys: Option<impl IntoNostrSigner>, |
| 28 | + client_config: ClientConfig, |
| 29 | + ) -> crate::error::Result<Self> { |
| 30 | + tracing::debug!(client_config = ?client_config, "Connecting to Nostr Relay Client(s)"); |
| 31 | + let client = match keys { |
| 32 | + None => Client::default(), |
| 33 | + Some(keys) => { |
| 34 | + let client = Client::new(keys); |
| 35 | + client.automatic_authentication(true); |
| 36 | + client |
| 37 | + } |
| 38 | + }; |
| 39 | + for url in relay_urls { |
| 40 | + let url = url |
| 41 | + .try_into_url() |
| 42 | + .map_err(|err| RelayClientError::FailedToConvertRelayUrl { |
| 43 | + err_msg: format!("{:?}", err), |
| 44 | + })?; |
| 45 | + client.add_relay(url).await?; |
| 46 | + } |
| 47 | + client.connect().await; |
| 48 | + Ok(Self { |
| 49 | + client, |
| 50 | + timeout: client_config.timeout, |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + #[instrument(skip_all, level = "debug", ret)] |
| 55 | + pub async fn req_and_wait(&self, filter: Filter) -> crate::error::Result<Events> { |
| 56 | + tracing::debug!(filter = ?filter, "Requesting events with filter"); |
| 57 | + let events = self.client.fetch_combined_events(filter, self.timeout).await?; |
| 58 | + Ok(events) |
| 59 | + } |
| 60 | + |
| 61 | + #[instrument(skip_all, level = "debug", ret)] |
| 62 | + pub async fn get_signer(&self) -> crate::error::Result<Arc<dyn NostrSigner>> { |
| 63 | + if !self.client.has_signer().await { |
| 64 | + return Err(RelayClientError::MissingSigner); |
| 65 | + } |
| 66 | + Ok(self.client.signer().await?) |
| 67 | + } |
| 68 | + |
| 69 | + #[instrument(skip_all, level = "debug", ret)] |
| 70 | + pub async fn get_relays(&self) -> HashMap<RelayUrl, Relay> { |
| 71 | + self.client.relays().await |
| 72 | + } |
| 73 | + |
| 74 | + #[instrument(skip_all, level = "debug", ret)] |
| 75 | + pub async fn publish_event(&self, event: &Event) -> crate::error::Result<EventId> { |
| 76 | + if !self.client.has_signer().await { |
| 77 | + return Err(RelayClientError::MissingSigner); |
| 78 | + } |
| 79 | + let event_id = self.client.send_event(event).await?; |
| 80 | + let event_id = Self::handle_relay_output(event_id)?; |
| 81 | + Ok(event_id) |
| 82 | + } |
| 83 | + |
| 84 | + #[instrument(skip(self), level = "debug")] |
| 85 | + pub async fn subscribe( |
| 86 | + &self, |
| 87 | + filter: Filter, |
| 88 | + opts: Option<SubscribeAutoCloseOptions>, |
| 89 | + ) -> crate::error::Result<SubscriptionId> { |
| 90 | + Ok(self.client.subscribe(filter, opts).await?.val) |
| 91 | + } |
| 92 | + |
| 93 | + #[instrument(skip(self), level = "debug")] |
| 94 | + pub async fn unsubscribe(&self, subscription_id: &SubscriptionId) { |
| 95 | + self.client.unsubscribe(subscription_id).await; |
| 96 | + } |
| 97 | + |
| 98 | + #[instrument(skip_all, level = "debug", ret)] |
| 99 | + pub async fn disconnect(&self) -> crate::error::Result<()> { |
| 100 | + self.client.disconnect().await; |
| 101 | + Ok(()) |
| 102 | + } |
| 103 | + |
| 104 | + #[instrument(level = "debug")] |
| 105 | + fn handle_relay_output<T: Debug>(output: Output<T>) -> crate::error::Result<T> { |
| 106 | + tracing::trace!(output = ?output, "Handling Relay output"); |
| 107 | + Ok(output.val) |
| 108 | + } |
| 109 | +} |
0 commit comments