Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 39 additions & 30 deletions apps/dav/lib/CalDAV/Reminder/ReminderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ public function onCalendarObjectCreate(array $objectData):void {
foreach ($recurrenceException->VALARM as $valarm) {
/** @var VAlarm $valarm */
$alarmHash = $this->getAlarmHash($valarm);
$triggerTime = $valarm->getEffectiveTriggerTime();
$triggerTime = $this->getEffectiveTriggerTime($valarm, $calendarTimeZone, $recurrenceException);
$diff = $now->diff($triggerTime);
if ($diff->invert === 1) {
continue;
}

$alarms = $this->getRemindersForVAlarm($valarm, $objectData, $calendarTimeZone,
$alarms = $this->getRemindersForVAlarm($valarm, $objectData, $triggerTime,
$eventHash, $alarmHash, true, true);
$this->writeRemindersToDatabase($alarms);
}
Expand Down Expand Up @@ -294,17 +294,7 @@ public function onCalendarObjectCreate(array $objectData):void {
}

try {
$triggerTime = $valarm->getEffectiveTriggerTime();
/**
* @psalm-suppress DocblockTypeContradiction
* https://github.com/vimeo/psalm/issues/9244
*/
if ($triggerTime->getTimezone() === false || $triggerTime->getTimezone()->getName() === 'UTC') {
$triggerTime = new DateTimeImmutable(
$triggerTime->format('Y-m-d H:i:s'),
$calendarTimeZone
);
}
$triggerTime = $this->getEffectiveTriggerTime($valarm, $calendarTimeZone, $masterItem);
} catch (InvalidDataException $e) {
continue;
}
Expand All @@ -323,7 +313,7 @@ public function onCalendarObjectCreate(array $objectData):void {
continue;
}

$alarms = $this->getRemindersForVAlarm($valarm, $objectData, $calendarTimeZone, $masterHash, $alarmHash, $isRecurring, false);
$alarms = $this->getRemindersForVAlarm($valarm, $objectData, $triggerTime, $masterHash, $alarmHash, $isRecurring, false);
$this->writeRemindersToDatabase($alarms);
$processedAlarms[] = $alarmHash;
}
Expand Down Expand Up @@ -362,7 +352,7 @@ public function onCalendarObjectDelete(array $objectData):void {
/**
* @param VAlarm $valarm
* @param array $objectData
* @param DateTimeZone $calendarTimeZone
* @param DateTimeImmutable $notificationDate
* @param string|null $eventHash
* @param string|null $alarmHash
* @param bool $isRecurring
Expand All @@ -371,7 +361,7 @@ public function onCalendarObjectDelete(array $objectData):void {
*/
private function getRemindersForVAlarm(VAlarm $valarm,
array $objectData,
DateTimeZone $calendarTimeZone,
DateTimeImmutable $notificationDate,
?string $eventHash = null,
?string $alarmHash = null,
bool $isRecurring = false,
Expand All @@ -385,18 +375,6 @@ private function getRemindersForVAlarm(VAlarm $valarm,

$recurrenceId = $this->getEffectiveRecurrenceIdOfVEvent($valarm->parent);
$isRelative = $this->isAlarmRelative($valarm);
/** @var DateTimeImmutable $notificationDate */
$notificationDate = $valarm->getEffectiveTriggerTime();
/**
* @psalm-suppress DocblockTypeContradiction
* https://github.com/vimeo/psalm/issues/9244
*/
if ($notificationDate->getTimezone() === false || $notificationDate->getTimezone()->getName() === 'UTC') {
$notificationDate = new DateTimeImmutable(
$notificationDate->format('Y-m-d H:i:s'),
$calendarTimeZone
);
}
$clonedNotificationDate = new \DateTime('now', $notificationDate->getTimezone());
$clonedNotificationDate->setTimestamp($notificationDate->getTimestamp());

Expand Down Expand Up @@ -487,6 +465,10 @@ private function deleteOrProcessNext(array $reminder,
}

$vevents = $this->getAllVEventsFromVCalendar($vevent->parent);
$masterItem = $this->getMasterItemFromListOfVEvents($vevents);
if ($masterItem === null) {
throw new InvalidDataException('Recurring reminder has no master event');
}
$recurrenceExceptions = $this->getRecurrenceExceptionFromListOfVEvents($vevents);
$now = $this->timeFactory->getDateTime();
$calendarTimeZone = $this->getCalendarTimeZone((int)$reminder['calendar_id']);
Expand Down Expand Up @@ -523,7 +505,7 @@ private function deleteOrProcessNext(array $reminder,
continue;
}

$triggerTime = $valarm->getEffectiveTriggerTime();
$triggerTime = $this->getEffectiveTriggerTime($valarm, $calendarTimeZone, $masterItem);

// If effective trigger time is in the past
// just skip and generate for next event
Expand All @@ -536,7 +518,7 @@ private function deleteOrProcessNext(array $reminder,
$alarms = $this->getRemindersForVAlarm($valarm, [
'calendarid' => $reminder['calendar_id'],
'id' => $reminder['object_id'],
], $calendarTimeZone, $reminder['event_hash'], $alarmHash, true, false);
], $triggerTime, $reminder['event_hash'], $alarmHash, true, false);
$this->writeRemindersToDatabase($alarms);

// Abort generating reminders after creating one successfully
Expand Down Expand Up @@ -829,6 +811,33 @@ private function isAlarmRelative(VAlarm $valarm):bool {
return $trigger instanceof VObject\Property\ICalendar\Duration;
}

private function getEffectiveTriggerTime(VAlarm $valarm, DateTimeZone $calendarTimeZone, VEvent $storedEvent): DateTimeImmutable {
$triggerTime = $valarm->getEffectiveTriggerTime();
if (!$this->isAlarmRelative($valarm)) {
return $triggerTime;
}

$relativeTo = $storedEvent->select('DTSTART')[0];
$trigger = $valarm->select('TRIGGER')[0];
$endDates = $storedEvent->select('DTEND');
if (strcasecmp((string)$trigger['RELATED'], 'END') === 0 && $endDates !== []) {
$relativeTo = $endDates[0];
}

// Recurrence expansion rewrites unresolved TZIDs as UTC, so inspect the stored date.
$isExplicitUtc = false;
if ($relativeTo instanceof VObject\Property\ICalendar\DateTime) {
$tzid = $relativeTo['TZID'];
$isExplicitUtc = str_ends_with((string)$relativeTo, 'Z')
|| ($tzid instanceof VObject\Parameter && \in_array((string)$tzid, ['UTC', 'Etc/UTC'], true));
}
if ($triggerTime->getTimezone()->getName() === 'UTC' && !$isExplicitUtc) {
return new DateTimeImmutable($triggerTime->format('Y-m-d H:i:s'), $calendarTimeZone);
}

return $triggerTime;
}

/**
* @param VEvent $vevent
* @return int
Expand Down
176 changes: 176 additions & 0 deletions apps/dav/tests/unit/CalDAV/Reminder/ReminderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,182 @@ public function testOnCalendarObjectCreateRecurringEntryWithRepeat():void {
$this->reminderService->onCalendarObjectCreate($objectData);
}

private const string CUSTOM_VTIMEZONE_ICS = <<<ICS
BEGIN:VTIMEZONE
TZID:Customized Time Zone
BEGIN:STANDARD
DTSTART:16011028T040000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
TZOFFSETFROM:+0300
TZOFFSETTO:+0200
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010325T030000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
TZOFFSETFROM:+0200
TZOFFSETTO:+0300
END:DAYLIGHT
END:VTIMEZONE
ICS;

public static function provideAlarmTimeZones(): array {
$cases = [
'UTC start' => ['DTSTART:20230204T090000Z', 'TRIGGER:-PT1H', '2023-02-04T08:00:00Z'],
'UTC TZID' => ['DTSTART;TZID=UTC:20230204T090000', 'TRIGGER:-PT1H', '2023-02-04T08:00:00Z'],
'Etc/UTC TZID' => ['DTSTART;TZID=Etc/UTC:20230204T090000', 'TRIGGER:-PT1H', '2023-02-04T08:00:00Z'],
'UTC end' => ["DTSTART:20230204T090000Z\nDTEND:20230204T100000Z", 'TRIGGER;RELATED=END:-PT1H', '2023-02-04T09:00:00Z'],
'UTC duration' => ["DTSTART:20230204T090000Z\nDURATION:PT2H", 'TRIGGER;RELATED=END:-PT1H', '2023-02-04T10:00:00Z'],
'absolute UTC trigger' => ['DTSTART:20230204T090000', 'TRIGGER;VALUE=DATE-TIME:20230204T080000Z', '2023-02-04T08:00:00Z'],
'floating start' => ['DTSTART:20230204T090000', 'TRIGGER:-PT1H', '2023-02-04T19:00:00Z'],
'floating end' => ["DTSTART:20230204T090000\nDTEND:20230204T100000", 'TRIGGER;RELATED=END:-PT1H', '2023-02-04T20:00:00Z'],
'UTC start floating end' => ["DTSTART:20230204T090000Z\nDTEND:20230204T100000", 'TRIGGER;RELATED=END:-PT1H', '2023-02-04T20:00:00Z'],
'floating start UTC end' => ["DTSTART:20230204T090000\nDTEND:20230204T100000Z", 'TRIGGER;RELATED=END:-PT1H', '2023-02-04T09:00:00Z'],
'floating duration' => ["DTSTART:20230204T090000\nDURATION:PT2H", 'TRIGGER;RELATED=END:-PT1H', '2023-02-04T21:00:00Z'],
'all-day start' => ['DTSTART;VALUE=DATE:20230204', 'TRIGGER:-PT1H', '2023-02-04T10:00:00Z'],
'explicit event timezone' => ['DTSTART;TZID=Europe/Vienna:20230204T090000', 'TRIGGER:-PT1H', '2023-02-04T07:00:00Z'],
'expired UTC alarm' => ['DTSTART:20230204T090000Z', 'TRIGGER:-PT1H', null, '2023-02-04T08:30:00Z'],
'floating recurrence exception' => ["DTSTART:20230204T090000\nRECURRENCE-ID:20230204T090000",
'TRIGGER:-PT1H', '2023-02-04T19:00:00Z', '2023-02-04T10:00:00Z', true],
'expired floating master' => ['DTSTART:20230204T090000', 'TRIGGER:-PT1H',
null, '2023-02-04T07:00:00Z', false, 'Europe/Helsinki'],
'expired floating exception' => ["DTSTART:20230204T090000\nRECURRENCE-ID:20230204T090000",
'TRIGGER:-PT1H', null, '2023-02-04T07:00:00Z', true, 'Europe/Helsinki'],
'expired UTC exception' => ["DTSTART:20230204T090000Z\nRECURRENCE-ID:20230204T090000Z",
'TRIGGER:-PT1H', null, '2023-02-04T08:30:00Z', true],
];
foreach (['FLE Standard Time', 'Europe/Kiev', 'Customized Time Zone', 'Unresolvable Time Zone'] as $timeZone) {
$cases[$timeZone] = ["DTSTART;TZID=$timeZone:20230204T090000", 'TRIGGER:-PT1H',
'2023-02-04T06:00:00Z', '2023-02-03T00:00:00Z', false, 'Europe/Helsinki',
$timeZone === 'Customized Time Zone' ? self::CUSTOM_VTIMEZONE_ICS : ''];
}
return $cases;
}

#[DataProvider('provideAlarmTimeZones')]
public function testOnCalendarObjectCreatePreservesExplicitAlarmTimeZone(
string $eventTimes,
string $trigger,
?string $expectedTime,
string $now = '2023-02-03T00:00:00Z',
bool $isException = false,
string $calendarTimeZone = 'Pacific/Pago_Pago',
string $timeZones = '',
): void {
$calendarData = <<<ICS
BEGIN:VCALENDAR
VERSION:2.0
$timeZones
BEGIN:VEVENT
UID:timezone-test
$eventTimes
BEGIN:VALARM
ACTION:DISPLAY
$trigger
END:VALARM
END:VEVENT
END:VCALENDAR
ICS;
$this->caldavBackend->method('getCalendarById')->willReturn([
'{urn:ietf:params:xml:ns:caldav}calendar-timezone' => "BEGIN:VCALENDAR\nBEGIN:VTIMEZONE\nTZID:$calendarTimeZone\nEND:VTIMEZONE\nEND:VCALENDAR",
]);
$this->timeFactory->method('getDateTime')->willReturn(new DateTime($now));
if ($expectedTime === null) {
$this->backend->expects(self::never())->method('insertReminder');
} else {
$this->backend->expects(self::once())
->method('insertReminder')
->with(1337, 42, 'timezone-test', $isException, self::anything(), $isException,
self::anything(), self::anything(), 'DISPLAY', !str_contains($trigger, 'VALUE=DATE-TIME'),
(new DateTime($expectedTime))->getTimestamp(), false)
->willReturn(1);
}

$this->reminderService->onCalendarObjectCreate([
'calendardata' => $calendarData,
'id' => '42',
'calendarid' => '1337',
'component' => 'vevent',
]);
}

public static function provideRecurringAlarmTimeZones(): array {
return [
'floating occurrence' => ['DTSTART:20230203T090000', '2023-02-04T10:00:00Z',
'2023-02-03T19:00:00Z', '2023-02-04T19:00:00Z'],
'UTC occurrence' => ['DTSTART:20230203T090000Z', '2023-02-04T07:00:00Z',
'2023-02-03T08:00:00Z', '2023-02-04T08:00:00Z'],
'expired UTC occurrence' => ['DTSTART:20230203T090000Z', '2023-02-04T10:00:00Z',
'2023-02-03T08:00:00Z', '2023-02-05T08:00:00Z'],
'explicit event timezone' => ['DTSTART;TZID=Europe/Vienna:20230203T090000', '2023-02-04T06:00:00Z',
'2023-02-03T07:00:00Z', '2023-02-04T07:00:00Z'],
'unresolved timezone' => ['DTSTART;TZID=Unresolvable Time Zone:20230203T090000', '2023-02-04T10:00:00Z',
'2023-02-03T19:00:00Z', '2023-02-04T19:00:00Z'],
'custom timezone' => ['DTSTART;TZID=Customized Time Zone:20230203T090000', '2023-02-04T10:00:00Z',
'2023-02-03T19:00:00Z', '2023-02-04T19:00:00Z', self::CUSTOM_VTIMEZONE_ICS],
];
}

#[DataProvider('provideRecurringAlarmTimeZones')]
public function testProcessRemindersPreservesRecurringAlarmTimeZone(
string $eventStart,
string $now,
string $firstReminder,
string $nextReminder,
string $timeZones = '',
): void {
$calendarData = <<<ICS
BEGIN:VCALENDAR
VERSION:2.0
$timeZones
BEGIN:VEVENT
UID:timezone-test
$eventStart
RRULE:FREQ=DAILY;COUNT=3
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT1H
END:VALARM
END:VEVENT
END:VCALENDAR
ICS;
$this->caldavBackend->method('getCalendarById')->willReturn([
'{urn:ietf:params:xml:ns:caldav}calendar-timezone' => self::PAGO_PAGO_VTIMEZONE_ICS,
]);
$this->timeFactory->method('getDateTime')->willReturnOnConsecutiveCalls(
new DateTime('2023-02-02T00:00:00Z'),
new DateTime($now),
);
$reminder = [];
$expectedDates = [
(new DateTime($firstReminder))->getTimestamp(),
(new DateTime($nextReminder))->getTimestamp(),
];
$this->backend->expects(self::exactly(2))->method('insertReminder')
->willReturnCallback(function (...$values) use (&$reminder, &$expectedDates): int {
$reminder = array_combine([
'calendar_id', 'object_id', 'uid', 'is_recurring', 'recurrence_id',
'is_recurrence_exception', 'event_hash', 'alarm_hash', 'type',
'is_relative', 'notification_date', 'is_repeat_based',
], $values);
self::assertSame(array_shift($expectedDates), $reminder['notification_date']);
return 1;
});
$this->backend->method('getRemindersToProcess')
->willReturnCallback(function () use (&$reminder, $calendarData): array {
return [$reminder + ['id' => 1, 'calendardata' => $calendarData]];
});
$this->backend->expects(self::once())->method('removeReminder')->with(1);
$this->notificationProviderManager->method('hasProvider')->willReturn(false);

$this->reminderService->onCalendarObjectCreate([
'calendardata' => $calendarData,
'id' => '42',
'calendarid' => '1337',
'component' => 'vevent',
]);
$this->reminderService->processReminders();
}

public function testOnCalendarObjectCreateWithEventTimezoneAndCalendarTimezone():void {
$objectData = [
'calendardata' => self::CALENDAR_DATA_ONE_TIME,
Expand Down
Loading