Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/relay/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ impl RelayPoolTrait for MockRelayPool {
async fn publish_to(&self, _urls: &[String], builder: EventBuilder) -> Result<EventId> {
self.publish(builder).await
}

/// Return stored events matching the filter.
async fn fetch_events(
&self,
filter: Filter,
_timeout: std::time::Duration,
) -> Result<Vec<Event>> {
let inner = self.inner.lock().await;
Ok(inner
.events
.iter()
.filter(|e| filter.match_event(e, MatchEventOptions::default()))
.cloned()
.collect())
}
}

// ── Helpers ───────────────────────────────────────────────────────────────────
Expand Down
17 changes: 17 additions & 0 deletions src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use async_trait::async_trait;
use crate::core::error::{Error, Result};
use nostr_sdk::prelude::*;
use std::sync::Arc;
use std::time::Duration;

/// Trait abstracting relay pool operations, enabling dependency injection and testing.
#[async_trait]
Expand All @@ -36,6 +37,8 @@ pub trait RelayPoolTrait: Send + Sync {
async fn subscribe(&self, filters: Vec<Filter>) -> Result<()>;
/// Sign and publish an event to specific relay URLs.
async fn publish_to(&self, urls: &[String], builder: EventBuilder) -> Result<EventId>;
/// Fetch events matching a filter from connected relays.
async fn fetch_events(&self, filter: Filter, timeout: Duration) -> Result<Vec<Event>>;
}

/// Relay pool wrapper for managing Nostr relay connections.
Expand Down Expand Up @@ -147,6 +150,16 @@ impl RelayPool {
.map_err(|e| Error::Transport(e.to_string()))?;
Ok(output.val)
}

/// Fetch events matching a filter from connected relays.
pub async fn fetch_events(&self, filter: Filter, timeout: Duration) -> Result<Vec<Event>> {
let events = self
.client
.fetch_events(filter, timeout)
.await
.map_err(|e| Error::Transport(e.to_string()))?;
Ok(events.into_iter().collect())
}
}

#[async_trait]
Expand Down Expand Up @@ -193,4 +206,8 @@ impl RelayPoolTrait for RelayPool {
async fn publish_to(&self, urls: &[String], builder: EventBuilder) -> Result<EventId> {
RelayPool::publish_to(self, urls, builder).await
}

async fn fetch_events(&self, filter: Filter, timeout: Duration) -> Result<Vec<Event>> {
RelayPool::fetch_events(self, filter, timeout).await
}
}
Loading
Loading