From 3662d1a5585cef5d7ccb9bf18ef36addd24e37f7 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 26 Dec 2021 15:05:06 +0000 Subject: [PATCH] Don't call get_relationship in relationships::queries::(un)follow --- src/mastodon_api/accounts/views.rs | 23 +++++++++++++++-------- src/models/relationships/queries.rs | 12 +++++------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index b0d9d2d..2f76995 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -198,7 +198,7 @@ async fn follow( let profile = get_profile_by_id(db_client, &account_id).await?; let maybe_remote_actor = profile.remote_actor() .map_err(|_| HttpError::InternalError)?; - let relationship = if let Some(remote_actor) = maybe_remote_actor { + if let Some(remote_actor) = maybe_remote_actor { // Remote follow let request = follows::create_follow_request(db_client, ¤t_user.id, &profile.id).await?; let activity = create_activity_follow( @@ -208,10 +208,14 @@ async fn follow( &remote_actor.id, ); deliver_activity(&config, ¤t_user, activity, vec![remote_actor]); - follows::get_relationship(db_client, ¤t_user.id, &profile.id).await? } else { - follows::follow(db_client, ¤t_user.id, &profile.id).await? + follows::follow(db_client, ¤t_user.id, &profile.id).await?; }; + let relationship = follows::get_relationship( + db_client, + ¤t_user.id, + &profile.id, + ).await?; Ok(HttpResponse::Ok().json(relationship)) } @@ -227,14 +231,14 @@ async fn unfollow( let target_profile = get_profile_by_id(db_client, &account_id).await?; let maybe_remote_actor = target_profile.remote_actor() .map_err(|_| HttpError::InternalError)?; - let relationship = if let Some(remote_actor) = maybe_remote_actor { + if let Some(remote_actor) = maybe_remote_actor { // Remote follow let follow_request = follows::get_follow_request_by_path( db_client, ¤t_user.id, &target_profile.id, ).await?; - let relationship = follows::unfollow( + follows::unfollow( db_client, ¤t_user.id, &target_profile.id, @@ -247,11 +251,14 @@ async fn unfollow( &remote_actor.id, ); deliver_activity(&config, ¤t_user, activity, vec![remote_actor]); - // TODO: uncouple unfollow and get_relationship - relationship } else { - follows::unfollow(db_client, ¤t_user.id, &target_profile.id).await? + follows::unfollow(db_client, ¤t_user.id, &target_profile.id).await?; }; + let relationship = follows::get_relationship( + db_client, + ¤t_user.id, + &target_profile.id, + ).await?; Ok(HttpResponse::Ok().json(relationship)) } diff --git a/src/models/relationships/queries.rs b/src/models/relationships/queries.rs index 9038e27..bcad411 100644 --- a/src/models/relationships/queries.rs +++ b/src/models/relationships/queries.rs @@ -86,7 +86,7 @@ pub async fn follow( db_client: &mut impl GenericClient, source_id: &Uuid, target_id: &Uuid, -) -> Result { +) -> Result<(), DatabaseError> { let transaction = db_client.transaction().await?; transaction.execute( " @@ -99,17 +99,16 @@ pub async fn follow( update_following_count(&transaction, source_id, 1).await?; if target_profile.is_local() { create_follow_notification(&transaction, source_id, target_id).await?; - } - let relationship = get_relationship(&transaction, source_id, target_id).await?; + }; transaction.commit().await?; - Ok(relationship) + Ok(()) } pub async fn unfollow( db_client: &mut impl GenericClient, source_id: &Uuid, target_id: &Uuid, -) -> Result { +) -> Result<(), DatabaseError> { let transaction = db_client.transaction().await?; let deleted_count = transaction.execute( " @@ -133,9 +132,8 @@ pub async fn unfollow( update_follower_count(&transaction, target_id, -1).await?; update_following_count(&transaction, source_id, -1).await?; } - let relationship = get_relationship(&transaction, source_id, target_id).await?; transaction.commit().await?; - Ok(relationship) + Ok(()) } pub async fn create_follow_request(