Skip to content

Commit

Permalink
sdk: temp add Client::fetch_combined_events
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 1, 2024
1 parent 4678d64 commit 497c72f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,34 @@ impl Client {
.into())
}

/// Get events both from database and relays
///
/// You can obtain the same result by merging the `Events` from different type of sources.
///
/// This method will be deprecated in the future!
/// This is a temporary solution for who still want to query events both from database and relays and merge the result.
/// The optimal solution is to execute a [`Client::sync`] to get all old events, [`Client::subscribe`] to get all
/// new future events, [`NostrDatabase::query`] to query events and [`Client::handle_notifications`] to listen-for/handle new events (i.e. to know when update the UI).
/// This will allow very fast queries, low bandwidth usage (depending on how many events the client have to sync) and a low load on relays.
///
/// If `gossip` is enabled (see [`Options::gossip`]) the events will be requested also to
/// NIP65 relays (automatically discovered) of public keys included in filters (if any).
pub async fn fetch_combined_events(
&self,
filters: Vec<Arc<Filter>>,
timeout: Option<Duration>,
) -> Result<Events> {
let filters = filters
.into_iter()
.map(|f| f.as_ref().deref().clone())
.collect();
Ok(self
.inner
.fetch_combined_events(filters, timeout)
.await?
.into())
}

pub async fn send_msg_to(&self, urls: Vec<String>, msg: Arc<ClientMessage>) -> Result<Output> {
Ok(self
.inner
Expand Down
28 changes: 28 additions & 0 deletions bindings/nostr-sdk-js/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,34 @@ impl JsClient {
Ok(events.into())
}

/// Get events both from database and relays
///
/// You can obtain the same result by merging the `Events` from different type of sources.
///
/// This method will be deprecated in the future!
/// This is a temporary solution for who still want to query events both from database and relays and merge the result.
/// The optimal solution is to execute a [`Client::sync`] to get all old events, [`Client::subscribe`] to get all
/// new future events, [`NostrDatabase::query`] to query events and [`Client::handle_notifications`] to listen-for/handle new events (i.e. to know when update the UI).
/// This will allow very fast queries, low bandwidth usage (depending on how many events the client have to reconcile) and a lower load on the relays.
///
/// If `gossip` is enabled (see [`Options::gossip`]) the events will be requested also to
/// NIP65 relays (automatically discovered) of public keys included in filters (if any).
#[wasm_bindgen(js_name = fetchCombinedEvents)]
pub async fn fetch_combined_events(
&self,
filters: Vec<JsFilter>,
timeout: Option<JsDuration>,
) -> Result<JsEvents> {
let filters: Vec<Filter> = filters.into_iter().map(|f| f.into()).collect();
let timeout: Option<Duration> = timeout.map(|d| *d);
let events: Events = self
.inner
.fetch_combined_events(filters, timeout)
.await
.map_err(into_err)?;
Ok(events.into())
}

/// Send client message to a specific relay
#[wasm_bindgen(js_name = sendMsgTo)]
pub async fn send_msg_to(&self, urls: Vec<String>, msg: &JsClientMessage) -> Result<JsOutput> {
Expand Down
53 changes: 53 additions & 0 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,59 @@ impl Client {
.to_vec())
}

/// Get events both from database and relays
///
///
/// This method will be deprecated in the future!
/// This is a temporary solution for who still want to query events both from database and relays and merge the result.
/// The optimal solution is to execute a [`Client::sync`] to reconcile missing events, [`Client::subscribe`] to get all
/// new future events, [`NostrDatabase::query`] to query stored events and [`Client::handle_notifications`] to listen-for/handle new events (i.e. to know when update the UI).
/// This will allow very fast queries, low bandwidth usage (depending on how many events the client have to reconcile) and a lower load on the relays.
///
/// If `gossip` is enabled (see [`Options::gossip`]) the events will be requested also to
/// NIP65 relays (automatically discovered) of public keys included in filters (if any).
///
/// You can obtain the same result with:
/// ```rust,no_run
/// # use std::time::Duration;
/// # use nostr_sdk::prelude::*;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # let client = Client::default();
/// # let filters = vec![Filter::new().limit(1)];
/// // Query database
/// let stored_events: Events = client.database().query(filters.clone()).await?;
///
/// // Query relays
/// let fetched_events: Events = client
/// .fetch_events(filters, Some(Duration::from_secs(10)))
/// .await?;
///
/// // Merge result
/// let events: Events = stored_events.merge(fetched_events);
///
/// // Iter and print result
/// for event in events.into_iter() {
/// println!("{}", event.as_json());
/// }
/// # Ok(())
/// # }
/// ```
pub async fn fetch_combined_events(
&self,
filters: Vec<Filter>,
timeout: Option<Duration>,
) -> Result<Events, Error> {
// Query database
let stored_events: Events = self.database().query(filters.clone()).await?;

// Query relays
let fetched_events: Events = self.fetch_events(filters, timeout).await?;

// Merge result
Ok(stored_events.merge(fetched_events))
}

/// Stream events
#[deprecated(since = "0.36.0", note = "Use `stream_events` instead")]
pub async fn stream_events_of(
Expand Down

0 comments on commit 497c72f

Please sign in to comment.