Skip to content

Commit dea1906

Browse files
committed
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>
1 parent ebbc5dd commit dea1906

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

apps/dav/lib/CalDAV/CalDavBackend.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
namespace OCA\DAV\CalDAV;
4141

4242
use DateTime;
43+
use DateTimeImmutable;
4344
use DateTimeInterface;
4445
use OCA\DAV\AppInfo\Application;
4546
use OCA\DAV\CalDAV\Sharing\Backend;
@@ -1965,6 +1966,10 @@ public function search(
19651966

19661967
$outerQuery->andWhere($outerQuery->expr()->in('c.id', $outerQuery->createFunction($innerQuery->getSQL())));
19671968

1969+
// Without explicit order by its undefined in which order the SQL server returns the events.
1970+
// For the pagination with hasLimit and hasTimeRange, a stable ordering is helpful.
1971+
$outerQuery->addOrderBy('id');
1972+
19681973
$offset = (int)$offset;
19691974
$outerQuery->setFirstResult($offset);
19701975

@@ -2000,7 +2005,7 @@ public function search(
20002005
$calendarObjects = $this->searchCalendarObjects($outerQuery, $start, $end);
20012006
}
20022007

2003-
return array_map(function ($o) use ($options) {
2008+
$calendarObjects = array_map(function ($o) use ($options) {
20042009
$calendarData = Reader::read($o['calendardata']);
20052010

20062011
// Expand recurrences if an explicit time range is requested
@@ -2036,6 +2041,17 @@ public function search(
20362041
}, $timezones),
20372042
];
20382043
}, $calendarObjects);
2044+
2045+
usort($calendarObjects, function (array $a, array $b) {
2046+
/** @var DateTimeImmutable $startA */
2047+
$startA = $a['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE);
2048+
/** @var DateTimeImmutable $startB */
2049+
$startB = $b['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE);
2050+
2051+
return $startA->getTimestamp() <=> $startB->getTimestamp();
2052+
});
2053+
2054+
return $calendarObjects;
20392055
}
20402056

20412057
private function searchCalendarObjects(IQueryBuilder $query, DateTimeInterface|null $start, DateTimeInterface|null $end): array {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
BEGIN:VCALENDAR
2+
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
3+
VERSION:2.0
4+
BEGIN:VEVENT
5+
CREATED:20240507T122246Z
6+
LAST-MODIFIED:20240507T175258Z
7+
DTSTAMP:20240507T175258Z
8+
UID:39e1b04f-d1cc-4622-bf97-11c38e070f43
9+
SUMMARY:Missing DTSTART 1
10+
DTEND;TZID=Europe/Berlin:20240514T133000
11+
TRANSP:OPAQUE
12+
X-MOZ-GENERATION:2
13+
END:VEVENT
14+
END:VCALENDAR
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
BEGIN:VCALENDAR
2+
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
3+
VERSION:2.0
4+
BEGIN:VEVENT
5+
CREATED:20240507T122246Z
6+
LAST-MODIFIED:20240507T175258Z
7+
DTSTAMP:20240507T175258Z
8+
UID:12413feb-4b8c-4e95-ae7f-9ec4f42f3348
9+
SUMMARY:Missing DTSTART 2
10+
DTEND;TZID=Europe/Berlin:20240514T133000
11+
TRANSP:OPAQUE
12+
X-MOZ-GENERATION:2
13+
END:VEVENT
14+
END:VCALENDAR

apps/dav/tests/unit/CalDAV/CalDavBackendTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,4 +1762,42 @@ public function testSearchWithLimitAndTimeRangeShouldReturnObjectsInTheSameOrder
17621762
'Recurrence starting before requested start',
17631763
);
17641764
}
1765+
1766+
public function testSearchShouldReturnObjectsInTheSameOrderMissingDate() {
1767+
$calendarId = $this->createTestCalendar();
1768+
$calendarInfo = [
1769+
'id' => $calendarId,
1770+
'principaluri' => 'user1',
1771+
'{http://owncloud.org/ns}owner-principal' => 'user1',
1772+
];
1773+
1774+
$testFiles = [
1775+
__DIR__ . '/../../misc/caldav-search-limit-timerange-6.ics', // <-- intentional!
1776+
__DIR__ . '/../../misc/caldav-search-limit-timerange-5.ics',
1777+
__DIR__ . '/../../misc/caldav-search-missing-start-1.ics',
1778+
__DIR__ . '/../../misc/caldav-search-missing-start-2.ics',
1779+
];
1780+
1781+
foreach ($testFiles as $testFile) {
1782+
$objectUri = static::getUniqueID('search-return-objects-in-same-order-');
1783+
$calendarData = \file_get_contents($testFile);
1784+
$this->backend->createCalendarObject($calendarId, $objectUri, $calendarData);
1785+
}
1786+
1787+
$results = $this->backend->search(
1788+
$calendarInfo,
1789+
'',
1790+
[],
1791+
[],
1792+
4,
1793+
null,
1794+
);
1795+
1796+
$this->assertCount(4, $results);
1797+
1798+
$this->assertEquals('Cake Tasting', $results[0]['objects'][0]['SUMMARY'][0]);
1799+
$this->assertEquals('Pasta Day', $results[1]['objects'][0]['SUMMARY'][0]);
1800+
$this->assertEquals('Missing DTSTART 1', $results[2]['objects'][0]['SUMMARY'][0]);
1801+
$this->assertEquals('Missing DTSTART 2', $results[3]['objects'][0]['SUMMARY'][0]);
1802+
}
17651803
}

lib/public/Calendar/ICalendar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getDisplayColor(): ?string;
6565
* ['timerange' => ['start' => new DateTime(...), 'end' => new DateTime(...)]]
6666
* @param int|null $limit - limit number of search results
6767
* @param int|null $offset - offset for paging of search results
68-
* @return array an array of events/journals/todos which are arrays of key-value-pairs
68+
* @return array an array of events/journals/todos which are arrays of key-value-pairs. the events are sorted by start date (closest first, furthest last)
6969
* @since 13.0.0
7070
*/
7171
public function search(string $pattern, array $searchProperties = [], array $options = [], ?int $limit = null, ?int $offset = null): array;

0 commit comments

Comments
 (0)