From 9027ebfe84b361931bca46fe23f47e6a8363d686 Mon Sep 17 00:00:00 2001 From: notplants Date: Fri, 14 Jan 2022 12:37:34 -0500 Subject: [PATCH 01/10] Create new sbot connection for each query --- examples/ssb-client.rs | 34 +++++++++++++++++++++------------- src/sbot.rs | 23 ++++++++--------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/examples/ssb-client.rs b/examples/ssb-client.rs index 10fea55..f9f0ac9 100644 --- a/examples/ssb-client.rs +++ b/examples/ssb-client.rs @@ -10,19 +10,19 @@ async fn run() -> Result<(), GolgiError> { let id = sbot_client.whoami().await?; println!("whoami: {}", id); - let name = SsbMessageContent::About { - about: id.clone(), - name: Some("golgi".to_string()), - title: None, - branch: None, - image: None, - description: None, - location: None, - start_datetime: None, - }; - - let name_msg_ref = sbot_client.publish(name).await?; - println!("name_msg_ref: {}", name_msg_ref); + // let name = SsbMessageContent::About { + // about: id.clone(), + // name: Some("golgi".to_string()), + // title: None, + // branch: None, + // image: None, + // description: None, + // location: None, + // start_datetime: None, + // }; + // + // let name_msg_ref = sbot_client.publish(name).await?; + // println!("name_msg_ref: {}", name_msg_ref); let post = SsbMessageContent::Post { text: "golgi go womp womp".to_string(), @@ -42,6 +42,14 @@ async fn run() -> Result<(), GolgiError> { let description = sbot_client.get_description(&author).await?; println!("found description: {:?}", description); + let post = SsbMessageContent::Post { + text: "golgi go womp womp2".to_string(), + mentions: None, + }; + + let post_msg_ref = sbot_client.publish(post).await?; + println!("post_msg_ref2: {}", post_msg_ref); + Ok(()) } diff --git a/src/sbot.rs b/src/sbot.rs index decc427..5d05c25 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -33,17 +33,13 @@ pub struct SbotConnection { } /// 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 { pub id: String, public_key: ed25519::PublicKey, private_key: ed25519::SecretKey, address: String, // aka caps key (scuttleverse identifier) - network_id: auth::Key, - // the primary connection with sbot which can be re-used for non-stream calls - // note that stream calls will each need their own SbotConnection - sbot_connection: SbotConnection, + network_id: auth::Key } impl Sbot { @@ -67,17 +63,12 @@ impl Sbot { .await .expect("couldn't read local secret"); - let sbot_connection = - Sbot::_get_sbot_connection_helper(address.clone(), network_id.clone(), pk, sk.clone()) - .await?; - Ok(Self { id, public_key: pk, private_key: sk, address, - network_id, - sbot_connection, + network_id }) } @@ -152,10 +143,11 @@ impl Sbot { /// Call the `whoami` RPC method and return an `id`. pub async fn whoami(&mut self) -> Result { - let req_id = self.sbot_connection.client.whoami_req_send().await?; + let mut sbot_connection = self.get_sbot_connection().await?; + let req_id = sbot_connection.client.whoami_req_send().await?; let result = utils::get_async( - &mut self.sbot_connection.rpc_reader, + &mut sbot_connection.rpc_reader, req_id, utils::json_res_parse, ) @@ -177,10 +169,11 @@ impl Sbot { /// `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.sbot_connection.client.publish_req_send(msg).await?; + let mut sbot_connection = self.get_sbot_connection().await?; + let req_id = sbot_connection.client.publish_req_send(msg).await?; utils::get_async( - &mut self.sbot_connection.rpc_reader, + &mut sbot_connection.rpc_reader, req_id, utils::string_res_parse, ) From 6fd27827a00ccddf171c02256d93550fa5636747 Mon Sep 17 00:00:00 2001 From: notplants Date: Fri, 14 Jan 2022 12:41:15 -0500 Subject: [PATCH 02/10] Fix example --- examples/ssb-client.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/ssb-client.rs b/examples/ssb-client.rs index f9f0ac9..9e0b4ad 100644 --- a/examples/ssb-client.rs +++ b/examples/ssb-client.rs @@ -10,19 +10,19 @@ async fn run() -> Result<(), GolgiError> { let id = sbot_client.whoami().await?; println!("whoami: {}", id); - // let name = SsbMessageContent::About { - // about: id.clone(), - // name: Some("golgi".to_string()), - // title: None, - // branch: None, - // image: None, - // description: None, - // location: None, - // start_datetime: None, - // }; - // - // let name_msg_ref = sbot_client.publish(name).await?; - // println!("name_msg_ref: {}", name_msg_ref); + let name = SsbMessageContent::About { + about: id.clone(), + name: Some("golgi".to_string()), + title: None, + branch: None, + image: None, + description: None, + location: None, + start_datetime: None, + }; + + let name_msg_ref = sbot_client.publish(name).await?; + println!("name_msg_ref: {}", name_msg_ref); let post = SsbMessageContent::Post { text: "golgi go womp womp".to_string(), From bacd954fa0fc4af80f10ed83f147f05e148d7f66 Mon Sep 17 00:00:00 2001 From: notplants Date: Fri, 14 Jan 2022 16:24:11 -0500 Subject: [PATCH 03/10] Working on friends methods --- src/sbot.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/sbot.rs b/src/sbot.rs index 5d05c25..c7f72be 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -180,6 +180,32 @@ impl Sbot { .await } + /// Call the `friends follow` RPC method and return a message reference. + pub async fn friends_follow(&mut self, ssb_id: &str) -> Result { + let mut sbot_connection = self.get_sbot_connection().await?; + let req_id = sbot_connection.client.friends_follow_req_send(ssb_id).await?; + + utils::get_async( + &mut sbot_connection.rpc_reader, + req_id, + utils::string_res_parse, + ) + .await + } + + /// Call the `friends isfollowing` RPC method and return a message reference. + /// returns true if src_id is following dest_id and false otherwise + pub async fn friends_is_following(&mut self, src_id: &str, dest_id: &str) -> Result { + let mut sbot_connection = self.get_sbot_connection().await?; + let req_id = sbot_connection.client.friends_isfollowing_req_send(src_id, dest_id).await?; + + utils::get_async( + &mut sbot_connection.rpc_reader, + req_id, + utils::string_res_parse, + ).await + } + /// Wrapper for publish which constructs and publishes a post message appropriately from a string. /// /// # Arguments From a72fbc9c08808d979f5e3763e3544e9e30e054f7 Mon Sep 17 00:00:00 2001 From: notplants Date: Sat, 15 Jan 2022 08:44:43 -0500 Subject: [PATCH 04/10] Add git hook --- git_hooks/pre-commit | 1 + src/sbot.rs | 105 ++++++++++++++++++++++++++----------------- 2 files changed, 66 insertions(+), 40 deletions(-) create mode 100755 git_hooks/pre-commit diff --git a/git_hooks/pre-commit b/git_hooks/pre-commit new file mode 100755 index 0000000..f3fb918 --- /dev/null +++ b/git_hooks/pre-commit @@ -0,0 +1 @@ +cargo fmt diff --git a/src/sbot.rs b/src/sbot.rs index c7f72be..f1fcfca 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -1,10 +1,10 @@ //! Sbot type and associated methods. -use std::collections::HashMap; use async_std::{ net::TcpStream, stream::{Stream, StreamExt}, }; use futures::pin_mut; +use std::collections::HashMap; use kuska_handshake::async_std::BoxStream; use kuska_sodiumoxide::crypto::{auth, sign::ed25519}; @@ -39,7 +39,7 @@ pub struct Sbot { private_key: ed25519::SecretKey, address: String, // aka caps key (scuttleverse identifier) - network_id: auth::Key + network_id: auth::Key, } impl Sbot { @@ -68,7 +68,7 @@ impl Sbot { public_key: pk, private_key: sk, address, - network_id + network_id, }) } @@ -136,8 +136,12 @@ impl Sbot { .client .getsubset_req_send(query, options) .await?; - let get_subset_stream = - get_source_stream(sbot_connection.rpc_reader, req_id, utils::ssb_message_res_parse).await; + let get_subset_stream = get_source_stream( + sbot_connection.rpc_reader, + req_id, + utils::ssb_message_res_parse, + ) + .await; Ok(get_subset_stream) } @@ -181,29 +185,45 @@ impl Sbot { } /// Call the `friends follow` RPC method and return a message reference. - pub async fn friends_follow(&mut self, ssb_id: &str) -> Result { + /// if state is true, then sets the following status to true + /// if state is false, then sets the following status to false (unfollow) + pub async fn friends_follow( + &mut self, + ssb_id: &str, + state: bool, + ) -> Result { let mut sbot_connection = self.get_sbot_connection().await?; - let req_id = sbot_connection.client.friends_follow_req_send(ssb_id).await?; + let req_id = sbot_connection + .client + .friends_follow_req_send(ssb_id, state) + .await?; + utils::get_async( + &mut sbot_connection.rpc_reader, + req_id, + utils::string_res_parse, + ) + .await + } + + /// Call the `friends isfollowing` RPC method and return a message reference. + /// returns true if src_id is following dest_id and false otherwise + pub async fn friends_is_following( + &mut self, + src_id: &str, + dest_id: &str, + ) -> Result { + let mut sbot_connection = self.get_sbot_connection().await?; + let req_id = sbot_connection + .client + .friends_isfollowing_req_send(src_id, dest_id) + .await?; utils::get_async( &mut sbot_connection.rpc_reader, req_id, utils::string_res_parse, ) - .await - } - - /// Call the `friends isfollowing` RPC method and return a message reference. - /// returns true if src_id is following dest_id and false otherwise - pub async fn friends_is_following(&mut self, src_id: &str, dest_id: &str) -> Result { - let mut sbot_connection = self.get_sbot_connection().await?; - let req_id = sbot_connection.client.friends_isfollowing_req_send(src_id, dest_id).await?; - - utils::get_async( - &mut sbot_connection.rpc_reader, - req_id, - utils::string_res_parse, - ).await + .await } /// Wrapper for publish which constructs and publishes a post message appropriately from a string. @@ -278,9 +298,7 @@ impl Sbot { // and remove this filter section // filter down to about messages let about_message_stream = get_subset_stream.filter(|msg| match msg { - Ok(val) => { - val.is_message_type(SsbMessageContentType::About) - } + Ok(val) => val.is_message_type(SsbMessageContentType::About), Err(_err) => false, }); // return about message stream @@ -298,13 +316,14 @@ impl Sbot { // now we have a stream of about messages with most recent at the front of the vector pin_mut!(about_message_stream); // iterate through the vector looking for most recent about message with the given key - let latest_about_message_res: Option> = about_message_stream - // find the first msg that contains the field `key` - .find(|res| match res { - Ok(msg) => msg.content.get(key).is_some(), - Err(_) => false, - }) - .await; + let latest_about_message_res: Option> = + about_message_stream + // find the first msg that contains the field `key` + .find(|res| match res { + Ok(msg) => msg.content.get(key).is_some(), + Err(_) => false, + }) + .await; // Option> -> Option let latest_about_message = latest_about_message_res.and_then(|msg| msg.ok()); // Option -> Option @@ -323,14 +342,20 @@ impl Sbot { } /// Get HashMap of profile info for given user - pub async fn get_profile_info(&mut self, ssb_id: &str) -> Result, GolgiError> { + pub async fn get_profile_info( + &mut self, + ssb_id: &str, + ) -> Result, GolgiError> { let mut keys_to_search_for = vec!["name", "description", "image"]; self.get_about_info(ssb_id, keys_to_search_for).await } /// Get HashMap of name and image for given user /// (this is can be used to display profile images of a list of users) - pub async fn get_name_and_image(&mut self, ssb_id: &str) -> Result, GolgiError> { + pub async fn get_name_and_image( + &mut self, + ssb_id: &str, + ) -> Result, GolgiError> { let mut keys_to_search_for = vec!["name", "image"]; self.get_about_info(ssb_id, keys_to_search_for).await } @@ -349,7 +374,7 @@ impl Sbot { pub async fn get_about_info( &mut self, ssb_id: &str, - mut keys_to_search_for: Vec<&str> + mut keys_to_search_for: Vec<&str>, ) -> Result, GolgiError> { // get about_message_stream let about_message_stream = self.get_about_message_stream(ssb_id).await?; @@ -361,7 +386,7 @@ impl Sbot { while let Some(res) = about_message_stream.next().await { // if there are no more keys we are looking for, then we are done if keys_to_search_for.len() == 0 { - break + break; } // if there are still keys we are looking for, then continue searching match res { @@ -369,7 +394,9 @@ impl Sbot { // for each key we are searching for, check if this about // message contains a value for that key for key in &keys_to_search_for.clone() { - let option_val = msg.content.get(key) + let option_val = msg + .content + .get(key) .and_then(|val| val.as_str()) .map(|val| val.to_string()); match option_val { @@ -379,15 +406,13 @@ impl Sbot { // remove this key fom keys_to_search_for, since we are no longer searching for it keys_to_search_for.retain(|val| val != key) } - None => { - continue - } + None => continue, } } } Err(err) => { // skip errors - continue + continue; } } } From c406bb48709846cad7da85295d1efe28d510b9cd Mon Sep 17 00:00:00 2001 From: notplants Date: Sat, 15 Jan 2022 10:36:52 -0500 Subject: [PATCH 05/10] Working friends methods --- examples/ssb-friends.rs | 62 ++++++++++++++++++++++++++++ src/sbot.rs | 89 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 examples/ssb-friends.rs diff --git a/examples/ssb-friends.rs b/examples/ssb-friends.rs new file mode 100644 index 0000000..0b65a78 --- /dev/null +++ b/examples/ssb-friends.rs @@ -0,0 +1,62 @@ +use async_std::stream::StreamExt; +use futures::pin_mut; +use std::process; + +use golgi::sbot::{SubsetQuery, SubsetQueryOptions, FriendsHopsOpts}; +use golgi::error::GolgiError; +use golgi::messages::SsbMessageContent; +use golgi::sbot::Sbot; + +async fn run() -> Result<(), GolgiError> { + let mut sbot_client = Sbot::init(None, None).await?; + + let id = sbot_client.whoami().await?; + println!("whoami: {}", id); + + // test ids to follow and block + let to_follow = "@5Pt3dKy2HTJ0mWuS78oIiklIX0gBz6BTfEnXsbvke9c=.ed25519"; + let to_block = "@7Y4nwfQmVtAilEzi5knXdS2gilW7cGKSHXdXoT086LM=.ed25519"; + + // follow to_follow + let response = sbot_client.set_relationship(to_follow, true, false).await?; + println!("follow_response: {:?}", response); + + // block to_block + let response = sbot_client.set_relationship(to_block, false, true).await?; + println!("follow_response: {:?}", response); + + // print all users you are following + let follows = sbot_client.friends_hops(FriendsHopsOpts { + max: 1, + start: None, + // doesnt seem like reverse does anything, currently + reverse: Some(false), + }).await?; + println!("follows: {:?}", follows); + + // print if you are following to_follow (should be true) + let mref = sbot_client.friends_is_following(&id, to_follow).await?; + println!("isfollowingmref: {}", mref); + + // print if you are blocking to_block (should be true) + let mref = sbot_client.friends_is_blocking(&id, to_block).await?; + println!("isblockingmref: {}", mref); + + // print if you are blocking to_follow (should be false) + let mref = sbot_client.friends_is_blocking(&id, to_follow).await?; + println!("isblockingmref(should be false): {}", mref); + + // print if you are following to_block (should be false) + let mref = sbot_client.friends_is_following(&id, to_block).await?; + println!("isfollowingmref(should be false): {}", mref); + + Ok(()) +} + +#[async_std::main] +async fn main() { + if let Err(e) = run().await { + eprintln!("Application error: {}", e); + process::exit(1); + } +} diff --git a/src/sbot.rs b/src/sbot.rs index f1fcfca..e4cd61f 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -21,7 +21,7 @@ use crate::utils; use crate::utils::get_source_stream; // re-export types from kuska -pub use kuska_ssb::api::dto::content::{SubsetQuery, SubsetQueryOptions}; +pub use kuska_ssb::api::dto::content::{SubsetQuery, SubsetQueryOptions, FriendsHopsOpts}; /// 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 @@ -184,9 +184,32 @@ impl Sbot { .await } + // Convenience method to set a relationship with following: true, blocking: false + pub async fn follow(&mut self, contact: &str) -> Result { + self.set_relationship(contact, true, false).await + } + + // Convenience method to set a relationship with following: false, blocking: true + pub async fn block(&mut self, contact: &str) -> Result { + self.set_relationship(contact, false, true).await + } + + /// 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 { + let msg = SsbMessageContent::Contact { + contact: Some(contact.to_string()), + following: Some(following), + blocking: Some(blocking), + autofollow: None + }; + self.publish(msg).await + } + /// Call the `friends follow` RPC method and return a message reference. /// if state is true, then sets the following status to true /// if state is false, then sets the following status to false (unfollow) + /// TODO: currently this method is not working (seems to be missing from go-sbot) + /// as a workaround, we can use set_relationship instead just fine pub async fn friends_follow( &mut self, ssb_id: &str, @@ -226,6 +249,70 @@ impl Sbot { .await } + /// Call the `friends isblocking` RPC method and return a message reference. + /// returns true if src_id is blocking dest_id and false otherwise + pub async fn friends_is_blocking( + &mut self, + src_id: &str, + dest_id: &str, + ) -> Result { + let mut sbot_connection = self.get_sbot_connection().await?; + let req_id = sbot_connection + .client + .friends_isblocking_req_send(src_id, dest_id) + .await?; + + utils::get_async( + &mut sbot_connection.rpc_reader, + req_id, + utils::string_res_parse, + ) + .await + } + + // Gets a Vec where each element is a peer you are following + pub async fn get_follows(&mut self) -> Result, GolgiError> { + self.friends_hops(FriendsHopsOpts { + max: 1, + start: None, + reverse: Some(false), + }).await + } + + // Gets a Vec where each element is a peer who follows you + /// TODO: currently this method is not working + /// go-sbot does not seem to listen to the reverse=True parameter + /// and just returns follows + pub async fn get_followers(&mut self) -> Result, GolgiError> { + self.friends_hops(FriendsHopsOpts { + max: 1, + start: None, + reverse: Some(true), + }).await + } + + /// Call the `friends hops` RPC method and return a Vector + /// where each element of the vector is the ssb_id of a peer. + /// + /// When opts.reverse = True, it should return peers who are following you + /// (but this is currently not working) + pub async fn friends_hops( + &mut self, + opts: FriendsHopsOpts, + ) -> Result, GolgiError> { + let mut sbot_connection = self.get_sbot_connection().await?; + let req_id = sbot_connection + .client + .friends_hops_req_send(opts) + .await?; + utils::get_source_until_eof( + &mut sbot_connection.rpc_reader, + req_id, + utils::string_res_parse, + ) + .await + } + /// Wrapper for publish which constructs and publishes a post message appropriately from a string. /// /// # Arguments From f54e4c47b0a5be3beed40282a48ba3d405e4e590 Mon Sep 17 00:00:00 2001 From: notplants Date: Sun, 16 Jan 2022 08:55:05 -0500 Subject: [PATCH 06/10] Update example --- examples/ssb-friends.rs | 16 +++++++++------- src/sbot.rs | 35 ++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/examples/ssb-friends.rs b/examples/ssb-friends.rs index 0b65a78..22b33a3 100644 --- a/examples/ssb-friends.rs +++ b/examples/ssb-friends.rs @@ -2,10 +2,10 @@ use async_std::stream::StreamExt; use futures::pin_mut; use std::process; -use golgi::sbot::{SubsetQuery, SubsetQueryOptions, FriendsHopsOpts}; use golgi::error::GolgiError; use golgi::messages::SsbMessageContent; use golgi::sbot::Sbot; +use golgi::sbot::{FriendsHopsOpts, SubsetQuery, SubsetQueryOptions}; async fn run() -> Result<(), GolgiError> { let mut sbot_client = Sbot::init(None, None).await?; @@ -26,12 +26,14 @@ async fn run() -> Result<(), GolgiError> { println!("follow_response: {:?}", response); // print all users you are following - let follows = sbot_client.friends_hops(FriendsHopsOpts { - max: 1, - start: None, - // doesnt seem like reverse does anything, currently - reverse: Some(false), - }).await?; + let follows = sbot_client + .friends_hops(FriendsHopsOpts { + max: 1, + start: None, + // doesnt seem like reverse does anything, currently + reverse: Some(false), + }) + .await?; println!("follows: {:?}", follows); // print if you are following to_follow (should be true) diff --git a/src/sbot.rs b/src/sbot.rs index e4cd61f..52c2797 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -21,7 +21,7 @@ use crate::utils; use crate::utils::get_source_stream; // 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 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 - pub async fn follow(&mut self, contact: &str) -> Result { + pub async fn follow(&mut self, contact: &str) -> Result { self.set_relationship(contact, true, false).await } // Convenience method to set a relationship with following: false, blocking: true - pub async fn block(&mut self, contact: &str) -> Result { + pub async fn block(&mut self, contact: &str) -> Result { self.set_relationship(contact, false, true).await } /// 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 { + pub async fn set_relationship( + &mut self, + contact: &str, + following: bool, + blocking: bool, + ) -> Result { let msg = SsbMessageContent::Contact { contact: Some(contact.to_string()), following: Some(following), blocking: Some(blocking), - autofollow: None + autofollow: None, }; self.publish(msg).await } @@ -267,7 +272,7 @@ impl Sbot { req_id, utils::string_res_parse, ) - .await + .await } // Gets a Vec where each element is a peer you are following @@ -276,7 +281,8 @@ impl Sbot { max: 1, start: None, reverse: Some(false), - }).await + }) + .await } // Gets a Vec where each element is a peer who follows you @@ -288,7 +294,8 @@ impl Sbot { max: 1, start: None, reverse: Some(true), - }).await + }) + .await } /// Call the `friends hops` RPC method and return a Vector @@ -296,21 +303,15 @@ impl Sbot { /// /// When opts.reverse = True, it should return peers who are following you /// (but this is currently not working) - pub async fn friends_hops( - &mut self, - opts: FriendsHopsOpts, - ) -> Result, GolgiError> { + pub async fn friends_hops(&mut self, opts: FriendsHopsOpts) -> Result, GolgiError> { let mut sbot_connection = self.get_sbot_connection().await?; - let req_id = sbot_connection - .client - .friends_hops_req_send(opts) - .await?; + let req_id = sbot_connection.client.friends_hops_req_send(opts).await?; utils::get_source_until_eof( &mut sbot_connection.rpc_reader, req_id, utils::string_res_parse, ) - .await + .await } /// Wrapper for publish which constructs and publishes a post message appropriately from a string. From d15474ad610e9f662f0d5f431b020e5117325401 Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 17 Jan 2022 10:55:32 -0500 Subject: [PATCH 07/10] Remove friends.follow --- src/sbot.rs | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/sbot.rs b/src/sbot.rs index 52c2797..f902487 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -210,29 +210,6 @@ impl Sbot { self.publish(msg).await } - /// Call the `friends follow` RPC method and return a message reference. - /// if state is true, then sets the following status to true - /// if state is false, then sets the following status to false (unfollow) - /// TODO: currently this method is not working (seems to be missing from go-sbot) - /// as a workaround, we can use set_relationship instead just fine - pub async fn friends_follow( - &mut self, - ssb_id: &str, - state: bool, - ) -> Result { - let mut sbot_connection = self.get_sbot_connection().await?; - let req_id = sbot_connection - .client - .friends_follow_req_send(ssb_id, state) - .await?; - utils::get_async( - &mut sbot_connection.rpc_reader, - req_id, - utils::string_res_parse, - ) - .await - } - /// Call the `friends isfollowing` RPC method and return a message reference. /// returns true if src_id is following dest_id and false otherwise pub async fn friends_is_following( From 79630703c2fee672dc1319386069e96d2bc02363 Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 17 Jan 2022 10:57:13 -0500 Subject: [PATCH 08/10] Remove pub from get_followers --- src/sbot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sbot.rs b/src/sbot.rs index f902487..8d26488 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -266,7 +266,7 @@ impl Sbot { /// TODO: currently this method is not working /// go-sbot does not seem to listen to the reverse=True parameter /// and just returns follows - pub async fn get_followers(&mut self) -> Result, GolgiError> { + async fn get_followers(&mut self) -> Result, GolgiError> { self.friends_hops(FriendsHopsOpts { max: 1, start: None, From 08cdc5bede9a1912f94d7a40ae618618eac2b2b7 Mon Sep 17 00:00:00 2001 From: glyph Date: Mon, 31 Jan 2022 10:46:58 +0200 Subject: [PATCH 09/10] use relationship query struct and latest kuska api --- examples/ssb-friends.rs | 44 ++++++++++++++++++++++++++++++----------- src/sbot.rs | 28 +++++++++++++------------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/examples/ssb-friends.rs b/examples/ssb-friends.rs index 22b33a3..e7a329e 100644 --- a/examples/ssb-friends.rs +++ b/examples/ssb-friends.rs @@ -5,29 +5,31 @@ use std::process; use golgi::error::GolgiError; use golgi::messages::SsbMessageContent; use golgi::sbot::Sbot; -use golgi::sbot::{FriendsHopsOpts, SubsetQuery, SubsetQueryOptions}; +use golgi::sbot::{FriendsHops, RelationshipQuery, SubsetQuery, SubsetQueryOptions}; async fn run() -> Result<(), GolgiError> { - let mut sbot_client = Sbot::init(None, None).await?; + let mut sbot_client = Sbot::init(Some("127.0.0.1:8009".to_string()), None).await?; let id = sbot_client.whoami().await?; println!("whoami: {}", id); // test ids to follow and block - let to_follow = "@5Pt3dKy2HTJ0mWuS78oIiklIX0gBz6BTfEnXsbvke9c=.ed25519"; - let to_block = "@7Y4nwfQmVtAilEzi5knXdS2gilW7cGKSHXdXoT086LM=.ed25519"; + let to_follow = String::from("@5Pt3dKy2HTJ0mWuS78oIiklIX0gBz6BTfEnXsbvke9c=.ed25519"); + let to_block = String::from("@7Y4nwfQmVtAilEzi5knXdS2gilW7cGKSHXdXoT086LM=.ed25519"); // follow to_follow - let response = sbot_client.set_relationship(to_follow, true, false).await?; + let response = sbot_client + .set_relationship(&to_follow, true, false) + .await?; println!("follow_response: {:?}", response); // block to_block - let response = sbot_client.set_relationship(to_block, false, true).await?; + let response = sbot_client.set_relationship(&to_block, false, true).await?; println!("follow_response: {:?}", response); // print all users you are following let follows = sbot_client - .friends_hops(FriendsHopsOpts { + .friends_hops(FriendsHops { max: 1, start: None, // doesnt seem like reverse does anything, currently @@ -37,19 +39,39 @@ async fn run() -> Result<(), GolgiError> { println!("follows: {:?}", follows); // print if you are following to_follow (should be true) - let mref = sbot_client.friends_is_following(&id, to_follow).await?; + let mref = sbot_client + .friends_is_following(RelationshipQuery { + source: id.clone(), + dest: to_follow.clone(), + }) + .await?; println!("isfollowingmref: {}", mref); // print if you are blocking to_block (should be true) - let mref = sbot_client.friends_is_blocking(&id, to_block).await?; + let mref = sbot_client + .friends_is_blocking(RelationshipQuery { + source: id.clone(), + dest: to_block.clone(), + }) + .await?; println!("isblockingmref: {}", mref); // print if you are blocking to_follow (should be false) - let mref = sbot_client.friends_is_blocking(&id, to_follow).await?; + let mref = sbot_client + .friends_is_blocking(RelationshipQuery { + source: id.clone(), + dest: to_follow, + }) + .await?; println!("isblockingmref(should be false): {}", mref); // print if you are following to_block (should be false) - let mref = sbot_client.friends_is_following(&id, to_block).await?; + let mref = sbot_client + .friends_is_following(RelationshipQuery { + source: id, + dest: to_block.clone(), + }) + .await?; println!("isfollowingmref(should be false): {}", mref); Ok(()) diff --git a/src/sbot.rs b/src/sbot.rs index 8d26488..9f95744 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -21,7 +21,9 @@ use crate::utils; use crate::utils::get_source_stream; // re-export types from kuska -pub use kuska_ssb::api::dto::content::{FriendsHopsOpts, SubsetQuery, SubsetQueryOptions}; +pub use kuska_ssb::api::dto::content::{ + FriendsHops, RelationshipQuery, SubsetQuery, SubsetQueryOptions, +}; /// 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 @@ -210,17 +212,16 @@ impl Sbot { self.publish(msg).await } - /// Call the `friends isfollowing` RPC method and return a message reference. - /// returns true if src_id is following dest_id and false otherwise + /// Call the `friends isFollowing` RPC method and return a message reference. + /// Returns true if src_id is following dest_id and false otherwise. pub async fn friends_is_following( &mut self, - src_id: &str, - dest_id: &str, + args: RelationshipQuery, ) -> Result { let mut sbot_connection = self.get_sbot_connection().await?; let req_id = sbot_connection .client - .friends_isfollowing_req_send(src_id, dest_id) + .friends_is_following_req_send(args) .await?; utils::get_async( @@ -232,16 +233,15 @@ impl Sbot { } /// Call the `friends isblocking` RPC method and return a message reference. - /// returns true if src_id is blocking dest_id and false otherwise + /// Returns true if src_id is blocking dest_id and false otherwise. pub async fn friends_is_blocking( &mut self, - src_id: &str, - dest_id: &str, + args: RelationshipQuery, ) -> Result { let mut sbot_connection = self.get_sbot_connection().await?; let req_id = sbot_connection .client - .friends_isblocking_req_send(src_id, dest_id) + .friends_is_blocking_req_send(args) .await?; utils::get_async( @@ -254,7 +254,7 @@ impl Sbot { // Gets a Vec where each element is a peer you are following pub async fn get_follows(&mut self) -> Result, GolgiError> { - self.friends_hops(FriendsHopsOpts { + self.friends_hops(FriendsHops { max: 1, start: None, reverse: Some(false), @@ -267,7 +267,7 @@ impl Sbot { /// go-sbot does not seem to listen to the reverse=True parameter /// and just returns follows async fn get_followers(&mut self) -> Result, GolgiError> { - self.friends_hops(FriendsHopsOpts { + self.friends_hops(FriendsHops { max: 1, start: None, reverse: Some(true), @@ -280,9 +280,9 @@ impl Sbot { /// /// When opts.reverse = True, it should return peers who are following you /// (but this is currently not working) - pub async fn friends_hops(&mut self, opts: FriendsHopsOpts) -> Result, GolgiError> { + pub async fn friends_hops(&mut self, args: FriendsHops) -> Result, GolgiError> { let mut sbot_connection = self.get_sbot_connection().await?; - let req_id = sbot_connection.client.friends_hops_req_send(opts).await?; + let req_id = sbot_connection.client.friends_hops_req_send(args).await?; utils::get_source_until_eof( &mut sbot_connection.rpc_reader, req_id, From 986cd7ecd26d87ff6d6e6db722a3f5ac31f8ef0d Mon Sep 17 00:00:00 2001 From: glyph Date: Fri, 4 Feb 2022 10:26:14 +0200 Subject: [PATCH 10/10] reset default address and port in example --- examples/ssb-friends.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ssb-friends.rs b/examples/ssb-friends.rs index e7a329e..1ce339f 100644 --- a/examples/ssb-friends.rs +++ b/examples/ssb-friends.rs @@ -8,7 +8,7 @@ use golgi::sbot::Sbot; use golgi::sbot::{FriendsHops, RelationshipQuery, SubsetQuery, SubsetQueryOptions}; async fn run() -> Result<(), GolgiError> { - let mut sbot_client = Sbot::init(Some("127.0.0.1:8009".to_string()), None).await?; + let mut sbot_client = Sbot::init(None, None).await?; let id = sbot_client.whoami().await?; println!("whoami: {}", id);