From 497c72f5a255c3d0cdf2a837e85c24be3d162fc0 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Fri, 18 Oct 2024 16:06:16 +0200 Subject: [PATCH] sdk: temp add `Client::fetch_combined_events` Signed-off-by: Yuki Kishimoto --- bindings/nostr-sdk-ffi/src/client/mod.rs | 28 +++++++++++++ bindings/nostr-sdk-js/src/client/mod.rs | 28 +++++++++++++ crates/nostr-sdk/src/client/mod.rs | 53 ++++++++++++++++++++++++ 3 files changed, 109 insertions(+) diff --git a/bindings/nostr-sdk-ffi/src/client/mod.rs b/bindings/nostr-sdk-ffi/src/client/mod.rs index 2cc333ea7..935e42cb7 100644 --- a/bindings/nostr-sdk-ffi/src/client/mod.rs +++ b/bindings/nostr-sdk-ffi/src/client/mod.rs @@ -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>, + timeout: Option, + ) -> Result { + 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, msg: Arc) -> Result { Ok(self .inner diff --git a/bindings/nostr-sdk-js/src/client/mod.rs b/bindings/nostr-sdk-js/src/client/mod.rs index b63e87cf1..1f24e0fa3 100644 --- a/bindings/nostr-sdk-js/src/client/mod.rs +++ b/bindings/nostr-sdk-js/src/client/mod.rs @@ -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, + timeout: Option, + ) -> Result { + let filters: Vec = filters.into_iter().map(|f| f.into()).collect(); + let timeout: Option = 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, msg: &JsClientMessage) -> Result { diff --git a/crates/nostr-sdk/src/client/mod.rs b/crates/nostr-sdk/src/client/mod.rs index 63df39ea8..9cdca61c1 100644 --- a/crates/nostr-sdk/src/client/mod.rs +++ b/crates/nostr-sdk/src/client/mod.rs @@ -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, + timeout: Option, + ) -> Result { + // 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(