From 6508aeb3eae9429b29db4359d33c9fdf569bd31f Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 1 Mar 2022 14:19:58 +0200 Subject: [PATCH 1/2] add unfollow and unblock methods --- src/api/friends.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/api/friends.rs b/src/api/friends.rs index b681b01..8a3e41f 100644 --- a/src/api/friends.rs +++ b/src/api/friends.rs @@ -3,7 +3,9 @@ //! Implements the following methods: //! //! - [`Sbot::block`] +//! - [`Sbot::unblock`] //! - [`Sbot::follow`] +//! - [`Sbot::unfollow`] //! - [`Sbot::friends_hops`] //! - [`Sbot::friends_is_blocking`] //! - [`Sbot::friends_is_following`] @@ -46,6 +48,35 @@ impl Sbot { self.set_relationship(contact, true, false).await } + /// Unfollow a peer. + /// + /// This is a convenience method to publish a contact message with + /// following: `false` and blocking: `false`. + /// + /// # Example + /// + /// ```rust + /// use golgi::{Sbot, GolgiError}; + /// + /// async fn unfollow_peer() -> Result<(), GolgiError> { + /// let mut sbot_client = Sbot::init(None, None).await?; + /// + /// let ssb_id = "@zqshk7o2Rpd/OaZ/MxH6xXONgonP1jH+edK9+GZb/NY=.ed25519"; + /// + /// match sbot_client.unfollow(ssb_id).await { + /// Ok(msg_ref) => { + /// println!("unfollow msg reference is: {}", msg_ref) + /// }, + /// Err(e) => eprintln!("failed to unfollow {}: {}", ssb_id, e) + /// } + /// + /// Ok(()) + /// } + /// ``` + pub async fn unfollow(&mut self, contact: &str) -> Result { + self.set_relationship(contact, false, false).await + } + /// Block a peer. /// /// This is a convenience method to publish a contact message with @@ -75,6 +106,35 @@ impl Sbot { self.set_relationship(contact, false, true).await } + /// Unblock a peer. + /// + /// This is a convenience method to publish a contact message with + /// following: `false` and blocking: `false`. + /// + /// # Example + /// + /// ```rust + /// use golgi::{Sbot, GolgiError}; + /// + /// async fn unblock_peer() -> Result<(), GolgiError> { + /// let mut sbot_client = Sbot::init(None, None).await?; + /// + /// let ssb_id = "@zqshk7o2Rpd/OaZ/MxH6xXONgonP1jH+edK9+GZb/NY=.ed25519"; + /// + /// match sbot_client.unblock(ssb_id).await { + /// Ok(msg_ref) => { + /// println!("unblock msg reference is: {}", msg_ref) + /// }, + /// Err(e) => eprintln!("failed to unblock {}: {}", ssb_id, e) + /// } + /// + /// Ok(()) + /// } + /// ``` + pub async fn unblock(&mut self, contact: &str) -> Result { + self.set_relationship(contact, false, false).await + } + /// Publish a contact message defining the relationship for a peer. /// /// # Example From e8294241ec80cb28cbf99b416160b4372d0dfc81 Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 1 Mar 2022 15:42:40 +0200 Subject: [PATCH 2/2] update set_relationship parameter types --- src/api/friends.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/api/friends.rs b/src/api/friends.rs index 8a3e41f..8975133 100644 --- a/src/api/friends.rs +++ b/src/api/friends.rs @@ -22,7 +22,7 @@ impl Sbot { /// Follow a peer. /// /// This is a convenience method to publish a contact message with - /// following: `true` and blocking: `false`. + /// following: `true`. /// /// # Example /// @@ -45,13 +45,13 @@ impl Sbot { /// } /// ``` pub async fn follow(&mut self, contact: &str) -> Result { - self.set_relationship(contact, true, false).await + self.set_relationship(contact, Some(true), None).await } /// Unfollow a peer. /// /// This is a convenience method to publish a contact message with - /// following: `false` and blocking: `false`. + /// following: `false`. /// /// # Example /// @@ -74,7 +74,7 @@ impl Sbot { /// } /// ``` pub async fn unfollow(&mut self, contact: &str) -> Result { - self.set_relationship(contact, false, false).await + self.set_relationship(contact, Some(false), None).await } /// Block a peer. @@ -103,13 +103,15 @@ impl Sbot { /// } /// ``` pub async fn block(&mut self, contact: &str) -> Result { - self.set_relationship(contact, false, true).await + // we want to unfollow and block + self.set_relationship(contact, Some(false), Some(true)) + .await } /// Unblock a peer. /// /// This is a convenience method to publish a contact message with - /// following: `false` and blocking: `false`. + /// blocking: `false`. /// /// # Example /// @@ -132,7 +134,7 @@ impl Sbot { /// } /// ``` pub async fn unblock(&mut self, contact: &str) -> Result { - self.set_relationship(contact, false, false).await + self.set_relationship(contact, None, Some(false)).await } /// Publish a contact message defining the relationship for a peer. @@ -146,8 +148,9 @@ impl Sbot { /// let mut sbot_client = Sbot::init(None, None).await?; /// /// let ssb_id = "@zqshk7o2Rpd/OaZ/MxH6xXONgonP1jH+edK9+GZb/NY=.ed25519"; - /// let following = true; - /// let blocking = false; + /// let following = Some(true); + /// // Could also be `None` to only publish the following relationship. + /// let blocking = Some(false); /// /// match sbot_client.set_relationship(ssb_id, following, blocking).await { /// Ok(msg_ref) => { @@ -162,13 +165,13 @@ impl Sbot { pub async fn set_relationship( &mut self, contact: &str, - following: bool, - blocking: bool, + following: Option, + blocking: Option, ) -> Result { let msg = SsbMessageContent::Contact { contact: Some(contact.to_string()), - following: Some(following), - blocking: Some(blocking), + following, + blocking, autofollow: None, }; self.publish(msg).await