Calendar.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 OCP\Activity\Exceptions\UnknownActivityException;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IEventMerger;
  10. use OCP\Activity\IManager;
  11. use OCP\IGroupManager;
  12. use OCP\IL10N;
  13. use OCP\IURLGenerator;
  14. use OCP\IUserManager;
  15. use OCP\L10N\IFactory;
  16. class Calendar extends Base {
  17. public const SUBJECT_ADD = 'calendar_add';
  18. public const SUBJECT_UPDATE = 'calendar_update';
  19. public const SUBJECT_MOVE_TO_TRASH = 'calendar_move_to_trash';
  20. public const SUBJECT_RESTORE = 'calendar_restore';
  21. public const SUBJECT_DELETE = 'calendar_delete';
  22. public const SUBJECT_PUBLISH = 'calendar_publish';
  23. public const SUBJECT_UNPUBLISH = 'calendar_unpublish';
  24. public const SUBJECT_SHARE_USER = 'calendar_user_share';
  25. public const SUBJECT_SHARE_GROUP = 'calendar_group_share';
  26. public const SUBJECT_UNSHARE_USER = 'calendar_user_unshare';
  27. public const SUBJECT_UNSHARE_GROUP = 'calendar_group_unshare';
  28. /** @var IL10N */
  29. protected $l;
  30. /**
  31. * @param IFactory $languageFactory
  32. * @param IURLGenerator $url
  33. * @param IManager $activityManager
  34. * @param IUserManager $userManager
  35. * @param IGroupManager $groupManager
  36. * @param IEventMerger $eventMerger
  37. */
  38. public function __construct(
  39. protected IFactory $languageFactory,
  40. IURLGenerator $url,
  41. protected IManager $activityManager,
  42. IUserManager $userManager,
  43. IGroupManager $groupManager,
  44. protected IEventMerger $eventMerger,
  45. ) {
  46. parent::__construct($userManager, $groupManager, $url);
  47. }
  48. /**
  49. * @param string $language
  50. * @param IEvent $event
  51. * @param IEvent|null $previousEvent
  52. * @return IEvent
  53. * @throws UnknownActivityException
  54. * @since 11.0.0
  55. */
  56. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  57. if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar') {
  58. throw new UnknownActivityException();
  59. }
  60. $this->l = $this->languageFactory->get('dav', $language);
  61. if ($this->activityManager->getRequirePNG()) {
  62. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.png')));
  63. } else {
  64. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar.svg')));
  65. }
  66. if ($event->getSubject() === self::SUBJECT_ADD) {
  67. $subject = $this->l->t('{actor} created calendar {calendar}');
  68. } elseif ($event->getSubject() === self::SUBJECT_ADD . '_self') {
  69. $subject = $this->l->t('You created calendar {calendar}');
  70. } elseif ($event->getSubject() === self::SUBJECT_DELETE) {
  71. $subject = $this->l->t('{actor} deleted calendar {calendar}');
  72. } elseif ($event->getSubject() === self::SUBJECT_DELETE . '_self') {
  73. $subject = $this->l->t('You deleted calendar {calendar}');
  74. } elseif ($event->getSubject() === self::SUBJECT_UPDATE) {
  75. $subject = $this->l->t('{actor} updated calendar {calendar}');
  76. } elseif ($event->getSubject() === self::SUBJECT_UPDATE . '_self') {
  77. $subject = $this->l->t('You updated calendar {calendar}');
  78. } elseif ($event->getSubject() === self::SUBJECT_MOVE_TO_TRASH) {
  79. $subject = $this->l->t('{actor} deleted calendar {calendar}');
  80. } elseif ($event->getSubject() === self::SUBJECT_MOVE_TO_TRASH . '_self') {
  81. $subject = $this->l->t('You deleted calendar {calendar}');
  82. } elseif ($event->getSubject() === self::SUBJECT_RESTORE) {
  83. $subject = $this->l->t('{actor} restored calendar {calendar}');
  84. } elseif ($event->getSubject() === self::SUBJECT_RESTORE . '_self') {
  85. $subject = $this->l->t('You restored calendar {calendar}');
  86. } elseif ($event->getSubject() === self::SUBJECT_PUBLISH . '_self') {
  87. $subject = $this->l->t('You shared calendar {calendar} as public link');
  88. } elseif ($event->getSubject() === self::SUBJECT_UNPUBLISH . '_self') {
  89. $subject = $this->l->t('You removed public link for calendar {calendar}');
  90. } elseif ($event->getSubject() === self::SUBJECT_SHARE_USER) {
  91. $subject = $this->l->t('{actor} shared calendar {calendar} with you');
  92. } elseif ($event->getSubject() === self::SUBJECT_SHARE_USER . '_you') {
  93. $subject = $this->l->t('You shared calendar {calendar} with {user}');
  94. } elseif ($event->getSubject() === self::SUBJECT_SHARE_USER . '_by') {
  95. $subject = $this->l->t('{actor} shared calendar {calendar} with {user}');
  96. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER) {
  97. $subject = $this->l->t('{actor} unshared calendar {calendar} from you');
  98. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_you') {
  99. $subject = $this->l->t('You unshared calendar {calendar} from {user}');
  100. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_by') {
  101. $subject = $this->l->t('{actor} unshared calendar {calendar} from {user}');
  102. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_self') {
  103. $subject = $this->l->t('{actor} unshared calendar {calendar} from themselves');
  104. } elseif ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_you') {
  105. $subject = $this->l->t('You shared calendar {calendar} with group {group}');
  106. } elseif ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_by') {
  107. $subject = $this->l->t('{actor} shared calendar {calendar} with group {group}');
  108. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_you') {
  109. $subject = $this->l->t('You unshared calendar {calendar} from group {group}');
  110. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_by') {
  111. $subject = $this->l->t('{actor} unshared calendar {calendar} from group {group}');
  112. } else {
  113. throw new UnknownActivityException();
  114. }
  115. $parsedParameters = $this->getParameters($event);
  116. $this->setSubjects($event, $subject, $parsedParameters);
  117. $event = $this->eventMerger->mergeEvents('calendar', $event, $previousEvent);
  118. if ($event->getChildEvent() === null) {
  119. if (isset($parsedParameters['user'])) {
  120. // Couldn't group by calendar, maybe we can group by users
  121. $event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
  122. } elseif (isset($parsedParameters['group'])) {
  123. // Couldn't group by calendar, maybe we can group by groups
  124. $event = $this->eventMerger->mergeEvents('group', $event, $previousEvent);
  125. }
  126. }
  127. return $event;
  128. }
  129. /**
  130. * @param IEvent $event
  131. * @return array
  132. */
  133. protected function getParameters(IEvent $event) {
  134. $subject = $event->getSubject();
  135. $parameters = $event->getSubjectParameters();
  136. // Nextcloud 13+
  137. if (isset($parameters['calendar'])) {
  138. switch ($subject) {
  139. case self::SUBJECT_ADD:
  140. case self::SUBJECT_ADD . '_self':
  141. case self::SUBJECT_DELETE:
  142. case self::SUBJECT_DELETE . '_self':
  143. case self::SUBJECT_UPDATE:
  144. case self::SUBJECT_UPDATE . '_self':
  145. case self::SUBJECT_MOVE_TO_TRASH:
  146. case self::SUBJECT_MOVE_TO_TRASH . '_self':
  147. case self::SUBJECT_RESTORE:
  148. case self::SUBJECT_RESTORE . '_self':
  149. case self::SUBJECT_PUBLISH . '_self':
  150. case self::SUBJECT_UNPUBLISH . '_self':
  151. case self::SUBJECT_SHARE_USER:
  152. case self::SUBJECT_UNSHARE_USER:
  153. case self::SUBJECT_UNSHARE_USER . '_self':
  154. return [
  155. 'actor' => $this->generateUserParameter($parameters['actor']),
  156. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  157. ];
  158. case self::SUBJECT_SHARE_USER . '_you':
  159. case self::SUBJECT_UNSHARE_USER . '_you':
  160. return [
  161. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  162. 'user' => $this->generateUserParameter($parameters['user']),
  163. ];
  164. case self::SUBJECT_SHARE_USER . '_by':
  165. case self::SUBJECT_UNSHARE_USER . '_by':
  166. return [
  167. 'actor' => $this->generateUserParameter($parameters['actor']),
  168. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  169. 'user' => $this->generateUserParameter($parameters['user']),
  170. ];
  171. case self::SUBJECT_SHARE_GROUP . '_you':
  172. case self::SUBJECT_UNSHARE_GROUP . '_you':
  173. return [
  174. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  175. 'group' => $this->generateGroupParameter($parameters['group']),
  176. ];
  177. case self::SUBJECT_SHARE_GROUP . '_by':
  178. case self::SUBJECT_UNSHARE_GROUP . '_by':
  179. return [
  180. 'actor' => $this->generateUserParameter($parameters['actor']),
  181. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  182. 'group' => $this->generateGroupParameter($parameters['group']),
  183. ];
  184. }
  185. }
  186. // Legacy - Do NOT Remove unless necessary
  187. // Removing this will break parsing of activities that were created on
  188. // Nextcloud 12, so we should keep this as long as it's acceptable.
  189. // Otherwise if people upgrade over multiple releases in a short period,
  190. // they will get the dead entries in their stream.
  191. switch ($subject) {
  192. case self::SUBJECT_ADD:
  193. case self::SUBJECT_ADD . '_self':
  194. case self::SUBJECT_DELETE:
  195. case self::SUBJECT_DELETE . '_self':
  196. case self::SUBJECT_UPDATE:
  197. case self::SUBJECT_UPDATE . '_self':
  198. case self::SUBJECT_PUBLISH . '_self':
  199. case self::SUBJECT_UNPUBLISH . '_self':
  200. case self::SUBJECT_SHARE_USER:
  201. case self::SUBJECT_UNSHARE_USER:
  202. case self::SUBJECT_UNSHARE_USER . '_self':
  203. return [
  204. 'actor' => $this->generateUserParameter($parameters[0]),
  205. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  206. ];
  207. case self::SUBJECT_SHARE_USER . '_you':
  208. case self::SUBJECT_UNSHARE_USER . '_you':
  209. return [
  210. 'user' => $this->generateUserParameter($parameters[0]),
  211. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  212. ];
  213. case self::SUBJECT_SHARE_USER . '_by':
  214. case self::SUBJECT_UNSHARE_USER . '_by':
  215. return [
  216. 'user' => $this->generateUserParameter($parameters[0]),
  217. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  218. 'actor' => $this->generateUserParameter($parameters[2]),
  219. ];
  220. case self::SUBJECT_SHARE_GROUP . '_you':
  221. case self::SUBJECT_UNSHARE_GROUP . '_you':
  222. return [
  223. 'group' => $this->generateGroupParameter($parameters[0]),
  224. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  225. ];
  226. case self::SUBJECT_SHARE_GROUP . '_by':
  227. case self::SUBJECT_UNSHARE_GROUP . '_by':
  228. return [
  229. 'group' => $this->generateGroupParameter($parameters[0]),
  230. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  231. 'actor' => $this->generateUserParameter($parameters[2]),
  232. ];
  233. }
  234. throw new \InvalidArgumentException();
  235. }
  236. }