#[cfg(not(target_arch = "wasm32"))]
use std::net::SocketAddr;
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
use std::path::Path;
use std::time::Duration;
use nostr_relay_pool::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GossipRelayLimits {
pub read_relays_per_user: usize,
pub write_relays_per_user: usize,
pub hint_relays_per_user: usize,
pub most_used_relays_per_user: usize,
pub nip17_relays: usize,
}
impl Default for GossipRelayLimits {
fn default() -> Self {
Self {
read_relays_per_user: 3,
write_relays_per_user: 3,
hint_relays_per_user: 1,
most_used_relays_per_user: 1,
nip17_relays: 3,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GossipOptions {
pub limits: GossipRelayLimits,
}
impl GossipOptions {
#[inline]
pub fn limits(mut self, limits: GossipRelayLimits) -> Self {
self.limits = limits;
self
}
}
#[derive(Debug, Clone, Default)]
pub struct ClientOptions {
pub(super) autoconnect: bool,
#[cfg(not(target_arch = "wasm32"))]
pub(super) connection: Connection,
pub(super) relay_limits: RelayLimits,
pub(super) max_avg_latency: Option<Duration>,
pub(super) sleep_when_idle: SleepWhenIdle,
pub(super) verify_subscriptions: bool,
pub(super) ban_relay_on_mismatch: bool,
pub(super) gossip: GossipOptions,
pub(super) pool: RelayPoolOptions,
}
impl ClientOptions {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn autoconnect(mut self, val: bool) -> Self {
self.autoconnect = val;
self
}
#[inline]
pub fn automatic_authentication(mut self, enabled: bool) -> Self {
self.pool = self.pool.automatic_authentication(enabled);
self
}
#[inline]
#[cfg(not(target_arch = "wasm32"))]
pub fn connection(mut self, connection: Connection) -> Self {
self.connection = connection;
self
}
#[inline]
pub fn relay_limits(mut self, limits: RelayLimits) -> Self {
self.relay_limits = limits;
self
}
#[inline]
pub fn max_avg_latency(mut self, max: Duration) -> Self {
self.max_avg_latency = Some(max);
self
}
#[inline]
pub fn sleep_when_idle(mut self, config: SleepWhenIdle) -> Self {
self.sleep_when_idle = config;
self
}
pub fn verify_subscriptions(mut self, enable: bool) -> Self {
self.verify_subscriptions = enable;
self
}
pub fn ban_relay_on_mismatch(mut self, ban_relay: bool) -> Self {
self.ban_relay_on_mismatch = ban_relay;
self
}
#[inline]
pub fn gossip(mut self, opts: GossipOptions) -> Self {
self.gossip = opts;
self
}
#[inline]
pub fn pool(mut self, opts: RelayPoolOptions) -> Self {
self.pool = opts;
self
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SleepWhenIdle {
#[default]
Disabled,
Enabled {
timeout: Duration,
},
}
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum ConnectionTarget {
#[default]
All,
Onion,
}
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Connection {
pub mode: ConnectionMode,
pub target: ConnectionTarget,
}
#[allow(clippy::derivable_impls)]
#[cfg(not(target_arch = "wasm32"))]
impl Default for Connection {
fn default() -> Self {
#[cfg(all(feature = "tor", not(target_os = "android"), not(target_os = "ios")))]
{
Self {
mode: ConnectionMode::tor(),
target: ConnectionTarget::Onion,
}
}
#[cfg(any(
not(feature = "tor"),
all(feature = "tor", any(target_os = "android", target_os = "ios")),
))]
Self {
mode: ConnectionMode::default(),
target: ConnectionTarget::default(),
}
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Connection {
#[inline]
pub fn new() -> Self {
Self {
mode: ConnectionMode::default(),
target: ConnectionTarget::default(),
}
}
#[inline]
pub fn mode(mut self, mode: ConnectionMode) -> Self {
self.mode = mode;
self
}
#[inline]
pub fn target(mut self, target: ConnectionTarget) -> Self {
self.target = target;
self
}
#[inline]
pub fn direct(mut self) -> Self {
self.mode = ConnectionMode::direct();
self
}
#[inline]
pub fn proxy(mut self, addr: SocketAddr) -> Self {
self.mode = ConnectionMode::proxy(addr);
self
}
#[inline]
#[cfg(feature = "tor")]
pub fn embedded_tor(mut self) -> Self {
self.mode = ConnectionMode::tor();
self
}
#[inline]
#[cfg(feature = "tor")]
pub fn embedded_tor_with_path<P>(mut self, path: P) -> Self
where
P: AsRef<Path>,
{
self.mode = ConnectionMode::tor_with_path(path);
self
}
}