aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib
diff options
context:
space:
mode:
authorDaniel Kesselberg <mail@danielkesselberg.de>2024-05-15 12:55:40 +0200
committerDaniel <mail@danielkesselberg.de>2024-06-03 12:18:19 +0200
commit0d920c87d479e646d343921b9c140e1127b69212 (patch)
treef78222019be867043928821c824a11a803a86152 /apps/dav/lib
parent4be308ab26c4b2b03cad4a332d2343155cb39624 (diff)
downloadnextcloud-server-0d920c87d479e646d343921b9c140e1127b69212.tar.gz
nextcloud-server-0d920c87d479e646d343921b9c140e1127b69212.zip
feat(caldav): order the calendar objects by start date for search
Sorting the events by the start date leads to more predictable results for the search API consumers. Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'apps/dav/lib')
-rw-r--r--apps/dav/lib/CalDAV/CalDavBackend.php18
1 files changed, 17 insertions, 1 deletions
diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php
index a189c858ed9..6331381a4ae 100644
--- a/apps/dav/lib/CalDAV/CalDavBackend.php
+++ b/apps/dav/lib/CalDAV/CalDavBackend.php
@@ -40,6 +40,7 @@
namespace OCA\DAV\CalDAV;
use DateTime;
+use DateTimeImmutable;
use DateTimeInterface;
use OCA\DAV\AppInfo\Application;
use OCA\DAV\Connector\Sabre\Principal;
@@ -1957,6 +1958,10 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
$outerQuery->andWhere($outerQuery->expr()->in('c.id', $outerQuery->createFunction($innerQuery->getSQL())));
+ // Without explicit order by its undefined in which order the SQL server returns the events.
+ // For the pagination with hasLimit and hasTimeRange, a stable ordering is helpful.
+ $outerQuery->addOrderBy('id');
+
$offset = (int)$offset;
$outerQuery->setFirstResult($offset);
@@ -1992,7 +1997,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
$calendarObjects = $this->searchCalendarObjects($outerQuery, $start, $end);
}
- return array_map(function ($o) use ($options) {
+ $calendarObjects = array_map(function ($o) use ($options) {
$calendarData = Reader::read($o['calendardata']);
// Expand recurrences if an explicit time range is requested
@@ -2028,6 +2033,17 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
}, $timezones),
];
}, $calendarObjects);
+
+ usort($calendarObjects, function (array $a, array $b) {
+ /** @var DateTimeImmutable $startA */
+ $startA = $a['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE);
+ /** @var DateTimeImmutable $startB */
+ $startB = $b['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE);
+
+ return $startA->getTimestamp() <=> $startB->getTimestamp();
+ });
+
+ return $calendarObjects;
}
private function searchCalendarObjects(IQueryBuilder $query, DateTimeInterface|null $start, DateTimeInterface|null $end): array {