Refactor unfollow_account view
This commit is contained in:
parent
1c098cb6b2
commit
90d0a6870e
2 changed files with 32 additions and 44 deletions
|
@ -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,
|
||||
|
|
|
@ -101,7 +101,7 @@ pub async fn unfollow(
|
|||
db_client: &mut impl GenericClient,
|
||||
source_id: &Uuid,
|
||||
target_id: &Uuid,
|
||||
) -> Result<(), DatabaseError> {
|
||||
) -> Result<Option<Uuid>, 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<bool, DatabaseError> {
|
||||
let deleted_count = db_client.execute(
|
||||
) -> Result<Option<Uuid>, 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")),
|
||||
|
|
Loading…
Reference in a new issue