From 6c09d04038c83292752577276667a530e2063e28 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Mon, 29 Jan 2024 10:54:21 +0000 Subject: [PATCH 1/2] fix: use atomic operations for count updates --- app/controllers/api/teams.php | 4 ++-- src/Appwrite/Platform/Workers/Deletes.php | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index d65a7c6a59..d3a7075bb5 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -533,8 +533,8 @@ App::post('/v1/teams/:teamId/memberships') } catch (Duplicate $th) { throw new Exception(Exception::TEAM_INVITE_ALREADY_EXISTS); } - $team->setAttribute('total', $team->getAttribute('total', 0) + 1); - $team = Authorization::skip(fn() => $dbForProject->updateDocument('teams', $team->getId(), $team)); + + Authorization::skip(fn() => $dbForProject->increaseDocumentAttribute('teams', $team->getId(), 'total', 1)); $dbForProject->deleteCachedDocument('users', $invitee->getId()); } else { diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 6375e92ca8..b5b2d82025 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -516,13 +516,8 @@ class Deletes extends Action if ($document->getAttribute('confirm')) { // Count only confirmed members $teamId = $document->getAttribute('teamId'); $team = $dbForProject->getDocument('teams', $teamId); - if (!$team->isEmpty()) { - $team = $dbForProject->updateDocument( - 'teams', - $teamId, - // Ensure that total >= 0 - $team->setAttribute('total', \max($team->getAttribute('total', 0) - 1, 0)) - ); + if (!$team->isEmpty() && $team->getAttribute('total', 0) > 0) { + $dbForProject->decreaseDocumentAttribute('teams', $teamId, 'total', 1); } } }); From 94c423c42990a7e7c5e712e8baece00bf5fb482b Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Mon, 29 Jan 2024 13:27:03 +0000 Subject: [PATCH 2/2] chore: add project details to messaging worker --- app/controllers/api/account.php | 2 ++ app/controllers/api/teams.php | 1 + 2 files changed, 3 insertions(+) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index be6ed68e05..411103c460 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1339,6 +1339,7 @@ App::post('/v1/account/sessions/phone') $queueForMessaging ->setRecipient($phone) ->setMessage($message) + ->setProject($project) ->trigger(); $queueForEvents->setPayload( @@ -2938,6 +2939,7 @@ App::post('/v1/account/verification/phone') $queueForMessaging ->setRecipient($user->getAttribute('phone')) ->setMessage($message) + ->setProject($project) ->trigger() ; diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index d3a7075bb5..a374c57cf1 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -641,6 +641,7 @@ App::post('/v1/teams/:teamId/memberships') $queueForMessaging ->setRecipient($phone) ->setMessage($message) + ->setProject($project) ->trigger(); } }