diff --git a/apps/dav/lib/CalDAV/Reminder/ReminderService.php b/apps/dav/lib/CalDAV/Reminder/ReminderService.php index 152b8ad08f410..aca3fb7a1fed4 100644 --- a/apps/dav/lib/CalDAV/Reminder/ReminderService.php +++ b/apps/dav/lib/CalDAV/Reminder/ReminderService.php @@ -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); } @@ -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; } @@ -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; } @@ -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 @@ -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, @@ -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()); @@ -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']); @@ -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 @@ -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 @@ -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 diff --git a/apps/dav/tests/unit/CalDAV/Reminder/ReminderServiceTest.php b/apps/dav/tests/unit/CalDAV/Reminder/ReminderServiceTest.php index c1136c2aa5fd3..e6b7e2e74fbba 100644 --- a/apps/dav/tests/unit/CalDAV/Reminder/ReminderServiceTest.php +++ b/apps/dav/tests/unit/CalDAV/Reminder/ReminderServiceTest.php @@ -562,6 +562,182 @@ public function testOnCalendarObjectCreateRecurringEntryWithRepeat():void { $this->reminderService->onCalendarObjectCreate($objectData); } + private const string CUSTOM_VTIMEZONE_ICS = << ['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 = <<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 = <<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,