Merge pull request 'Add unfollow and unblock methods' (#34) from unfollow_and_unblock into main

Reviewed-on: #34
This commit is contained in:
glyph 2022-03-03 08:17:06 +00:00
commit 9ad38fb0e8
1 changed files with 72 additions and 9 deletions

View File

@ -3,7 +3,9 @@
//! Implements the following methods: //! Implements the following methods:
//! //!
//! - [`Sbot::block`] //! - [`Sbot::block`]
//! - [`Sbot::unblock`]
//! - [`Sbot::follow`] //! - [`Sbot::follow`]
//! - [`Sbot::unfollow`]
//! - [`Sbot::friends_hops`] //! - [`Sbot::friends_hops`]
//! - [`Sbot::friends_is_blocking`] //! - [`Sbot::friends_is_blocking`]
//! - [`Sbot::friends_is_following`] //! - [`Sbot::friends_is_following`]
@ -20,7 +22,7 @@ impl Sbot {
/// Follow a peer. /// Follow a peer.
/// ///
/// This is a convenience method to publish a contact message with /// This is a convenience method to publish a contact message with
/// following: `true` and blocking: `false`. /// following: `true`.
/// ///
/// # Example /// # Example
/// ///
@ -43,7 +45,36 @@ impl Sbot {
/// } /// }
/// ``` /// ```
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, Some(true), None).await
}
/// Unfollow a peer.
///
/// This is a convenience method to publish a contact message with
/// following: `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<String, GolgiError> {
self.set_relationship(contact, Some(false), None).await
} }
/// Block a peer. /// Block a peer.
@ -72,7 +103,38 @@ impl Sbot {
/// } /// }
/// ``` /// ```
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 // 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
/// 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<String, GolgiError> {
self.set_relationship(contact, None, Some(false)).await
} }
/// Publish a contact message defining the relationship for a peer. /// Publish a contact message defining the relationship for a peer.
@ -86,8 +148,9 @@ impl Sbot {
/// let mut sbot_client = Sbot::init(None, None).await?; /// let mut sbot_client = Sbot::init(None, None).await?;
/// ///
/// let ssb_id = "@zqshk7o2Rpd/OaZ/MxH6xXONgonP1jH+edK9+GZb/NY=.ed25519"; /// let ssb_id = "@zqshk7o2Rpd/OaZ/MxH6xXONgonP1jH+edK9+GZb/NY=.ed25519";
/// let following = true; /// let following = Some(true);
/// let blocking = false; /// // Could also be `None` to only publish the following relationship.
/// let blocking = Some(false);
/// ///
/// match sbot_client.set_relationship(ssb_id, following, blocking).await { /// match sbot_client.set_relationship(ssb_id, following, blocking).await {
/// Ok(msg_ref) => { /// Ok(msg_ref) => {
@ -102,13 +165,13 @@ impl Sbot {
pub async fn set_relationship( pub async fn set_relationship(
&mut self, &mut self,
contact: &str, contact: &str,
following: bool, following: Option<bool>,
blocking: bool, blocking: Option<bool>,
) -> Result<String, GolgiError> { ) -> 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,
blocking: Some(blocking), blocking,
autofollow: None, autofollow: None,
}; };
self.publish(msg).await self.publish(msg).await