Update example

This commit is contained in:
notplants 2022-01-16 08:55:05 -05:00
parent c406bb4870
commit f54e4c47b0
2 changed files with 27 additions and 24 deletions

View File

@ -2,10 +2,10 @@ use async_std::stream::StreamExt;
use futures::pin_mut; use futures::pin_mut;
use std::process; use std::process;
use golgi::sbot::{SubsetQuery, SubsetQueryOptions, FriendsHopsOpts};
use golgi::error::GolgiError; use golgi::error::GolgiError;
use golgi::messages::SsbMessageContent; use golgi::messages::SsbMessageContent;
use golgi::sbot::Sbot; use golgi::sbot::Sbot;
use golgi::sbot::{FriendsHopsOpts, SubsetQuery, SubsetQueryOptions};
async fn run() -> Result<(), GolgiError> { async fn run() -> Result<(), GolgiError> {
let mut sbot_client = Sbot::init(None, None).await?; let mut sbot_client = Sbot::init(None, None).await?;
@ -26,12 +26,14 @@ async fn run() -> Result<(), GolgiError> {
println!("follow_response: {:?}", response); println!("follow_response: {:?}", response);
// print all users you are following // print all users you are following
let follows = sbot_client.friends_hops(FriendsHopsOpts { let follows = sbot_client
max: 1, .friends_hops(FriendsHopsOpts {
start: None, max: 1,
// doesnt seem like reverse does anything, currently start: None,
reverse: Some(false), // doesnt seem like reverse does anything, currently
}).await?; reverse: Some(false),
})
.await?;
println!("follows: {:?}", follows); println!("follows: {:?}", follows);
// print if you are following to_follow (should be true) // print if you are following to_follow (should be true)

View File

@ -21,7 +21,7 @@ use crate::utils;
use crate::utils::get_source_stream; use crate::utils::get_source_stream;
// re-export types from kuska // re-export types from kuska
pub use kuska_ssb::api::dto::content::{SubsetQuery, SubsetQueryOptions, FriendsHopsOpts}; pub use kuska_ssb::api::dto::content::{FriendsHopsOpts, SubsetQuery, SubsetQueryOptions};
/// A struct representing a connection with a running sbot. /// A struct representing a connection with a running sbot.
/// A client and an rpc_reader can together be used to make requests to the sbot /// A client and an rpc_reader can together be used to make requests to the sbot
@ -185,22 +185,27 @@ impl Sbot {
} }
// Convenience method to set a relationship with following: true, blocking: false // Convenience method to set a relationship with following: true, blocking: false
pub async fn follow(&mut self, contact: &str) -> Result<String, GolgiError> { pub async fn follow(&mut self, contact: &str) -> Result<String, GolgiError> {
self.set_relationship(contact, true, false).await self.set_relationship(contact, true, false).await
} }
// Convenience method to set a relationship with following: false, blocking: true // Convenience method to set a relationship with following: false, blocking: true
pub async fn block(&mut self, contact: &str) -> Result<String, GolgiError> { pub async fn block(&mut self, contact: &str) -> Result<String, GolgiError> {
self.set_relationship(contact, false, true).await self.set_relationship(contact, false, true).await
} }
/// Publishes a contact relationship to the given user (with ssb_id) with the given state. /// Publishes a contact relationship to the given user (with ssb_id) with the given state.
pub async fn set_relationship(&mut self, contact: &str, following: bool, blocking: bool) -> Result<String, GolgiError> { pub async fn set_relationship(
&mut self,
contact: &str,
following: bool,
blocking: bool,
) -> Result<String, GolgiError> {
let msg = SsbMessageContent::Contact { let msg = SsbMessageContent::Contact {
contact: Some(contact.to_string()), contact: Some(contact.to_string()),
following: Some(following), following: Some(following),
blocking: Some(blocking), blocking: Some(blocking),
autofollow: None autofollow: None,
}; };
self.publish(msg).await self.publish(msg).await
} }
@ -267,7 +272,7 @@ impl Sbot {
req_id, req_id,
utils::string_res_parse, utils::string_res_parse,
) )
.await .await
} }
// Gets a Vec<String> where each element is a peer you are following // Gets a Vec<String> where each element is a peer you are following
@ -276,7 +281,8 @@ impl Sbot {
max: 1, max: 1,
start: None, start: None,
reverse: Some(false), reverse: Some(false),
}).await })
.await
} }
// Gets a Vec<String> where each element is a peer who follows you // Gets a Vec<String> where each element is a peer who follows you
@ -288,7 +294,8 @@ impl Sbot {
max: 1, max: 1,
start: None, start: None,
reverse: Some(true), reverse: Some(true),
}).await })
.await
} }
/// Call the `friends hops` RPC method and return a Vector<String> /// Call the `friends hops` RPC method and return a Vector<String>
@ -296,21 +303,15 @@ impl Sbot {
/// ///
/// When opts.reverse = True, it should return peers who are following you /// When opts.reverse = True, it should return peers who are following you
/// (but this is currently not working) /// (but this is currently not working)
pub async fn friends_hops( pub async fn friends_hops(&mut self, opts: FriendsHopsOpts) -> Result<Vec<String>, GolgiError> {
&mut self,
opts: FriendsHopsOpts,
) -> Result<Vec<String>, GolgiError> {
let mut sbot_connection = self.get_sbot_connection().await?; let mut sbot_connection = self.get_sbot_connection().await?;
let req_id = sbot_connection let req_id = sbot_connection.client.friends_hops_req_send(opts).await?;
.client
.friends_hops_req_send(opts)
.await?;
utils::get_source_until_eof( utils::get_source_until_eof(
&mut sbot_connection.rpc_reader, &mut sbot_connection.rpc_reader,
req_id, req_id,
utils::string_res_parse, utils::string_res_parse,
) )
.await .await
} }
/// Wrapper for publish which constructs and publishes a post message appropriately from a string. /// Wrapper for publish which constructs and publishes a post message appropriately from a string.