Skip to content

Commit 06db779

Browse files
fix(dav): apply stored additional properties to calendar rows
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
1 parent 975d9b7 commit 06db779

15 files changed

Lines changed: 375 additions & 22 deletions

‎apps/dav/appinfo/v1/caldav.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
$config,
8686
Server::get(\OCA\DAV\CalDAV\Sharing\Backend::class),
8787
Server::get(FederatedCalendarMapper::class),
88+
Server::get(\OCA\DAV\DAV\CustomPropertiesService::class),
8889
Server::get(ICacheFactory::class),
8990
true
9091
);

‎apps/dav/composer/composer/autoload_classmap.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288
'OCA\\DAV\\Controller\\OutOfOfficeController' => $baseDir . '/../lib/Controller/OutOfOfficeController.php',
289289
'OCA\\DAV\\Controller\\UpcomingEventsController' => $baseDir . '/../lib/Controller/UpcomingEventsController.php',
290290
'OCA\\DAV\\DAV\\CustomPropertiesBackend' => $baseDir . '/../lib/DAV/CustomPropertiesBackend.php',
291+
'OCA\\DAV\\DAV\\CustomPropertiesService' => $baseDir . '/../lib/DAV/CustomPropertiesService.php',
291292
'OCA\\DAV\\DAV\\GroupPrincipalBackend' => $baseDir . '/../lib/DAV/GroupPrincipalBackend.php',
292293
'OCA\\DAV\\DAV\\PublicAuth' => $baseDir . '/../lib/DAV/PublicAuth.php',
293294
'OCA\\DAV\\DAV\\RemoteUserPrincipalBackend' => $baseDir . '/../lib/DAV/RemoteUserPrincipalBackend.php',

‎apps/dav/composer/composer/autoload_static.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ class ComposerStaticInitDAV
303303
'OCA\\DAV\\Controller\\OutOfOfficeController' => __DIR__ . '/..' . '/../lib/Controller/OutOfOfficeController.php',
304304
'OCA\\DAV\\Controller\\UpcomingEventsController' => __DIR__ . '/..' . '/../lib/Controller/UpcomingEventsController.php',
305305
'OCA\\DAV\\DAV\\CustomPropertiesBackend' => __DIR__ . '/..' . '/../lib/DAV/CustomPropertiesBackend.php',
306+
'OCA\\DAV\\DAV\\CustomPropertiesService' => __DIR__ . '/..' . '/../lib/DAV/CustomPropertiesService.php',
306307
'OCA\\DAV\\DAV\\GroupPrincipalBackend' => __DIR__ . '/..' . '/../lib/DAV/GroupPrincipalBackend.php',
307308
'OCA\\DAV\\DAV\\PublicAuth' => __DIR__ . '/..' . '/../lib/DAV/PublicAuth.php',
308309
'OCA\\DAV\\DAV\\RemoteUserPrincipalBackend' => __DIR__ . '/..' . '/../lib/DAV/RemoteUserPrincipalBackend.php',

‎apps/dav/lib/CalDAV/CalDavBackend.php‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use OCA\DAV\CalDAV\Federation\FederatedCalendarMapper;
1818
use OCA\DAV\CalDAV\Sharing\Backend;
1919
use OCA\DAV\Connector\Sabre\Principal;
20+
use OCA\DAV\DAV\CustomPropertiesBackend;
21+
use OCA\DAV\DAV\CustomPropertiesService;
2022
use OCA\DAV\DAV\Sharing\IShareable;
2123
use OCA\DAV\Events\CachedCalendarObjectCreatedEvent;
2224
use OCA\DAV\Events\CachedCalendarObjectDeletedEvent;
@@ -220,6 +222,7 @@ public function __construct(
220222
private IConfig $config,
221223
private Sharing\Backend $calendarSharingBackend,
222224
private FederatedCalendarMapper $federatedCalendarMapper,
225+
private CustomPropertiesService $customPropertiesService,
223226
ICacheFactory $cacheFactory,
224227
private bool $legacyEndpoint = false,
225228
) {
@@ -459,7 +462,7 @@ public function getCalendarsForUser($principalUri) {
459462
}
460463
$result->closeCursor();
461464

462-
return array_values($calendars);
465+
return $this->addAdditionalPropertiesToCalendars(array_values($calendars));
463466
}, $this->db);
464467
}
465468

@@ -508,7 +511,7 @@ public function getUsersOwnCalendars($principalUri) {
508511
}
509512
}
510513
$stmt->closeCursor();
511-
return array_values($calendars);
514+
return $this->addAdditionalPropertiesToCalendars(array_values($calendars));
512515
}
513516

