EventComparisonService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2022 Anna Larch <anna.larch@gmx.net>
  5. *
  6. * @author 2022 Anna Larch <anna.larch@gmx.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV;
  25. use OCA\DAV\AppInfo\Application;
  26. use OCA\DAV\CalDAV\Schedule\IMipService;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\IConfig;
  29. use Sabre\VObject\Component\VCalendar;
  30. use Sabre\VObject\Component\VEvent;
  31. use Sabre\VObject\Component\VTimeZone;
  32. use Sabre\VObject\Component\VTodo;
  33. use function max;
  34. class EventComparisonService {
  35. /** @var string[] */
  36. private const EVENT_DIFF = [
  37. 'RECURRENCE-ID',
  38. 'RRULE',
  39. 'SEQUENCE',
  40. 'LAST-MODIFIED'
  41. ];
  42. /**
  43. * If found, remove the event from $eventsToFilter that
  44. * is identical to the passed $filterEvent
  45. * and return whether an identical event was found
  46. *
  47. * This function takes into account the SEQUENCE,
  48. * RRULE, RECURRENCE-ID and LAST-MODIFIED parameters
  49. *
  50. * @param VEvent $filterEvent
  51. * @param array $eventsToFilter
  52. * @return bool true if there was an identical event found and removed, false if there wasn't
  53. */
  54. private function removeIfUnchanged(VEvent $filterEvent, array &$eventsToFilter): bool {
  55. $filterEventData = [];
  56. foreach(self::EVENT_DIFF as $eventDiff) {
  57. $filterEventData[] = IMipService::readPropertyWithDefault($filterEvent, $eventDiff, '');
  58. }
  59. /** @var VEvent $component */
  60. foreach ($eventsToFilter as $k => $eventToFilter) {
  61. $eventToFilterData = [];
  62. foreach(self::EVENT_DIFF as $eventDiff) {
  63. $eventToFilterData[] = IMipService::readPropertyWithDefault($eventToFilter, $eventDiff, '');
  64. }
  65. // events are identical and can be removed
  66. if (empty(array_diff($filterEventData, $eventToFilterData))) {
  67. unset($eventsToFilter[$k]);
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**
  74. * Compare two VCalendars with each other and find all changed elements
  75. *
  76. * Returns an array of old and new events
  77. *
  78. * Old events are only detected if they are also changed
  79. * If there is no corresponding old event for a VEvent, it
  80. * has been newly created
  81. *
  82. * @param VCalendar $new
  83. * @param VCalendar|null $old
  84. * @return array<string, VEvent[]>
  85. */
  86. public function findModified(VCalendar $new, ?VCalendar $old): array {
  87. $newEventComponents = $new->getComponents();
  88. foreach ($newEventComponents as $k => $event) {
  89. if(!$event instanceof VEvent) {
  90. unset($newEventComponents[$k]);
  91. }
  92. }
  93. if(empty($old)) {
  94. return ['old' => null, 'new' => $newEventComponents];
  95. }
  96. $oldEventComponents = $old->getComponents();
  97. if(is_array($oldEventComponents) && !empty($oldEventComponents)) {
  98. foreach ($oldEventComponents as $k => $event) {
  99. if(!$event instanceof VEvent) {
  100. unset($oldEventComponents[$k]);
  101. continue;
  102. }
  103. if($this->removeIfUnchanged($event, $newEventComponents)) {
  104. unset($oldEventComponents[$k]);
  105. }
  106. }
  107. }
  108. return ['old' => array_values($oldEventComponents), 'new' => array_values($newEventComponents)];
  109. }
  110. }