You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TagService.php 4.1KB

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