Skip to content
Prev Previous commit
Next Next commit
feat(cardav): support result truncation for addressbook federation
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 authored and kesselb committed Jul 16, 2025
commit b98407244cd2126255b4cc99f6b10d7d1dd9f8d0
19 changes: 6 additions & 13 deletions apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCP\IDBConnection;
use OCP\IUserManager;
use PDO;
use Psr\Log\LoggerInterface;
use Sabre\CardDAV\Backend\BackendInterface;
use Sabre\CardDAV\Backend\SyncSupport;
use Sabre\CardDAV\Plugin;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function __construct(
private IUserManager $userManager,
private IEventDispatcher $dispatcher,
private Sharing\Backend $sharingBackend,
private LoggerInterface $logger,
private IConfig $config,
) {
}
Expand Down Expand Up @@ -897,10 +899,10 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,
$result['result_truncated'] = false;
$result['added'] = [];
} else {
$lastID = $values[array_key_last($values)]['id'];
$lastID = end($values)['id'];
$result['added'] = array_column($values, 'uri');
$result['syncToken'] = count($result['added']) >= $limit ? "init_{$lastID}_$initialSyncToken" : $initialSyncToken ;
$result['result_truncated'] = count($result['added']) >= $limit;
$result['syncToken'] = count($result['added']) === $limit ? "init_{$lastID}_$initialSyncToken" : $initialSyncToken ;
$result['result_truncated'] = count($result['added']) === $limit;
}
} elseif ($syncToken) {
$qb = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -931,11 +933,6 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,
$result['syncToken'] = $row['synctoken'];
}
$stmt->closeCursor();

// No changes found, use current token
if (empty($changes)) {
$result['syncToken'] = $currentToken;
}

foreach ($changes as $uri => $operation) {
switch ($operation) {
Expand All @@ -961,12 +958,8 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,
$qb->setMaxResults($limit);
$stmt = $qb->executeQuery();
$values = $stmt->fetchAll(\PDO::FETCH_ASSOC);
if (empty($values)) {
$result['added'] = [];
return $result;
}
$lastID = $values[array_key_last($values)]['id'];
if (count($values) >= $limit) {
if (count(array_values($values)) >= $limit) {
$result['syncToken'] = 'init_' . $lastID . '_' . $currentToken;
$result['result_truncated'] = true;
}
Expand Down
3 changes: 3 additions & 0 deletions apps/dav/lib/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public function __construct() {
);

$contactsSharingBackend = Server::get(\OCA\DAV\CardDAV\Sharing\Backend::class);
$logger = Server::get(\Psr\Log\LoggerInterface::class);
$config = Server::get(IConfig::class);

$pluginManager = new PluginManager(\OC::$server, Server::get(IAppManager::class));
Expand All @@ -141,6 +142,7 @@ public function __construct() {
$userManager,
$dispatcher,
$contactsSharingBackend,
$logger,
$config
);
$usersAddressBookRoot = new AddressBookRoot($userPrincipalBackend, $usersCardDavBackend, $pluginManager, $userSession->getUser(), $groupManager, 'principals/users');
Expand All @@ -152,6 +154,7 @@ public function __construct() {
$userManager,
$dispatcher,
$contactsSharingBackend,
$logger,
$config
);
$systemAddressBookRoot = new AddressBookRoot(new SystemPrincipalBackend(), $systemCardDavBackend, $pluginManager, $userSession->getUser(), $groupManager, 'principals/system');
Expand Down
1 change: 0 additions & 1 deletion apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public function __construct(

$this->server->addPlugin(new ExceptionLoggerPlugin('webdav', $logger));
$this->server->addPlugin(new LockPlugin());
$this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());

// acl
$acl = new DavAclPlugin();
Expand Down
5 changes: 5 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@
*/
'carddav_sync_request_truncation' => 50000,

/**
* The limit applied to the synchronization report request, e.g. federated system address books (as run by `occ federation:sync-addressbooks`).
*/
'carddav_sync_request_truncation' => 50000,

/**
* `true` enables a relaxed session timeout, where the session timeout would no longer be
* handled by Nextcloud but by either the PHP garbage collection or the expiration of
Expand Down