Event.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\CalDAV\Activity\Provider;
  28. use OC_App;
  29. use OCP\Activity\IEvent;
  30. use OCP\Activity\IEventMerger;
  31. use OCP\Activity\IManager;
  32. use OCP\App\IAppManager;
  33. use OCP\IGroupManager;
  34. use OCP\IL10N;
  35. use OCP\IURLGenerator;
  36. use OCP\IUserManager;
  37. use OCP\L10N\IFactory;
  38. class Event extends Base {
  39. public const SUBJECT_OBJECT_ADD = 'object_add';
  40. public const SUBJECT_OBJECT_UPDATE = 'object_update';
  41. public const SUBJECT_OBJECT_MOVE = 'object_move';
  42. public const SUBJECT_OBJECT_MOVE_TO_TRASH = 'object_move_to_trash';
  43. public const SUBJECT_OBJECT_RESTORE = 'object_restore';
  44. public const SUBJECT_OBJECT_DELETE = 'object_delete';
  45. /** @var IFactory */
  46. protected $languageFactory;
  47. /** @var IL10N */
  48. protected $l;
  49. /** @var IManager */
  50. protected $activityManager;
  51. /** @var IEventMerger */
  52. protected $eventMerger;
  53. /** @var IAppManager */
  54. protected $appManager;
  55. /**
  56. * @param IFactory $languageFactory
  57. * @param IURLGenerator $url
  58. * @param IManager $activityManager
  59. * @param IUserManager $userManager
  60. * @param IGroupManager $groupManager
  61. * @param IEventMerger $eventMerger
  62. * @param IAppManager $appManager
  63. */
  64. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IGroupManager $groupManager, IEventMerger $eventMerger, IAppManager $appManager) {
  65. parent::__construct($userManager, $groupManager, $url);
  66. $this->languageFactory = $languageFactory;
  67. $this->activityManager = $activityManager;
  68. $this->eventMerger = $eventMerger;
  69. $this->appManager = $appManager;
  70. }
  71. /**
  72. * @param array $eventData
  73. * @return array
  74. */
  75. protected function generateObjectParameter(array $eventData) {
  76. if (!isset($eventData['id']) || !isset($eventData['name'])) {
  77. throw new \InvalidArgumentException();
  78. }
  79. $params = [
  80. 'type' => 'calendar-event',
  81. 'id' => $eventData['id'],
  82. 'name' => trim($eventData['name']) !== '' ? $eventData['name'] : $this->l->t('Untitled event'),
  83. ];
  84. if (isset($eventData['link']) && is_array($eventData['link']) && $this->appManager->isEnabledForUser('calendar')) {
  85. try {
  86. // The calendar app needs to be manually loaded for the routes to be loaded
  87. OC_App::loadApp('calendar');
  88. $linkData = $eventData['link'];
  89. $objectId = base64_encode('/remote.php/dav/calendars/' . $linkData['owner'] . '/' . $linkData['calendar_uri'] . '/' . $linkData['object_uri']);
  90. $link = [
  91. 'view' => 'dayGridMonth',
  92. 'timeRange' => 'now',
  93. 'mode' => 'sidebar',
  94. 'objectId' => $objectId,
  95. 'recurrenceId' => 'next'
  96. ];
  97. $params['link'] = $this->url->linkToRouteAbsolute('calendar.view.indexview.timerange.edit', $link);
  98. } catch (\Exception $error) {
  99. // Do nothing
  100. }
  101. }
  102. return $params;
  103. }
  104. /**
  105. * @param string $language
  106. * @param IEvent $event
  107. * @param IEvent|null $previousEvent
  108. * @return IEvent
  109. * @throws \InvalidArgumentException
  110. * @since 11.0.0
  111. */
  112. public function parse($language, IEvent $event, IEvent $previousEvent = null) {
  113. if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar_event') {
  114. throw new \InvalidArgumentException();
  115. }
  116. $this->l = $this->languageFactory->get('dav', $language);
  117. if ($this->activityManager->getRequirePNG()) {
  118. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.png')));
  119. } else {
  120. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar.svg')));
  121. }
  122. if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_event') {
  123. $subject = $this->l->t('{actor} created event {event} in calendar {calendar}');
  124. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_event_self') {
  125. $subject = $this->l->t('You created event {event} in calendar {calendar}');
  126. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_event') {
  127. $subject = $this->l->t('{actor} deleted event {event} from calendar {calendar}');
  128. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_event_self') {
  129. $subject = $this->l->t('You deleted event {event} from calendar {calendar}');
  130. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_event') {
  131. $subject = $this->l->t('{actor} updated event {event} in calendar {calendar}');
  132. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_event_self') {
  133. $subject = $this->l->t('You updated event {event} in calendar {calendar}');
  134. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_event') {
  135. $subject = $this->l->t('{actor} moved event {event} from calendar {sourceCalendar} to calendar {targetCalendar}');
  136. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_event_self') {
  137. $subject = $this->l->t('You moved event {event} from calendar {sourceCalendar} to calendar {targetCalendar}');
  138. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event') {
  139. $subject = $this->l->t('{actor} deleted event {event} from calendar {calendar}');
  140. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event_self') {
  141. $subject = $this->l->t('You deleted event {event} from calendar {calendar}');
  142. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_RESTORE . '_event') {
  143. $subject = $this->l->t('{actor} restored event {event} of calendar {calendar}');
  144. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_RESTORE . '_event_self') {
  145. $subject = $this->l->t('You restored event {event} of calendar {calendar}');
  146. } else {
  147. throw new \InvalidArgumentException();
  148. }
  149. $parsedParameters = $this->getParameters($event);
  150. $this->setSubjects($event, $subject, $parsedParameters);
  151. $event = $this->eventMerger->mergeEvents('event', $event, $previousEvent);
  152. return $event;
  153. }
  154. /**
  155. * @param IEvent $event
  156. * @return array
  157. */
  158. protected function getParameters(IEvent $event) {
  159. $subject = $event->getSubject();
  160. $parameters = $event->getSubjectParameters();
  161. // Nextcloud 13+
  162. if (isset($parameters['calendar'])) {
  163. switch ($subject) {
  164. case self::SUBJECT_OBJECT_ADD . '_event':
  165. case self::SUBJECT_OBJECT_DELETE . '_event':
  166. case self::SUBJECT_OBJECT_UPDATE . '_event':
  167. case self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event':
  168. case self::SUBJECT_OBJECT_RESTORE . '_event':
  169. return [
  170. 'actor' => $this->generateUserParameter($parameters['actor']),
  171. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  172. 'event' => $this->generateClassifiedObjectParameter($parameters['object']),
  173. ];
  174. case self::SUBJECT_OBJECT_ADD . '_event_self':
  175. case self::SUBJECT_OBJECT_DELETE . '_event_self':
  176. case self::SUBJECT_OBJECT_UPDATE . '_event_self':
  177. case self::SUBJECT_OBJECT_MOVE_TO_TRASH . '_event_self':
  178. case self::SUBJECT_OBJECT_RESTORE . '_event_self':
  179. return [
  180. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  181. 'event' => $this->generateClassifiedObjectParameter($parameters['object']),
  182. ];
  183. }
  184. }
  185. if (isset($parameters['sourceCalendar']) && isset($parameters['targetCalendar'])) {
  186. switch ($subject) {
  187. case self::SUBJECT_OBJECT_MOVE . '_event':
  188. return [
  189. 'actor' => $this->generateUserParameter($parameters['actor']),
  190. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  191. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  192. 'event' => $this->generateClassifiedObjectParameter($parameters['object']),
  193. ];
  194. case self::SUBJECT_OBJECT_MOVE . '_event_self':
  195. return [
  196. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  197. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  198. 'event' => $this->generateClassifiedObjectParameter($parameters['object']),
  199. ];
  200. }
  201. }
  202. // Legacy - Do NOT Remove unless necessary
  203. // Removing this will break parsing of activities that were created on
  204. // Nextcloud 12, so we should keep this as long as it's acceptable.
  205. // Otherwise if people upgrade over multiple releases in a short period,
  206. // they will get the dead entries in their stream.
  207. switch ($subject) {
  208. case self::SUBJECT_OBJECT_ADD . '_event':
  209. case self::SUBJECT_OBJECT_DELETE . '_event':
  210. case self::SUBJECT_OBJECT_UPDATE . '_event':
  211. return [
  212. 'actor' => $this->generateUserParameter($parameters[0]),
  213. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  214. 'event' => $this->generateObjectParameter($parameters[2]),
  215. ];
  216. case self::SUBJECT_OBJECT_ADD . '_event_self':
  217. case self::SUBJECT_OBJECT_DELETE . '_event_self':
  218. case self::SUBJECT_OBJECT_UPDATE . '_event_self':
  219. return [
  220. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  221. 'event' => $this->generateObjectParameter($parameters[2]),
  222. ];
  223. }
  224. throw new \InvalidArgumentException();
  225. }
  226. private function generateClassifiedObjectParameter(array $eventData) {
  227. $parameter = $this->generateObjectParameter($eventData);
  228. if (!empty($eventData['classified'])) {
  229. $parameter['name'] = $this->l->t('Busy');
  230. }
  231. return $parameter;
  232. }
  233. }