Event.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\CalDAV\Activity\Provider;
  7. use OC_App;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IEventMerger;
  10. use OCP\Activity\IManager;
  11. use OCP\App\IAppManager;
  12. use OCP\IGroupManager;
  13. use OCP\IL10N;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserManager;
  16. use OCP\L10N\IFactory;
  17. class Event extends Base {
  18. public const SUBJECT_OBJECT_ADD = 'object_add';
  19. public const SUBJECT_OBJECT_UPDATE = 'object_update';
  20. public const SUBJECT_OBJECT_MOVE = 'object_move';
  21. public const SUBJECT_OBJECT_MOVE_TO_TRASH = 'object_move_to_trash';
  22. public const SUBJECT_OBJECT_RESTORE = 'object_restore';
  23. public const SUBJECT_OBJECT_DELETE = 'object_delete';
  24. /** @var IFactory */
  25. protected $languageFactory;
  26. /** @var IL10N */
  27. protected $l;
  28. /** @var IManager */
  29. protected $activityManager;
  30. /** @var IEventMerger */
  31. protected $eventMerger;
  32. /** @var IAppManager */
  33. protected $appManager;
  34. /**
  35. * @param IFactory $languageFactory
  36. * @param IURLGenerator $url
  37. * @param IManager $activityManager
  38. * @param IUserManager $userManager
  39. * @param IGroupManager $groupManager
  40. * @param IEventMerger $eventMerger
  41. * @param IAppManager $appManager
  42. */
  43. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IGroupManager $groupManager, IEventMerger $eventMerger, IAppManager $appManager) {
  44. parent::__construct($userManager, $groupManager, $url);
  45. $this->languageFactory = $languageFactory;
  46. $this->activityManager = $activityManager;
  47. $this->eventMerger = $eventMerger;
  48. $this->appManager = $appManager;
  49. }
  50. /**
  51. * @param array $eventData
  52. * @return array
  53. */
  54. protected function generateObjectParameter(array $eventData, string $affectedUser): array {
  55. if (!isset($eventData['id']) || !isset($eventData['name'])) {
  56. throw new \InvalidArgumentException();
  57. }
  58. $params = [
  59. 'type' => 'calendar-event',
  60. 'id' => $eventData['id'],
  61. 'name' => trim($eventData['name']) !== '' ? $eventData['name'] : $this->l->t('Untitled event'),
  62. ];
  63. if (isset($eventData['link']) && is_array($eventData['link']) && $this->appManager->isEnabledForUser('calendar')) {
  64. try {
  65. // The calendar app needs to be manually loaded for the routes to be loaded
  66. OC_App::loadApp('calendar');
  67. $linkData = $eventData['link'];
  68. if ($affectedUser === $linkData['owner']) {
  69. $objectId = base64_encode($this->url->getWebroot() . '/remote.php/dav/calendars/' . $linkData['owner'] . '/' . $linkData['calendar_uri'] . '/' . $linkData['object_uri']);
  70. } else {
  71. // Can't use the "real" owner and calendar names here because we create a custom
  72. // calendar for incoming shares with the name "<calendar>_shared_by_<sharer>".
  73. // Hack: Fix the link by generating it for the incoming shared calendar instead,
  74. // as seen from the affected user.
  75. $objectId = base64_encode($this->url->getWebroot() . '/remote.php/dav/calendars/' . $affectedUser . '/' . $linkData['calendar_uri'] . '_shared_by_' . $linkData['owner'] . '/' . $linkData['object_uri']);
  76. }
  77. $link = [
  78. 'view' => 'dayGridMonth',
  79. 'timeRange' => 'now',
  80. 'mode' => 'sidebar',
  81. 'objectId' => $objectId,
  82. 'recurrenceId' => 'next'
  83. ];
  84. $params['link'] = $this->url->linkToRouteAbsolute('calendar.view.indexview.timerange.edit', $link);
  85. } catch (\Exception $error) {
  86. // Do nothing
  87. }
  88. }
  89. return $params;
  90. }
  91. /**
  92. * @param string $language
  93. * @param IEvent $event
  94. * @param IEvent|null $previousEvent
  95. * @return IEvent
  96. * @throws \InvalidArgumentException
  97. * @since 11.0.0
  98. */
  99. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  100. if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar_event') {
  101. throw new \InvalidArgumentException();
  102. }
  103. $this->l = $this->languageFactory->get('dav', $language);
  104. if ($this->activityManager->getRequirePNG()) {
  105. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.png')));
  106. } else {
  107. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar.svg')));
  108. }
  109. if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_event') {
  110. $subject = $this->l->t('{actor} created event {event} in calendar {calendar}');
  111. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_event_self') {
  112. $subject = $this->l->t('You created event {event} in calendar {calendar}');
  113. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_event') {
  114. $subject = $this->l->t('{actor} deleted event {event} from calendar {calendar}');
  115. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_event_self') {
  116. $subject = $this->l->t('You deleted event {event} from calendar {calendar}');
  117. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_event') {
  118. $subject = $this->l->t('{actor} updated event {event} in calendar {calendar}');
  119. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_event_self') {
  120. $subject = $this->l->t('You updated event {event} in calendar {calendar}');
  121. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_event') {
  122. $subject = $this->l->t('{actor} moved event {event} from calendar {sourceCalendar} to calendar {targetCalendar}');
  123. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_event_self') {
  124. $subject = $this->l->t('You moved event {event} from calendar {sourceCalendar} to calendar {targetCalendar}');
  125. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event') {
  126. $subject = $this->l->t('{actor} deleted event {event} from calendar {calendar}');
  127. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event_self') {
  128. $subject = $this->l->t('You deleted event {event} from calendar {calendar}');
  129. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_RESTORE . '_event') {
  130. $subject = $this->l->t('{actor} restored event {event} of calendar {calendar}');
  131. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_RESTORE . '_event_self') {
  132. $subject = $this->l->t('You restored event {event} of calendar {calendar}');
  133. } else {
  134. throw new \InvalidArgumentException();
  135. }
  136. $parsedParameters = $this->getParameters($event);
  137. $this->setSubjects($event, $subject, $parsedParameters);
  138. $event = $this->eventMerger->mergeEvents('event', $event, $previousEvent);
  139. return $event;
  140. }
  141. /**
  142. * @param IEvent $event
  143. * @return array
  144. */
  145. protected function getParameters(IEvent $event) {
  146. $subject = $event->getSubject();
  147. $parameters = $event->getSubjectParameters();
  148. // Nextcloud 13+
  149. if (isset($parameters['calendar'])) {
  150. switch ($subject) {
  151. case self::SUBJECT_OBJECT_ADD . '_event':
  152. case self::SUBJECT_OBJECT_DELETE . '_event':
  153. case self::SUBJECT_OBJECT_UPDATE . '_event':
  154. case self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event':
  155. case self::SUBJECT_OBJECT_RESTORE . '_event':
  156. return [
  157. 'actor' => $this->generateUserParameter($parameters['actor']),
  158. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  159. 'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
  160. ];
  161. case self::SUBJECT_OBJECT_ADD . '_event_self':
  162. case self::SUBJECT_OBJECT_DELETE . '_event_self':
  163. case self::SUBJECT_OBJECT_UPDATE . '_event_self':
  164. case self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event_self':
  165. case self::SUBJECT_OBJECT_RESTORE . '_event_self':
  166. return [
  167. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  168. 'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
  169. ];
  170. }
  171. }
  172. if (isset($parameters['sourceCalendar']) && isset($parameters['targetCalendar'])) {
  173. switch ($subject) {
  174. case self::SUBJECT_OBJECT_MOVE . '_event':
  175. return [
  176. 'actor' => $this->generateUserParameter($parameters['actor']),
  177. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  178. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  179. 'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
  180. ];
  181. case self::SUBJECT_OBJECT_MOVE . '_event_self':
  182. return [
  183. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  184. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  185. 'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
  186. ];
  187. }
  188. }
  189. // Legacy - Do NOT Remove unless necessary
  190. // Removing this will break parsing of activities that were created on
  191. // Nextcloud 12, so we should keep this as long as it's acceptable.
  192. // Otherwise if people upgrade over multiple releases in a short period,
  193. // they will get the dead entries in their stream.
  194. switch ($subject) {
  195. case self::SUBJECT_OBJECT_ADD . '_event':
  196. case self::SUBJECT_OBJECT_DELETE . '_event':
  197. case self::SUBJECT_OBJECT_UPDATE . '_event':
  198. return [
  199. 'actor' => $this->generateUserParameter($parameters[0]),
  200. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  201. 'event' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
  202. ];
  203. case self::SUBJECT_OBJECT_ADD . '_event_self':
  204. case self::SUBJECT_OBJECT_DELETE . '_event_self':
  205. case self::SUBJECT_OBJECT_UPDATE . '_event_self':
  206. return [
  207. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  208. 'event' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
  209. ];
  210. }
  211. throw new \InvalidArgumentException();
  212. }
  213. private function generateClassifiedObjectParameter(array $eventData, string $affectedUser): array {
  214. $parameter = $this->generateObjectParameter($eventData, $affectedUser);
  215. if (!empty($eventData['classified'])) {
  216. $parameter['name'] = $this->l->t('Busy');
  217. }
  218. return $parameter;
  219. }
  220. }