From 2cc672d26b33939a44d71a346c9f3726f4b951db Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Fri, 28 Apr 2023 12:32:18 +0200 Subject: fix(dav): Run system address book create-if-not-exists in transaction Signed-off-by: Christoph Wurst --- apps/dav/lib/CardDAV/SyncService.php | 67 ++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 26 deletions(-) (limited to 'apps/dav/lib/CardDAV') diff --git a/apps/dav/lib/CardDAV/SyncService.php b/apps/dav/lib/CardDAV/SyncService.php index da798c5768e..f3de07df214 100644 --- a/apps/dav/lib/CardDAV/SyncService.php +++ b/apps/dav/lib/CardDAV/SyncService.php @@ -1,4 +1,5 @@ backend = $backend; @@ -56,6 +65,7 @@ class SyncService { $this->logger = $logger; $this->converter = $converter; $this->certPath = ''; + $this->dbConnection = $dbConnection; } /** @@ -86,12 +96,14 @@ class SyncService { $cardUri = basename($resource); if (isset($status[200])) { $vCard = $this->download($url, $userName, $sharedSecret, $resource); - $existingCard = $this->backend->getCard($addressBookId, $cardUri); - if ($existingCard === false) { - $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); - } else { - $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); - } + $this->atomic(function() use ($addressBookId, $cardUri, $vCard) { + $existingCard = $this->backend->getCard($addressBookId, $cardUri); + if ($existingCard === false) { + $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); + } else { + $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); + } + }, $this->dbConnection); } else { $this->backend->deleteCard($addressBookId, $cardUri); } @@ -104,14 +116,15 @@ class SyncService { * @throws \Sabre\DAV\Exception\BadRequest */ public function ensureSystemAddressBookExists(string $principal, string $uri, array $properties): ?array { - $book = $this->backend->getAddressBooksByUri($principal, $uri); - if (!is_null($book)) { - return $book; - } - // FIXME This might break in clustered DB setup - $this->backend->createAddressBook($principal, $uri, $properties); + return $this->atomic(function() use ($principal, $uri, $properties) { + $book = $this->backend->getAddressBooksByUri($principal, $uri); + if (!is_null($book)) { + return $book; + } + $this->backend->createAddressBook($principal, $uri, $properties); - return $this->backend->getAddressBooksByUri($principal, $uri); + return $this->backend->getAddressBooksByUri($principal, $uri); + }, $this->dbConnection); } /** @@ -206,7 +219,7 @@ class SyncService { /** * @param IUser $user */ - public function updateUser(IUser $user) { + public function updateUser(IUser $user): void { $systemAddressBook = $this->getLocalSystemAddressBook(); $addressBookId = $systemAddressBook['id']; $name = $user->getBackendClassName(); @@ -214,20 +227,22 @@ class SyncService { $cardId = "$name:$userId.vcf"; if ($user->isEnabled()) { - $card = $this->backend->getCard($addressBookId, $cardId); - if ($card === false) { - $vCard = $this->converter->createCardFromUser($user); - if ($vCard !== null) { - $this->backend->createCard($addressBookId, $cardId, $vCard->serialize(), false); - } - } else { - $vCard = $this->converter->createCardFromUser($user); - if (is_null($vCard)) { - $this->backend->deleteCard($addressBookId, $cardId); + $this->atomic(function() use ($addressBookId, $cardId, $user) { + $card = $this->backend->getCard($addressBookId, $cardId); + if ($card === false) { + $vCard = $this->converter->createCardFromUser($user); + if ($vCard !== null) { + $this->backend->createCard($addressBookId, $cardId, $vCard->serialize(), false); + } } else { - $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); + $vCard = $this->converter->createCardFromUser($user); + if (is_null($vCard)) { + $this->backend->deleteCard($addressBookId, $cardId); + } else { + $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); + } } - } + }, $this->dbConnection); } else { $this->backend->deleteCard($addressBookId, $cardId); } -- cgit v1.2.3