Skip to content

Commit 4dd6e4f

Browse files
authored
Merge pull request #54276 from nextcloud/backport/50092/stable31
[stable31] feat(cardav): support result truncation for addressbook federation
2 parents a211af9 + 481aaf1 commit 4dd6e4f

File tree

11 files changed

+168
-50
lines changed

11 files changed

+168
-50
lines changed

apps/dav/appinfo/v1/carddav.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
\OC::$server->getUserManager(),
5757
\OC::$server->get(IEventDispatcher::class),
5858
\OC::$server->get(\OCA\DAV\CardDAV\Sharing\Backend::class),
59+
\OC::$server->get(OCP\IConfig::class),
60+
5961
);
6062

6163
$debugging = \OC::$server->getConfig()->getSystemValue('debug', false);

apps/dav/lib/CardDAV/AddressBook.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace OCA\DAV\CardDAV;
99

1010
use OCA\DAV\DAV\Sharing\IShareable;
11-
use OCA\DAV\Exception\UnsupportedLimitOnInitialSyncException;
1211
use OCP\DB\Exception;
1312
use OCP\IL10N;
1413
use OCP\Server;
@@ -234,9 +233,6 @@ private function canWrite(): bool {
234233
}
235234

236235
public function getChanges($syncToken, $syncLevel, $limit = null) {
237-
if (!$syncToken && $limit) {
238-
throw new UnsupportedLimitOnInitialSyncException();
239-
}
240236

241237
return parent::getChanges($syncToken, $syncLevel, $limit);
242238
}

apps/dav/lib/CardDAV/CardDavBackend.php

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use OCP\DB\Exception;
2424
use OCP\DB\QueryBuilder\IQueryBuilder;
2525
use OCP\EventDispatcher\IEventDispatcher;
26+
use OCP\IConfig;
2627
use OCP\IDBConnection;
2728
use OCP\IUserManager;
2829
use PDO;
@@ -59,6 +60,7 @@ public function __construct(
5960
private IUserManager $userManager,
6061
private IEventDispatcher $dispatcher,
6162
private Sharing\Backend $sharingBackend,
63+
private IConfig $config,
6264
) {
6365
}
6466

@@ -850,6 +852,8 @@ public function deleteCard($addressBookId, $cardUri) {
850852
* @return array
851853
*/
852854
public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) {
855+
$maxLimit = $this->config->getSystemValueInt('carddav_sync_request_truncation', 2500);
856+
$limit = ($limit === null) ? $maxLimit : min($limit, $maxLimit);
853857
// Current synctoken
854858
return $this->atomic(function () use ($addressBookId, $syncToken, $syncLevel, $limit) {
855859
$qb = $this->db->getQueryBuilder();
@@ -872,10 +876,35 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,
872876
'modified' => [],
873877
'deleted' => [],
874878
];
875-
876-
if ($syncToken) {
879+
if (str_starts_with($syncToken, 'init_')) {
880+
$syncValues = explode('_', $syncToken);
881+
$lastID = $syncValues[1];
882+
$initialSyncToken = $syncValues[2];
877883
$qb = $this->db->getQueryBuilder();
878-
$qb->select('uri', 'operation')
884+
$qb->select('id', 'uri')
885+
->from('cards')
886+
->where(
887+
$qb->expr()->andX(
888+
$qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)),
889+
$qb->expr()->gt('id', $qb->createNamedParameter($lastID)))
890+
)->orderBy('id')
891+
->setMaxResults($limit);
892+
$stmt = $qb->executeQuery();
893+
$values = $stmt->fetchAll(\PDO::FETCH_ASSOC);
894+
$stmt->closeCursor();
895+
if (count($values) === 0) {
896+
$result['syncToken'] = $initialSyncToken;
897+
$result['result_truncated'] = false;
898+
$result['added'] = [];
899+
} else {
900+
$lastID = $values[array_key_last($values)]['id'];
901+
$result['added'] = array_column($values, 'uri');
902+
$result['syncToken'] = count($result['added']) >= $limit ? "init_{$lastID}_$initialSyncToken" : $initialSyncToken ;
903+
$result['result_truncated'] = count($result['added']) >= $limit;
904+
}
905+
} elseif ($syncToken) {
906+
$qb = $this->db->getQueryBuilder();
907+
$qb->select('uri', 'operation', 'synctoken')
879908
->from('addressbookchanges')
880909
->where(
881910
$qb->expr()->andX(
@@ -885,22 +914,31 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,
885914
)
886915
)->orderBy('synctoken');
887916

888-
if (is_int($limit) && $limit > 0) {
917+
if ($limit > 0) {
889918
$qb->setMaxResults($limit);
890919
}
891920

892921
// Fetching all changes
893922
$stmt = $qb->executeQuery();
923+
$rowCount = $stmt->rowCount();
894924

895925
$changes = [];
926+
$highestSyncToken = 0;
896927

897928
// This loop ensures that any duplicates are overwritten, only the
898929
// last change on a node is relevant.
899930
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
900931
$changes[$row['uri']] = $row['operation'];
932+
$highestSyncToken = $row['synctoken'];
901933
}
934+
902935
$stmt->closeCursor();
903936

937+
// No changes found, use current token
938+
if (empty($changes)) {
939+
$result['syncToken'] = $currentToken;
940+
}
941+
904942
foreach ($changes as $uri => $operation) {
905943
switch ($operation) {
906944
case 1:
@@ -914,16 +952,43 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,
914952
break;
915953
}
916954
}
955+
956+
/*
957+
* The synctoken in oc_addressbooks is always the highest synctoken in oc_addressbookchanges for a given addressbook plus one (see addChange).
958+
*
959+
* For truncated results, it is expected that we return the highest token from the response, so the client can continue from the latest change.
960+
*
961+
* For non-truncated results, it is expected to return the currentToken. If we return the highest token, as with truncated results, the client will always think it is one change behind.
962+
*
963+
* Therefore, we differentiate between truncated and non-truncated results when returning the synctoken.
964+
*/
965+
if ($rowCount === $limit && $highestSyncToken < $currentToken) {
966+
$result['syncToken'] = $highestSyncToken;
967+
$result['result_truncated'] = true;
968+
}
917969
} else {
918970
$qb = $this->db->getQueryBuilder();
919-
$qb->select('uri')
971+
$qb->select('id', 'uri')
920972
->from('cards')
921973
->where(
922974
$qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId))
923975
);
924976
// No synctoken supplied, this is the initial sync.
977+
$qb->setMaxResults($limit);
925978
$stmt = $qb->executeQuery();
926-
$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
979+
$values = $stmt->fetchAll(\PDO::FETCH_ASSOC);
980+
if (empty($values)) {
981+
$result['added'] = [];
982+
return $result;
983+
}
984+
$lastID = $values[array_key_last($values)]['id'];
985+
if (count($values) >= $limit) {
986+
$result['syncToken'] = 'init_' . $lastID . '_' . $currentToken;
987+
$result['result_truncated'] = true;
988+
}
989+
990+
$result['added'] = array_column($values, 'uri');
991+
927992
$stmt->closeCursor();
928993
}
929994
return $result;

