1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Perform subset queries.
//!
//! Implements the following methods:
//!
//! - [`Sbot::get_subset_stream`]

use async_std::stream::Stream;

use crate::{error::GolgiError, messages::SsbMessageValue, sbot::Sbot, utils};

// re-export subset-related kuska types
pub use kuska_ssb::api::dto::content::{SubsetQuery, SubsetQueryOptions};

impl Sbot {
    /// Make a subset query, as defined by the [Subset replication for SSB specification](https://github.com/ssb-ngi-pointer/ssb-subset-replication-spec).
    ///
    /// Calls the `partialReplication. getSubset` RPC method.
    ///
    /// # Arguments
    ///
    /// * `query` - A `SubsetQuery` which specifies the desired query filters.
    /// * `options` - An Option<`SubsetQueryOptions`> which, if provided, adds
    /// additional specifications to the query, such as page limit and/or
    /// descending results.
    ///
    /// # Example
    ///
    /// ```rust
    /// use async_std::stream::StreamExt;
    /// use golgi::{
    ///     Sbot,
    ///     GolgiError,
    ///     api::get_subset::{
    ///         SubsetQuery,
    ///         SubsetQueryOptions
    ///     }
    /// };
    ///
    /// async fn query() -> Result<(), GolgiError> {
    ///     let mut sbot_client = Sbot::init(None, None).await?;
    ///
    ///     let post_query = SubsetQuery::Type {
    ///         op: "type".to_string(),
    ///         string: "post".to_string()
    ///     };
    ///
    ///     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?;
    ///
    ///     // 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(_) => (),
    ///     });
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_subset_stream(
        &mut self,
        query: SubsetQuery,
        options: Option<SubsetQueryOptions>,
    ) -> Result<impl Stream<Item = Result<SsbMessageValue, GolgiError>>, GolgiError> {
        let mut sbot_connection = self.get_sbot_connection().await?;
        let req_id = sbot_connection
            .client
            .getsubset_req_send(query, options)
            .await?;
        let get_subset_stream = utils::get_source_stream(
            sbot_connection.rpc_reader,
            req_id,
            utils::ssb_message_res_parse,
        )
        .await;
        Ok(get_subset_stream)
    }
}