1#[cfg(not(target_arch = "wasm32"))]
8use std::net::SocketAddr;
9#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
10use std::path::Path;
11use std::time::Duration;
12
13use nostr_relay_pool::prelude::*;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct GossipRelayLimits {
18 pub read_relays_per_user: usize,
20 pub write_relays_per_user: usize,
22 pub hint_relays_per_user: usize,
24 pub most_used_relays_per_user: usize,
26 pub nip17_relays: usize,
28}
29
30impl Default for GossipRelayLimits {
31 fn default() -> Self {
32 Self {
33 read_relays_per_user: 3,
34 write_relays_per_user: 3,
35 hint_relays_per_user: 1,
36 most_used_relays_per_user: 1,
37 nip17_relays: 3,
38 }
39 }
40}
41
42#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
44pub struct GossipOptions {
45 pub limits: GossipRelayLimits,
47}
48
49impl GossipOptions {
50 #[inline]
52 pub fn limits(mut self, limits: GossipRelayLimits) -> Self {
53 self.limits = limits;
54 self
55 }
56}
57
58#[derive(Debug, Clone, Default)]
60pub struct ClientOptions {
61 pub(super) autoconnect: bool,
62 #[cfg(not(target_arch = "wasm32"))]
63 pub(super) connection: Connection,
64 pub(super) relay_limits: RelayLimits,
65 pub(super) max_avg_latency: Option<Duration>,
66 pub(super) sleep_when_idle: SleepWhenIdle,
67 pub(super) verify_subscriptions: bool,
68 pub(super) ban_relay_on_mismatch: bool,
69 pub(super) gossip: GossipOptions,
70 pub(super) pool: RelayPoolOptions,
71}
72
73impl ClientOptions {
74 #[inline]
76 pub fn new() -> Self {
77 Self::default()
78 }
79
80 #[inline]
84 pub fn autoconnect(mut self, val: bool) -> Self {
85 self.autoconnect = val;
86 self
87 }
88
89 #[inline]
93 pub fn automatic_authentication(mut self, enabled: bool) -> Self {
94 self.pool = self.pool.automatic_authentication(enabled);
95 self
96 }
97
98 #[inline]
100 #[cfg(not(target_arch = "wasm32"))]
101 pub fn connection(mut self, connection: Connection) -> Self {
102 self.connection = connection;
103 self
104 }
105
106 #[inline]
108 pub fn relay_limits(mut self, limits: RelayLimits) -> Self {
109 self.relay_limits = limits;
110 self
111 }
112
113 #[inline]
117 pub fn max_avg_latency(mut self, max: Duration) -> Self {
118 self.max_avg_latency = Some(max);
119 self
120 }
121
122 #[inline]
124 pub fn sleep_when_idle(mut self, config: SleepWhenIdle) -> Self {
125 self.sleep_when_idle = config;
126 self
127 }
128
129 pub fn verify_subscriptions(mut self, enable: bool) -> Self {
131 self.verify_subscriptions = enable;
132 self
133 }
134
135 pub fn ban_relay_on_mismatch(mut self, ban_relay: bool) -> Self {
137 self.ban_relay_on_mismatch = ban_relay;
138 self
139 }
140
141 #[inline]
143 pub fn gossip(mut self, opts: GossipOptions) -> Self {
144 self.gossip = opts;
145 self
146 }
147
148 #[inline]
150 pub fn pool(mut self, opts: RelayPoolOptions) -> Self {
151 self.pool = opts;
152 self
153 }
154}
155
156#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
158pub enum SleepWhenIdle {
159 #[default]
161 Disabled,
162 Enabled {
164 timeout: Duration,
168 },
169}
170
171#[cfg(not(target_arch = "wasm32"))]
173#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
174pub enum ConnectionTarget {
175 #[default]
177 All,
178 Onion,
180}
181
182#[cfg(not(target_arch = "wasm32"))]
184#[derive(Debug, Clone, PartialEq, Eq, Hash)]
185pub struct Connection {
186 pub mode: ConnectionMode,
188 pub target: ConnectionTarget,
190}
191
192#[allow(clippy::derivable_impls)]
193#[cfg(not(target_arch = "wasm32"))]
194impl Default for Connection {
195 fn default() -> Self {
196 #[cfg(all(feature = "tor", not(target_os = "android"), not(target_os = "ios")))]
197 {
198 Self {
199 mode: ConnectionMode::tor(),
200 target: ConnectionTarget::Onion,
201 }
202 }
203
204 #[cfg(any(
205 not(feature = "tor"),
206 all(feature = "tor", any(target_os = "android", target_os = "ios")),
207 ))]
208 Self {
209 mode: ConnectionMode::default(),
210 target: ConnectionTarget::default(),
211 }
212 }
213}
214
215#[cfg(not(target_arch = "wasm32"))]
216impl Connection {
217 #[inline]
219 pub fn new() -> Self {
220 Self {
221 mode: ConnectionMode::default(),
222 target: ConnectionTarget::default(),
223 }
224 }
225
226 #[inline]
228 pub fn mode(mut self, mode: ConnectionMode) -> Self {
229 self.mode = mode;
230 self
231 }
232
233 #[inline]
235 pub fn target(mut self, target: ConnectionTarget) -> Self {
236 self.target = target;
237 self
238 }
239
240 #[inline]
242 pub fn direct(mut self) -> Self {
243 self.mode = ConnectionMode::direct();
244 self
245 }
246
247 #[inline]
249 pub fn proxy(mut self, addr: SocketAddr) -> Self {
250 self.mode = ConnectionMode::proxy(addr);
251 self
252 }
253
254 #[inline]
256 #[cfg(feature = "tor")]
257 pub fn embedded_tor(mut self) -> Self {
258 self.mode = ConnectionMode::tor();
259 self
260 }
261
262 #[inline]
266 #[cfg(feature = "tor")]
267 pub fn embedded_tor_with_path<P>(mut self, path: P) -> Self
268 where
269 P: AsRef<Path>,
270 {
271 self.mode = ConnectionMode::tor_with_path(path);
272 self
273 }
274}