unfollow peer when unsubscribing

This commit is contained in:
glyph 2022-08-11 14:27:45 +01:00
parent 643c1f6a1a
commit d67d0f566f
2 changed files with 33 additions and 9 deletions

View File

@ -163,7 +163,9 @@ pub async fn subscribe_form(
) -> Redirect { ) -> Redirect {
info!("Subscribing to peer {}", &peer.public_key); 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); info!("Public key {} is valid", &peer.public_key);
// Retrieve the name of the peer to which we are subscribing. // Retrieve the name of the peer to which we are subscribing.
@ -208,30 +210,45 @@ pub async fn subscribe_form(
&peer.public_key &peer.public_key
) )
} }
} else {
warn!("Public key {} is invalid", &peer.public_key);
} }
Redirect::to(uri!(home)) Redirect::to(uri!(home))
} }
#[post("/unsubscribe", data = "<peer>")] #[post("/unsubscribe", data = "<peer>")]
pub fn unsubscribe_form(db: &State<Database>, peer: Form<PeerForm>) -> Redirect { pub async fn unsubscribe_form(
db: &State<Database>,
whoami: &State<WhoAmI>,
peer: Form<PeerForm>,
) -> Redirect {
info!("Unsubscribing from peer {}", &peer.public_key); info!("Unsubscribing from peer {}", &peer.public_key);
if let Err(e) = utils::validate_public_key(&peer.public_key) { if let Err(e) = utils::validate_public_key(&peer.public_key) {
warn!("Public key {} is invalid: {}", &peer.public_key, e) warn!("Public key {} is invalid: {}", &peer.public_key, e)
} else { } else {
info!("Public key {} is valid", &peer.public_key); info!("Public key {} is valid", &peer.public_key);
match db.remove_peer(&peer.public_key) { if db.remove_peer(&peer.public_key).is_ok() {
Ok(_) => info!( info!(
"Removed peer {} from 'peers' database tree", "Removed peer {} from 'peers' database tree",
&peer.public_key &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", "Failed to remove peer {} from 'peers' database tree",
&peer.public_key &peer.public_key
), );
} }
} }

View File

@ -46,6 +46,13 @@ pub async fn follow_peer(public_key: &str) -> Result<String, String> {
sbot.follow(public_key).await.map_err(|e| e.to_string()) sbot.follow(public_key).await.map_err(|e| e.to_string())
} }
/// Unfollow a peer.
pub async fn unfollow_peer(public_key: &str) -> Result<String, String> {
let mut sbot = init_sbot().await?;
sbot.unfollow(public_key).await.map_err(|e| e.to_string())
}
/// Check follow status. /// Check follow status.
/// ///
/// Is peer A (`public_key_a`) following peer B (`public_key_b`)? /// Is peer A (`public_key_a`) following peer B (`public_key_b`)?