EventComparisonService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV;
  8. use OCA\DAV\CalDAV\Schedule\IMipService;
  9. use Sabre\VObject\Component\VCalendar;
  10. use Sabre\VObject\Component\VEvent;
  11. class EventComparisonService {
  12. /** @var string[] */
  13. private const EVENT_DIFF = [
  14. 'RECURRENCE-ID',
  15. 'RRULE',
  16. 'SEQUENCE',
  17. 'LAST-MODIFIED'
  18. ];
  19. /**
  20. * If found, remove the event from $eventsToFilter that
  21. * is identical to the passed $filterEvent
  22. * and return whether an identical event was found
  23. *
  24. * This function takes into account the SEQUENCE,
  25. * RRULE, RECURRENCE-ID and LAST-MODIFIED parameters
  26. *
  27. * @param VEvent $filterEvent
  28. * @param array $eventsToFilter
  29. * @return bool true if there was an identical event found and removed, false if there wasn't
  30. */
  31. private function removeIfUnchanged(VEvent $filterEvent, array &$eventsToFilter): bool {
  32. $filterEventData = [];
  33. foreach(self::EVENT_DIFF as $eventDiff) {
  34. $filterEventData[] = IMipService::readPropertyWithDefault($filterEvent, $eventDiff, '');
  35. }
  36. /** @var VEvent $component */
  37. foreach ($eventsToFilter as $k => $eventToFilter) {
  38. $eventToFilterData = [];
  39. foreach(self::EVENT_DIFF as $eventDiff) {
  40. $eventToFilterData[] = IMipService::readPropertyWithDefault($eventToFilter, $eventDiff, '');
  41. }
  42. // events are identical and can be removed
  43. if ($filterEventData === $eventToFilterData) {
  44. unset($eventsToFilter[$k]);
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. /**
  51. * Compare two VCalendars with each other and find all changed elements
  52. *
  53. * Returns an array of old and new events
  54. *
  55. * Old events are only detected if they are also changed
  56. * If there is no corresponding old event for a VEvent, it
  57. * has been newly created
  58. *
  59. * @param VCalendar $new
  60. * @param VCalendar|null $old
  61. * @return array<string, VEvent[]|null>
  62. */
  63. public function findModified(VCalendar $new, ?VCalendar $old): array {
  64. $newEventComponents = $new->getComponents();
  65. foreach ($newEventComponents as $k => $event) {
  66. if(!$event instanceof VEvent) {
  67. unset($newEventComponents[$k]);
  68. }
  69. }
  70. if(empty($old)) {
  71. return ['old' => null, 'new' => $newEventComponents];
  72. }
  73. $oldEventComponents = $old->getComponents();
  74. if(is_array($oldEventComponents) && !empty($oldEventComponents)) {
  75. foreach ($oldEventComponents as $k => $event) {
  76. if(!$event instanceof VEvent) {
  77. unset($oldEventComponents[$k]);
  78. continue;
  79. }
  80. if($this->removeIfUnchanged($event, $newEventComponents)) {
  81. unset($oldEventComponents[$k]);
  82. }
  83. }
  84. }
  85. return ['old' => array_values($oldEventComponents), 'new' => array_values($newEventComponents)];
  86. }
  87. }