Event.php 10 KB

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