|
| 1 | +// Copyright (c) 2022-2023 Yuki Kishimoto |
| 2 | +// Copyright (c) 2023-2025 Rust Nostr Developers |
| 3 | +// Distributed under the MIT software license |
| 4 | + |
| 5 | +//! Nostr Gossip |
| 6 | +
|
| 7 | +#![forbid(unsafe_code)] |
| 8 | +#![warn(missing_docs)] |
| 9 | +#![warn(rustdoc::bare_urls)] |
| 10 | +#![warn(clippy::large_futures)] |
| 11 | + |
| 12 | +use std::any::Any; |
| 13 | +use std::collections::HashSet; |
| 14 | +use std::fmt::Debug; |
| 15 | + |
| 16 | +use nostr::prelude::*; |
| 17 | + |
| 18 | +pub mod error; |
| 19 | +pub mod prelude; |
| 20 | + |
| 21 | +use self::error::GossipError; |
| 22 | + |
| 23 | +/// Gossip list kind |
| 24 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 25 | +pub enum GossipListKind { |
| 26 | + /// NIP-17 |
| 27 | + Nip17, |
| 28 | + /// NIP-65 |
| 29 | + Nip65, |
| 30 | +} |
| 31 | + |
| 32 | +/// Public key status |
| 33 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 34 | +pub enum GossipPublicKeyStatus { |
| 35 | + /// The public key data is updated |
| 36 | + Updated, |
| 37 | + /// The public key data is outdated |
| 38 | + Outdated, |
| 39 | +} |
| 40 | + |
| 41 | +impl GossipPublicKeyStatus { |
| 42 | + /// Check if the public key data is outdated |
| 43 | + #[inline] |
| 44 | + pub fn is_outdated(&self) -> bool { |
| 45 | + matches!(self, Self::Outdated) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// Best relay selection. |
| 50 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 51 | +pub enum BestRelaySelection { |
| 52 | + /// Get all the best relays for **reading** and **writing** events (NIP-65) |
| 53 | + All { |
| 54 | + /// Limit for read relays |
| 55 | + read: usize, |
| 56 | + /// Limit for write relays |
| 57 | + write: usize, |
| 58 | + /// Limit for hints |
| 59 | + hints: usize, |
| 60 | + /// Limit for most received relays |
| 61 | + most_received: usize, |
| 62 | + }, |
| 63 | + /// Get the best relays for **reading** events (NIP-65) |
| 64 | + Read { |
| 65 | + /// Limit |
| 66 | + limit: usize, |
| 67 | + }, |
| 68 | + /// Get the best relays for **writing** events (NIP-65) |
| 69 | + Write { |
| 70 | + /// Limit |
| 71 | + limit: usize, |
| 72 | + }, |
| 73 | + /// Get the best relays for **reading** and **writing** private messages (NIP-17) |
| 74 | + PrivateMessage { |
| 75 | + /// Limit |
| 76 | + limit: usize, |
| 77 | + }, |
| 78 | + /// Relays found in hints |
| 79 | + Hints { |
| 80 | + /// Limit |
| 81 | + limit: usize, |
| 82 | + }, |
| 83 | + /// Relays that received most events |
| 84 | + MostReceived { |
| 85 | + /// Limit |
| 86 | + limit: usize, |
| 87 | + }, |
| 88 | +} |
| 89 | + |
| 90 | +/// Nostr gossip trait. |
| 91 | +pub trait NostrGossip: Any + Debug + Send + Sync { |
| 92 | + /// Process an [`Event`] |
| 93 | + /// |
| 94 | + /// Optionally takes the [`RelayUrl`] from where the [`Event`] comes from. |
| 95 | + fn process<'a>( |
| 96 | + &'a self, |
| 97 | + event: &'a Event, |
| 98 | + relay_url: Option<&'a RelayUrl>, |
| 99 | + ) -> BoxedFuture<'a, Result<(), GossipError>>; |
| 100 | + |
| 101 | + /// Check the [`PublicKey`] status |
| 102 | + fn status<'a>( |
| 103 | + &'a self, |
| 104 | + public_key: &'a PublicKey, |
| 105 | + list: GossipListKind, |
| 106 | + ) -> BoxedFuture<'a, Result<GossipPublicKeyStatus, GossipError>>; |
| 107 | + |
| 108 | + /// Update the last check timestamp for stale [`PublicKey`]. |
| 109 | + fn update_fetch_attempt<'a>( |
| 110 | + &'a self, |
| 111 | + public_key: &'a PublicKey, |
| 112 | + list: GossipListKind, |
| 113 | + ) -> BoxedFuture<'a, Result<(), GossipError>>; |
| 114 | + |
| 115 | + /// Get the best relays for a [`PublicKey`]. |
| 116 | + fn get_best_relays<'a>( |
| 117 | + &'a self, |
| 118 | + public_key: &'a PublicKey, |
| 119 | + selection: BestRelaySelection, |
| 120 | + ) -> BoxedFuture<'a, Result<HashSet<RelayUrl>, GossipError>>; |
| 121 | +} |
0 commit comments