2022-01-15 15:36:52 +00:00
|
|
|
use std::process;
|
|
|
|
|
2022-02-08 09:54:21 +00:00
|
|
|
use golgi::{
|
|
|
|
api::friends::{FriendsHops, RelationshipQuery},
|
|
|
|
GolgiError, Sbot,
|
|
|
|
};
|
2022-01-15 15:36:52 +00:00
|
|
|
|
2022-02-15 08:26:51 +00:00
|
|
|
// Golgi is an asynchronous library so we must call it from within an
|
|
|
|
// async function. The `GolgiError` type encapsulates all possible
|
|
|
|
// error variants for the library.
|
2022-01-15 15:36:52 +00:00
|
|
|
async fn run() -> Result<(), GolgiError> {
|
2022-02-15 08:26:51 +00:00
|
|
|
// Attempt to connect to an sbot instance using the default IP address,
|
|
|
|
// port and network key (aka. capabilities key).
|
2022-02-08 13:04:44 +00:00
|
|
|
let mut sbot_client = Sbot::init(None, None).await?;
|
2022-01-15 15:36:52 +00:00
|
|
|
|
2022-02-15 08:26:51 +00:00
|
|
|
// Call the `whoami` RPC method to retrieve the public key for the sbot
|
|
|
|
// identity. This is our 'local' public key.
|
2022-01-15 15:36:52 +00:00
|
|
|
let id = sbot_client.whoami().await?;
|
2022-02-15 08:26:51 +00:00
|
|
|
|
|
|
|
// Print the public key (identity) to `stdout`.
|
2022-01-15 15:36:52 +00:00
|
|
|
println!("whoami: {}", id);
|
|
|
|
|
2022-02-15 08:26:51 +00:00
|
|
|
// Define IDs (public keys) to follow and block.
|
2022-01-31 08:46:58 +00:00
|
|
|
let to_follow = String::from("@5Pt3dKy2HTJ0mWuS78oIiklIX0gBz6BTfEnXsbvke9c=.ed25519");
|
|
|
|
let to_block = String::from("@7Y4nwfQmVtAilEzi5knXdS2gilW7cGKSHXdXoT086LM=.ed25519");
|
2022-01-15 15:36:52 +00:00
|
|
|
|
2022-02-15 08:26:51 +00:00
|
|
|
// Set the relationship of the local identity to the `to_follow` identity.
|
|
|
|
// In this case, the `set_relationship` method publishes a `contact`
|
|
|
|
// message which defines following as `true` and blocking as `false`.
|
|
|
|
// A message reference is returned for the published `contact` message.
|
2022-01-31 08:46:58 +00:00
|
|
|
let response = sbot_client
|
|
|
|
.set_relationship(&to_follow, true, false)
|
|
|
|
.await?;
|
2022-02-15 08:26:51 +00:00
|
|
|
|
|
|
|
// Print the message reference to `stdout`.
|
2022-01-15 15:36:52 +00:00
|
|
|
println!("follow_response: {:?}", response);
|
|
|
|
|
2022-02-15 08:26:51 +00:00
|
|
|
// Set the relationship of the local identity to the `to_block` identity.
|
|
|
|
// In this case, the `set_relationship` method publishes a `contact`
|
|
|
|
// message which defines following as `false` and blocking as `true`.
|
|
|
|
// A message reference is returned for the published `contact` message.
|
2022-01-31 08:46:58 +00:00
|
|
|
let response = sbot_client.set_relationship(&to_block, false, true).await?;
|
2022-02-15 08:26:51 +00:00
|
|
|
|
|
|
|
// Print the message reference to `stdout`.
|
2022-01-15 15:36:52 +00:00
|
|
|
println!("follow_response: {:?}", response);
|
|
|
|
|
2022-02-15 08:26:51 +00:00
|
|
|
// Golgi also exposes convenience methods for following and blocking.
|
|
|
|
// Here is an example of a simpler way to follow an identity.
|
|
|
|
let _follow_response = sbot_client.follow(&to_follow).await?;
|
|
|
|
|
|
|
|
// Blocking can be achieved in a similar fashion.
|
|
|
|
let _block_response = sbot_client.block(&to_block).await?;
|
|
|
|
|
|
|
|
// Get a list of peers within 1 hop of the local identity.
|
2022-01-16 13:55:05 +00:00
|
|
|
let follows = sbot_client
|
2022-01-31 08:46:58 +00:00
|
|
|
.friends_hops(FriendsHops {
|
2022-01-16 13:55:05 +00:00
|
|
|
max: 1,
|
|
|
|
start: None,
|
2022-02-15 08:26:51 +00:00
|
|
|
// The `reverse` parameter is not currently implemented in `go-sbot`.
|
2022-01-16 13:55:05 +00:00
|
|
|
reverse: Some(false),
|
|
|
|
})
|
|
|
|
.await?;
|
2022-02-15 08:26:51 +00:00
|
|
|
|
|
|
|
// Print the list of peers to `stdout`.
|
2022-01-15 15:36:52 +00:00
|
|
|
println!("follows: {:?}", follows);
|
|
|
|
|
2022-02-15 08:26:51 +00:00
|
|
|
// Determine if an identity (`source`) is following a second identity (`dest`).
|
|
|
|
// This method will return `true` or `false`.
|
2022-01-31 08:46:58 +00:00
|
|
|
let mref = sbot_client
|
|
|
|
.friends_is_following(RelationshipQuery {
|
|
|
|
source: id.clone(),
|
|
|
|
dest: to_follow.clone(),
|
|
|
|
})
|
|
|
|
.await?;
|
2022-02-15 08:26:51 +00:00
|
|
|
|
|
|
|
// Print the follow status to `stdout`.
|
2022-01-15 15:36:52 +00:00
|
|
|
println!("isfollowingmref: {}", mref);
|
|
|
|
|
2022-02-15 08:26:51 +00:00
|
|
|
// Determine if an identity (`source`) is blocking a second identity (`dest`).
|
2022-01-31 08:46:58 +00:00
|
|
|
let mref = sbot_client
|
|
|
|
.friends_is_blocking(RelationshipQuery {
|
|
|
|
source: id.clone(),
|
|
|
|
dest: to_block.clone(),
|
|
|
|
})
|
|
|
|
.await?;
|
2022-02-15 08:26:51 +00:00
|
|
|
|
|
|
|
// Print the block status to `stdout`.
|
2022-01-15 15:36:52 +00:00
|
|
|
println!("isblockingmref: {}", mref);
|
|
|
|
|
2022-01-31 08:46:58 +00:00
|
|
|
let mref = sbot_client
|
|
|
|
.friends_is_blocking(RelationshipQuery {
|
|
|
|
source: id.clone(),
|
|
|
|
dest: to_follow,
|
|
|
|
})
|
|
|
|
.await?;
|
2022-02-15 08:26:51 +00:00
|
|
|
|
|
|
|
// Output should be `false`.
|
2022-01-15 15:36:52 +00:00
|
|
|
println!("isblockingmref(should be false): {}", mref);
|
|
|
|
|
2022-01-31 08:46:58 +00:00
|
|
|
let mref = sbot_client
|
|
|
|
.friends_is_following(RelationshipQuery {
|
|
|
|
source: id,
|
|
|
|
dest: to_block.clone(),
|
|
|
|
})
|
|
|
|
.await?;
|
2022-02-15 08:26:51 +00:00
|
|
|
|
|
|
|
// Output should be `false`.
|
2022-01-15 15:36:52 +00:00
|
|
|
println!("isfollowingmref(should be false): {}", mref);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-15 08:26:51 +00:00
|
|
|
// Enable an async main function and execute the `run()` function,
|
|
|
|
// catching any errors and printing them to `stderr` before exiting the
|
|
|
|
// process.
|
2022-01-15 15:36:52 +00:00
|
|
|
#[async_std::main]
|
|
|
|
async fn main() {
|
|
|
|
if let Err(e) = run().await {
|
|
|
|
eprintln!("Application error: {}", e);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|