Addressbook.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CardDAV\Activity\Provider;
  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 Addressbook extends Base {
  17. public const SUBJECT_ADD = 'addressbook_add';
  18. public const SUBJECT_UPDATE = 'addressbook_update';
  19. public const SUBJECT_DELETE = 'addressbook_delete';
  20. public const SUBJECT_SHARE_USER = 'addressbook_user_share';
  21. public const SUBJECT_SHARE_GROUP = 'addressbook_group_share';
  22. public const SUBJECT_UNSHARE_USER = 'addressbook_user_unshare';
  23. public const SUBJECT_UNSHARE_GROUP = 'addressbook_group_unshare';
  24. /** @var IFactory */
  25. protected $languageFactory;
  26. /** @var IManager */
  27. protected $activityManager;
  28. /** @var IEventMerger */
  29. protected $eventMerger;
  30. public function __construct(IFactory $languageFactory,
  31. IURLGenerator $url,
  32. IManager $activityManager,
  33. IUserManager $userManager,
  34. IGroupManager $groupManager,
  35. IEventMerger $eventMerger) {
  36. parent::__construct($userManager, $groupManager, $url);
  37. $this->languageFactory = $languageFactory;
  38. $this->activityManager = $activityManager;
  39. $this->eventMerger = $eventMerger;
  40. }
  41. /**
  42. * @param string $language
  43. * @param IEvent $event
  44. * @param IEvent|null $previousEvent
  45. * @return IEvent
  46. * @throws \InvalidArgumentException
  47. */
  48. public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
  49. if ($event->getApp() !== 'dav' || $event->getType() !== 'contacts') {
  50. throw new \InvalidArgumentException();
  51. }
  52. $l = $this->languageFactory->get('dav', $language);
  53. if ($this->activityManager->getRequirePNG()) {
  54. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/contacts-dark.png')));
  55. } else {
  56. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/contacts.svg')));
  57. }
  58. if ($event->getSubject() === self::SUBJECT_ADD) {
  59. $subject = $l->t('{actor} created address book {addressbook}');
  60. } elseif ($event->getSubject() === self::SUBJECT_ADD . '_self') {
  61. $subject = $l->t('You created address book {addressbook}');
  62. } elseif ($event->getSubject() === self::SUBJECT_DELETE) {
  63. $subject = $l->t('{actor} deleted address book {addressbook}');
  64. } elseif ($event->getSubject() === self::SUBJECT_DELETE . '_self') {
  65. $subject = $l->t('You deleted address book {addressbook}');
  66. } elseif ($event->getSubject() === self::SUBJECT_UPDATE) {
  67. $subject = $l->t('{actor} updated address book {addressbook}');
  68. } elseif ($event->getSubject() === self::SUBJECT_UPDATE . '_self') {
  69. $subject = $l->t('You updated address book {addressbook}');
  70. } elseif ($event->getSubject() === self::SUBJECT_SHARE_USER) {
  71. $subject = $l->t('{actor} shared address book {addressbook} with you');
  72. } elseif ($event->getSubject() === self::SUBJECT_SHARE_USER . '_you') {
  73. $subject = $l->t('You shared address book {addressbook} with {user}');
  74. } elseif ($event->getSubject() === self::SUBJECT_SHARE_USER . '_by') {
  75. $subject = $l->t('{actor} shared address book {addressbook} with {user}');
  76. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER) {
  77. $subject = $l->t('{actor} unshared address book {addressbook} from you');
  78. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_you') {
  79. $subject = $l->t('You unshared address book {addressbook} from {user}');
  80. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_by') {
  81. $subject = $l->t('{actor} unshared address book {addressbook} from {user}');
  82. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_self') {
  83. $subject = $l->t('{actor} unshared address book {addressbook} from themselves');
  84. } elseif ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_you') {
  85. $subject = $l->t('You shared address book {addressbook} with group {group}');
  86. } elseif ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_by') {
  87. $subject = $l->t('{actor} shared address book {addressbook} with group {group}');
  88. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_you') {
  89. $subject = $l->t('You unshared address book {addressbook} from group {group}');
  90. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_by') {
  91. $subject = $l->t('{actor} unshared address book {addressbook} from group {group}');
  92. } else {
  93. throw new \InvalidArgumentException();
  94. }
  95. $parsedParameters = $this->getParameters($event, $l);
  96. $this->setSubjects($event, $subject, $parsedParameters);
  97. $event = $this->eventMerger->mergeEvents('addressbook', $event, $previousEvent);
  98. if ($event->getChildEvent() === null) {
  99. if (isset($parsedParameters['user'])) {
  100. // Couldn't group by calendar, maybe we can group by users
  101. $event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
  102. } elseif (isset($parsedParameters['group'])) {
  103. // Couldn't group by calendar, maybe we can group by groups
  104. $event = $this->eventMerger->mergeEvents('group', $event, $previousEvent);
  105. }
  106. }
  107. return $event;
  108. }
  109. protected function getParameters(IEvent $event, IL10N $l): array {
  110. $subject = $event->getSubject();
  111. $parameters = $event->getSubjectParameters();
  112. switch ($subject) {
  113. case self::SUBJECT_ADD:
  114. case self::SUBJECT_ADD . '_self':
  115. case self::SUBJECT_DELETE:
  116. case self::SUBJECT_DELETE . '_self':
  117. case self::SUBJECT_UPDATE:
  118. case self::SUBJECT_UPDATE . '_self':
  119. case self::SUBJECT_SHARE_USER:
  120. case self::SUBJECT_UNSHARE_USER:
  121. case self::SUBJECT_UNSHARE_USER . '_self':
  122. return [
  123. 'actor' => $this->generateUserParameter($parameters['actor']),
  124. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  125. ];
  126. case self::SUBJECT_SHARE_USER . '_you':
  127. case self::SUBJECT_UNSHARE_USER . '_you':
  128. return [
  129. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  130. 'user' => $this->generateUserParameter($parameters['user']),
  131. ];
  132. case self::SUBJECT_SHARE_USER . '_by':
  133. case self::SUBJECT_UNSHARE_USER . '_by':
  134. return [
  135. 'actor' => $this->generateUserParameter($parameters['actor']),
  136. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  137. 'user' => $this->generateUserParameter($parameters['user']),
  138. ];
  139. case self::SUBJECT_SHARE_GROUP . '_you':
  140. case self::SUBJECT_UNSHARE_GROUP . '_you':
  141. return [
  142. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  143. 'group' => $this->generateGroupParameter($parameters['group']),
  144. ];
  145. case self::SUBJECT_SHARE_GROUP . '_by':
  146. case self::SUBJECT_UNSHARE_GROUP . '_by':
  147. return [
  148. 'actor' => $this->generateUserParameter($parameters['actor']),
  149. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  150. 'group' => $this->generateGroupParameter($parameters['group']),
  151. ];
  152. }
  153. throw new \InvalidArgumentException();
  154. }
  155. }