From 27b2a325720354647e068a93bba5442005d0b032 Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:01:38 +0100 Subject: [PATCH 01/13] missing character in use statement dependency --- part_2_subscribe_form/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/part_2_subscribe_form/README.md b/part_2_subscribe_form/README.md index f14fe48..d6033fc 100644 --- a/part_2_subscribe_form/README.md +++ b/part_2_subscribe_form/README.md @@ -168,7 +168,7 @@ Then add the subscription route handlers to the existing code in `src/routes.rs` ```rust use log::info; -use rocket::{form::Form, get, post, response::Redirect, uri, FromFor} +use rocket::{form::Form, get, post, response::Redirect, uri, FromForm} #[derive(FromForm)] pub struct PeerForm { From 5c0d4726c0e611a2d49314730def4473139f1cde Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:03:14 +0100 Subject: [PATCH 02/13] remove duplicate route mounting --- part_2_subscribe_form/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/part_2_subscribe_form/README.md b/part_2_subscribe_form/README.md index d6033fc..4768100 100644 --- a/part_2_subscribe_form/README.md +++ b/part_2_subscribe_form/README.md @@ -105,7 +105,7 @@ use rocket_dyn_templates::Template; async fn rocket() -> _ { rocket::build() .attach(Template::fairing()) - .mount("/", routes![home, subscribe_form, unsubscribe_form]) + .mount("/", routes![home]) } ``` From 66a5f8b58d444d47e8f9c8343d078eae50dfba4d Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:08:50 +0100 Subject: [PATCH 03/13] add instruction to declare utils module in main.rs --- part_2_subscribe_form/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/part_2_subscribe_form/README.md b/part_2_subscribe_form/README.md index 4768100..ed07907 100644 --- a/part_2_subscribe_form/README.md +++ b/part_2_subscribe_form/README.md @@ -244,6 +244,12 @@ pub fn validate_public_key(public_key: &str) -> Result<(), String> { } ``` +The `utils` module needs to registered in `main.rs`. Without this addition, the module will not be compiled and the `validate_public_key` function will not be available to the rest of our program. Add this line at the top of `src/main.rs`: + +```rust +mod utils; +``` + Now the validation function can be called from our subscribe / unsubscribe route handlers, allowing us to ensure the provided public key is valid before using it to make further RPC calls to the sbot: `src/routes.rs` From 59947f6166054a30b387ca0307368a79c2f17452 Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:11:14 +0100 Subject: [PATCH 04/13] add missing RelationshipQuery import --- part_2_subscribe_form/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/part_2_subscribe_form/README.md b/part_2_subscribe_form/README.md index ed07907..2bd1c48 100644 --- a/part_2_subscribe_form/README.md +++ b/part_2_subscribe_form/README.md @@ -345,6 +345,8 @@ In order to do this using the `golgi` RPC library, we have to construct a `Relat `src/sbot.rs` ```rust +use golgi::api::friends::RelationshipQuery; + pub async fn is_following(public_key_a: &str, public_key_b: &str) -> Result { let mut sbot = init_sbot().await?; From 3946f2f7dcc67fd0fa37acf2c4574c79d464fc87 Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:17:00 +0100 Subject: [PATCH 05/13] add instructions to include sled and xdg dependencies --- part_3_database_follows/README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/part_3_database_follows/README.md b/part_3_database_follows/README.md index 034f988..0772bf3 100644 --- a/part_3_database_follows/README.md +++ b/part_3_database_follows/README.md @@ -62,7 +62,14 @@ impl Database { } ``` -The initialisation method requires a `Path` in order to open / create the database. We can use the [xdg](https://crates.io/crates/xdg) crate to generate a path using the XDG Base Directory specification. Open `src/main.rs` and add the following code: +The initialisation method requires a `Path` in order to open / create the database. We can use the [xdg](https://crates.io/crates/xdg) crate to generate a path using the XDG Base Directory specification. Add the dependencies for `sled` and `xdg` to `Cargo.toml`: + +```toml +sled = "0.34" +xdg = "2.4.1" +``` + +Now open `src/main.rs` and add the following code: ```rust mod db; From 473e9ac22519e44afa8c798bbc712c45284b0abf Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:19:50 +0100 Subject: [PATCH 06/13] fix semicolon --- part_3_database_follows/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/part_3_database_follows/README.md b/part_3_database_follows/README.md index 0772bf3..5855854 100644 --- a/part_3_database_follows/README.md +++ b/part_3_database_follows/README.md @@ -74,7 +74,7 @@ Now open `src/main.rs` and add the following code: ```rust mod db; -use xdg::BaseDirectories. +use xdg::BaseDirectories; use crate::{db::Database, routes::*}; From f8009163dd1db61208f32aa9964d93995ce2ae9a Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:20:34 +0100 Subject: [PATCH 07/13] add missing Path import --- part_3_database_follows/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/part_3_database_follows/README.md b/part_3_database_follows/README.md index 5855854..a2d98d4 100644 --- a/part_3_database_follows/README.md +++ b/part_3_database_follows/README.md @@ -35,6 +35,8 @@ We're going to use [sled](https://sled.rs/) in order to store the data used by o `src/db.rs` ```rust +use std::path::Path; + use sled::{Db, Tree}; #[derive(Clone)] From c42af06f02e72b254998f657f94f2d194beba53b Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:25:46 +0100 Subject: [PATCH 08/13] add instruction to import serde --- part_3_database_follows/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/part_3_database_follows/README.md b/part_3_database_follows/README.md index a2d98d4..ba537c5 100644 --- a/part_3_database_follows/README.md +++ b/part_3_database_follows/README.md @@ -118,6 +118,14 @@ pub struct Peer { } ``` +Before our new code will compile we need to add `serde` to our manifest file. Serde is used to *ser*ialize and *de*serialize data (like our `Peer` struct defined above). + +`Cargo.toml` + +```toml +serde = "1" +``` + In addition to the datastructure itself, we'll implement a couple of methods to be able to create and modify instances of the `struct`. `src/db.rs` From 6f1536a09d0c18a1aea817784d4036795c58c7bc Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:28:24 +0100 Subject: [PATCH 09/13] add instruction to import bincode dependency --- part_3_database_follows/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/part_3_database_follows/README.md b/part_3_database_follows/README.md index ba537c5..a1a28c7 100644 --- a/part_3_database_follows/README.md +++ b/part_3_database_follows/README.md @@ -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 From 504ca0807679554d9cfd7060c25a06094410adee Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:31:44 +0100 Subject: [PATCH 10/13] fix premature use of --- part_3_database_follows/README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/part_3_database_follows/README.md b/part_3_database_follows/README.md index a1a28c7..4dc487a 100644 --- a/part_3_database_follows/README.md +++ b/part_3_database_follows/README.md @@ -220,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), } } _ => (), From 61f0883841d4075d36b508a233d10314602913be Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:32:52 +0100 Subject: [PATCH 11/13] fix use of incorrect macro --- part_3_database_follows/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/part_3_database_follows/README.md b/part_3_database_follows/README.md index 4dc487a..53a4c3b 100644 --- a/part_3_database_follows/README.md +++ b/part_3_database_follows/README.md @@ -292,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) From 465919792f7e72bc0ac1b469a0e620125a6712b2 Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:37:22 +0100 Subject: [PATCH 12/13] don't treat already-not-following as error --- part_3_database_follows/README.md | 8 ++++++++ part_3_database_follows/src/sbot.rs | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/part_3_database_follows/README.md b/part_3_database_follows/README.md index 53a4c3b..14fb638 100644 --- a/part_3_database_follows/README.md +++ b/part_3_database_follows/README.md @@ -323,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 { diff --git a/part_3_database_follows/src/sbot.rs b/part_3_database_follows/src/sbot.rs index 32300be..c874c85 100644 --- a/part_3_database_follows/src/sbot.rs +++ b/part_3_database_follows/src/sbot.rs @@ -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 { From b4281db1f2ef80bad547a2373269fdd7e47c3501 Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 2 Oct 2022 17:41:15 +0100 Subject: [PATCH 13/13] add message of thanks to daan --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c579d97..95c5b72 100644 --- a/README.md +++ b/README.md @@ -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/).