From d67d0f566f858be9f844c0252629e7ee3501d51d Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 11 Aug 2022 14:27:45 +0100 Subject: [PATCH] unfollow peer when unsubscribing --- src/routes.rs | 35 ++++++++++++++++++++++++++--------- src/sbot.rs | 7 +++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/routes.rs b/src/routes.rs index 264d869..79db946 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -163,7 +163,9 @@ pub async fn subscribe_form( ) -> Redirect { info!("Subscribing to peer {}", &peer.public_key); - if utils::validate_public_key(&peer.public_key).is_ok() { + if let Err(e) = utils::validate_public_key(&peer.public_key) { + warn!("Public key {} is invalid: {}", &peer.public_key, e) + } else { info!("Public key {} is valid", &peer.public_key); // Retrieve the name of the peer to which we are subscribing. @@ -208,30 +210,45 @@ pub async fn subscribe_form( &peer.public_key ) } - } else { - warn!("Public key {} is invalid", &peer.public_key); } Redirect::to(uri!(home)) } #[post("/unsubscribe", data = "")] -pub fn unsubscribe_form(db: &State, peer: Form) -> Redirect { +pub async fn unsubscribe_form( + db: &State, + whoami: &State, + peer: Form, +) -> Redirect { info!("Unsubscribing from peer {}", &peer.public_key); if let Err(e) = utils::validate_public_key(&peer.public_key) { warn!("Public key {} is invalid: {}", &peer.public_key, e) } else { info!("Public key {} is valid", &peer.public_key); - match db.remove_peer(&peer.public_key) { - Ok(_) => info!( + if db.remove_peer(&peer.public_key).is_ok() { + info!( "Removed peer {} from 'peers' database tree", &peer.public_key - ), - Err(_e) => warn!( + ); + match sbot::is_following(&whoami.0, &peer.public_key).await { + Ok(status) if status.as_str() == "true" => { + info!("Unfollowing peer {}", &peer.public_key); + match sbot::unfollow_peer(&peer.public_key).await { + Ok(_) => { + info!("Unfollowed peer {}", &peer.public_key); + } + Err(e) => warn!("Failed to unfollow peer {}: {}", &peer.public_key, e), + } + } + _ => (), + } + } else { + warn!( "Failed to remove peer {} from 'peers' database tree", &peer.public_key - ), + ); } } diff --git a/src/sbot.rs b/src/sbot.rs index 97c7d9b..8e19946 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -46,6 +46,13 @@ pub async fn follow_peer(public_key: &str) -> Result { sbot.follow(public_key).await.map_err(|e| e.to_string()) } +/// Unfollow a peer. +pub async fn unfollow_peer(public_key: &str) -> Result { + let mut sbot = init_sbot().await?; + + sbot.unfollow(public_key).await.map_err(|e| e.to_string()) +} + /// Check follow status. /// /// Is peer A (`public_key_a`) following peer B (`public_key_b`)?