From 8840697de0f47ab9752381450c1a5a55527be02a Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sun, 16 Jul 2023 22:08:39 +0200 Subject: [PATCH] update --- .../Services/SynchronizeAddressBook.php | 31 ++++++----- .../UpdateSubscriptionLocalSyncToken.php | 6 +-- .../Utils/AddressBookSynchronizer.php | 54 +++++++++---------- .../Services/Utils/Dav/DavClient.php | 3 +- 4 files changed, 50 insertions(+), 44 deletions(-) diff --git a/app/Domains/Contact/DavClient/Services/SynchronizeAddressBook.php b/app/Domains/Contact/DavClient/Services/SynchronizeAddressBook.php index d0bbf18c682..36cac613339 100644 --- a/app/Domains/Contact/DavClient/Services/SynchronizeAddressBook.php +++ b/app/Domains/Contact/DavClient/Services/SynchronizeAddressBook.php @@ -12,6 +12,8 @@ class SynchronizeAddressBook extends BaseService { + private AddressBookSubscription $subscription; + /** * Get the validation rules that apply to the service. */ @@ -28,20 +30,23 @@ public function execute(array $data): void { $this->validateRules($data); - // TODO: check if account is limited + $this->validate($data); - $subscription = AddressBookSubscription::findOrFail($data['addressbook_subscription_id']); + $force = Arr::get($data, 'force', false); - if ($subscription->user->account_id !== $data['account_id']) { - throw new ModelNotFoundException(); - } + $this->synchronize($force); + } - if (! $subscription->active) { + private function synchronize(bool $force) + { + if (! $this->subscription->active) { return; } try { - $this->sync($data, $subscription); + app(AddressBookSynchronizer::class) + ->withSubscription($this->subscription) + ->execute($force); } catch (ClientException $e) { Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [ 'body' => $e->hasResponse() ? $e->getResponse()->getBody() : null, @@ -50,12 +55,14 @@ public function execute(array $data): void } } - private function sync(array $data, AddressBookSubscription $subscription) + private function validate(array $data): void { - $force = Arr::get($data, 'force', false); + $this->subscription = AddressBookSubscription::findOrFail($data['addressbook_subscription_id']); + + if ($this->subscription->user->account_id !== $data['account_id']) { + throw new ModelNotFoundException(); + } - app(AddressBookSynchronizer::class) - ->withSubscription($subscription) - ->execute($force); + // TODO: check if account is limited } } diff --git a/app/Domains/Contact/DavClient/Services/UpdateSubscriptionLocalSyncToken.php b/app/Domains/Contact/DavClient/Services/UpdateSubscriptionLocalSyncToken.php index 39395593045..2e1d52656f3 100644 --- a/app/Domains/Contact/DavClient/Services/UpdateSubscriptionLocalSyncToken.php +++ b/app/Domains/Contact/DavClient/Services/UpdateSubscriptionLocalSyncToken.php @@ -32,9 +32,9 @@ public function execute(array $data): void */ private function updateSyncToken(AddressBookSubscription $subscription): void { - $backend = app(CardDAVBackend::class)->withUser($subscription->user); - - $token = $backend->getCurrentSyncToken($subscription->vault_id); + $token = app(CardDAVBackend::class) + ->withUser($subscription->user) + ->getCurrentSyncToken($subscription->vault_id); if ($token !== null) { $subscription->localSyncToken = $token->id; diff --git a/app/Domains/Contact/DavClient/Services/Utils/AddressBookSynchronizer.php b/app/Domains/Contact/DavClient/Services/Utils/AddressBookSynchronizer.php index 03dc5d2bd08..deeb458b6c8 100644 --- a/app/Domains/Contact/DavClient/Services/Utils/AddressBookSynchronizer.php +++ b/app/Domains/Contact/DavClient/Services/Utils/AddressBookSynchronizer.php @@ -26,13 +26,22 @@ public function execute(bool $force = false): void { $this->client = $this->subscription->getClient(); - $force ? $this->forcesync() : $this->sync(); + $batch = $force + ? $this->forcesync() + : $this->sync(); + + Bus::batch($batch) + ->then(fn () => app(UpdateSubscriptionLocalSyncToken::class)->execute([ + 'addressbook_subscription_id' => $this->subscription->id, + ])) + ->allowFailures() + ->dispatch(); } /** * Sync the address book. */ - private function sync() + private function sync(): Collection { // Get changes to sync $localChanges = $this->backend()->getChangesForAddressBook($this->subscription->vault_id, (string) $this->subscription->localSyncToken, 1); @@ -53,26 +62,19 @@ private function sync() ); } - Bus::batch($batch) - ->then(fn () => app(UpdateSubscriptionLocalSyncToken::class)->execute([ - 'addressbook_subscription_id' => $this->subscription->id, - ])) - ->allowFailures() - ->dispatch(); + return $batch; } /** * Sync the address book. */ - private function forcesync() + private function forcesync(): Collection { - $backend = $this->backend(); - // Get changes to sync - $localChanges = $backend->getChangesForAddressBook($this->subscription->vault_id, (string) $this->subscription->localSyncToken, 1); + $localChanges = $this->backend()->getChangesForAddressBook($this->subscription->vault_id, (string) $this->subscription->localSyncToken, 1); // Get current list of contacts - $localContacts = $backend->getObjects($this->subscription->vault_id); + $localContacts = $this->backend()->getObjects($this->subscription->vault_id); // Get distant changes to sync $distContacts = $this->getAllContactsEtag(); @@ -90,12 +92,7 @@ private function forcesync() ); } - Bus::batch($batch) - ->then(fn () => app(UpdateSubscriptionLocalSyncToken::class)->execute([ - 'addressbook_subscription_id' => $this->subscription->id, - ])) - ->allowFailures() - ->dispatch(); + return $batch; } /** @@ -103,13 +100,15 @@ private function forcesync() */ private function getDistantChanges(): Collection { - $etags = collect($this->getDistantEtags()); - $contacts = $etags->filter(fn ($contact, $href): bool => $this->filterDistantContacts($contact, $href) + $etags = $this->getDistantEtags(); + $data = collect($etags); + + $contacts = $data->filter(fn ($contact, $href): bool => $this->filterDistantContacts($contact, $href) ) ->map(fn (array $contact, string $href): ContactDto => new ContactDto($href, Arr::get($contact, 'properties.200.{DAV:}getetag')) ); - $deleted = $etags->filter(fn ($contact): bool => is_array($contact) && $contact['status'] === '404' + $deleted = $data->filter(fn ($contact): bool => is_array($contact) && $contact['status'] === '404' ) ->map(fn (array $contact, string $href): ContactDto => new ContactDeleteDto($href) ); @@ -138,16 +137,14 @@ private function filterDistantContacts(mixed $contact, string $href): bool */ private function getDistantEtags(): array { - if ($this->hasCapability('syncCollection')) { + return $this->hasCapability('syncCollection') // With sync-collection - return $this->callSyncCollectionWhenNeeded(); - } else { + ? $this->callSyncCollectionWhenNeeded() // With PROPFIND - return $this->client->propFind([ + : $this->client->propFind([ '{DAV:}getcontenttype', '{DAV:}getetag', ], 1); - } } /** @@ -197,7 +194,8 @@ private function getAllContactsEtag(): Collection return collect(); } - $data = collect($this->client->addressbookQuery('{DAV:}getetag')); + $query = $this->client->addressbookQuery('{DAV:}getetag'); + $data = collect($query); $updated = $data->filter(fn ($contact): bool => is_array($contact) && $contact['status'] === '200' ) diff --git a/app/Domains/Contact/DavClient/Services/Utils/Dav/DavClient.php b/app/Domains/Contact/DavClient/Services/Utils/Dav/DavClient.php index b1228884236..32865c1940c 100644 --- a/app/Domains/Contact/DavClient/Services/Utils/Dav/DavClient.php +++ b/app/Domains/Contact/DavClient/Services/Utils/Dav/DavClient.php @@ -355,7 +355,8 @@ public function getSupportedReportSet(array $options = []): array if (($prop = Arr::get($properties, $propName)) && is_array($prop)) { $prop = array_map(fn ($supportedReport) => $this->iterateOver($supportedReport, '{DAV:}supported-report', fn ($report) => $this->iterateOver($report, '{DAV:}report', fn ($type) => Arr::get($type, 'name') ) - ), $prop); + ), + $prop); } return $prop;