apps/dav/lib/CardDAV/SyncService.php

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Sabre\DAV\Xml\Response\MultiStatus;
2323
use Sabre\DAV\Xml\Service;
2424
use Sabre\VObject\Reader;
25+
use Sabre\Xml\ParseException;
2526
use function is_null;
2627

2728
class SyncService {
@@ -43,9 +44,10 @@ public function __construct(
4344
}
4445

4546
/**
47+
* @psalm-return list{0: ?string, 1: boolean}
4648
* @throws \Exception
4749
*/
48-
public function syncRemoteAddressBook(string $url, string $userName, string $addressBookUrl, string $sharedSecret, ?string $syncToken, string $targetBookHash, string $targetPrincipal, array $targetProperties): string {
50+
public function syncRemoteAddressBook(string $url, string $userName, string $addressBookUrl, string $sharedSecret, ?string $syncToken, string $targetBookHash, string $targetPrincipal, array $targetProperties): array {
4951
// 1. create addressbook
5052
$book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookHash, $targetProperties);
5153
$addressBookId = $book['id'];
@@ -83,7 +85,10 @@ public function syncRemoteAddressBook(string $url, string $userName, string $add
8385
}
8486
}
8587

86-
return $response['token'];
88+
return [
89+
$response['token'],
90+
$response['truncated'],
91+
];
8792
}
8893

