Addressbook.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CardDAV\Activity\Provider;
  25. use OCP\Activity\IEvent;
  26. use OCP\Activity\IEventMerger;
  27. use OCP\Activity\IManager;
  28. use OCP\IGroupManager;
  29. use OCP\IL10N;
  30. use OCP\IURLGenerator;
  31. use OCP\IUserManager;
  32. use OCP\L10N\IFactory;
  33. class Addressbook extends Base {
  34. public const SUBJECT_ADD = 'addressbook_add';
  35. public const SUBJECT_UPDATE = 'addressbook_update';
  36. public const SUBJECT_DELETE = 'addressbook_delete';
  37. public const SUBJECT_SHARE_USER = 'addressbook_user_share';
  38. public const SUBJECT_SHARE_GROUP = 'addressbook_group_share';
  39. public const SUBJECT_UNSHARE_USER = 'addressbook_user_unshare';
  40. public const SUBJECT_UNSHARE_GROUP = 'addressbook_group_unshare';
  41. /** @var IFactory */
  42. protected $languageFactory;
  43. /** @var IManager */
  44. protected $activityManager;
  45. /** @var IEventMerger */
  46. protected $eventMerger;
  47. public function __construct(IFactory $languageFactory,
  48. IURLGenerator $url,
  49. IManager $activityManager,
  50. IUserManager $userManager,
  51. IGroupManager $groupManager,
  52. IEventMerger $eventMerger) {
  53. parent::__construct($userManager, $groupManager, $url);
  54. $this->languageFactory = $languageFactory;
  55. $this->activityManager = $activityManager;
  56. $this->eventMerger = $eventMerger;
  57. }
  58. /**
  59. * @param string $language
  60. * @param IEvent $event
  61. * @param IEvent|null $previousEvent
  62. * @return IEvent
  63. * @throws \InvalidArgumentException
  64. */
  65. public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
  66. if ($event->getApp() !== 'dav' || $event->getType() !== 'contacts') {
  67. throw new \InvalidArgumentException();
  68. }
  69. $l = $this->languageFactory->get('dav', $language);
  70. if ($this->activityManager->getRequirePNG()) {
  71. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/contacts-dark.png')));
  72. } else {
  73. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/contacts.svg')));
  74. }
  75. if ($event->getSubject() === self::SUBJECT_ADD) {
  76. $subject = $l->t('{actor} created address book {addressbook}');
  77. } elseif ($event->getSubject() === self::SUBJECT_ADD . '_self') {
  78. $subject = $l->t('You created address book {addressbook}');
  79. } elseif ($event->getSubject() === self::SUBJECT_DELETE) {
  80. $subject = $l->t('{actor} deleted address book {addressbook}');
  81. } elseif ($event->getSubject() === self::SUBJECT_DELETE . '_self') {
  82. $subject = $l->t('You deleted address book {addressbook}');
  83. } elseif ($event->getSubject() === self::SUBJECT_UPDATE) {
  84. $subject = $l->t('{actor} updated address book {addressbook}');
  85. } elseif ($event->getSubject() === self::SUBJECT_UPDATE . '_self') {
  86. $subject = $l->t('You updated address book {addressbook}');
  87. } elseif ($event->getSubject() === self::SUBJECT_SHARE_USER) {
  88. $subject = $l->t('{actor} shared address book {addressbook} with you');
  89. } elseif ($event->getSubject() === self::SUBJECT_SHARE_USER . '_you') {
  90. $subject = $l->t('You shared address book {addressbook} with {user}');
  91. } elseif ($event->getSubject() === self::SUBJECT_SHARE_USER . '_by') {
  92. $subject = $l->t('{actor} shared address book {addressbook} with {user}');
  93. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER) {
  94. $subject = $l->t('{actor} unshared address book {addressbook} from you');
  95. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_you') {
  96. $subject = $l->t('You unshared address book {addressbook} from {user}');
  97. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_by') {
  98. $subject = $l->t('{actor} unshared address book {addressbook} from {user}');
  99. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_self') {
  100. $subject = $l->t('{actor} unshared address book {addressbook} from themselves');
  101. } elseif ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_you') {
  102. $subject = $l->t('You shared address book {addressbook} with group {group}');
  103. } elseif ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_by') {
  104. $subject = $l->t('{actor} shared address book {addressbook} with group {group}');
  105. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_you') {
  106. $subject = $l->t('You unshared address book {addressbook} from group {group}');
  107. } elseif ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_by') {
  108. $subject = $l->t('{actor} unshared address book {addressbook} from group {group}');
  109. } else {
  110. throw new \InvalidArgumentException();
  111. }
  112. $parsedParameters = $this->getParameters($event, $l);
  113. $this->setSubjects($event, $subject, $parsedParameters);
  114. $event = $this->eventMerger->mergeEvents('addressbook', $event, $previousEvent);
  115. if ($event->getChildEvent() === null) {
  116. if (isset($parsedParameters['user'])) {
  117. // Couldn't group by calendar, maybe we can group by users
  118. $event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
  119. } elseif (isset($parsedParameters['group'])) {
  120. // Couldn't group by calendar, maybe we can group by groups
  121. $event = $this->eventMerger->mergeEvents('group', $event, $previousEvent);
  122. }
  123. }
  124. return $event;
  125. }
  126. protected function getParameters(IEvent $event, IL10N $l): array {
  127. $subject = $event->getSubject();
  128. $parameters = $event->getSubjectParameters();
  129. switch ($subject) {
  130. case self::SUBJECT_ADD:
  131. case self::SUBJECT_ADD . '_self':
  132. case self::SUBJECT_DELETE:
  133. case self::SUBJECT_DELETE . '_self':
  134. case self::SUBJECT_UPDATE:
  135. case self::SUBJECT_UPDATE . '_self':
  136. case self::SUBJECT_SHARE_USER:
  137. case self::SUBJECT_UNSHARE_USER:
  138. case self::SUBJECT_UNSHARE_USER . '_self':
  139. return [
  140. 'actor' => $this->generateUserParameter($parameters['actor']),
  141. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  142. ];
  143. case self::SUBJECT_SHARE_USER . '_you':
  144. case self::SUBJECT_UNSHARE_USER . '_you':
  145. return [
  146. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  147. 'user' => $this->generateUserParameter($parameters['user']),
  148. ];
  149. case self::SUBJECT_SHARE_USER . '_by':
  150. case self::SUBJECT_UNSHARE_USER . '_by':
  151. return [
  152. 'actor' => $this->generateUserParameter($parameters['actor']),
  153. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  154. 'user' => $this->generateUserParameter($parameters['user']),
  155. ];
  156. case self::SUBJECT_SHARE_GROUP . '_you':
  157. case self::SUBJECT_UNSHARE_GROUP . '_you':
  158. return [
  159. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  160. 'group' => $this->generateGroupParameter($parameters['group']),
  161. ];
  162. case self::SUBJECT_SHARE_GROUP . '_by':
  163. case self::SUBJECT_UNSHARE_GROUP . '_by':
  164. return [
  165. 'actor' => $this->generateUserParameter($parameters['actor']),
  166. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  167. 'group' => $this->generateGroupParameter($parameters['group']),
  168. ];
  169. }
  170. throw new \InvalidArgumentException();
  171. }
  172. }