Skip to content

Commit 72cd5a3

Browse files
kesselbbackportbot[bot]
authored andcommitted
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 <[email protected]> [skip ci]
1 parent d02ecb5 commit 72cd5a3

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-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\Connector\Sabre\Principal;
@@ -1950,6 +1951,10 @@ public function search(
19501951

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

1954+
// Without explicit order by its undefined in which order the SQL server returns the events.
1955+
// For the pagination with hasLimit and hasTimeRange, a stable ordering is helpful.
1956+
$outerQuery->addOrderBy('id');
1957+
19531958
$offset = (int)$offset;
19541959
$outerQuery->setFirstResult($offset);
19551960

@@ -1985,7 +1990,7 @@ public function search(
19851990
$calendarObjects = $this->searchCalendarObjects($outerQuery, $start, $end);
19861991
}
19871992

1988-
return array_map(function ($o) use ($options) {
1993+
$calendarObjects = array_map(function ($o) use ($options) {
19891994
$calendarData = Reader::read($o['calendardata']);
19901995

19911996
// Expand recurrences if an explicit time range is requested
@@ -2021,6 +2026,17 @@ public function search(
20212026
}, $timezones),
20222027
];
20232028
}, $calendarObjects);
2029+
2030+
usort($calendarObjects, function (array $a, array $b) {
2031+
/** @var DateTimeImmutable $startA */
2032+
$startA = $a['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE);
2033+
/** @var DateTimeImmutable $startB */
2034+
$startB = $b['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE);
2035+
2036+
return $startA->getTimestamp() <=> $startB->getTimestamp();
2037+
});
2038+
2039+
return $calendarObjects;
20242040
}
20252041

20262042
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

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)