Skip to content

Commit

Permalink
fixup! feat: add example contact on first login
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Jan 20, 2025
1 parent b1ab0de commit 8260a53
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 48 deletions.
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
'OCA\\DAV\\Server' => $baseDir . '/../lib/Server.php',
'OCA\\DAV\\ServerFactory' => $baseDir . '/../lib/ServerFactory.php',
'OCA\\DAV\\Service\\AbsenceService' => $baseDir . '/../lib/Service/AbsenceService.php',
'OCA\\DAV\\Service\\DefaultContactService' => $baseDir . '/../lib/Service/DefaultContactService.php',
'OCA\\DAV\\Settings\\AvailabilitySettings' => $baseDir . '/../lib/Settings/AvailabilitySettings.php',
'OCA\\DAV\\Settings\\CalDAVSettings' => $baseDir . '/../lib/Settings/CalDAVSettings.php',
'OCA\\DAV\\SetupChecks\\NeedsSystemAddressBookSync' => $baseDir . '/../lib/SetupChecks/NeedsSystemAddressBookSync.php',
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Server' => __DIR__ . '/..' . '/../lib/Server.php',
'OCA\\DAV\\ServerFactory' => __DIR__ . '/..' . '/../lib/ServerFactory.php',
'OCA\\DAV\\Service\\AbsenceService' => __DIR__ . '/..' . '/../lib/Service/AbsenceService.php',
'OCA\\DAV\\Service\\DefaultContactService' => __DIR__ . '/..' . '/../lib/Service/DefaultContactService.php',
'OCA\\DAV\\Settings\\AvailabilitySettings' => __DIR__ . '/..' . '/../lib/Settings/AvailabilitySettings.php',
'OCA\\DAV\\Settings\\CalDAVSettings' => __DIR__ . '/..' . '/../lib/Settings/CalDAVSettings.php',
'OCA\\DAV\\SetupChecks\\NeedsSystemAddressBookSync' => __DIR__ . '/..' . '/../lib/SetupChecks/NeedsSystemAddressBookSync.php',
Expand Down
52 changes: 4 additions & 48 deletions apps/dav/lib/Listener/AddressbookListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
use OCA\DAV\Events\AddressBookDeletedEvent;
use OCA\DAV\Events\AddressBookShareUpdatedEvent;
use OCA\DAV\Events\AddressBookUpdatedEvent;
use OCP\App\IAppManager;
use OCA\DAV\Service\DefaultContactService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\AppData\IAppDataFactory;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Factory\UlidFactory;
use Throwable;
use function sprintf;

Expand All @@ -28,10 +26,7 @@ class AddressbookListener implements IEventListener {
public function __construct(
private ActivityBackend $activityBackend,
private LoggerInterface $logger,
private CardDavBackend $cardDav,
private IAppManager $appManager,
private UlidFactory $Uidfactory,
private IAppDataFactory $appDataFactory,
private DefaultContactService $defaultContactService,
) {
}

Expand All @@ -41,47 +36,8 @@ public function handle(Event $event): void {
$this->activityBackend->onAddressbookCreate(
$event->getAddressBookData()
);
$this->logger->error('omek zabour: ', ['ab' => $event->getAddressBookData()]);
if ($event->getAddressBookData()['uri'] === 'contacts') {
$appData = $this->appDataFactory->get('contacts');
try {
$folder = $appData->getFolder('defaultContact');
$defaultContactFile = $folder->getFile('defaultContact.vcf');
$vcard = $defaultContactFile->getContent();
} catch (\Exception $e) {
$this->logger->error('Couldn\'t get default contact file', ['exception' => $e]);
return;
}
$newUid = $this->Uidfactory->create()->toRfc4122();
$newRev = date('Ymd\THis\Z');


$vcard = (strpos($vcard, 'UID:') !== false) ? preg_replace(
'/UID:.*?(\r\n|\n)/',
"UID:$newUid\n",
$vcard
) : str_replace(
'END:VCARD',
"UID:$newUid\nEND:VCARD",
$vcard
);


// Add or update REV
$vcard = (strpos($vcard, 'REV:') !== false) ? preg_replace(
'/REV:.*?(\r\n|\n)/',
"REV:$newRev\n",
$vcard
) : str_replace(
'END:VCARD:',
"REV:$newRev\END:VCARD:",
$vcard
);
try {
$this->cardDav->createCard($event->getAddressBookData()['id'], 'default', $vcard, false);
} catch (\Exception $e) {
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
}
if ($event->getAddressBookData()['uri'] === CardDavBackend::PERSONAL_ADDRESSBOOK_URI) {
$this->defaultContactService->createDefaultContact((string)$event->getAddressBookId());
}

$this->logger->debug(
Expand Down
70 changes: 70 additions & 0 deletions apps/dav/lib/Service/DefaultContactService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\DAV\Service;

use OCA\DAV\CardDAV\CardDavBackend;
use OCP\App\IAppManager;
use OCP\Files\AppData\IAppDataFactory;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;

class DefaultContactService {
public function __construct(
private CardDavBackend $cardDav,
private IAppManager $appManager,
private IAppDataFactory $appDataFactory,
private LoggerInterface $logger,
) {
}

public function createDefaultContact(string $addressBookId): void {
$appData = $this->appDataFactory->get('contacts');
try {
$folder = $appData->getFolder('defaultContact');
$defaultContactFile = $folder->getFile('defaultContact.vcf');
$vcard = $defaultContactFile->getContent();
} catch (\Exception $e) {
$this->logger->error('Couldn\'t get default contact file', ['exception' => $e]);
return;
}

// Make sure the UId is unique
$newUid = Uuid::v4()->toRfc4122();
$newRev = date('Ymd\THis\Z');


$vcard = (strpos($vcard, 'UID:') !== false) ? preg_replace(
'/UID:.*?(\r\n|\n)/',
"UID:$newUid\n",
$vcard
) : str_replace(
'END:VCARD',
"UID:$newUid\nEND:VCARD",
$vcard
);

// Add or update REV
$vcard = (strpos($vcard, 'REV:') !== false) ? preg_replace(
'/REV:.*?(\r\n|\n)/',
"REV:$newRev\n",
$vcard
) : str_replace(
'END:VCARD:',
"REV:$newRev\END:VCARD:",
$vcard
);
try {
$this->cardDav->createCard($addressBookId, 'default', $vcard, false);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}

}
}

0 comments on commit 8260a53

Please sign in to comment.