Calendar.php 11 KB

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