1
0

FavoriteProvider.php 5.6 KB

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