Skip to content

Commit

Permalink
refactor(dav): various modernizations of CardDAV SyncService
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Oct 26, 2023
1 parent 407b108 commit ef01811
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions apps/dav/lib/CardDAV/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
namespace OCA\DAV\CardDAV;

use OC\Accounts\AccountManager;
use OCP\AppFramework\Db\TTransactional;
use OCP\AppFramework\Http;
use OCP\IDBConnection;
Expand All @@ -42,6 +41,7 @@
use Sabre\DAV\Xml\Service;
use Sabre\HTTP\ClientHttpException;
use Sabre\VObject\Reader;
use Sabre\Xml\ParseException;
use function is_null;

class SyncService {
Expand Down Expand Up @@ -181,6 +181,9 @@ protected function download(string $url, string $userName, string $sharedSecret,
return $client->request('GET', $resourcePath);
}

/**
* @throws \DOMException
*/
private function buildSyncCollectionRequestBody(?string $syncToken): string {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
Expand All @@ -201,9 +204,9 @@ private function buildSyncCollectionRequestBody(?string $syncToken): string {
/**
* @param string $body
* @return array
* @throws \Sabre\Xml\ParseException
* @throws ParseException
*/
private function parseMultiStatus($body) {
private function parseMultiStatus(string $body): array {
$xml = new Service();

/** @var MultiStatus $multiStatus */
Expand All @@ -217,9 +220,6 @@ private function parseMultiStatus($body) {
return ['response' => $result, 'token' => $multiStatus->getSyncToken()];
}

/**
* @param IUser $user
*/
public function updateUser(IUser $user): void {
$systemAddressBook = $this->getLocalSystemAddressBook();
$addressBookId = $systemAddressBook['id'];
Expand All @@ -228,13 +228,12 @@ public function updateUser(IUser $user): void {
if ($user->isEnabled()) {
$this->atomic(function() use ($addressBookId, $cardId, $user) {
$card = $this->backend->getCard($addressBookId, $cardId);
$vCard = $this->converter->createCardFromUser($user);
if ($card === false) {
$vCard = $this->converter->createCardFromUser($user);
if ($vCard !== null) {
$this->backend->createCard($addressBookId, $cardId, $vCard->serialize(), false);
}
} else {
$vCard = $this->converter->createCardFromUser($user);
if (is_null($vCard)) {
$this->backend->deleteCard($addressBookId, $cardId);
} else {
Expand All @@ -247,21 +246,15 @@ public function updateUser(IUser $user): void {
}
}

/**
* @param IUser|string $userOrCardId
*/
public function deleteUser($userOrCardId) {
public function deleteUser(IUser|string $userOrCardId): void {
$systemAddressBook = $this->getLocalSystemAddressBook();
if ($userOrCardId instanceof IUser) {
$userOrCardId = self::getCardUri($userOrCardId);
}
$this->backend->deleteCard($systemAddressBook['id'], $userOrCardId);
}

/**
* @return array|null
*/
public function getLocalSystemAddressBook() {
public function getLocalSystemAddressBook(): ?array {
if (is_null($this->localSystemAddressBook)) {
$systemPrincipal = "principals/system/system";
$this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [
Expand All @@ -272,7 +265,7 @@ public function getLocalSystemAddressBook() {
return $this->localSystemAddressBook;
}

public function syncInstance(\Closure $progressCallback = null) {
public function syncInstance(\Closure $progressCallback = null): void {
$systemAddressBook = $this->getLocalSystemAddressBook();
$this->userManager->callForAllUsers(function ($user) use ($systemAddressBook, $progressCallback) {
$this->updateUser($user);
Expand Down

0 comments on commit ef01811

Please sign in to comment.