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 <hamzamahjoubi221@gmail.com>
  • Loading branch information
hamza221 committed Jan 22, 2025
1 parent b3b6ed6 commit 3af4fc4
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions apps/dav/lib/Service/DefaultContactService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,31 @@ public function createDefaultContact(string $addressBookId): void {
try {
$folder = $appData->getFolder('defaultContact');
$defaultContactFile = $folder->getFile('defaultContact.vcf');
$vcard = $defaultContactFile->getContent();
$data = $defaultContactFile->getContent();
} catch (\Exception $e) {
$this->logger->error('Couldn\'t get default contact file', ['exception' => $e]);
return;
}

// Make sure the UId is unique
// 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
);
$vcard = \Sabre\VObject\Reader::read($data, \Sabre\VObject\Reader::OPTION_FORGIVING);
$vcard->UID->setValue($newUid);
$vcard->REV->setValue($newRev);

// Level 3 means that the document is invalid
// https://sabre.io/vobject/vcard/#validating-vcard
$level3Warnings = array_filter($vcard->validate(), function ($warning) {
return $warning['level'] === 3;
});

if (!empty($level3Warnings)) {
$this->logger->error('Default contact is invalid', ['warnings' => $level3Warnings]);
return;
}
try {
$this->cardDav->createCard($addressBookId, 'default', $vcard, false);
$this->cardDav->createCard($addressBookId, 'default', $vcard->serialize(), false);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
Expand Down

0 comments on commit 3af4fc4

Please sign in to comment.