TagService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files\Service;
  25. use OCA\Files\Activity\FavoriteProvider;
  26. use OCP\Activity\IManager;
  27. use OCP\Files\Folder;
  28. use OCP\ITags;
  29. use OCP\IUser;
  30. use OCP\IUserSession;
  31. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  32. use Symfony\Component\EventDispatcher\GenericEvent;
  33. /**
  34. * Service class to manage tags on files.
  35. */
  36. class TagService {
  37. /** @var IUserSession */
  38. private $userSession;
  39. /** @var IManager */
  40. private $activityManager;
  41. /** @var ITags */
  42. private $tagger;
  43. /** @var Folder */
  44. private $homeFolder;
  45. /** @var EventDispatcherInterface */
  46. private $dispatcher;
  47. /**
  48. * @param IUserSession $userSession
  49. * @param IManager $activityManager
  50. * @param ITags $tagger
  51. * @param Folder $homeFolder
  52. * @param EventDispatcherInterface $dispatcher
  53. */
  54. public function __construct(
  55. IUserSession $userSession,
  56. IManager $activityManager,
  57. ITags $tagger,
  58. Folder $homeFolder,
  59. EventDispatcherInterface $dispatcher
  60. ) {
  61. $this->userSession = $userSession;
  62. $this->activityManager = $activityManager;
  63. $this->tagger = $tagger;
  64. $this->homeFolder = $homeFolder;
  65. $this->dispatcher = $dispatcher;
  66. }
  67. /**
  68. * Updates the tags of the specified file path.
  69. * The passed tags are absolute, which means they will
  70. * replace the actual tag selection.
  71. *
  72. * @param string $path path
  73. * @param array $tags array of tags
  74. * @return array list of tags
  75. * @throws \OCP\Files\NotFoundException if the file does not exist
  76. */
  77. public function updateFileTags($path, $tags) {
  78. $fileId = $this->homeFolder->get($path)->getId();
  79. $currentTags = $this->tagger->getTagsForObjects([$fileId]);
  80. if (!empty($currentTags)) {
  81. $currentTags = current($currentTags);
  82. }
  83. $newTags = array_diff($tags, $currentTags);
  84. foreach ($newTags as $tag) {
  85. if ($tag === ITags::TAG_FAVORITE) {
  86. $this->addActivity(true, $fileId, $path);
  87. }
  88. $this->tagger->tagAs($fileId, $tag);
  89. }
  90. $deletedTags = array_diff($currentTags, $tags);
  91. foreach ($deletedTags as $tag) {
  92. if ($tag === ITags::TAG_FAVORITE) {
  93. $this->addActivity(false, $fileId, $path);
  94. }
  95. $this->tagger->unTag($fileId, $tag);
  96. }
  97. // TODO: re-read from tagger to make sure the
  98. // list is up to date, in case of concurrent changes ?
  99. return $tags;
  100. }
  101. /**
  102. * @param bool $addToFavorite
  103. * @param int $fileId
  104. * @param string $path
  105. */
  106. protected function addActivity($addToFavorite, $fileId, $path) {
  107. $user = $this->userSession->getUser();
  108. if (!$user instanceof IUser) {
  109. return;
  110. }
  111. $eventName = $addToFavorite ? 'addFavorite' : 'removeFavorite';
  112. $this->dispatcher->dispatch(self::class . '::' . $eventName, new GenericEvent(null, [
  113. 'userId' => $user->getUID(),
  114. 'fileId' => $fileId,
  115. 'path' => $path,
  116. ]));
  117. $event = $this->activityManager->generateEvent();
  118. try {
  119. $event->setApp('files')
  120. ->setObject('files', $fileId, $path)
  121. ->setType('favorite')
  122. ->setAuthor($user->getUID())
  123. ->setAffectedUser($user->getUID())
  124. ->setTimestamp(time())
  125. ->setSubject(
  126. $addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED,
  127. ['id' => $fileId, 'path' => $path]
  128. );
  129. $this->activityManager->publish($event);
  130. } catch (\InvalidArgumentException $e) {
  131. } catch (\BadMethodCallException $e) {
  132. }
  133. }
  134. }