8994
/**
@@ -127,7 +132,7 @@ public function ensureLocalSystemAddressBookExists(): ?array {
127132

128133
private function prepareUri(string $host, string $path): string {
129134
/*
130-
* The trailing slash is important for merging the uris together.
135+
* The trailing slash is important for merging the uris.
131136
*
132137
* $host is stored in oc_trusted_servers.url and usually without a trailing slash.
133138
*
@@ -158,7 +163,9 @@ private function prepareUri(string $host, string $path): string {
158163
}
159164

160165
/**
166+
* @return array{response: array<string, array<array-key, mixed>>, token: ?string, truncated: bool}
161167
* @throws ClientExceptionInterface
168+
* @throws ParseException
162169
*/
163170
protected function requestSyncReport(string $url, string $userName, string $addressBookUrl, string $sharedSecret, ?string $syncToken): array {
164171
$client = $this->clientService->newClient();
@@ -181,7 +188,7 @@ protected function requestSyncReport(string $url, string $userName, string $addr
181188
$body = $response->getBody();
182189
assert(is_string($body));
183190

184-
return $this->parseMultiStatus($body);
191+
return $this->parseMultiStatus($body, $addressBookUrl);
185192
}
186193

187194
protected function download(string $url, string $userName, string $sharedSecret, string $resourcePath): string {
@@ -219,22 +226,50 @@ private function buildSyncCollectionRequestBody(?string $syncToken): string {
219226
}
220227

221228
/**
222-
* @param string $body
223-
* @return array
224-
* @throws \Sabre\Xml\ParseException
229+
* @return array{response: array<string, array<array-key, mixed>>, token: ?string, truncated: bool}
230+
* @throws ParseException
225231
*/
226-
private function parseMultiStatus($body) {
227-
$xml = new Service();
228-
232+
private function parseMultiStatus(string $body, string $addressBookUrl): array {
229233
/** @var MultiStatus $multiStatus */
230-
$multiStatus = $xml->expect('{DAV:}multistatus', $body);
234+
$multiStatus = (new Service())->expect('{DAV:}multistatus', $body);
231235

232236
$result = [];
237+
$truncated = false;
238+
233239
foreach ($multiStatus->getResponses() as $response) {
234-
$result[$response->getHref()] = $response->getResponseProperties();
240+
$href = $response->getHref();
241+
if ($response->getHttpStatus() === '507' && $this->isResponseForRequestUri($href, $addressBookUrl)) {
242+
$truncated = true;
243+
} else {
244+
$result[$response->getHref()] = $response->getResponseProperties();
245+
}
235246
}
236247

237-
return ['response' => $result, 'token' => $multiStatus->getSyncToken()];
248+
return ['response' => $result, 'token' => $multiStatus->getSyncToken(), 'truncated' => $truncated];
249+
}
250+
251+
/**
252+
* Determines whether the provided response URI corresponds to the given request URI.
253+
*/
254+
private function isResponseForRequestUri(string $responseUri, string $requestUri): bool {
255+
/*
256+
* Example response uri:
257+
*
258+
* /remote.php/dav/addressbooks/system/system/system/
259+
* /cloud/remote.php/dav/addressbooks/system/system/system/ (when installed in a subdirectory)
260+
*
261+
* Example request uri:
262+
*
263+
* remote.php/dav/addressbooks/system/system/system
264+
*
265+
* References:
266+
* https://github.com/nextcloud/3rdparty/blob/e0a509739b13820f0a62ff9cad5d0fede00e76ee/sabre/dav/lib/DAV/Sync/Plugin.php#L172-L174
267+
* https://github.com/nextcloud/server/blob/b40acb34a39592070d8455eb91c5364c07928c50/apps/federation/lib/SyncFederationAddressBooks.php#L41
268+
*/
269+
return str_ends_with(
270+
rtrim($responseUri, '/'),
271+
rtrim($requestUri, '/')
272+
);
238273
}
239274

240275
/**

apps/dav/lib/CardDAV/SystemAddressbook.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99
namespace OCA\DAV\CardDAV;
1010

11-
use OCA\DAV\Exception\UnsupportedLimitOnInitialSyncException;
1211
use OCA\Federation\TrustedServers;
1312
use OCP\Accounts\IAccountManager;
1413
use OCP\IConfig;
@@ -212,14 +211,7 @@ public function getChild($name): Card {
212211
}
213212
return new Card($this->carddavBackend, $this->addressBookInfo, $obj);
214213
}
215-
216-
/**
217-
* @throws UnsupportedLimitOnInitialSyncException
218-
*/
219214
public function getChanges($syncToken, $syncLevel, $limit = null) {
220-
if (!$syncToken && $limit) {
221-
throw new UnsupportedLimitOnInitialSyncException();
222-
}
223215

224216
if (!$this->carddavBackend instanceof SyncSupport) {
225217
return null;

apps/dav/lib/RootCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public function __construct() {
132132
$userManager,
133133
$dispatcher,
134134
$contactsSharingBackend,
135+
$config
135136
);
136137
$usersAddressBookRoot = new AddressBookRoot($userPrincipalBackend, $usersCardDavBackend, $pluginManager, $userSession->getUser(), $groupManager, 'principals/users');
137138
$usersAddressBookRoot->disableListing = $disableListing;
@@ -142,6 +143,7 @@ public function __construct() {
142143
$userManager,
143144
$dispatcher,
144145
$contactsSharingBackend,
146+
$config
145147
);
146148
$systemAddressBookRoot = new AddressBookRoot(new SystemPrincipalBackend(), $systemCardDavBackend, $pluginManager, $userSession->getUser(), $groupManager, 'principals/system');
147149
$systemAddressBookRoot->disableListing = $disableListing;

0 commit comments

Comments
 (0)