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 23, 2023
1 parent 6e879d3 commit 22fd23c
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 @@ -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 {
Expand Down Expand Up @@ -252,6 +261,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 @@ -278,6 +288,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 @@ -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,
Expand All @@ -310,6 +322,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 @@ -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,
Expand Down Expand Up @@ -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 {
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 @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 22fd23c

Please sign in to comment.