2021-12-03 09:37:22 +00:00
//! Sbot type and associated methods.
2021-12-02 17:48:16 +00:00
use async_std ::net ::TcpStream ;
2021-12-02 17:55:31 +00:00
2021-12-02 17:48:16 +00:00
use kuska_handshake ::async_std ::BoxStream ;
use kuska_sodiumoxide ::crypto ::{ auth , sign ::ed25519 } ;
2021-12-02 17:55:31 +00:00
use kuska_ssb ::{
api ::{
2021-12-03 09:37:22 +00:00
dto ::{
//content::{About, Post},
2021-12-27 17:56:43 +00:00
content ::{ SubsetQuery , TypedMessage } ,
2021-12-03 09:37:22 +00:00
CreateHistoryStreamIn ,
} ,
2021-12-02 17:55:31 +00:00
ApiCaller ,
} ,
discovery , keystore ,
keystore ::OwnedIdentity ,
rpc ::{ RpcReader , RpcWriter } ,
2021-12-27 21:43:44 +00:00
feed ::{ Feed , Message }
2021-12-02 17:48:16 +00:00
} ;
2021-12-28 15:24:35 +00:00
use serde_json ::Value ;
2021-12-02 17:48:16 +00:00
use crate ::error ::GolgiError ;
use crate ::utils ;
2021-12-03 09:37:22 +00:00
/// 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.
2021-12-02 17:48:16 +00:00
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 < TcpStream > ,
rpc_reader : RpcReader < TcpStream > ,
}
impl Sbot {
2021-12-03 09:37:22 +00:00
/// 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.
2021-12-02 17:48:16 +00:00
pub async fn init ( ip_port : Option < String > , net_id : Option < String > ) -> Result < Sbot , GolgiError > {
2021-12-22 19:47:41 +00:00
let address = if ip_port . is_none ( ) {
" 127.0.0.1:8008 " . to_string ( )
2021-12-02 17:48:16 +00:00
} else {
2021-12-22 19:47:41 +00:00
ip_port . unwrap ( )
} ;
2021-12-02 17:48:16 +00:00
2021-12-22 19:47:41 +00:00
let network_id = if net_id . is_none ( ) {
discovery ::ssb_net_id ( )
2021-12-02 17:48:16 +00:00
} else {
2021-12-22 19:47:41 +00:00
auth ::Key ::from_slice ( & hex ::decode ( net_id . unwrap ( ) ) . unwrap ( ) ) . unwrap ( )
} ;
2021-12-02 17:48:16 +00:00
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 ) ) ;
Ok ( Self {
id ,
public_key : pk ,
private_key : sk ,
address ,
network_id ,
client ,
rpc_reader ,
} )
}
2021-12-27 17:56:43 +00:00
/// 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 getsubset ( & mut self , query : SubsetQuery ) -> Result < String , GolgiError > {
let req_id = self . client . getsubset_req_send ( query ) . await ? ;
2021-12-27 21:43:44 +00:00
println! ( " getting subbset! " ) ;
2021-12-27 17:56:43 +00:00
utils ::get_async ( & mut self . rpc_reader , req_id , utils ::getsubset_res_parse ) . await
}
2021-12-03 09:37:22 +00:00
/// Call the `whoami` RPC method and return an `id`.
2021-12-02 17:48:16 +00:00
pub async fn whoami ( & mut self ) -> Result < String , GolgiError > {
let req_id = self . client . whoami_req_send ( ) . await ? ;
utils ::get_async ( & mut self . rpc_reader , req_id , utils ::whoami_res_parse )
. await
. map ( | whoami | whoami . id )
}
2021-12-27 21:43:44 +00:00
/// Call the `createLogStream` RPC method and return a Vec<Message>
pub async fn log ( & mut self ) -> Result < String , GolgiError > {
let req_id = self . client . log_req_send ( ) . await ? ;
utils ::get_async ( & mut self . rpc_reader , req_id , utils ::getsubset_res_parse )
. await
}
2021-12-03 09:37:22 +00:00
/// Call the `publish` RPC method and return a message reference.
///
/// # Arguments
///
/// * `msg` - A `TypedMessage` `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 : TypedMessage ) -> Result < String , GolgiError > {
let req_id = self . client . publish_req_send ( msg ) . await ? ;
utils ::get_async ( & mut self . rpc_reader , req_id , utils ::publish_res_parse ) . await
}
2021-12-22 19:47:41 +00:00
/// Wrapper for publish which constructs and publishes a post message appropriately from a string.
2021-12-22 19:42:01 +00:00
///
/// # Arguments
///
2021-12-24 15:34:07 +00:00
/// * `text` - A reference to a string slice which represents the text to be published in the post
2021-12-22 19:42:01 +00:00
pub async fn publish_post ( & mut self , text : & str ) -> Result < String , GolgiError > {
2021-12-27 17:56:43 +00:00
let msg = TypedMessage ::Post {
text : text . to_string ( ) ,
mentions : None ,
} ;
2021-12-22 19:42:01 +00:00
self . publish ( msg ) . await
}
2021-12-22 21:19:52 +00:00
/// Wrapper for publish which constructs and publishes an about description message appropriately from a string.
///
/// # Arguments
///
2021-12-24 15:34:07 +00:00
/// * `description` - A reference to a string slice which represents the text to be published as an about description.
2021-12-22 21:19:52 +00:00
pub async fn publish_description ( & mut self , description : & str ) -> Result < String , GolgiError > {
let msg = TypedMessage ::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
}
2021-12-03 09:37:22 +00:00
/*
2021-12-02 17:48:16 +00:00
pub async fn publish_post ( & mut self , post : Post ) -> Result < String , GolgiError > {
let req_id = self . client . publish_req_send ( post ) . await ? ;
utils ::get_async ( & mut self . rpc_reader , req_id , utils ::publish_res_parse ) . await
}
2021-12-03 09:37:22 +00:00
* /
2021-12-02 17:48:16 +00:00
2021-12-03 09:37:22 +00:00
/// Call the `createHistoryStream` RPC method and print the output.
2021-12-28 15:24:35 +00:00
pub async fn create_history_stream ( & mut self , id : String ) -> Result < Vec < Value > , GolgiError > {
2021-12-02 17:48:16 +00:00
let args = CreateHistoryStreamIn ::new ( id ) ;
let req_id = self . client . create_history_stream_req_send ( & args ) . await ? ;
2021-12-27 21:43:44 +00:00
// TODO: message_res_parse is currently throwing "Failed to serializ JSON slice"
// utils::get_async_until_eof(&mut self.rpc_reader, req_id, utils::message_res_parse).await
2021-12-28 15:24:35 +00:00
utils ::get_async_until_eof ( & mut self . rpc_reader , req_id , utils ::json_res_parse ) . await
2021-12-02 17:48:16 +00:00
}
}