golgi/examples/ssb-stream-example.rs

93 lines
2.9 KiB
Rust
Raw Normal View History

2022-01-04 19:09:49 +00:00
use std::process;
use golgi::error::GolgiError;
use golgi::sbot::Sbot;
2022-01-04 19:58:08 +00:00
use async_std::stream::{Stream, StreamExt};
use futures::{pin_mut, TryStreamExt};
use golgi::messages::{SsbMessageContentType, SsbMessageValue};
2022-01-04 19:09:49 +00:00
async fn run() -> Result<(), GolgiError> {
let mut sbot_client = Sbot::init(None, None).await?;
let id = sbot_client.whoami().await?;
println!("{}", id);
let author = "@L/z54cbc8V1kL1/MiBhpEKuN3QJkSoZYNaukny3ghIs=.ed25519";
2022-01-04 19:58:08 +00:00
// create a history stream
2022-01-04 19:09:49 +00:00
let mut history_stream = sbot_client.create_history_stream(author.to_string()).await?;
2022-01-04 19:58:08 +00:00
// loop through the results until the end of the stream
pin_mut!(history_stream); // needed for iteration
println!("looping through stream");
2022-01-04 19:09:49 +00:00
while let Some(res) = history_stream.next().await {
match res {
Ok(value) => {
println!("value: {:?}", value);
},
Err(err) => {
println!("err: {:?}", err);
}
}
2022-01-04 19:58:08 +00:00
}
println!("reached end of stream");
2022-01-04 19:09:49 +00:00
2022-01-04 20:00:05 +00:00
// // create a history stream and convert it into a Vec<SsbMessageValue> using try_collect
// // (if there is any error in the results, it will be raised)
// let mut history_stream = sbot_client.create_history_stream(author.to_string()).await?;
// let results : Vec<SsbMessageValue> = history_stream.try_collect().await?;
// for x in results {
// println!("x: {:?}", x);
// }
//
// // example to create a history stream and use a map to convert stream of SsbMessageValue
// // into a stream of KeyTypeTuple (local struct for storing message_key and message_type)
// #[derive(Debug)]
// struct KeyTypeTuple {
// message_key: String,
// message_type: SsbMessageContentType,
// };
// let mut history_stream = sbot_client.create_history_stream(author.to_string()).await?;
// let type_stream = history_stream.map(|msg| {
// match msg {
// Ok(val) => {
// let message_type = val.get_message_type()?;
// let tuple = KeyTypeTuple {
// message_key: val.signature,
// message_type: message_type,
// };
// Ok(tuple)
// }
// Err(err) => {
// Err(err)
// }
// }
// });
// pin_mut!(type_stream); // needed for iteration
// println!("looping through type stream");
// while let Some(res) = type_stream.next().await {
// match res {
// Ok(value) => {
// println!("value: {:?}", value);
// },
// Err(err) => {
// println!("err: {:?}", err);
// }
// }
// }
// println!("reached end of type stream");
//
// // return Ok
// Ok(())
2022-01-04 19:09:49 +00:00
}
#[async_std::main]
async fn main() {
if let Err(e) = run().await {
eprintln!("Application error: {}", e);
process::exit(1);
}
}