514517
/**
@@ -674,6 +677,8 @@ public function getCalendarByUri($principal, $uri) {
674677
$calendar = $this->addOwnerPrincipalToCalendar($calendar);
675678
$calendar = $this->addResourceTypeToCalendar($row, $calendar);
676679

680+
[$calendar] = $this->addAdditionalPropertiesToCalendars([$calendar]);
681+
677682
return $calendar;
678683
}
679684

@@ -4011,6 +4016,49 @@ private function convertPrincipal($principalUri, $toV2) {
40114016
return $principalUri;
40124017
}
40134018

4019+
/**
4020+
* Adds the properties the user whose calendar home a calendar is in has stored on it
4021+
*
4022+
* Stored properties only fill in values the calendar does not provide itself. On calendars
4023+
* shared with the user, the properties a sharee can customize override the owner's values.
4024+
*
4025+
* @param array[] $calendars
4026+
* @return array[]
4027+
*/
4028+
private function addAdditionalPropertiesToCalendars(array $calendars): array {
4029+
$ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal';
4030+
4031+
$paths = [];
4032+
$calendarPaths = [];
4033+
foreach ($calendars as $key => $calendar) {
4034+
[$prefix, $userId] = Uri\split($calendar['principaluri']);
4035+
if ($prefix !== 'principals/users' && $prefix !== 'principals') {
4036+
continue;
4037+
}
4038+
$path = 'calendars/' . $userId . '/' . $calendar['uri'];
4039+
$paths[$userId][] = $path;
4040+
$calendarPaths[$key] = [$userId, $path];
4041+
}
4042+
if ($paths === []) {
4043+
return $calendars;
4044+
}
4045+
4046+
$properties = $this->customPropertiesService->getProperties($paths);
4047+
foreach ($calendarPaths as $key => [$userId, $path]) {
4048+
$storedProperties = $properties[$userId][$path] ?? [];
4049+
if ($storedProperties === []) {
4050+
continue;
4051+
}
4052+
$calendar = array_merge($storedProperties, $calendars[$key]);
4053+
if (isset($calendar[$ownerPrincipalKey]) && $calendar[$ownerPrincipalKey] !== $calendar['principaluri']) {
4054+
$calendar = array_merge($calendar, array_intersect_key($storedProperties, array_flip(CustomPropertiesBackend::SHARED_CALENDAR_PROPERTIES)));
4055+
}
4056+
$calendars[$key] = $calendar;
4057+
}
4058+
4059+
return $calendars;
4060+
}
4061+
40144062
/**
40154063
* adds information about an owner to the calendar data
40164064
*

‎apps/dav/lib/CalDAV/CalendarImpl.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function getPermissions(): int {
159159
*/
160160
#[\Override]
161161
public function isEnabled(): bool {
162-
return $this->calendarInfo['{http://owncloud.org/ns}calendar-enabled'] ?? true;
162+
return (bool)($this->calendarInfo['{http://owncloud.org/ns}calendar-enabled'] ?? true);
163163
}
164164

