Skip to content

Commit f4b72d2

Browse files
committed
pool: add SendOutput and SendEventOutput structs
Return relay urls to which `messages`/`events` have or not been sent for `send_*` and `batch_*` methods. Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 588911c commit f4b72d2

File tree

17 files changed

+493
-227
lines changed

17 files changed

+493
-227
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
* nostr: rename NIP-51 `EventBuilder` set constructors and `Kind` variants ([Yuki Kishimoto])
3131
* nostr: small adj. to NIP-47 `ListTransactionsRequestParams` and `LookupInvoiceResponseResult` structs ([Yuki Kishimoto])
3232
* pool: use per-purpose dedicated relay channels ([Yuki Kishimoto])
33+
* pool: return relay urls to which `messages`/`events` have or not been sent for `send_*` and `batch_*` methods ([Yuki Kishimoto])
3334
* ffi(sdk): convert `RelayPool::handle_notifications` method to async/future ([Yuki Kishimoto])
3435

3536
### Added
3637

3738
* nostr: add `EventBuilder::interest_set` ([Yuki Kishimoto])
39+
* pool: add `SendOutput` and `SendEventOutput` structs ([Yuki Kishimoto])
3840
* book: add some python examples ([RydalWater])
3941

4042
### Fixed

bindings/nostr-sdk-ffi/bindings-python/examples/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async def main():
2424
client = Client(signer)
2525

2626
# Add relays and connect
27-
await client.add_relays(["wss://relay.damus.io", "wss://nos.lol"])
27+
await client.add_relays(["wss://relay.damus.io", "wss://nos.lol", "wss://nostr.wine"])
2828
await client.connect()
2929

3030
# Send an event using the Nostr Signer
@@ -36,10 +36,12 @@ async def main():
3636
custom_keys = Keys.generate()
3737
print("Mining a POW text note...")
3838
event = EventBuilder.text_note("Hello from rust-nostr Python bindings!", []).to_pow_event(custom_keys, 20)
39-
event_id = await client.send_event(event)
39+
output = await client.send_event(event)
4040
print("Event sent:")
41-
print(f" hex: {event_id.to_hex()}")
42-
print(f" bech32: {event_id.to_bech32()}")
41+
print(f" hex: {output.id.to_hex()}")
42+
print(f" bech32: {output.id.to_bech32()}")
43+
print(f" Successfully sent to: {output.success}")
44+
print(f" Failed to send to: {output.failed}")
4345

4446
await asyncio.sleep(2.0)
4547

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

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub use self::options::Options;
2626
pub use self::signer::NostrSigner;
2727
use self::zapper::{ZapDetails, ZapEntity};
2828
use crate::error::Result;
29+
use crate::pool::result::{SendEventOutput, SendOutput};
2930
use crate::relay::options::{NegentropyOptions, SubscribeAutoCloseOptions};
3031
use crate::relay::{RelayBlacklist, RelayOptions};
3132
use crate::{HandleNotification, NostrDatabase, Relay};
@@ -390,37 +391,44 @@ impl Client {
390391
.collect())
391392
}
392393

