Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Jul 16, 2023
1 parent 988d8e7 commit 8840697
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 44 deletions.
31 changes: 19 additions & 12 deletions app/Domains/Contact/DavClient/Services/SynchronizeAddressBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class SynchronizeAddressBook extends BaseService
{
private AddressBookSubscription $subscription;

/**
* Get the validation rules that apply to the service.
*/
Expand All @@ -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,
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -90,26 +92,23 @@ private function forcesync()
);
}

Bus::batch($batch)
->then(fn () => app(UpdateSubscriptionLocalSyncToken::class)->execute([
'addressbook_subscription_id' => $this->subscription->id,
]))
->allowFailures()
->dispatch();
return $batch;
}

/**
* Get distant changes to sync.
*/
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)
);
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8840697

Please sign in to comment.