diff --git a/examples/toy-chat/src/main.rs b/examples/toy-chat/src/main.rs index 4547765..5684cf7 100644 --- a/examples/toy-chat/src/main.rs +++ b/examples/toy-chat/src/main.rs @@ -138,7 +138,7 @@ impl App { let one_day_in_secs = 60 * 60 * 24; let time_start = (Duration::from_secs(Utc::now().timestamp() as u64) - Duration::from_secs(one_day_in_secs)) - .as_nanos() as usize; + .as_nanos() as u64; let include_data = true; @@ -147,6 +147,7 @@ impl App { STORE_NODE, include_data, Some(time_start), + None, None).await.unwrap(); let messages:Vec<_> = messages .iter() diff --git a/waku-bindings/src/node/mod.rs b/waku-bindings/src/node/mod.rs index 1ca219c..d6c8cbc 100644 --- a/waku-bindings/src/node/mod.rs +++ b/waku-bindings/src/node/mod.rs @@ -16,8 +16,7 @@ pub use multiaddr::Multiaddr; pub use secp256k1::{PublicKey, SecretKey}; use std::marker::PhantomData; use std::time::Duration; -use store::StoreWakuMessageResponse; -use uuid::Uuid; +use store::{StoreQueryRequest, StoreWakuMessageResponse}; // internal use crate::general::contenttopic::{Encoding, WakuContentTopic}; use crate::general::libwaku_response::LibwakuResponse; @@ -177,31 +176,26 @@ impl WakuNodeHandle { content_topics: Vec, peer_addr: &str, include_data: bool, // is true, resp contains payload, etc. Only msg_hashes otherwise - time_start: Option, // unix time nanoseconds - time_end: Option, // unix time nanoseconds + time_start: Option, // unix time nanoseconds + time_end: Option, // unix time nanoseconds + timeout_millis: Option, ) -> Result> { let mut cursor: Option = None; let mut messages: Vec = Vec::new(); loop { - let request_id = Uuid::new_v4(); - let response = store::waku_store_query( - &self.ctx, - request_id.to_string(), - include_data, - pubsub_topic.clone(), - content_topics.clone(), - time_start, - time_end, - None, // message_hashes - cursor, // pagination_cursor - true, // pagination_forward - Some(25), // pagination_limit, - peer_addr, - None, // timeout_millis - ) - .await?; + let query = StoreQueryRequest::new() + .with_pubsub_topic(pubsub_topic.clone()) + .with_content_topics(content_topics.clone()) + .with_include_data(include_data) + .with_time_start(time_start) + .with_time_end(time_end) + .with_pagination_cursor(cursor) + .with_pagination_forward(true); + + let response = + store::waku_store_query(&self.ctx, query, peer_addr, timeout_millis).await?; messages.extend(response.messages); diff --git a/waku-bindings/src/node/store.rs b/waku-bindings/src/node/store.rs index 51ac707..37c6f3d 100644 --- a/waku-bindings/src/node/store.rs +++ b/waku-bindings/src/node/store.rs @@ -2,8 +2,10 @@ // std use std::ffi::CString; +use uuid::Uuid; // internal use crate::general::libwaku_response::{handle_response, LibwakuResponse}; +use crate::general::time::get_now_in_nanosecs; use crate::general::waku_decode::WakuDecode; use crate::general::{ contenttopic::WakuContentTopic, messagehash::MessageHash, pubsubtopic::PubsubTopic, Result, @@ -24,7 +26,7 @@ pub struct PagingOptions { /// Criteria used to retrieve historical messages #[derive(Clone, Serialize, Debug)] -struct StoreQueryRequest { +pub struct StoreQueryRequest { /// if true, the store-response will include the full message content. If false, /// the store-response will only include a list of message hashes. request_id: String, @@ -33,9 +35,9 @@ struct StoreQueryRequest { pubsub_topic: Option, content_topics: Vec, #[serde(skip_serializing_if = "Option::is_none")] - time_start: Option, + time_start: Option, #[serde(skip_serializing_if = "Option::is_none")] - time_end: Option, + time_end: Option, #[serde(skip_serializing_if = "Option::is_none")] message_hashes: Option>, #[serde(skip_serializing_if = "Option::is_none")] @@ -45,6 +47,63 @@ struct StoreQueryRequest { pagination_limit: Option, } +impl StoreQueryRequest { + pub fn new() -> Self { + StoreQueryRequest { + request_id: Uuid::new_v4().to_string(), + include_data: true, + pubsub_topic: None, + content_topics: Vec::new(), + time_start: Some(get_now_in_nanosecs()), + time_end: Some(get_now_in_nanosecs()), + message_hashes: None, + pagination_cursor: None, + pagination_forward: true, + pagination_limit: Some(25), + } + } + + pub fn with_include_data(mut self, include_data: bool) -> Self { + self.include_data = include_data; + self + } + + pub fn with_pubsub_topic(mut self, pubsub_topic: Option) -> Self { + self.pubsub_topic = pubsub_topic; + self + } + + pub fn with_content_topics(mut self, content_topics: Vec) -> Self { + self.content_topics = content_topics; + self + } + + pub fn with_time_start(mut self, time_start: Option) -> Self { + self.time_start = time_start; + self + } + + pub fn with_time_end(mut self, time_end: Option) -> Self { + self.time_end = time_end; + self + } + + pub fn with_message_hashes(mut self, message_hashes: Vec) -> Self { + self.message_hashes = Some(message_hashes); + self + } + + pub fn with_pagination_cursor(mut self, pagination_cursor: Option) -> Self { + self.pagination_cursor = pagination_cursor; + self + } + + pub fn with_pagination_forward(mut self, pagination_forward: bool) -> Self { + self.pagination_forward = pagination_forward; + self + } +} + #[derive(Clone, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct StoreWakuMessageResponse { @@ -78,32 +137,10 @@ impl WakuDecode for StoreResponse { pub async fn waku_store_query( ctx: &WakuNodeContext, - request_id: String, - include_data: bool, - pubsub_topic: Option, - content_topics: Vec, - time_start: Option, - time_end: Option, - message_hashes: Option>, - pagination_cursor: Option, // Message hash (key) from where to start query (exclusive) - pagination_forward: bool, - pagination_limit: Option, + query: StoreQueryRequest, peer_addr: &str, timeout_millis: Option, ) -> Result { - let query = StoreQueryRequest { - request_id, - include_data, - pubsub_topic, - content_topics, - time_start, - time_end, - message_hashes, - pagination_cursor, - pagination_forward, - pagination_limit, - }; - let json_query = CString::new( serde_json::to_string(&query).expect("StoreQuery should always be able to be serialized"), )