//! Sbot type and associated methods. use std::fmt::Debug; use async_std::net::TcpStream; use async_std::stream::{Stream, StreamExt}; use async_stream::stream; use kuska_handshake::async_std::BoxStream; use kuska_sodiumoxide::crypto::{auth, sign::ed25519}; use kuska_ssb::{ api::{ dto::{CreateHistoryStreamIn}, ApiCaller, }, discovery, keystore, keystore::OwnedIdentity, rpc::{RpcReader, RpcWriter, RecvMsg}, }; use crate::error::GolgiError; use crate::messages::{SsbMessageKVT, SsbMessageContent, SsbMessageValue, SsbMessageContentType}; use crate::utils; // re-export types from kuska pub use kuska_ssb::api::dto::content::SubsetQuery; use kuska_ssb::rpc::RequestNo; pub struct SbotConnection { client: ApiCaller, rpc_reader: RpcReader, } /// The Scuttlebutt identity, keys and configuration parameters for connecting to a local sbot /// instance, as well as handles for calling RPC methods and receiving responses. pub struct Sbot { id: String, public_key: ed25519::PublicKey, private_key: ed25519::SecretKey, address: String, // aka caps key (scuttleverse identifier) network_id: auth::Key, client: ApiCaller, rpc_reader: RpcReader, sbot_connections: Vec, } impl Sbot { /// Initiate a connection with an sbot instance. Define the IP address, port and network key /// for the sbot, then retrieve the public key, private key (secret) and identity from the /// `.ssb-go/secret` file. Open a TCP stream to the sbot and perform the secret handshake. If successful, create a box stream and split it into a writer and reader. Return RPC handles to the sbot as part of the `struct` output. pub async fn init(ip_port: Option, net_id: Option) -> Result { let address = if ip_port.is_none() { "127.0.0.1:8008".to_string() } else { ip_port.unwrap() }; let network_id = if net_id.is_none() { discovery::ssb_net_id() } else { auth::Key::from_slice(&hex::decode(net_id.unwrap()).unwrap()).unwrap() }; let OwnedIdentity { pk, sk, id } = keystore::from_gosbot_local() .await .expect("couldn't read local secret"); let socket = TcpStream::connect(&address) .await .map_err(|source| GolgiError::Io { source, context: "socket error; failed to initiate tcp stream connection".to_string(), })?; let handshake = kuska_handshake::async_std::handshake_client( &mut &socket, network_id.clone(), pk, sk.clone(), pk, ) .await .map_err(GolgiError::Handshake)?; let (box_stream_read, box_stream_write) = BoxStream::from_handshake(socket.clone(), socket, handshake, 0x8000).split_read_write(); let rpc_reader = RpcReader::new(box_stream_read); let client = ApiCaller::new(RpcWriter::new(box_stream_write)); let mut sbot_connections = Vec::new(); Ok(Self { id, public_key: pk, private_key: sk, address, network_id, client, rpc_reader, sbot_connections }) } pub async fn get_sbot_connection(&self, ip_port: Option, net_id: Option) -> Result { let address = if ip_port.is_none() { "127.0.0.1:8008".to_string() } else { ip_port.unwrap() }; let network_id = if net_id.is_none() { discovery::ssb_net_id() } else { auth::Key::from_slice(&hex::decode(net_id.unwrap()).unwrap()).unwrap() }; let socket = TcpStream::connect(&address) .await .map_err(|source| GolgiError::Io { source, context: "socket error; failed to initiate tcp stream connection".to_string(), })?; let handshake = kuska_handshake::async_std::handshake_client( &mut &socket, network_id.clone(), self.public_key, self.private_key.clone(), self.public_key, ) .await .map_err(GolgiError::Handshake)?; let (box_stream_read, box_stream_write) = BoxStream::from_handshake(socket.clone(), socket, handshake, 0x8000).split_read_write(); let rpc_reader = RpcReader::new(box_stream_read); let client = ApiCaller::new(RpcWriter::new(box_stream_write)); let sbot_connection = SbotConnection { rpc_reader, client, }; Ok(sbot_connection) } /// Call the `partialReplication getSubset` RPC method and return a vector /// of messages as KVTs (key, value, timestamp). // TODO: add args for `descending` and `page` (max number of msgs in response) pub async fn get_subset(&mut self, query: SubsetQuery) -> Result, GolgiError> { let req_id = self.client.getsubset_req_send(query).await?; utils::get_source_until_eof(&mut self.rpc_reader, req_id, utils::kvt_res_parse).await } /// Call the `whoami` RPC method and return an `id`. pub async fn whoami(&mut self) -> Result { let req_id = self.client.whoami_req_send().await?; utils::get_async(&mut self.rpc_reader, req_id, utils::string_res_parse).await } /// Call the `publish` RPC method and return a message reference. /// /// # Arguments /// /// * `msg` - A `SsbMessageContent` `enum` whose variants include `Pub`, `Post`, `Contact`, `About`, /// `Channel` and `Vote`. See the `kuska_ssb` documentation for further details such as field /// names and accepted values for each variant. pub async fn publish(&mut self, msg: SsbMessageContent) -> Result { let req_id = self.client.publish_req_send(msg).await?; utils::get_async(&mut self.rpc_reader, req_id, utils::string_res_parse).await } /// Wrapper for publish which constructs and publishes a post message appropriately from a string. /// /// # Arguments /// /// * `text` - A reference to a string slice which represents the text to be published in the post pub async fn publish_post(&mut self, text: &str) -> Result { let msg = SsbMessageContent::Post { text: text.to_string(), mentions: None, }; self.publish(msg).await } /// Wrapper for publish which constructs and publishes an about description message appropriately from a string. /// /// # Arguments /// /// * `description` - A reference to a string slice which represents the text to be published as an about description. pub async fn publish_description(&mut self, description: &str) -> Result { let msg = SsbMessageContent::About { about: self.id.to_string(), name: None, title: None, branch: None, image: None, description: Some(description.to_string()), location: None, start_datetime: None, }; self.publish(msg).await } /// Wrapper for publish which constructs and publishes an about name message appropriately from a string. /// /// # Arguments /// /// * `name` - A reference to a string slice which represents the text to be published as an about name. pub async fn publish_name(&mut self, name: &str) -> Result { let msg = SsbMessageContent::About { about: self.id.to_string(), name: Some(name.to_string()), title: None, branch: None, image: None, description: None, location: None, start_datetime: None, }; self.publish(msg).await } /// Get the about messages for a particular user in order of recency. pub async fn get_about_messages(&mut self, ssb_id: &str) -> Result, GolgiError> { let query = SubsetQuery::Author{ op: "author".to_string(), feed: ssb_id.to_string(), }; let kvts: Vec = self.get_subset(query).await?; let messages: Vec = kvts.into_iter().map(|kvt| kvt.value).collect(); // TODO: after fixing sbot regression, // change this subset query to filter by type about in addition to author // and remove this filter section // filter down to about messages let mut about_messages: Vec = messages.into_iter().filter(|msg| { msg.is_message_type(SsbMessageContentType::About) }).collect(); // TODO: use subset query to order messages instead of doing it this way about_messages.sort_by(|a, b| { b.timestamp.partial_cmp(&a.timestamp).unwrap() }); // return about messages Ok(about_messages) } /// Get value of latest about message with given key from given user pub async fn get_latest_about_message(&mut self, ssb_id: &str, key: &str) -> Result, GolgiError> { // vector of about messages with most recent at the front of the vector let about_messages = self.get_about_messages(ssb_id).await?; // iterate through the vector looking for most recent about message with the given key let latest_about = about_messages .iter() // find the first msg that contains the field `key` .find(|msg| msg.content.get(key).is_some()) // map the found msg (`Some(SsbMessageValue)`) to a `Some(Some(&Value))` .map(|msg| msg.content.get(key)) // flatten `Some(Some(&Value))` into `Some(&Value)` .flatten() // map `Some(&Value)` to `Some(Some(&str))` .map(|msg_val| msg_val.as_str()) // flatten `Some(Some(&str))` to `Some(&str)` .flatten() // map `Some(&str))` to `Some(String)` .map(|msg_str| msg_str.to_string()); // return value is either `Ok(Some(String))` or `Ok(None)` Ok(latest_about) } pub async fn get_name(&mut self, ssb_id: &str) -> Result, GolgiError> { self.get_latest_about_message(ssb_id, "name").await } pub async fn get_description(&mut self, ssb_id: &str) -> Result, GolgiError> { self.get_latest_about_message(ssb_id, "description").await } /// Call the `createHistoryStream` RPC method /// and return a Stream of Result pub async fn create_history_stream( &mut self, id: String, ) -> Result>, GolgiError> { let mut sbot_connection = self.get_sbot_connection(None, None).await.unwrap(); let args = CreateHistoryStreamIn::new(id); let req_id = sbot_connection.client.create_history_stream_req_send(&args).await?; let history_stream = Sbot::get_source_stream(sbot_connection.rpc_reader, req_id, utils::ssb_message_res_parse).await; Ok(history_stream) } /// Takes in an rpc request number, and a handling function (parsing results of type T), /// and produces an async_std::stream::Stream /// of results of type T where the handling functions is called /// on all rpc_reader responses which match the request number /// /// # Arguments /// /// * `req_no` - A `RequestNo` of the response to listen for /// * `f` - A function which takes in an array of u8 and returns a Result. /// This is a function which parses the response from the RpcReader. T is a generic type, /// so this parse function can return multiple possible types (String, json, custom struct etc.) pub async fn get_source_stream<'a, F, T>(mut rpc_reader: RpcReader, req_no: RequestNo, f: F) -> impl Stream> where F: Fn(&[u8]) -> Result, T: Debug + serde::Deserialize<'a>, { // we use the async_stream::stream macro to allow for creating a stream which calls async functions // see https://users.rust-lang.org/t/how-to-create-async-std-stream-which-calls-async-function-in-poll-next/69760 let source_stream = stream! { loop { // get the next message from the rpc_reader let (id, msg) = rpc_reader.recv().await?; let x : i32 = id.clone(); // check if the next message from rpc_reader matches the req_no we are looking for // if it matches, then this rpc response is for the given request // and if it doesn't match, then we ignore it if x == req_no { match msg { RecvMsg::RpcResponse(_type, body) => { // parse an item of type T from the message body using the provided // function for parsing let item = f(&body)?; // return Ok(item) as the next value in the stream yield Ok(item) } RecvMsg::ErrorResponse(message) => { // if an error is received // return an Err(err) as the next value in the stream yield Err(GolgiError::Sbot(message.to_string())); } // if we find a CancelStreamResponse // this is the end of the stream RecvMsg::CancelStreamRespose() => break, // if we find an unknown response, we just continue the loop _ => {} } } } }; // finally return the stream object source_stream } }