Compare commits

...

5 Commits

Author SHA1 Message Date
glyph b4281db1f2 add message of thanks to daan 2022-10-02 17:41:15 +01:00
glyph 465919792f don't treat already-not-following as error 2022-10-02 17:37:22 +01:00
glyph 61f0883841 fix use of incorrect macro 2022-10-02 17:32:52 +01:00
glyph 504ca08076 fix premature use of 2022-10-02 17:31:44 +01:00
glyph 6f1536a09d add instruction to import bincode dependency 2022-10-02 17:28:24 +01:00
3 changed files with 34 additions and 11 deletions

View File

@ -29,6 +29,8 @@ Author: [@glyph](https://mycelial.technology/)
I am grateful to the Butts who voted to fund this work, all contributors to the [SSBC](https://opencollective.com/secure-scuttlebutt-consortium) and Erick Lavoie (`@elavoie` / `@IgYpd+tCtXnlE2tYX/8rR2AGt+P8svC98WH3MdYAa8Y=.ed25519`) in particular - both for partially funding this work and for developing and overseeing the community grant process.
A big thank you to Daan Wynen (aka. [black-puppydog](https://github.com/black-puppydog)) for providing feedback and fixes for code examples in this tutorial series.
## Contribute
If you wish to support my work on the Rust Scuttlebutt ecosystem, please consider contributing to my [Liberapay account](https://liberapay.com/glyph/).

View File

@ -188,6 +188,12 @@ impl Database {
You'll notice in the above code snippet that we're serialising the peer data as bincode before inserting it. The sled database we're using expects values in the form of a byte vector; bincode thus provides a neat way of storing complex datastructures (such as our `Peer` `struct`).
Add the `bincode` dependency to `Cargo.toml` and then test that everything compiles correctly:
```toml
bincode = "1.3"
```
That's enough database code for the moment. Now we can return to our Scuttlebutt-related code and complete the peer subscription flows.
### Follow / Unfollow a Peer
@ -214,33 +220,33 @@ At this point we have the capability to check whether we follow a peer, to add a
```rust
// Update this match block in `subscribe_form`
match sbot::is_following(&whoami, remote_peer).await {
match sbot::is_following(&whoami, &peer.public_key).await {
Ok(status) if status.as_str() == "false" => {
// If we are not following the peer, call the `follow_peer` method.
match sbot::follow_peer(remote_peer).await {
Ok(_) => info!("Followed peer {}", &remote_peer),
Err(e) => warn!("Failed to follow peer {}: {}", &remote_peer, e),
match sbot::follow_peer(&peer.public_key).await {
Ok(_) => info!("Followed peer {}", &peer.public_key),
Err(e) => warn!("Failed to follow peer {}: {}", &peer.public_key, e),
}
}
Ok(status) if status.as_str() == "true" => {
info!(
"Already following peer {}. No further action taken",
&remote_peer
&peer.public_key
)
}
_ => (),
}
// Update this match block in `unsubscribe_form`
match sbot::is_following(&whoami, remote_peer).await {
match sbot::is_following(&whoami, &peer.public_key).await {
Ok(status) if status.as_str() == "true" => {
// If we are following the peer, call the `unfollow_peer` method.
info!("Unfollowing peer {}", &remote_peer);
match sbot::unfollow_peer(remote_peer).await {
info!("Unfollowing peer {}", &peer.public_key);
match sbot::unfollow_peer(&peer.public_key).await {
Ok(_) => {
info!("Unfollowed peer {}", &remote_peer);
info!("Unfollowed peer {}", &peer.public_key);
}
Err(e) => warn!("Failed to unfollow peer {}: {}", &remote_peer, e),
Err(e) => warn!("Failed to unfollow peer {}: {}", &peer.public_key, e),
}
}
_ => (),
@ -286,7 +292,7 @@ pub async fn follow_if_not_following(remote_peer: &str) -> Result<(), String> {
Ok(())
}
Err(e) => {
let err_msg = warn!("Failed to follow peer {}: {}", &remote_peer, e);
let err_msg = format!("Failed to follow peer {}: {}", &remote_peer, e);
warn!("{}", err_msg);
Err(err_msg)
@ -317,6 +323,14 @@ pub async fn follow_if_not_following(remote_peer: &str) -> Result<(), String> {
pub async fn unfollow_if_following(remote_peer: &str) {
if let Ok(whoami) = whoami().await {
match is_following(&whoami, remote_peer).await {
Ok(status) if status.as_str() == "false" => {
info!(
"Not currently following peer {}. No further action taken",
&remote_peer
);
Ok(())
}
Ok(status) if status.as_str() == "true" => {
info!("Unfollowing peer {}", &remote_peer);
match unfollow_peer(remote_peer).await {

View File

@ -103,6 +103,13 @@ pub async fn follow_if_not_following(remote_peer: &str) -> Result<(), String> {
pub async fn unfollow_if_following(remote_peer: &str) -> Result<(), String> {
if let Ok(whoami) = whoami().await {
match is_following(&whoami, remote_peer).await {
Ok(status) if status.as_str() == "false" => {
info!(
"Not currently following peer {}. No further action taken",
&remote_peer
);
Ok(())
}
Ok(status) if status.as_str() == "true" => {
info!("Unfollowing peer {}", &remote_peer);
match unfollow_peer(remote_peer).await {