Card.php 4.1 KB

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