1
0

FavoriteProvider.php 4.6 KB

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