Repository navigation
perf(dav): sync federated calendars with calendar-multiget #65944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| namespace OCA\DAV\CalDAV\Federation; | ||
|
|
||
| use OCA\DAV\CalDAV\CalDavBackend; | ||
| use OCA\DAV\CalDAV\Plugin; | ||
| use OCA\DAV\Service\ASyncService; | ||
| use OCP\AppFramework\Http; | ||
| use OCP\Federation\ICloudIdManager; | ||
|
|
@@ -82,13 +83,26 @@ public function syncOne(FederatedCalendarEntity $calendar): int { | |
| } | ||
|
|
||
| // Process changes from remote | ||
| $downloadedEvents = 0; | ||
| $changedResources = []; | ||
| foreach ($response['response'] as $resource => $status) { | ||
| $objectUri = basename($resource); | ||
| if (isset($status[200])) { | ||
| // Object created or updated | ||
| $absoluteUrl = $this->prepareUri($credentials['remoteUrl'], $resource); | ||
| $calendarData = $this->requestGet($absoluteUrl, $credentials['username'], $credentials['token']); | ||
| $changedResources[] = $resource; | ||
| } else { | ||
| // Object deleted | ||
| $this->backend->deleteCalendarObject( | ||
| $calendar->getId(), | ||
| basename($resource), | ||
| CalDavBackend::CALENDAR_TYPE_FEDERATED, | ||
| true | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| $downloadedEvents = 0; | ||
| if ($changedResources !== []) { | ||
| foreach ($this->fetchCalendarObjects($credentials, $changedResources) as $resource => $calendarData) { | ||
| $objectUri = basename($resource); | ||
| $existingObject = $this->backend->getCalendarObject( | ||
| $calendar->getId(), | ||
| $objectUri, | ||
|
|
@@ -110,14 +124,6 @@ public function syncOne(FederatedCalendarEntity $calendar): int { | |
| ); | ||
| } | ||
| $downloadedEvents++; | ||
| } else { | ||
| // Object deleted | ||
| $this->backend->deleteCalendarObject( | ||
| $calendar->getId(), | ||
| $objectUri, | ||
| CalDavBackend::CALENDAR_TYPE_FEDERATED, | ||
| true | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -147,6 +153,39 @@ public function syncOne(FederatedCalendarEntity $calendar): int { | |
| return $downloadedEvents; | ||
| } | ||
|
|
||
| /** | ||
| * Download the calendar data of the given resources, using calendar-multiget when the remote supports it. | ||
| * | ||
| * @param array{username: string, remoteUrl: string, token: string} $credentials | ||
| * @param list<string> $hrefs | ||
| * @return \Generator<string, string> Calendar data keyed by href | ||
| * | ||
| * @throws ClientExceptionInterface If a remote request fails. | ||
| */ | ||
| private function fetchCalendarObjects(array $credentials, array $hrefs): \Generator { | ||
| $reportName = '{' . Plugin::NS_CALDAV . '}calendar-multiget'; | ||
| // Older remotes do not support calendar-multiget for federated calendars and | ||
| // would log the rejected report as an error, so ask before sending it | ||
| if (!$this->requestReportSupport($credentials['remoteUrl'], $credentials['username'], $credentials['token'], $reportName)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this PROPFIND runs on every sync of every calendar with changes. Caching the result per remote for the duration of a run would save most of them. |
||
| foreach ($hrefs as $href) { | ||
| $absoluteUrl = $this->prepareUri($credentials['remoteUrl'], $href); | ||
| yield $href => $this->requestGet($absoluteUrl, $credentials['username'], $credentials['token']); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| foreach (array_chunk($hrefs, self::MULTIGET_BATCH_SIZE) as $batch) { | ||
| yield from $this->requestMultiGet( | ||
| $credentials['remoteUrl'], | ||
| $credentials['username'], | ||
| $credentials['token'], | ||
| $batch, | ||
| $reportName, | ||
| '{' . Plugin::NS_CALDAV . '}calendar-data', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sabre's CalDAV plugin strips every Converting back to CRLF (VObject read + serialize, or |
||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a calendar object on the remote server. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -231,6 +231,11 @@ public function __construct( | |
| $this->server->addPlugin(\OCP\Server::get(CalDavValidatePlugin::class)); | ||
| } | ||
|
|
||
| // federated calendar shares only need the CalDAV reports, e.g. calendar-multiget for syncing | ||
| if ($this->requestIsForSubtree(['remote-calendars'])) { | ||
| $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment says only the CalDAV reports are needed, but this registers the whole CalDAV plugin for Registering only what the sync needs, or explicitly accepting and testing the new PUT validation, would avoid that. |
||
| } | ||
|
|
||
| // addressbook plugins | ||
| if ($this->requestIsForSubtree(['addressbooks', 'principals'])) { | ||
| $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OCP\Server::get(IRequest::class), \OCP\Server::get(RateLimiting::class))); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requestMultiGet()can also throwSabre\Xml\LibXMLException(see the parse-failure comment on #65942). That's not aClientExceptionInterface, so it gets past the catch blocks ofFederatedCalendarPeriodicSyncJobandFederatedCalendarSyncJob: the periodic job stops for all remaining calendars, andFederatedCalendarSyncJobskips itssetLastSync(1)fallback.