Skip to content

Commit

Permalink
sdk: add RelayConnectionType
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 11, 2023
1 parent ba57899 commit f10241e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 19 additions & 0 deletions crates/nostr-sdk/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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 {
Expand Down Expand Up @@ -246,6 +255,7 @@ pub struct Relay {
#[cfg(not(target_arch = "wasm32"))]
proxy: Option<SocketAddr>,
status: Arc<RwLock<RelayStatus>>,
connection_type: RelayConnectionType,
#[cfg(feature = "nip11")]
document: Arc<RwLock<RelayInformationDocument>>,
opts: RelayOptions,
Expand All @@ -271,6 +281,7 @@ impl Relay {
#[cfg(not(target_arch = "wasm32"))]
pub fn new(
url: Url,
connection_type: RelayConnectionType,
pool_sender: Sender<RelayPoolMessage>,
notification_sender: broadcast::Sender<RelayPoolNotification>,
proxy: Option<SocketAddr>,
Expand All @@ -282,6 +293,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,
Expand All @@ -301,6 +313,7 @@ impl Relay {
#[cfg(target_arch = "wasm32")]
pub fn new(
url: Url,
connection_type: RelayConnectionType,
pool_sender: Sender<RelayPoolMessage>,
notification_sender: broadcast::Sender<RelayPoolNotification>,
opts: RelayOptions,
Expand All @@ -310,6 +323,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,
Expand Down Expand Up @@ -342,6 +356,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 {
Expand Down
6 changes: 4 additions & 2 deletions crates/nostr-sdk/src/relay/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use tokio::sync::{broadcast, Mutex, RwLock};

use super::options::RelayPoolOptions;
use super::{
Error as RelayError, FilterOptions, InternalSubscriptionId, Relay, RelayOptions,
RelaySendOptions, RelayStatus,
Error as RelayError, FilterOptions, InternalSubscriptionId, Relay, RelayConnectionType,
RelayOptions, RelaySendOptions, RelayStatus,
};
use crate::util::TryIntoUrl;

Expand Down Expand Up @@ -392,6 +392,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,
Expand All @@ -414,6 +415,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,
Expand Down

0 comments on commit f10241e

Please sign in to comment.