165165
/**

‎apps/dav/lib/CalDAV/CalendarProvider.php‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,7 @@ public function getCalendars(string $principalUri, array $calendarUris = []): ar
4747

4848
$iCalendars = [];
4949

50-
$additionalProperties = $this->getAdditionalPropertiesForCalendars($calendarInfos);
5150
foreach ($calendarInfos as $calendarInfo) {
52-
$user = str_replace('principals/users/', '', $calendarInfo['principaluri']);
53-
$path = 'calendars/' . $user . '/' . $calendarInfo['uri'];
54-
55-
$calendarInfo = array_merge($calendarInfo, $additionalProperties[$path] ?? []);
56-
5751
$calendar = new Calendar($this->calDavBackend, $calendarInfo, $this->l10n, $this->config, $this->logger);
5852
$iCalendars[] = new CalendarImpl(
5953
$calendar,

‎apps/dav/lib/Command/CreateCalendar.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
1515
use OCA\DAV\CalDAV\Sharing\Backend;
1616
use OCA\DAV\Connector\Sabre\Principal;
17+
use OCA\DAV\DAV\CustomPropertiesService;
1718
use OCP\Accounts\IAccountManager;
1819
use OCP\App\IAppManager;
1920
use OCP\EventDispatcher\IEventDispatcher;
@@ -87,6 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8788
$config,
8889
Server::get(Backend::class),
8990
Server::get(FederatedCalendarMapper::class),
91+
Server::get(CustomPropertiesService::class),
9092
Server::get(ICacheFactory::class),
9193
);
9294
$caldav->createCalendar("principals/users/$user", $name, []);

‎apps/dav/lib/DAV/CustomPropertiesBackend.php‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ class CustomPropertiesBackend implements BackendInterface {
108108
'{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL' => Href::class,
109109
];
110110

111+
/**
112+
* Calendar properties a sharee can set on a calendar shared with them, overriding the owner's values
113+
*
114+
* @var string[]
115+
*/
116+
public const array SHARED_CALENDAR_PROPERTIES = [
117+
'{DAV:}displayname',
118+
'{urn:ietf:params:xml:ns:caldav}calendar-description',
119+
'{urn:ietf:params:xml:ns:caldav}calendar-timezone',
120+
'{http://apple.com/ns/ical/}calendar-order',
121+
'{http://apple.com/ns/ical/}calendar-color',
122+
'{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp',
123+
'{http://nextcloud.com/ns}disable-alarm-notifications',
124+
];
125+
111126
/**
112127
* Map of well-known property names to default values
113128
* @var array<string, string>
@@ -188,17 +203,7 @@ public function propFind($path, PropFind $propFind): void {
188203
// two '/' => this a calendar (no calendar-home nor calendar object)
189204
if (str_starts_with($path, 'calendars/') && substr_count($path, '/') === 2) {
190205
$allRequestedProps = $propFind->getRequestedProperties();
191-
$customPropertiesForShares = [
192-
'{DAV:}displayname',
193-
'{urn:ietf:params:xml:ns:caldav}calendar-description',
194-
'{urn:ietf:params:xml:ns:caldav}calendar-timezone',
195-
'{http://apple.com/ns/ical/}calendar-order',
196-
'{http://apple.com/ns/ical/}calendar-color',
197-
'{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp',
198-
'{http://nextcloud.com/ns}disable-alarm-notifications',
199-
];
200-
201-
foreach ($customPropertiesForShares as $customPropertyForShares) {
206+
foreach (self::SHARED_CALENDAR_PROPERTIES as $customPropertyForShares) {
202207
if (in_array($customPropertyForShares, $allRequestedProps, true)) {
203208
$requestedProps[] = $customPropertyForShares;
204209
}
@@ -590,7 +595,7 @@ private function updateProperties(string $path, array $properties): bool {
590595
/**
591596
* Long paths are hashed to ensure they fit in the database
592597
*/
593-
private function formatPath(string $path): string {
598+
public static function formatPath(string $path): string {
594599
if (strlen($path) > 250) {
595600
return sha1($path);
596601
}
@@ -669,7 +674,7 @@ private function encodeValueForDatabase(string $path, string $name, mixed $value
669674
/**
670675
* @return mixed|Complex|string
671676
*/
672-
private function decodeValueFromDatabase(string $value, int $valueType): mixed {
677+
public static function decodeValueFromDatabase(string $value, int $valueType): mixed {
673678
switch ($valueType) {
674679
case self::PROPERTY_TYPE_XML:
675680
return new Complex($value);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\DAV\DAV;
11+
12+
use OCA\DAV\Db\PropertyMapper;
13+
14+
class CustomPropertiesService {
15+
16+
public function __construct(
17+
private readonly PropertyMapper $propertyMapper,
18+
) {
19+
}
20+
21+
/**
22+
* Get the properties users have stored on the given paths
23+
*
24+
* @param array<string, string[]> $paths paths indexed by the user who stored the properties
25+
* @return array<string, array<string, array<string, mixed>>> decoded property values indexed by user, path and property name
26+
*/
27+
public function getProperties(array $paths): array {
28+
$formattedPaths = [];
29+
foreach ($paths as $userId => $userPaths) {
30+
foreach ($userPaths as $path) {
31+
$formattedPaths[$userId][CustomPropertiesBackend::formatPath($path)] = $path;
32+
}
33+
}
34+
if ($formattedPaths === []) {
35+
return [];
36+
}
37+
38+
$properties = [];
39+
foreach ($this->propertyMapper->findPropertiesByPathsAndUsers(array_map('array_keys', $formattedPaths)) as $property) {
40+
$path = $formattedPaths[$property->getUserid()][$property->getPropertypath()] ?? null;
41+
if ($path !== null) {
42+
$properties[$property->getUserid()][$path][$property->getPropertyname()] = CustomPropertiesBackend::decodeValueFromDatabase($property->getPropertyvalue(), (int)$property->getValuetype());
43+
}
44+
}
45+
46+
return $properties;
47+
}
48+
}

‎apps/dav/lib/RootCollection.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use OCA\DAV\CardDAV\AddressBookRoot;
2424
use OCA\DAV\CardDAV\CardDavBackend;
2525
use OCA\DAV\Connector\Sabre\Principal;
26+
use OCA\DAV\DAV\CustomPropertiesService;
2627
use OCA\DAV\DAV\GroupPrincipalBackend;
2728
use OCA\DAV\DAV\RemoteUserPrincipalBackend;
2829
use OCA\DAV\DAV\SystemPrincipalBackend;
@@ -113,6 +114,7 @@ public function __construct() {
113114
$config,
114115
$calendarSharingBackend,
115116
Server::get(FederatedCalendarMapper::class),
117+
Server::get(CustomPropertiesService::class),
116118
Server::get(ICacheFactory::class),
117119
false,
118120
);

0 commit comments

Comments
 (0)