2022-11-18 13:10:56 +00:00
|
|
|
use std::process;
|
|
|
|
|
|
|
|
use async_std::stream::StreamExt;
|
|
|
|
|
2022-11-22 07:44:55 +00:00
|
|
|
use golgi::{api::tangles::TanglesThread, sbot::Keystore, GolgiError, Sbot};
|
2022-11-18 13:10:56 +00:00
|
|
|
|
|
|
|
// Golgi is an asynchronous library so we must call it from within an
|
|
|
|
// async function. The `GolgiError` type encapsulates all possible
|
|
|
|
// error variants for the library.
|
|
|
|
async fn run() -> Result<(), GolgiError> {
|
|
|
|
// Attempt to initialise a connection to an sbot instance using the
|
|
|
|
// secret file at the Patchwork path and the default IP address, port
|
|
|
|
// and network key (aka. capabilities key).
|
|
|
|
//let mut sbot_client = Sbot::init(Keystore::Patchwork, None, None).await?;
|
|
|
|
let mut sbot_client =
|
|
|
|
Sbot::init(Keystore::GoSbot, Some("127.0.0.1:8021".to_string()), None).await?;
|
|
|
|
|
|
|
|
// Call the `whoami` RPC method to retrieve the public key for the sbot
|
|
|
|
// identity. This is our 'local' public key.
|
|
|
|
let id = sbot_client.whoami().await?;
|
|
|
|
|
|
|
|
// Print the public key (identity) to `stdout`.
|
|
|
|
println!("whoami: {}", id);
|
|
|
|
|
|
|
|
/* TANGLES THREAD EXAMPLE */
|
|
|
|
|
|
|
|
// Define the message key that will be used to generate the tangle stream.
|
|
|
|
// This must be the key of the root message in the thread (ie. the first
|
|
|
|
// message of the thread).
|
|
|
|
let msg_key = "%vY53ethgEG1tNsKqmLm6isNSbsu+jwMf/UNGMfUiLm0=.sha256".to_string();
|
|
|
|
|
|
|
|
// Instantiate the arguments for the `tangles.thread` RPC by instantiating
|
|
|
|
// a new instant of the `TanglesThread` struct.
|
|
|
|
let args = TanglesThread::new(msg_key).keys_values(true, true);
|
|
|
|
|
|
|
|
// It's also possible to define the maximum number of messages
|
|
|
|
// returned from the stream by setting the `limit` value:
|
|
|
|
// Note: a limit of 3 will return a maximum of 4 messages! The root
|
|
|
|
// message + 3 other messages in the thread.
|
|
|
|
// let args = TanglesThread::new(msg_key).keys_values(true, true).limit(3);
|
|
|
|
|
|
|
|
// Create an ordered stream of all messages comprising the thread
|
|
|
|
// in which the given message resides. Messages are returned as KVTs
|
|
|
|
// (Key Value Timestamp).
|
|
|
|
let thread_stream = sbot_client.tangles_thread(args).await?;
|
|
|
|
|
|
|
|
// Pin the stream to the stack to allow polling of the `future`.
|
|
|
|
futures::pin_mut!(thread_stream);
|
|
|
|
|
|
|
|
println!("looping through stream");
|
|
|
|
|
|
|
|
// Iterate through each element in the stream and match on the `Result`.
|
|
|
|
// In this case, each element has type `Result<SsbMessageKVT, GolgiError>`.
|
|
|
|
while let Some(res) = thread_stream.next().await {
|
|
|
|
match res {
|
|
|
|
Ok(kvt) => {
|
|
|
|
// Print the `SsbMessageKVT` of this element to `stdout`.
|
|
|
|
println!("kvt: {:?}", kvt);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// Print the `GolgiError` of this element to `stderr`.
|
|
|
|
eprintln!("err: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("reached end of stream");
|
|
|
|
|
|
|
|
// Take a look at the `examples/streams.rs` file in this repo for more
|
|
|
|
// ways of handling streams.
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable an async main function and execute the `run()` function,
|
|
|
|
// catching any errors and printing them to `stderr` before exiting the
|
|
|
|
// process.
|
|
|
|
#[async_std::main]
|
|
|
|
async fn main() {
|
|
|
|
if let Err(e) = run().await {
|
|
|
|
eprintln!("Application error: {}", e);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|