Skip to content

Commit

Permalink
nostr: add nip17::extract_relay_list and `nip17::extract_owned_rela…
Browse files Browse the repository at this point in the history
…y_list`

Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
  • Loading branch information
yukibtc committed Nov 22, 2024
1 parent f286099 commit b347ec0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
* nostr: add `NostrSigner::backend` ([Yuki Kishimoto])
* nostr: add `EventBuilder::private_msg` ([Yuki Kishimoto])
* nostr: add `EventBuilder::tag` and `EventBuilder::tags` ([Yuki Kishimoto])
* nostr: add `nip17::extract_relay_list` and `nip17::extract_owned_relay_list` ([Yuki Kishimoto])
* database: add `NostrEventsDatabase` trait ([Yuki Kishimoto])
* pool: add relay reconnection and disconnection unit tests ([Yuki Kishimoto])
* sdk: allow to specify relay pool notification channel size in `Options` ([Yuki Kishimoto])
Expand Down
1 change: 1 addition & 0 deletions crates/nostr/src/nips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod nip10;
pub mod nip11;
pub mod nip13;
pub mod nip15;
pub mod nip17;
pub mod nip19;
pub mod nip21;
pub mod nip26;
Expand Down
43 changes: 43 additions & 0 deletions crates/nostr/src/nips/nip17.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

//! NIP17: Private Direct Message
//!
//! <https://github.com/nostr-protocol/nips/blob/master/17.md>

use alloc::boxed::Box;
use core::iter;

use crate::types::Url;
use crate::{Event, Kind, TagStandard};

/// Extracts the relay list
pub fn extract_relay_list<'a>(event: &'a Event) -> Box<dyn Iterator<Item = &'a Url> + 'a> {
if event.kind != Kind::InboxRelays {
return Box::new(iter::empty());
}

Box::new(event.tags.iter().filter_map(|tag| {
if let Some(TagStandard::Relay(url)) = tag.as_standardized() {
Some(url)
} else {
None
}
}))
}

/// Extracts the relay list
pub fn extract_owned_relay_list(event: Event) -> Box<dyn Iterator<Item = Url>> {
if event.kind != Kind::InboxRelays {
return Box::new(iter::empty());
}

Box::new(event.tags.into_iter().filter_map(|tag| {
if let Some(TagStandard::Relay(url)) = tag.to_standardized() {
Some(url)
} else {
None
}
}))
}
2 changes: 2 additions & 0 deletions crates/nostr/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

//! Prelude

#![allow(unused_imports)]
#![allow(unknown_lints)]
#![allow(ambiguous_glob_reexports)]
#![doc(hidden)]
Expand Down Expand Up @@ -40,6 +41,7 @@ pub use crate::nips::nip10::{self, *};
pub use crate::nips::nip11::{self, *};
pub use crate::nips::nip13::{self, *};
pub use crate::nips::nip15::{self, *};
pub use crate::nips::nip17::{self, *};
pub use crate::nips::nip19::{self, *};
pub use crate::nips::nip21::{self, *};
pub use crate::nips::nip26::{self, *};
Expand Down

0 comments on commit b347ec0

Please sign in to comment.