From 3781d38f95015bdc1a1cbb99bfedd732a957972d Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 10 Jun 2024 03:36:29 +0200 Subject: [PATCH] fix: dont over-decrement on follow undo --- upub/core/src/traits/process.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/upub/core/src/traits/process.rs b/upub/core/src/traits/process.rs index ea4388e..2e4eef4 100644 --- a/upub/core/src/traits/process.rs +++ b/upub/core/src/traits/process.rs @@ -335,21 +335,33 @@ pub async fn undo(_ctx: &crate::Context, activity: impl apb::Activity, tx: &Data ) .await? .ok_or(ProcessorError::Incomplete)?; + + // no pending relation to undo + let relation = crate::model::relation::Entity::find() + .filter(model::relation::Column::Follower.eq(internal_uid)) + .filter(model::relation::Column::Following.eq(internal_uid_following)) + .one(tx) + .await? + .ok_or(ProcessorError::AlreadyProcessed)?; + crate::model::relation::Entity::delete_many() .filter(crate::model::relation::Column::Follower.eq(internal_uid)) .filter(crate::model::relation::Column::Following.eq(internal_uid_following)) .exec(tx) .await?; - crate::model::actor::Entity::update_many() - .filter(crate::model::actor::Column::Internal.eq(internal_uid)) - .col_expr(crate::model::actor::Column::FollowingCount, Expr::col(crate::model::actor::Column::FollowingCount).sub(1)) - .exec(tx) - .await?; - crate::model::actor::Entity::update_many() - .filter(crate::model::actor::Column::Internal.eq(internal_uid_following)) - .col_expr(crate::model::actor::Column::FollowersCount, Expr::col(crate::model::actor::Column::FollowersCount).sub(1)) - .exec(tx) - .await?; + + if relation.accept.is_some() { + crate::model::actor::Entity::update_many() + .filter(crate::model::actor::Column::Internal.eq(internal_uid)) + .col_expr(crate::model::actor::Column::FollowingCount, Expr::col(crate::model::actor::Column::FollowingCount).sub(1)) + .exec(tx) + .await?; + crate::model::actor::Entity::update_many() + .filter(crate::model::actor::Column::Internal.eq(internal_uid_following)) + .col_expr(crate::model::actor::Column::FollowersCount, Expr::col(crate::model::actor::Column::FollowersCount).sub(1)) + .exec(tx) + .await?; + } }, _ => return Err(ProcessorError::Unprocessable(activity.id()?.to_string())), }