From 22fd23c11f640d2c0e863101cc577d5e22b36e33 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Wed, 11 Oct 2023 12:02:39 +0200 Subject: [PATCH] sdk: add `RelayConnectionType` --- crates/nostr-sdk/src/relay/mod.rs | 19 +++++++++++++++++++ crates/nostr-sdk/src/relay/pool.rs | 6 ++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/nostr-sdk/src/relay/mod.rs b/crates/nostr-sdk/src/relay/mod.rs index 77938568f..2a10ab80c 100644 --- a/crates/nostr-sdk/src/relay/mod.rs +++ b/crates/nostr-sdk/src/relay/mod.rs @@ -146,6 +146,15 @@ impl fmt::Display for RelayStatus { } } +/// Relay Connection Type +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum RelayConnectionType { + /// Manual: realy added manually + Manual, + /// Gossip: relay added by gossip + Gossip, +} + /// Relay event #[derive(Debug)] pub enum RelayEvent { @@ -252,6 +261,7 @@ pub struct Relay { #[cfg(not(target_arch = "wasm32"))] proxy: Option, status: Arc>, + connection_type: RelayConnectionType, #[cfg(feature = "nip11")] document: Arc>, opts: RelayOptions, @@ -278,6 +288,7 @@ impl Relay { #[cfg(not(target_arch = "wasm32"))] pub fn new( url: Url, + connection_type: RelayConnectionType, pool_sender: Sender, notification_sender: broadcast::Sender, proxy: Option, @@ -290,6 +301,7 @@ impl Relay { url, proxy, status: Arc::new(RwLock::new(RelayStatus::Initialized)), + connection_type, #[cfg(feature = "nip11")] document: Arc::new(RwLock::new(RelayInformationDocument::new())), opts, @@ -310,6 +322,7 @@ impl Relay { #[cfg(target_arch = "wasm32")] pub fn new( url: Url, + connection_type: RelayConnectionType, pool_sender: Sender, notification_sender: broadcast::Sender, opts: RelayOptions, @@ -320,6 +333,7 @@ impl Relay { Self { url, status: Arc::new(RwLock::new(RelayStatus::Initialized)), + connection_type, #[cfg(feature = "nip11")] document: Arc::new(RwLock::new(RelayInformationDocument::new())), opts, @@ -353,6 +367,11 @@ impl Relay { *status } + /// Relay Connection Type + pub fn connection_type(&self) -> RelayConnectionType { + self.connection_type + } + /// Get [`RelayStatus`] #[cfg(feature = "blocking")] pub fn status_blocking(&self) -> RelayStatus { diff --git a/crates/nostr-sdk/src/relay/pool.rs b/crates/nostr-sdk/src/relay/pool.rs index ff520578d..abf3969a7 100644 --- a/crates/nostr-sdk/src/relay/pool.rs +++ b/crates/nostr-sdk/src/relay/pool.rs @@ -22,8 +22,8 @@ use tokio::sync::{broadcast, Mutex, RwLock}; use super::options::RelayPoolOptions; use super::{ - Error as RelayError, FilterOptions, InternalSubscriptionId, Limits, Relay, RelayOptions, - RelaySendOptions, RelayStatus, + Error as RelayError, FilterOptions, InternalSubscriptionId, Limits, Relay, RelayConnectionType, + RelayOptions, RelaySendOptions, RelayStatus, }; use crate::util::TryIntoUrl; @@ -448,6 +448,7 @@ impl RelayPool { if !relays.contains_key(&url) { let relay = Relay::new( url, + RelayConnectionType::Manual, self.pool_task_sender.clone(), self.notification_sender.clone(), proxy, @@ -473,6 +474,7 @@ impl RelayPool { if !relays.contains_key(&url) { let relay = Relay::new( url, + RelayConnectionType::Manual, self.pool_task_sender.clone(), self.notification_sender.clone(), opts,