FavoriteProvider.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Files\Activity;
  24. use OCP\Activity\IEvent;
  25. use OCP\Activity\IEventMerger;
  26. use OCP\Activity\IManager;
  27. use OCP\Activity\IProvider;
  28. use OCP\IL10N;
  29. use OCP\IURLGenerator;
  30. use OCP\L10N\IFactory;
  31. class FavoriteProvider implements IProvider {
  32. const SUBJECT_ADDED = 'added_favorite';
  33. const SUBJECT_REMOVED = 'removed_favorite';
  34. /** @var IFactory */
  35. protected $languageFactory;
  36. /** @var IL10N */
  37. protected $l;
  38. /** @var IURLGenerator */
  39. protected $url;
  40. /** @var IManager */
  41. protected $activityManager;
  42. /** @var IEventMerger */
  43. protected $eventMerger;
  44. /**
  45. * @param IFactory $languageFactory
  46. * @param IURLGenerator $url
  47. * @param IManager $activityManager
  48. * @param IEventMerger $eventMerger
  49. */
  50. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IEventMerger $eventMerger) {
  51. $this->languageFactory = $languageFactory;
  52. $this->url = $url;
  53. $this->activityManager = $activityManager;
  54. $this->eventMerger = $eventMerger;
  55. }
  56. /**
  57. * @param string $language
  58. * @param IEvent $event
  59. * @param IEvent|null $previousEvent
  60. * @return IEvent
  61. * @throws \InvalidArgumentException
  62. * @since 11.0.0
  63. */
  64. public function parse($language, IEvent $event, IEvent $previousEvent = null) {
  65. if ($event->getApp() !== 'files' || $event->getType() !== 'favorite') {
  66. throw new \InvalidArgumentException();
  67. }
  68. $this->l = $this->languageFactory->get('files', $language);
  69. if ($this->activityManager->isFormattingFilteredObject()) {
  70. try {
  71. return $this->parseShortVersion($event);
  72. } catch (\InvalidArgumentException $e) {
  73. // Ignore and simply use the long version...
  74. }
  75. }
  76. return $this->parseLongVersion($event, $previousEvent);
  77. }
  78. /**
  79. * @param IEvent $event
  80. * @return IEvent
  81. * @throws \InvalidArgumentException
  82. * @since 11.0.0
  83. */
  84. public function parseShortVersion(IEvent $event) {
  85. if ($event->getSubject() === self::SUBJECT_ADDED) {
  86. $event->setParsedSubject($this->l->t('Added to favorites'));
  87. if ($this->activityManager->getRequirePNG()) {
  88. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.png')));
  89. } else {
  90. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')));
  91. }
  92. } else if ($event->getSubject() === self::SUBJECT_REMOVED) {
  93. $event->setType('unfavorite');
  94. $event->setParsedSubject($this->l->t('Removed from favorites'));
  95. if ($this->activityManager->getRequirePNG()) {
  96. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.png')));
  97. } else {
  98. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.svg')));
  99. }
  100. } else {
  101. throw new \InvalidArgumentException();
  102. }
  103. return $event;
  104. }
  105. /**
  106. * @param IEvent $event
  107. * @param IEvent|null $previousEvent
  108. * @return IEvent
  109. * @throws \InvalidArgumentException
  110. * @since 11.0.0
  111. */
  112. public function parseLongVersion(IEvent $event, IEvent $previousEvent = null) {
  113. if ($event->getSubject() === self::SUBJECT_ADDED) {
  114. $subject = $this->l->t('You added {file} to your favorites');
  115. if ($this->activityManager->getRequirePNG()) {
  116. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.png')));
  117. } else {
  118. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')));
  119. }
  120. } else if ($event->getSubject() === self::SUBJECT_REMOVED) {
  121. $event->setType('unfavorite');
  122. $subject = $this->l->t('You removed {file} from your favorites');
  123. if ($this->activityManager->getRequirePNG()) {
  124. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.png')));
  125. } else {
  126. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.svg')));
  127. }
  128. } else {
  129. throw new \InvalidArgumentException();
  130. }
  131. $this->setSubjects($event, $subject);
  132. $event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
  133. return $event;
  134. }
  135. /**
  136. * @param IEvent $event
  137. * @param string $subject
  138. */
  139. protected function setSubjects(IEvent $event, $subject) {
  140. $subjectParams = $event->getSubjectParameters();
  141. if (empty($subjectParams)) {
  142. // Try to fall back to the old way, but this does not work for emails.
  143. // But at least old activities still work.
  144. $subjectParams = [
  145. 'id' => $event->getObjectId(),
  146. 'path' => $event->getObjectName(),
  147. ];
  148. }
  149. $parameter = [
  150. 'type' => 'file',
  151. 'id' => $subjectParams['id'],
  152. 'name' => basename($subjectParams['path']),
  153. 'path' => trim($subjectParams['path'], '/'),
  154. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $subjectParams['id']]),
  155. ];
  156. $event->setParsedSubject(str_replace('{file}', $parameter['path'], $subject))
  157. ->setRichSubject($subject, ['file' => $parameter]);
  158. }
  159. }