Card.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\App\IAppManager;
  29. use OCP\IGroupManager;
  30. use OCP\IL10N;
  31. use OCP\IURLGenerator;
  32. use OCP\IUserManager;
  33. use OCP\L10N\IFactory;
  34. class Card extends Base {
  35. public const SUBJECT_ADD = 'card_add';
  36. public const SUBJECT_UPDATE = 'card_update';
  37. public const SUBJECT_DELETE = 'card_delete';
  38. /** @var IFactory */
  39. protected $languageFactory;
  40. /** @var IManager */
  41. protected $activityManager;
  42. /** @var IEventMerger */
  43. protected $eventMerger;
  44. /** @var IAppManager */
  45. protected $appManager;
  46. public function __construct(IFactory $languageFactory,
  47. IURLGenerator $url,
  48. IManager $activityManager,
  49. IUserManager $userManager,
  50. IGroupManager $groupManager,
  51. IEventMerger $eventMerger,
  52. IAppManager $appManager) {
  53. parent::__construct($userManager, $groupManager, $url);
  54. $this->languageFactory = $languageFactory;
  55. $this->activityManager = $activityManager;
  56. $this->eventMerger = $eventMerger;
  57. $this->appManager = $appManager;
  58. }
  59. /**
  60. * @param string $language
  61. * @param IEvent $event
  62. * @param IEvent|null $previousEvent
  63. * @return IEvent
  64. * @throws \InvalidArgumentException
  65. */
  66. public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent {
  67. if ($event->getApp() !== 'dav' || $event->getType() !== 'contacts') {
  68. throw new \InvalidArgumentException();
  69. }
  70. $l = $this->languageFactory->get('dav', $language);
  71. if ($this->activityManager->getRequirePNG()) {
  72. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/contacts-dark.png')));
  73. } else {
  74. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/contacts.svg')));
  75. }
  76. if ($event->getSubject() === self::SUBJECT_ADD) {
  77. $subject = $l->t('{actor} created contact {card} in address book {addressbook}');
  78. } elseif ($event->getSubject() === self::SUBJECT_ADD . '_self') {
  79. $subject = $l->t('You created contact {card} in address book {addressbook}');
  80. } elseif ($event->getSubject() === self::SUBJECT_DELETE) {
  81. $subject = $l->t('{actor} deleted contact {card} from address book {addressbook}');
  82. } elseif ($event->getSubject() === self::SUBJECT_DELETE . '_self') {
  83. $subject = $l->t('You deleted contact {card} from address book {addressbook}');
  84. } elseif ($event->getSubject() === self::SUBJECT_UPDATE) {
  85. $subject = $l->t('{actor} updated contact {card} in address book {addressbook}');
  86. } elseif ($event->getSubject() === self::SUBJECT_UPDATE . '_self') {
  87. $subject = $l->t('You updated contact {card} in address book {addressbook}');
  88. } else {
  89. throw new \InvalidArgumentException();
  90. }
  91. $parsedParameters = $this->getParameters($event, $l);
  92. $this->setSubjects($event, $subject, $parsedParameters);
  93. $event = $this->eventMerger->mergeEvents('card', $event, $previousEvent);
  94. return $event;
  95. }
  96. protected function getParameters(IEvent $event, IL10N $l): array {
  97. $subject = $event->getSubject();
  98. $parameters = $event->getSubjectParameters();
  99. switch ($subject) {
  100. case self::SUBJECT_ADD:
  101. case self::SUBJECT_DELETE:
  102. case self::SUBJECT_UPDATE:
  103. return [
  104. 'actor' => $this->generateUserParameter($parameters['actor']),
  105. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  106. 'card' => $this->generateCardParameter($parameters['card']),
  107. ];
  108. case self::SUBJECT_ADD . '_self':
  109. case self::SUBJECT_DELETE . '_self':
  110. case self::SUBJECT_UPDATE . '_self':
  111. return [
  112. 'addressbook' => $this->generateAddressbookParameter($parameters['addressbook'], $l),
  113. 'card' => $this->generateCardParameter($parameters['card']),
  114. ];
  115. }
  116. throw new \InvalidArgumentException();
  117. }
  118. private function generateCardParameter(array $cardData): array {
  119. return [
  120. 'type' => 'addressbook-contact',
  121. 'id' => $cardData['id'],
  122. 'name' => $cardData['name'],
  123. ];
  124. }
  125. }