Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use queue to update contacts with carddav #5575

Merged
merged 6 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 9 additions & 37 deletions app/Http/Controllers/DAV/Backend/CardDAV/CardDAVBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace App\Http\Controllers\DAV\Backend\CardDAV;

use Sabre\DAV;
use Illuminate\Support\Arr;
use App\Jobs\Dav\UpdateVCard;
use App\Models\Contact\Contact;
use App\Models\Account\AddressBook;
use App\Services\VCard\ExportVCard;
use App\Services\VCard\ImportVCard;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use Sabre\DAV\Server as SabreServer;
use Sabre\CardDAV\Backend\SyncSupport;
Expand All @@ -19,6 +19,7 @@
use App\Http\Controllers\DAV\Backend\IDAVBackend;
use App\Http\Controllers\DAV\Backend\SyncDAVBackend;
use App\Http\Controllers\DAV\DAVACL\PrincipalBackend;
use App\Services\DavClient\Utils\Model\ContactUpdateDto;

class CardDAVBackend extends AbstractBackend implements SyncSupport, IDAVBackend
{
Expand Down Expand Up @@ -185,13 +186,7 @@ public function prepareCard($contact): array
$carddata = $contact->vcard;
try {
if (empty($carddata)) {
$vcard = app(ExportVCard::class)
->execute([
'account_id' => $this->user->account_id,
'contact_id' => $contact->id,
]);

$carddata = $vcard->serialize();
$carddata = $this->refreshObject($contact);
}

return [
Expand Down Expand Up @@ -369,36 +364,13 @@ public function createCard($addressBookId, $cardUri, $cardData)
*/
public function updateCard($addressBookId, $cardUri, $cardData): ?string
{
$contact_id = null;
if ($cardUri) {
$contact = $this->getObject($addressBookId, $cardUri);
$dto = new ContactUpdateDto($cardUri, '"'.md5($cardData).'"', $cardData);

if ($contact) {
$contact_id = $contact->id;
}
}
$job = new UpdateVCard($this->user, $addressBookId, $dto);

try {
$result = app(ImportVCard::class)
->execute([
'account_id' => $this->user->account_id,
'user_id' => $this->user->id,
'contact_id' => $contact_id,
'entry' => $cardData,
'behaviour' => ImportVCard::BEHAVIOUR_REPLACE,
'addressBookName' => $addressBookId == $this->backendUri() ? null : $addressBookId,
]);

if (! Arr::has($result, 'error')) {
$contact = Contact::where('account_id', $this->user->account_id)
->find($result['contact_id']);

return '"'.md5($contact->vcard).'"';
}
} catch (\Exception $e) {
Log::debug(__CLASS__.' updateCard: '.(string) $e, [$e]);
throw $e;
}
Bus::batch([$job])
->allowFailures()
->dispatch();

return null;
}
Expand Down
61 changes: 57 additions & 4 deletions app/Jobs/Dav/UpdateVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace App\Jobs\Dav;

use App\Models\User\User;
use Illuminate\Support\Arr;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use App\Models\Contact\Contact;
use App\Services\VCard\ImportVCard;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
Expand Down Expand Up @@ -57,13 +60,63 @@ public function handle(): void
return;
}

Log::info(__CLASS__.' update '.$this->contact->uri);
$newtag = $this->updateCard($this->addressBookName, $this->contact->uri, $this->contact->card);

if ($newtag !== $this->contact->etag) {
Log::warning(__CLASS__.' wrong etag when updating contact. Expected '.$this->contact->etag.', get '.$newtag, [
'contacturl' => $this->contact->uri,
'carddata' => $this->contact->card,
]);
}
}

/**
* Update the contact with the carddata.
*
* @param mixed $addressBookId
* @param string $cardUri
* @param string $cardData
* @return string|null
*/
private function updateCard($addressBookId, $cardUri, $cardData): ?string
{
$backend = app(CardDAVBackend::class)->init($this->user);
$newtag = $backend->updateCard($this->addressBookName, $this->contact->uri, $this->contact->card);

if ($newtag !== $this->contact->etag) {
Log::warning(__CLASS__.' wrong etag when updating contact. Expected '.$this->contact->etag.', get '.$newtag);
$contact_id = null;
if ($cardUri) {
$contact = $backend->getObject($addressBookId, $cardUri);

if ($contact) {
$contact_id = $contact->id;
}
}

try {
$result = app(ImportVCard::class)
->execute([
'account_id' => $this->user->account_id,
'user_id' => $this->user->id,
'contact_id' => $contact_id,
'entry' => $cardData,
'behaviour' => ImportVCard::BEHAVIOUR_REPLACE,
'addressBookName' => $addressBookId === $backend->backendUri() ? null : $addressBookId,
]);

if (! Arr::has($result, 'error')) {
$contact = Contact::where('account_id', $this->user->account_id)
->find($result['contact_id']);

return '"'.md5($contact->vcard).'"';
}
} catch (\Exception $e) {
Log::debug(__CLASS__.' updateCard: '.(string) $e, [
$e,
'contacturl' => $cardUri,
'carddata' => $cardData,
]);
throw $e;
}

return null;
}
}
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sonar.organization=monicahq
sonar.sources=app,bootstrap,config,database,public,resources,routes
sonar.exclusions=bootstrap/cache/*,public/vendor/**,resources/lang/**
sonar.tests=tests
sonar.coverage.exclusions=routes/*.php,config/*.php,bootstrap/**,resources/**/*.php,database/factories/**/*.php,database/migrations/*.php,public/*.php,resources/**/*.vue,resources/**/*.js
sonar.coverage.exclusions=routes/*.php,config/**/*.php,bootstrap/**,resources/**/*.php,database/**/*.php,public/*.php,resources/**/*.vue,resources/**/*.js
sonar.cpd.exclusions=routes/*.php,config/*.php,bootstrap/**,resources/**/*.php,database/**/*.php

# Encoding of the source code. Default is default system encoding
Expand Down
2 changes: 1 addition & 1 deletion tests/Api/DAV/VCardContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public function test_carddav_update_existing_contact_no_modify()

$response->assertStatus(204);
$response->assertHeader('X-Sabre-Version');
$response->assertHeader('ETag');
//$response->assertHeader('ETag'); // etag no more sent
}

public function test_carddav_contacts_report_version4()
Expand Down