From 000901c0cbca8464b5a89bcc93c5474f6564bafd Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Sun, 1 Oct 2023 00:00:00 +0100 Subject: feat(prs-create) send to multiple relays add tests but these currently don't work when run together --- src/client.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/client.rs') diff --git a/src/client.rs b/src/client.rs index a6f7dda..e0e0494 100644 --- a/src/client.rs +++ b/src/client.rs @@ -18,6 +18,7 @@ use nostr::Event; pub struct Client { client: nostr_sdk::Client, + pub fallback_relays: Vec, } #[async_trait] @@ -26,6 +27,7 @@ pub trait Connect { fn default() -> Self; fn new(opts: Params) -> Self; async fn connect(&self) -> Result<()>; + async fn disconnect(&self) -> Result<()>; async fn send_event_to(&self, url: &str, event: nostr::event::Event) -> Result; } @@ -34,19 +36,31 @@ impl Connect for Client { fn default() -> Self { Client { client: nostr_sdk::Client::new(&nostr::Keys::generate()), + fallback_relays: vec![ + "ws://localhost:8080".to_string(), + "ws://localhost:8052".to_string(), + ], } } fn new(opts: Params) -> Self { Client { client: nostr_sdk::Client::new(&opts.keys.unwrap_or(nostr::Keys::generate())), + fallback_relays: opts.fallback_relays, } } async fn connect(&self) -> Result<()> { - self.client.add_relay("ws://localhost:8080", None).await?; + for relay in &self.fallback_relays { + self.client.add_relay(relay.as_str(), None).await?; + } self.client.connect().await; - // self.client.s Ok(()) } + + async fn disconnect(&self) -> Result<()> { + self.client.disconnect().await?; + Ok(()) + } + async fn send_event_to(&self, url: &str, event: Event) -> Result { Ok(self.client.send_event_to(url, event).await?) } @@ -55,6 +69,7 @@ impl Connect for Client { #[derive(Default)] pub struct Params { pub keys: Option, + pub fallback_relays: Vec, } impl Params { @@ -62,4 +77,8 @@ impl Params { self.keys = Some(keys); self } + pub fn with_fallback_relays(mut self, fallback_relays: Vec) -> Self { + self.fallback_relays = fallback_relays; + self + } } -- cgit v1.2.3