Skip to content

Commit

Permalink
fix(caldav): rename default calendar to keep it in the trashbin inste…
Browse files Browse the repository at this point in the history
…ad of purging it

When doing a PROPFIND on default-calendar-url, if the current default calendar (fallbacking on personal uri)
is in the trashbin, it's being purged so that it's recreated.

This leads to loss of data.

We can simply rename the calendar URI and add a unique suffix so that it doesn't conflict with the new calendar
being created.
Shares are fine because they reference the resourceid and not the calendar URI.

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Jan 3, 2025
1 parent 72e8241 commit 74deaf1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions apps/dav/lib/CalDAV/Schedule/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function getAddressesForPrincipal($principal) {
if ($result === null) {
$result = [];
}

// iterate through items and html decode values
foreach ($result as $key => $value) {
$result[$key] = urldecode($value);
Expand Down Expand Up @@ -197,12 +197,12 @@ public function calendarObjectChange(RequestInterface $request, ResponseInterfac
}
// process request
$this->processICalendarChange($currentObject, $vCal, $addresses, [], $modified);

if ($currentObject) {
// Destroy circular references so PHP will GC the object.
$currentObject->destroy();
}

} catch (SameOrganizerForAllComponentsException $e) {
$this->handleSameOrganizerException($e, $vCal, $calendarPath);
}
Expand Down Expand Up @@ -430,12 +430,11 @@ public function propFindDefaultCalendarUrl(PropFind $propFind, INode $node) {
} else {
// Otherwise if we have really nothing, create a new calendar
if ($currentCalendarDeleted) {
// If the calendar exists but is deleted, we need to purge it first
// This may cause some issues in a non synchronous database setup
// If the calendar exists but is in the trash bin, we rename its uri
// so that we can create the new one and still restore the previous one
$calendar = $this->getCalendar($calendarHome, $uri);
if ($calendar instanceof Calendar) {
$calendar->disableTrashbin();
$calendar->delete();
$this->moveCalendar($calendarHome, $principalUrl, $uri, $uri . '-back-' . time());
}
}
$this->createCalendar($calendarHome, $principalUrl, $uri, $displayName);
Expand Down Expand Up @@ -572,7 +571,7 @@ private function isAvailableAtTime(string $email, \DateTimeInterface $start, \Da
$homePath = $result[0][200]['{' . self::NS_CALDAV . '}calendar-home-set']->getHref();
/** @var Calendar $node */
foreach ($this->server->tree->getNodeForPath($homePath)->getChildren() as $node) {

if (!$node instanceof ICalendar) {
continue;
}
Expand Down Expand Up @@ -704,6 +703,10 @@ private function createCalendar(CalendarHome $calendarHome, string $principalUri
]);
}

private function moveCalendar(CalendarHome $calendarHome, string $principalUri, string $oldUri, string $newUri): void {
$calendarHome->getCalDAVBackend()->moveCalendar($oldUri, $principalUri, $principalUri, $newUri);

Check failure on line 707 in apps/dav/lib/CalDAV/Schedule/Plugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/dav/lib/CalDAV/Schedule/Plugin.php:707:38: UndefinedInterfaceMethod: Method Sabre\CalDAV\Backend\BackendInterface::moveCalendar does not exist (see https://psalm.dev/181)
}

/**
* Try to handle the given exception gracefully or throw it if necessary.
*
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/tests/unit/CalDAV/Schedule/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public function testPropFindDefaultCalendarUrl(string $principalUri, ?string $ca
'{DAV:}displayname' => $displayName,
]);

$calendarHomeObject->expects($this->once())
$calendarHomeObject->expects($this->exactly($deleted ? 2 : 1))
->method('getCalDAVBackend')
->with()
->willReturn($calendarBackend);
Expand Down

0 comments on commit 74deaf1

Please sign in to comment.