Addressbook.php 7.1 KB

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