From 90d0a6870e79fb67ee4f28e953a86851a5bc4fb2 Mon Sep 17 00:00:00 2001 From: silverpill Date: Thu, 2 Jun 2022 00:47:04 +0000 Subject: [PATCH] Refactor unfollow_account view --- src/mastodon_api/accounts/views.rs | 47 ++++++++++------------------- src/models/relationships/queries.rs | 29 ++++++++++-------- 2 files changed, 32 insertions(+), 44 deletions(-) diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index 04557ff..e07299b 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -32,7 +32,6 @@ use crate::models::profiles::types::{IdentityProof, ProfileUpdateData}; use crate::models::relationships::queries::{ create_follow_request, follow, - get_follow_request_by_path, get_followers, get_following, hide_replies, @@ -374,37 +373,23 @@ async fn unfollow_account( let db_client = &mut **get_database_client(&db_pool).await?; let current_user = get_current_user(db_client, auth.token()).await?; let target = get_profile_by_id(db_client, &account_id).await?; - if let Some(remote_actor) = target.actor_json { - // Get follow request ID then unfollow and delete it - match get_follow_request_by_path( - db_client, - ¤t_user.id, - &target.id, - ).await { - Ok(follow_request) => { - unfollow( - db_client, - ¤t_user.id, - &target.id, - ).await?; - // Federate - prepare_undo_follow( - config.instance(), - ¤t_user, - &remote_actor, - &follow_request.id, - ).spawn_deliver(); - }, - Err(DatabaseError::NotFound(_)) => (), // not following - Err(other_error) => return Err(other_error.into()), - }; - } else { - match unfollow(db_client, ¤t_user.id, &target.id).await { - Ok(_) => (), - Err(DatabaseError::NotFound(_)) => (), // not following - Err(other_error) => return Err(other_error.into()), - }; + match unfollow(db_client, ¤t_user.id, &target.id).await { + Ok(Some(follow_request_id)) => { + // Remote follow + let remote_actor = target.actor_json + .ok_or(HttpError::InternalError)?; + prepare_undo_follow( + config.instance(), + ¤t_user, + &remote_actor, + &follow_request_id, + ).spawn_deliver(); + }, + Ok(None) => (), // local follow + Err(DatabaseError::NotFound(_)) => (), // not following + Err(other_error) => return Err(other_error.into()), }; + let relationship = get_relationship( db_client, ¤t_user.id, diff --git a/src/models/relationships/queries.rs b/src/models/relationships/queries.rs index 40a78e9..a09d084 100644 --- a/src/models/relationships/queries.rs +++ b/src/models/relationships/queries.rs @@ -101,7 +101,7 @@ pub async fn unfollow( db_client: &mut impl GenericClient, source_id: &Uuid, target_id: &Uuid, -) -> Result<(), DatabaseError> { +) -> Result, DatabaseError> { let transaction = db_client.transaction().await?; let deleted_count = transaction.execute( " @@ -119,7 +119,7 @@ pub async fn unfollow( source_id, target_id, ).await?; - if !relationship_deleted && !follow_request_deleted { + if !relationship_deleted && follow_request_deleted.is_none() { return Err(DatabaseError::NotFound("relationship")); }; if relationship_deleted { @@ -128,7 +128,7 @@ pub async fn unfollow( update_following_count(&transaction, source_id, -1).await?; } transaction.commit().await?; - Ok(()) + Ok(follow_request_deleted) } pub async fn create_follow_request( @@ -203,16 +203,20 @@ async fn delete_follow_request( db_client: &impl GenericClient, source_id: &Uuid, target_id: &Uuid, -) -> Result { - let deleted_count = db_client.execute( +) -> Result, DatabaseError> { + let maybe_row = db_client.query_opt( " DELETE FROM follow_request WHERE source_id = $1 AND target_id = $2 + RETURNING id ", &[&source_id, &target_id], ).await?; - let is_success = deleted_count > 0; - Ok(is_success) + let maybe_request_id = if let Some(row) = maybe_row { + let request_id: Uuid = row.try_get("id")?; + Some(request_id) + } else { None }; + Ok(maybe_request_id) } pub async fn get_follow_request_by_id( @@ -500,12 +504,11 @@ mod tests { .await.unwrap(); assert_eq!(following[0].id, target.id); // Unfollow - unfollow(db_client, &source.id, &target.id).await.unwrap(); - let follow_request_result = get_follow_request_by_path( - db_client, - &source.id, - &target.id, - ).await; + let follow_request_id = unfollow(db_client, &source.id, &target.id) + .await.unwrap().unwrap(); + assert_eq!(follow_request_id, follow_request.id); + let follow_request_result = + get_follow_request_by_id(db_client, &follow_request_id).await; assert!(matches!( follow_request_result, Err(DatabaseError::NotFound("follow request")),