golgi/src/api/friends.rs

127 lines
4.2 KiB
Rust
Raw Normal View History

2022-02-08 09:52:52 +00:00
//! Define peer relationships and query the social graph.
//!
//! Implements the following methods:
//!
//! - [`Sbot::block`]
//! - [`Sbot::follow`]
//! - [`Sbot::friends_hops`]
//! - [`Sbot::friends_is_blocking`]
//! - [`Sbot::friends_is_following`]
//! - [`Sbot::get_follows`]
//! - [`Sbot::set_relationship`]
use crate::{error::GolgiError, messages::SsbMessageContent, sbot::Sbot, utils};
// re-export friends-related kuska types
pub use kuska_ssb::api::dto::content::{FriendsHops, RelationshipQuery};
2022-02-07 11:54:47 +00:00
impl Sbot {
2022-02-08 09:52:52 +00:00
/// Convenience method to set a relationship with following: `true`,
/// blocking: `false`.
2022-02-07 11:54:47 +00:00
pub async fn follow(&mut self, contact: &str) -> Result<String, GolgiError> {
self.set_relationship(contact, true, false).await
}
2022-02-08 09:52:52 +00:00
/// Convenience method to set a relationship with following: `false`,
/// blocking: `true`.
2022-02-07 11:54:47 +00:00
pub async fn block(&mut self, contact: &str) -> Result<String, GolgiError> {
self.set_relationship(contact, false, true).await
}
2022-02-08 09:52:52 +00:00
/// Publish a contact relationship to the given peer (with ssb_id) with
/// the given state.
2022-02-07 11:54:47 +00:00
pub async fn set_relationship(
&mut self,
contact: &str,
following: bool,
blocking: bool,
) -> Result<String, GolgiError> {
let msg = SsbMessageContent::Contact {
contact: Some(contact.to_string()),
following: Some(following),
blocking: Some(blocking),
autofollow: None,
};
self.publish(msg).await
}
/// Call the `friends isFollowing` RPC method and return a message reference.
2022-02-08 09:52:52 +00:00
/// Returns `true` if `src_id` is following `dest_id` and `false` otherwise.
2022-02-07 11:54:47 +00:00
pub async fn friends_is_following(
&mut self,
args: RelationshipQuery,
) -> Result<String, GolgiError> {
let mut sbot_connection = self.get_sbot_connection().await?;
let req_id = sbot_connection
.client
.friends_is_following_req_send(args)
.await?;
utils::get_async(
&mut sbot_connection.rpc_reader,
req_id,
utils::string_res_parse,
)
.await
}
/// Call the `friends isblocking` RPC method and return a message reference.
2022-02-08 09:52:52 +00:00
/// Returns `true` if `src_id` is blocking `dest_id` and `false` otherwise.
2022-02-07 11:54:47 +00:00
pub async fn friends_is_blocking(
&mut self,
args: RelationshipQuery,
) -> Result<String, GolgiError> {
let mut sbot_connection = self.get_sbot_connection().await?;
let req_id = sbot_connection
.client
.friends_is_blocking_req_send(args)
.await?;
utils::get_async(
&mut sbot_connection.rpc_reader,
req_id,
utils::string_res_parse,
)
.await
}
2022-02-07 12:07:48 +00:00
/// Return a Vec<String> where each element is a peer you are following.
2022-02-07 11:54:47 +00:00
pub async fn get_follows(&mut self) -> Result<Vec<String>, GolgiError> {
self.friends_hops(FriendsHops {
max: 1,
start: None,
reverse: Some(false),
})
.await
}
// Gets a Vec<String> 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
2022-02-08 09:52:52 +00:00
/// and just returns follows.
2022-02-07 12:07:48 +00:00
async fn _get_followers(&mut self) -> Result<Vec<String>, GolgiError> {
2022-02-07 11:54:47 +00:00
self.friends_hops(FriendsHops {
max: 1,
start: None,
reverse: Some(true),
})
.await
}
/// Call the `friends hops` RPC method and return a Vector<String>
/// 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
2022-02-08 09:52:52 +00:00
/// (but this is currently not working).
2022-02-07 11:54:47 +00:00
pub async fn friends_hops(&mut self, args: FriendsHops) -> Result<Vec<String>, GolgiError> {
let mut sbot_connection = self.get_sbot_connection().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,
utils::string_res_parse,
)
.await
}
}