Event.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. $calendarUri = $this->urlencodeLowerHex($linkData['calendar_uri']);
  69. if ($affectedUser === $linkData['owner']) {
  70. $objectId = base64_encode($this->url->getWebroot() . '/remote.php/dav/calendars/' . $linkData['owner'] . '/' . $calendarUri . '/' . $linkData['object_uri']);
  71. } else {
  72. // Can't use the "real" owner and calendar names here because we create a custom
  73. // calendar for incoming shares with the name "<calendar>_shared_by_<sharer>".
  74. // Hack: Fix the link by generating it for the incoming shared calendar instead,
  75. // as seen from the affected user.
  76. $objectId = base64_encode($this->url->getWebroot() . '/remote.php/dav/calendars/' . $affectedUser . '/' . $calendarUri . '_shared_by_' . $linkData['owner'] . '/' . $linkData['object_uri']);
  77. }
  78. $link = [
  79. 'view' => 'dayGridMonth',
  80. 'timeRange' => 'now',
  81. 'mode' => 'sidebar',
  82. 'objectId' => $objectId,
  83. 'recurrenceId' => 'next'
  84. ];
  85. $params['link'] = $this->url->linkToRouteAbsolute('calendar.view.indexview.timerange.edit', $link);
  86. } catch (\Exception $error) {
  87. // Do nothing
  88. }
  89. }
  90. return $params;
  91. }
  92. /**
  93. * @param string $language
  94. * @param IEvent $event
  95. * @param IEvent|null $previousEvent
  96. * @return IEvent
  97. * @throws \InvalidArgumentException
  98. * @since 11.0.0
  99. */
  100. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  101. if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar_event') {
  102. throw new \InvalidArgumentException();
  103. }
  104. $this->l = $this->languageFactory->get('dav', $language);
  105. if ($this->activityManager->getRequirePNG()) {
  106. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.png')));
  107. } else {
  108. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar.svg')));
  109. }
  110. if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_event') {
  111. $subject = $this->l->t('{actor} created event {event} in calendar {calendar}');
  112. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_event_self') {
  113. $subject = $this->l->t('You created event {event} in calendar {calendar}');
  114. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_event') {
  115. $subject = $this->l->t('{actor} deleted event {event} from calendar {calendar}');
  116. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_event_self') {
  117. $subject = $this->l->t('You deleted event {event} from calendar {calendar}');
  118. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_event') {
  119. $subject = $this->l->t('{actor} updated event {event} in calendar {calendar}');
  120. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_event_self') {
  121. $subject = $this->l->t('You updated event {event} in calendar {calendar}');
  122. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_event') {
  123. $subject = $this->l->t('{actor} moved event {event} from calendar {sourceCalendar} to calendar {targetCalendar}');
  124. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_event_self') {
  125. $subject = $this->l->t('You moved event {event} from calendar {sourceCalendar} to calendar {targetCalendar}');
  126. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event') {
  127. $subject = $this->l->t('{actor} deleted event {event} from calendar {calendar}');
  128. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event_self') {
  129. $subject = $this->l->t('You deleted event {event} from calendar {calendar}');
  130. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_RESTORE . '_event') {
  131. $subject = $this->l->t('{actor} restored event {event} of calendar {calendar}');
  132. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_RESTORE . '_event_self') {
  133. $subject = $this->l->t('You restored event {event} of calendar {calendar}');
  134. } else {
  135. throw new \InvalidArgumentException();
  136. }
  137. $parsedParameters = $this->getParameters($event);
  138. $this->setSubjects($event, $subject, $parsedParameters);
  139. $event = $this->eventMerger->mergeEvents('event', $event, $previousEvent);
  140. return $event;
  141. }
  142. /**
  143. * @param IEvent $event
  144. * @return array
  145. */
  146. protected function getParameters(IEvent $event) {
  147. $subject = $event->getSubject();
  148. $parameters = $event->getSubjectParameters();
  149. // Nextcloud 13+
  150. if (isset($parameters['calendar'])) {
  151. switch ($subject) {
  152. case self::SUBJECT_OBJECT_ADD . '_event':
  153. case self::SUBJECT_OBJECT_DELETE . '_event':
  154. case self::SUBJECT_OBJECT_UPDATE . '_event':
  155. case self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event':
  156. case self::SUBJECT_OBJECT_RESTORE . '_event':
  157. return [
  158. 'actor' => $this->generateUserParameter($parameters['actor']),
  159. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  160. 'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
  161. ];
  162. case self::SUBJECT_OBJECT_ADD . '_event_self':
  163. case self::SUBJECT_OBJECT_DELETE . '_event_self':
  164. case self::SUBJECT_OBJECT_UPDATE . '_event_self':
  165. case self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event_self':
  166. case self::SUBJECT_OBJECT_RESTORE . '_event_self':
  167. return [
  168. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  169. 'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
  170. ];
  171. }
  172. }
  173. if (isset($parameters['sourceCalendar']) && isset($parameters['targetCalendar'])) {
  174. switch ($subject) {
  175. case self::SUBJECT_OBJECT_MOVE . '_event':
  176. return [
  177. 'actor' => $this->generateUserParameter($parameters['actor']),
  178. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  179. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  180. 'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
  181. ];
  182. case self::SUBJECT_OBJECT_MOVE . '_event_self':
  183. return [
  184. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  185. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  186. 'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
  187. ];
  188. }
  189. }
  190. // Legacy - Do NOT Remove unless necessary
  191. // Removing this will break parsing of activities that were created on
  192. // Nextcloud 12, so we should keep this as long as it's acceptable.
  193. // Otherwise if people upgrade over multiple releases in a short period,
  194. // they will get the dead entries in their stream.
  195. switch ($subject) {
  196. case self::SUBJECT_OBJECT_ADD . '_event':
  197. case self::SUBJECT_OBJECT_DELETE . '_event':
  198. case self::SUBJECT_OBJECT_UPDATE . '_event':
  199. return [
  200. 'actor' => $this->generateUserParameter($parameters[0]),
  201. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  202. 'event' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
  203. ];
  204. case self::SUBJECT_OBJECT_ADD . '_event_self':
  205. case self::SUBJECT_OBJECT_DELETE . '_event_self':
  206. case self::SUBJECT_OBJECT_UPDATE . '_event_self':
  207. return [
  208. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  209. 'event' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
  210. ];
  211. }
  212. throw new \InvalidArgumentException();
  213. }
  214. private function generateClassifiedObjectParameter(array $eventData, string $affectedUser): array {
  215. $parameter = $this->generateObjectParameter($eventData, $affectedUser);
  216. if (!empty($eventData['classified'])) {
  217. $parameter['name'] = $this->l->t('Busy');
  218. }
  219. return $parameter;
  220. }
  221. /**
  222. * Return urlencoded string but with lower cased hex sequences.
  223. * The remaining casing will be untouched.
  224. */
  225. private function urlencodeLowerHex(string $raw): string {
  226. return preg_replace_callback(
  227. '/%[0-9A-F]{2}/',
  228. static fn (array $matches) => strtolower($matches[0]),
  229. urlencode($raw),
  230. );
  231. }
  232. }