FavoriteProvider.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files\Activity;
  7. use OCP\Activity\IEvent;
  8. use OCP\Activity\IEventMerger;
  9. use OCP\Activity\IManager;
  10. use OCP\Activity\IProvider;
  11. use OCP\IL10N;
  12. use OCP\IURLGenerator;
  13. use OCP\L10N\IFactory;
  14. class FavoriteProvider implements IProvider {
  15. public const SUBJECT_ADDED = 'added_favorite';
  16. public const SUBJECT_REMOVED = 'removed_favorite';
  17. /** @var IFactory */
  18. protected $languageFactory;
  19. /** @var IL10N */
  20. protected $l;
  21. /** @var IURLGenerator */
  22. protected $url;
  23. /** @var IManager */
  24. protected $activityManager;
  25. /** @var IEventMerger */
  26. protected $eventMerger;
  27. /**
  28. * @param IFactory $languageFactory
  29. * @param IURLGenerator $url
  30. * @param IManager $activityManager
  31. * @param IEventMerger $eventMerger
  32. */
  33. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IEventMerger $eventMerger) {
  34. $this->languageFactory = $languageFactory;
  35. $this->url = $url;
  36. $this->activityManager = $activityManager;
  37. $this->eventMerger = $eventMerger;
  38. }
  39. /**
  40. * @param string $language
  41. * @param IEvent $event
  42. * @param IEvent|null $previousEvent
  43. * @return IEvent
  44. * @throws \InvalidArgumentException
  45. * @since 11.0.0
  46. */
  47. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  48. if ($event->getApp() !== 'files' || $event->getType() !== 'favorite') {
  49. throw new \InvalidArgumentException();
  50. }
  51. $this->l = $this->languageFactory->get('files', $language);
  52. if ($this->activityManager->isFormattingFilteredObject()) {
  53. try {
  54. return $this->parseShortVersion($event);
  55. } catch (\InvalidArgumentException $e) {
  56. // Ignore and simply use the long version...
  57. }
  58. }
  59. return $this->parseLongVersion($event, $previousEvent);
  60. }
  61. /**
  62. * @param IEvent $event
  63. * @return IEvent
  64. * @throws \InvalidArgumentException
  65. * @since 11.0.0
  66. */
  67. public function parseShortVersion(IEvent $event) {
  68. if ($event->getSubject() === self::SUBJECT_ADDED) {
  69. $event->setParsedSubject($this->l->t('Added to favorites'));
  70. if ($this->activityManager->getRequirePNG()) {
  71. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.png')));
  72. } else {
  73. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')));
  74. }
  75. } elseif ($event->getSubject() === self::SUBJECT_REMOVED) {
  76. $event->setType('unfavorite');
  77. $event->setParsedSubject($this->l->t('Removed from favorites'));
  78. if ($this->activityManager->getRequirePNG()) {
  79. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.png')));
  80. } else {
  81. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.svg')));
  82. }
  83. } else {
  84. throw new \InvalidArgumentException();
  85. }
  86. return $event;
  87. }
  88. /**
  89. * @param IEvent $event
  90. * @param IEvent|null $previousEvent
  91. * @return IEvent
  92. * @throws \InvalidArgumentException
  93. * @since 11.0.0
  94. */
  95. public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) {
  96. if ($event->getSubject() === self::SUBJECT_ADDED) {
  97. $subject = $this->l->t('You added {file} to your favorites');
  98. if ($this->activityManager->getRequirePNG()) {
  99. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.png')));
  100. } else {
  101. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')));
  102. }
  103. } elseif ($event->getSubject() === self::SUBJECT_REMOVED) {
  104. $event->setType('unfavorite');
  105. $subject = $this->l->t('You removed {file} from your favorites');
  106. if ($this->activityManager->getRequirePNG()) {
  107. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.png')));
  108. } else {
  109. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.svg')));
  110. }
  111. } else {
  112. throw new \InvalidArgumentException();
  113. }
  114. $this->setSubjects($event, $subject);
  115. $event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
  116. return $event;
  117. }
  118. /**
  119. * @param IEvent $event
  120. * @param string $subject
  121. */
  122. protected function setSubjects(IEvent $event, $subject) {
  123. $subjectParams = $event->getSubjectParameters();
  124. if (empty($subjectParams)) {
  125. // Try to fall back to the old way, but this does not work for emails.
  126. // But at least old activities still work.
  127. $subjectParams = [
  128. 'id' => $event->getObjectId(),
  129. 'path' => $event->getObjectName(),
  130. ];
  131. }
  132. $parameter = [
  133. 'type' => 'file',
  134. 'id' => $subjectParams['id'],
  135. 'name' => basename($subjectParams['path']),
  136. 'path' => trim($subjectParams['path'], '/'),
  137. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $subjectParams['id']]),
  138. ];
  139. $event->setRichSubject($subject, ['file' => $parameter]);
  140. }
  141. }