393-
pub async fn send_msg(&self, msg: Arc<ClientMessage>) -> Result<()> {
394-
Ok(self.inner.send_msg(msg.as_ref().deref().clone()).await?)
394+
pub async fn send_msg(&self, msg: Arc<ClientMessage>) -> Result<SendOutput> {
395+
Ok(self
396+
.inner
397+
.send_msg(msg.as_ref().deref().clone())
398+
.await?
399+
.into())
395400
}
396401

397-
pub async fn send_msg_to(&self, urls: Vec<String>, msg: Arc<ClientMessage>) -> Result<()> {
402+
pub async fn send_msg_to(
403+
&self,
404+
urls: Vec<String>,
405+
msg: Arc<ClientMessage>,
406+
) -> Result<SendOutput> {
398407
Ok(self
399408
.inner
400409
.send_msg_to(urls, msg.as_ref().deref().clone())
401-
.await?)
410+
.await?
411+
.into())
402412
}
403413

404-
pub async fn send_event(&self, event: Arc<Event>) -> Result<Arc<EventId>> {
405-
Ok(Arc::new(
406-
self.inner
407-
.send_event(event.as_ref().deref().clone())
408-
.await?
409-
.into(),
410-
))
414+
pub async fn send_event(&self, event: Arc<Event>) -> Result<SendEventOutput> {
415+
Ok(self
416+
.inner
417+
.send_event(event.as_ref().deref().clone())
418+
.await?
419+
.into())
411420
}
412421

413422
pub async fn send_event_to(
414423
&self,
415424
urls: Vec<String>,
416425
event: Arc<Event>,
417-
) -> Result<Arc<EventId>> {
418-
Ok(Arc::new(
419-
self.inner
420-
.send_event_to(urls, event.as_ref().deref().clone())
421-
.await?
422-
.into(),
423-
))
426+
) -> Result<SendEventOutput> {
427+
Ok(self
428+
.inner
429+
.send_event_to(urls, event.as_ref().deref().clone())
430+
.await?
431+
.into())
424432
}
425433

426434
/// Signs the `EventBuilder` into an `Event` using the `NostrSigner`
@@ -436,13 +444,12 @@ impl Client {
436444
/// Take an [`EventBuilder`], sign it by using the [`NostrSigner`] and broadcast to all relays.
437445
///
438446
/// Rise an error if the [`NostrSigner`] is not set.
439-
pub async fn send_event_builder(&self, builder: Arc<EventBuilder>) -> Result<Arc<EventId>> {
440-
Ok(Arc::new(
441-
self.inner
442-
.send_event_builder(builder.as_ref().deref().clone())
443-
.await?
444-
.into(),
445-
))
447+
pub async fn send_event_builder(&self, builder: Arc<EventBuilder>) -> Result<SendEventOutput> {
448+
Ok(self
449+
.inner
450+
.send_event_builder(builder.as_ref().deref().clone())
451+
.await?
452+
.into())
446453
}
447454

448455
/// Take an [`EventBuilder`], sign it by using the [`NostrSigner`] and broadcast to specific relays.
@@ -452,22 +459,20 @@ impl Client {
452459
&self,
453460
urls: Vec<String>,
454461
builder: Arc<EventBuilder>,
455-
) -> Result<Arc<EventId>> {
456-
Ok(Arc::new(
457-
self.inner
458-
.send_event_builder_to(urls, builder.as_ref().deref().clone())
459-
.await?
460-
.into(),
461-
))
462+
) -> Result<SendEventOutput> {
463+
Ok(self
464+
.inner
465+
.send_event_builder_to(urls, builder.as_ref().deref().clone())
466+
.await?
467+
.into())
462468
}
463469

464-
pub async fn set_metadata(&self, metadata: Arc<Metadata>) -> Result<Arc<EventId>> {
465-
Ok(Arc::new(
466-
self.inner
467-
.set_metadata(metadata.as_ref().deref())
468-
.await?
469-
.into(),
470-
))
470+
pub async fn set_metadata(&self, metadata: Arc<Metadata>) -> Result<SendEventOutput> {
471+
Ok(self
472+
.inner
473+
.set_metadata(metadata.as_ref().deref())
474+
.await?
475+
.into())
471476
}
472477

473478
/// Encrypted direct msg
@@ -480,14 +485,13 @@ impl Client {
480485
receiver: &PublicKey,
481486
msg: String,
482487
reply: Option<Arc<EventId>>,
483-
) -> Result<Arc<EventId>> {
488+
) -> Result<SendEventOutput> {
484489
#[allow(deprecated)]
485-
Ok(Arc::new(
486-
self.inner
487-
.send_direct_msg(**receiver, msg, reply.map(|r| **r))
488-
.await?
489-
.into(),
490-
))
490+
Ok(self
491+
.inner
492+
.send_direct_msg(**receiver, msg, reply.map(|r| **r))
493+
.await?
494+
.into())
491495
}
492496

493497
/// Send private direct message
@@ -499,55 +503,50 @@ impl Client {
499503
receiver: &PublicKey,
500504
message: String,
501505
reply_to: Option<Arc<EventId>>,
502-
) -> Result<()> {
506+
) -> Result<SendEventOutput> {
503507
Ok(self
504508
.inner
505509
.send_private_msg(**receiver, message, reply_to.map(|t| **t))
506-
.await?)
510+
.await?
511+
.into())
507512
}
508513

509514
/// Repost
510515
pub async fn repost(
511516
&self,
512517
event: Arc<Event>,
513518
relay_url: Option<String>,
514-
) -> Result<Arc<EventId>> {
515-
Ok(Arc::new(
516-
self.inner
517-
.repost(event.as_ref().deref(), relay_url.map(UncheckedUrl::from))
518-
.await?
519-
.into(),
520-
))
519+
) -> Result<SendEventOutput> {
520+
Ok(self
521+
.inner
522+
.repost(event.as_ref().deref(), relay_url.map(UncheckedUrl::from))
523+
.await?
524+
.into())
521525
}
522526

523527
/// Like event
524528
///
525529
/// <https://github.com/nostr-protocol/nips/blob/master/25.md>
526-
pub async fn like(&self, event: Arc<Event>) -> Result<Arc<EventId>> {
527-
Ok(Arc::new(
528-
self.inner.like(event.as_ref().deref()).await?.into(),
529-
))
530+
pub async fn like(&self, event: Arc<Event>) -> Result<SendEventOutput> {
531+
Ok(self.inner.like(event.as_ref().deref()).await?.into())
530532
}
531533

532534
/// Disike event
533535
///
534536
/// <https://github.com/nostr-protocol/nips/blob/master/25.md>
535-
pub async fn dislike(&self, event: Arc<Event>) -> Result<Arc<EventId>> {
536-
Ok(Arc::new(
537-
self.inner.dislike(event.as_ref().deref()).await?.into(),
538-
))
537+
pub async fn dislike(&self, event: Arc<Event>) -> Result<SendEventOutput> {
538+
Ok(self.inner.dislike(event.as_ref().deref()).await?.into())
539539
}
540540

541541
/// React to an [`Event`]
542542
///
543543
/// <https://github.com/nostr-protocol/nips/blob/master/25.md>
544-
pub async fn reaction(&self, event: Arc<Event>, reaction: String) -> Result<Arc<EventId>> {
545-
Ok(Arc::new(
546-
self.inner
547-
.reaction(event.as_ref().deref(), reaction)
548-
.await?
549-
.into(),
550-
))
544+
pub async fn reaction(&self, event: Arc<Event>, reaction: String) -> Result<SendEventOutput> {
545+
Ok(self
546+
.inner
547+
.reaction(event.as_ref().deref(), reaction)
548+
.await?
549+
.into())
551550
}
552551

553552
/// Send a Zap!
@@ -571,28 +570,28 @@ impl Client {
571570
receiver: &PublicKey,
572571
rumor: Arc<EventBuilder>,
573572
expiration: Option<Arc<Timestamp>>,
574-
) -> Result<()> {
573+
) -> Result<SendEventOutput> {
575574
Ok(self
576575
.inner
577576
.gift_wrap(
578577
**receiver,
579578
rumor.as_ref().deref().clone(),
580579
expiration.map(|t| **t),
581580
)
582-
.await?)
581+
.await?
582+
.into())
583583
}
584584

585585
pub async fn file_metadata(
586586
&self,
587587
description: String,
588588
metadata: Arc<FileMetadata>,
589-
) -> Result<Arc<EventId>> {
590-
Ok(Arc::new(
591-
self.inner
592-
.file_metadata(description, metadata.as_ref().deref().clone())
593-
.await?
594-
.into(),
595-
))
589+
) -> Result<SendEventOutput> {
590+
Ok(self
591+
.inner
592+
.file_metadata(description, metadata.as_ref().deref().clone())
593+
.await?
594+
.into())
596595
}
597596

598597
pub async fn reconcile(&self, filter: Arc<Filter>, opts: Arc<NegentropyOptions>) -> Result<()> {

0 commit comments

Comments
 (0)