From 8a37bfcb619c98e30fd9deca433a87169c0a8022 Mon Sep 17 00:00:00 2001 From: glyph Date: Wed, 16 Feb 2022 14:16:57 +0200 Subject: [PATCH] add subset stream example --- examples/streams.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/examples/streams.rs b/examples/streams.rs index 8d05342..f2c7ffd 100644 --- a/examples/streams.rs +++ b/examples/streams.rs @@ -4,6 +4,7 @@ use async_std::stream::StreamExt; use futures::TryStreamExt; use golgi::{ + api::get_subset::{SubsetQuery, SubsetQueryOptions}, messages::{SsbMessageContentType, SsbMessageValue}, GolgiError, Sbot, }; @@ -25,6 +26,8 @@ async fn run() -> Result<(), GolgiError> { let author = id.clone(); + /* HISTORY STREAM EXAMPLE */ + // Create an ordered stream of all messages authored by the `author` // identity. let history_stream = sbot_client @@ -110,6 +113,37 @@ async fn run() -> Result<(), GolgiError> { println!("reached end of type stream"); + /* SUBSET STREAM EXAMPLE */ + + // Compose a subset query for `post` message types. + let post_query = SubsetQuery::Type { + op: "type".to_string(), + string: "post".to_string(), + }; + + // Define the options for the query. + let post_query_opts = SubsetQueryOptions { + descending: Some(true), + keys: None, + page_limit: Some(5), + }; + + // Return 5 `post` type messages from any author in descending order. + let query_stream = sbot_client + .get_subset_stream(post_query, Some(post_query_opts)) + .await?; + + println!("looping through post query stream"); + + // Iterate over the stream and pretty-print each returned message + // value while ignoring any errors. + query_stream.for_each(|msg| match msg { + Ok(val) => println!("{:#?}", val), + Err(_) => (), + }); + + println!("reached end of post query stream"); + Ok(()) }