FavoriteProvider.php 4.8 KB

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