Skip to content

Commit 1929f6b

Browse files
committed
sdk: inherit pool subscriptions only when calling Client::add_relay or Client::add_read_relay methods
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 03cde8e commit 1929f6b

File tree

6 files changed

+77
-30
lines changed

6 files changed

+77
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
* sdk: update `Client::shutdown` method fingerprint ([Yuki Kishimoto])
7474
* sdk: deprecate `Client::add_relay_with_opts` and `Client::add_relays` ([Yuki Kishimoto])
7575
* sdk: deprecate `RelayPool::send_msg` and `RelayPool::batch_msg` ([Yuki Kishimoto])
76+
* sdk: inherit pool subscriptions only when calling `Client::add_relay` or `Client::add_read_relay` methods ([Yuki Kishimoto])
7677
* ffi(nostr): impl `Display` for `Coordinate` ([Yuki Kishimoto])
7778
* ffi(sdk): change `Connection::embedded_tor` fingerprint for `android` and `ios` targets ([Yuki Kishimoto])
7879

bindings/nostr-sdk-ffi/src/client/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ impl Client {
195195
/// Add read relay
196196
///
197197
/// If relay already exists, this method add the `READ` flag to it and return `false`.
198+
///
199+
/// If are set pool subscriptions, the new added relay will inherit them. Use `subscribe_to` method instead of `subscribe`,
200+
/// to avoid to set pool subscriptions.
198201
pub async fn add_read_relay(&self, url: String) -> Result<bool> {
199202
Ok(self.inner.add_read_relay(url).await?)
200203
}

bindings/nostr-sdk-js/src/client/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ impl JsClient {
213213
/// Add read relay
214214
///
215215
/// If relay already exists, this method add the `READ` flag to it and return `false`.
216+
///
217+
/// If are set pool subscriptions, the new added relay will inherit them. Use `subscribe_to` method instead of `subscribe`,
218+
/// to avoid to set pool subscriptions.
216219
#[wasm_bindgen(js_name = addReadRelay)]
217220
pub async fn add_read_relay(&self, url: String) -> Result<bool> {
218221
self.inner.add_read_relay(url).await.map_err(into_err)

crates/nostr-relay-pool/src/pool/internal.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,12 @@ impl InternalRelayPool {
205205
subscriptions.clear();
206206
}
207207

208-
pub async fn add_relay<U>(&self, url: U, opts: RelayOptions) -> Result<bool, Error>
208+
pub async fn add_relay<U>(
209+
&self,
210+
url: U,
211+
inherit_pool_subscriptions: bool,
212+
opts: RelayOptions,
213+
) -> Result<bool, Error>
209214
where
210215
U: TryIntoUrl,
211216
Error: From<<U as TryIntoUrl>::Err>,
@@ -217,33 +222,36 @@ impl InternalRelayPool {
217222
let mut relays = self.relays.write().await;
218223

219224
// Check if map already contains url
220-
if !relays.contains_key(&url) {
221-
// Compose new relay
222-
let relay = Relay::custom(url, self.database.clone(), self.blacklist.clone(), opts);
225+
if relays.contains_key(&url) {
226+
return Ok(false);
227+
}
223228

224-
// Set notification sender
225-
relay
226-
.set_notification_sender(Some(self.notification_sender.clone()))
227-
.await;
229+
// Compose new relay
230+
let relay = Relay::custom(url, self.database.clone(), self.blacklist.clone(), opts);
228231

229-
// Set relay subscriptions
232+
// Set notification sender
233+
relay
234+
.set_notification_sender(Some(self.notification_sender.clone()))
235+
.await;
236+
237+
// Set relay subscriptions
238+
if inherit_pool_subscriptions {
230239
let subscriptions = self.subscriptions().await;
231240
for (id, filters) in subscriptions.into_iter() {
232241
relay.inner.update_subscription(id, filters, false).await;
233242
}
243+
}
234244

235-
// Insert relay into map
236-
relays.insert(relay.url(), relay);
245+
// Insert relay into map
246+
relays.insert(relay.url(), relay);
237247

238-
Ok(true)
239-
} else {
240-
Ok(false)
241-
}
248+
Ok(true)
242249
}
243250

244251
pub async fn get_or_add_relay<U>(
245252
&self,
246253
url: U,
254+
inherit_pool_subscriptions: bool,
247255
opts: RelayOptions,
248256
) -> Result<Option<Relay>, Error>
249257
where
@@ -253,7 +261,8 @@ impl InternalRelayPool {
253261
match self.relay(url.clone()).await {
254262
Ok(relay) => Ok(Some(relay)),
255263
Err(..) => {
256-
self.add_relay(url, opts).await?;
264+
self.add_relay(url, inherit_pool_subscriptions, opts)
265+
.await?;
257266
Ok(None)
258267
}
259268
}

crates/nostr-relay-pool/src/pool/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl RelayPool {
175175
U: TryIntoUrl,
176176
Error: From<<U as TryIntoUrl>::Err>,
177177
{
178-
self.inner.add_relay(url, opts).await
178+
self.inner.add_relay(url, true, opts).await
179179
}
180180

181181
/// Try to get relay by `url` or add it to pool.
@@ -185,13 +185,16 @@ impl RelayPool {
185185
pub async fn get_or_add_relay<U>(
186186
&self,
187187
url: U,
188+
inherit_pool_subscriptions: bool,
188189
opts: RelayOptions,
189190
) -> Result<Option<Relay>, Error>
190191
where
191192
U: TryIntoUrl + Clone,
192193
Error: From<<U as TryIntoUrl>::Err>,
193194
{
194-
self.inner.get_or_add_relay(url, opts).await
195+
self.inner
196+
.get_or_add_relay(url, inherit_pool_subscriptions, opts)
197+
.await
195198
}
196199

197200
/// Disconnect and remove relay

crates/nostr-sdk/src/client/mod.rs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ impl Client {
414414
async fn get_or_add_relay_with_flag<U>(
415415
&self,
416416
url: U,
417+
inherit_pool_subscriptions: bool,
417418
flag: RelayServiceFlags,
418419
) -> Result<bool, Error>
419420
where
@@ -430,7 +431,11 @@ impl Client {
430431
let opts: RelayOptions = opts.flags(flag);
431432

432433
// Add relay with opts or edit current one
433-
match self.pool.get_or_add_relay::<&Url>(&url, opts).await? {
434+
match self
435+
.pool
436+
.get_or_add_relay::<&Url>(&url, inherit_pool_subscriptions, opts)
437+
.await?
438+
{
434439
Some(relay) => {
435440
relay.flags_ref().add(flag);
436441
Ok(false)
@@ -481,7 +486,7 @@ impl Client {
481486
U: TryIntoUrl,
482487
pool::Error: From<<U as TryIntoUrl>::Err>,
483488
{
484-
self.get_or_add_relay_with_flag(url, RelayServiceFlags::default())
489+
self.get_or_add_relay_with_flag(url, true, RelayServiceFlags::default())
485490
.await
486491
}
487492

@@ -496,21 +501,32 @@ impl Client {
496501
U: TryIntoUrl,
497502
pool::Error: From<<U as TryIntoUrl>::Err>,
498503
{
499-
self.get_or_add_relay_with_flag(url, RelayServiceFlags::PING | RelayServiceFlags::DISCOVERY)
500-
.await
504+
self.get_or_add_relay_with_flag(
505+
url,
506+
false,
507+
RelayServiceFlags::PING | RelayServiceFlags::DISCOVERY,
508+
)
509+
.await
501510
}
502511

503512
/// Add read relay
504513
///
505514
/// If relay already exists, this method add the `READ` flag to it and return `false`.
515+
///
516+
/// If are set pool subscriptions, the new added relay will inherit them. Use `subscribe_to` method instead of `subscribe`,
517+
/// to avoid to set pool subscriptions.
506518
#[inline]
507519
pub async fn add_read_relay<U>(&self, url: U) -> Result<bool, Error>
508520
where
509521
U: TryIntoUrl,
510522
pool::Error: From<<U as TryIntoUrl>::Err>,
511523
{
512-
self.get_or_add_relay_with_flag(url, RelayServiceFlags::PING | RelayServiceFlags::READ)
513-
.await
524+
self.get_or_add_relay_with_flag(
525+
url,
526+
true,
527+
RelayServiceFlags::PING | RelayServiceFlags::READ,
528+
)
529+
.await
514530
}
515531

516532
/// Add write relay
@@ -522,8 +538,12 @@ impl Client {
522538
U: TryIntoUrl,
523539
pool::Error: From<<U as TryIntoUrl>::Err>,
524540
{
525-
self.get_or_add_relay_with_flag(url, RelayServiceFlags::PING | RelayServiceFlags::WRITE)
526-
.await
541+
self.get_or_add_relay_with_flag(
542+
url,
543+
false,
544+
RelayServiceFlags::PING | RelayServiceFlags::WRITE,
545+
)
546+
.await
527547
}
528548

529549
#[inline]
@@ -532,8 +552,12 @@ impl Client {
532552
U: TryIntoUrl,
533553
pool::Error: From<<U as TryIntoUrl>::Err>,
534554
{
535-
self.get_or_add_relay_with_flag(url, RelayServiceFlags::PING | RelayServiceFlags::INBOX)
536-
.await
555+
self.get_or_add_relay_with_flag(
556+
url,
557+
false,
558+
RelayServiceFlags::PING | RelayServiceFlags::INBOX,
559+
)
560+
.await
537561
}
538562

539563
#[inline]
@@ -542,8 +566,12 @@ impl Client {
542566
U: TryIntoUrl,
543567
pool::Error: From<<U as TryIntoUrl>::Err>,
544568
{
545-
self.get_or_add_relay_with_flag(url, RelayServiceFlags::PING | RelayServiceFlags::OUTBOX)
546-
.await
569+
self.get_or_add_relay_with_flag(
570+
url,
571+
false,
572+
RelayServiceFlags::PING | RelayServiceFlags::OUTBOX,
573+
)
574+
.await
547575
}
548576

549577
/// Add new relay with custom [`RelayOptions`]

0 commit comments

Comments
 (0)