@@ -38,107 +38,107 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class TagService { |
| 40 | 40 | |
| 41 | - /** @var IUserSession */ |
|
| 42 | - private $userSession; |
|
| 43 | - /** @var IManager */ |
|
| 44 | - private $activityManager; |
|
| 45 | - /** @var ITags|null */ |
|
| 46 | - private $tagger; |
|
| 47 | - /** @var Folder */ |
|
| 48 | - private $homeFolder; |
|
| 49 | - /** @var EventDispatcherInterface */ |
|
| 50 | - private $dispatcher; |
|
| 41 | + /** @var IUserSession */ |
|
| 42 | + private $userSession; |
|
| 43 | + /** @var IManager */ |
|
| 44 | + private $activityManager; |
|
| 45 | + /** @var ITags|null */ |
|
| 46 | + private $tagger; |
|
| 47 | + /** @var Folder */ |
|
| 48 | + private $homeFolder; |
|
| 49 | + /** @var EventDispatcherInterface */ |
|
| 50 | + private $dispatcher; |
|
| 51 | 51 | |
| 52 | - public function __construct( |
|
| 53 | - IUserSession $userSession, |
|
| 54 | - IManager $activityManager, |
|
| 55 | - ?ITags $tagger, |
|
| 56 | - Folder $homeFolder, |
|
| 57 | - EventDispatcherInterface $dispatcher |
|
| 58 | - ) { |
|
| 59 | - $this->userSession = $userSession; |
|
| 60 | - $this->activityManager = $activityManager; |
|
| 61 | - $this->tagger = $tagger; |
|
| 62 | - $this->homeFolder = $homeFolder; |
|
| 63 | - $this->dispatcher = $dispatcher; |
|
| 64 | - } |
|
| 52 | + public function __construct( |
|
| 53 | + IUserSession $userSession, |
|
| 54 | + IManager $activityManager, |
|
| 55 | + ?ITags $tagger, |
|
| 56 | + Folder $homeFolder, |
|
| 57 | + EventDispatcherInterface $dispatcher |
|
| 58 | + ) { |
|
| 59 | + $this->userSession = $userSession; |
|
| 60 | + $this->activityManager = $activityManager; |
|
| 61 | + $this->tagger = $tagger; |
|
| 62 | + $this->homeFolder = $homeFolder; |
|
| 63 | + $this->dispatcher = $dispatcher; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Updates the tags of the specified file path. |
|
| 68 | - * The passed tags are absolute, which means they will |
|
| 69 | - * replace the actual tag selection. |
|
| 70 | - * |
|
| 71 | - * @param string $path path |
|
| 72 | - * @param array $tags array of tags |
|
| 73 | - * @return array list of tags |
|
| 74 | - * @throws \OCP\Files\NotFoundException if the file does not exist |
|
| 75 | - */ |
|
| 76 | - public function updateFileTags($path, $tags) { |
|
| 77 | - if ($this->tagger === null) { |
|
| 78 | - throw new \RuntimeException('No tagger set'); |
|
| 79 | - } |
|
| 66 | + /** |
|
| 67 | + * Updates the tags of the specified file path. |
|
| 68 | + * The passed tags are absolute, which means they will |
|
| 69 | + * replace the actual tag selection. |
|
| 70 | + * |
|
| 71 | + * @param string $path path |
|
| 72 | + * @param array $tags array of tags |
|
| 73 | + * @return array list of tags |
|
| 74 | + * @throws \OCP\Files\NotFoundException if the file does not exist |
|
| 75 | + */ |
|
| 76 | + public function updateFileTags($path, $tags) { |
|
| 77 | + if ($this->tagger === null) { |
|
| 78 | + throw new \RuntimeException('No tagger set'); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - $fileId = $this->homeFolder->get($path)->getId(); |
|
| 81 | + $fileId = $this->homeFolder->get($path)->getId(); |
|
| 82 | 82 | |
| 83 | - $currentTags = $this->tagger->getTagsForObjects([$fileId]); |
|
| 83 | + $currentTags = $this->tagger->getTagsForObjects([$fileId]); |
|
| 84 | 84 | |
| 85 | - if (!empty($currentTags)) { |
|
| 86 | - $currentTags = current($currentTags); |
|
| 87 | - } |
|
| 85 | + if (!empty($currentTags)) { |
|
| 86 | + $currentTags = current($currentTags); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - $newTags = array_diff($tags, $currentTags); |
|
| 90 | - foreach ($newTags as $tag) { |
|
| 91 | - if ($tag === ITags::TAG_FAVORITE) { |
|
| 92 | - $this->addActivity(true, $fileId, $path); |
|
| 93 | - } |
|
| 94 | - $this->tagger->tagAs($fileId, $tag); |
|
| 95 | - } |
|
| 96 | - $deletedTags = array_diff($currentTags, $tags); |
|
| 97 | - foreach ($deletedTags as $tag) { |
|
| 98 | - if ($tag === ITags::TAG_FAVORITE) { |
|
| 99 | - $this->addActivity(false, $fileId, $path); |
|
| 100 | - } |
|
| 101 | - $this->tagger->unTag($fileId, $tag); |
|
| 102 | - } |
|
| 89 | + $newTags = array_diff($tags, $currentTags); |
|
| 90 | + foreach ($newTags as $tag) { |
|
| 91 | + if ($tag === ITags::TAG_FAVORITE) { |
|
| 92 | + $this->addActivity(true, $fileId, $path); |
|
| 93 | + } |
|
| 94 | + $this->tagger->tagAs($fileId, $tag); |
|
| 95 | + } |
|
| 96 | + $deletedTags = array_diff($currentTags, $tags); |
|
| 97 | + foreach ($deletedTags as $tag) { |
|
| 98 | + if ($tag === ITags::TAG_FAVORITE) { |
|
| 99 | + $this->addActivity(false, $fileId, $path); |
|
| 100 | + } |
|
| 101 | + $this->tagger->unTag($fileId, $tag); |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - // TODO: re-read from tagger to make sure the |
|
| 105 | - // list is up to date, in case of concurrent changes ? |
|
| 106 | - return $tags; |
|
| 107 | - } |
|
| 104 | + // TODO: re-read from tagger to make sure the |
|
| 105 | + // list is up to date, in case of concurrent changes ? |
|
| 106 | + return $tags; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * @param bool $addToFavorite |
|
| 111 | - * @param int $fileId |
|
| 112 | - * @param string $path |
|
| 113 | - */ |
|
| 114 | - protected function addActivity($addToFavorite, $fileId, $path) { |
|
| 115 | - $user = $this->userSession->getUser(); |
|
| 116 | - if (!$user instanceof IUser) { |
|
| 117 | - return; |
|
| 118 | - } |
|
| 109 | + /** |
|
| 110 | + * @param bool $addToFavorite |
|
| 111 | + * @param int $fileId |
|
| 112 | + * @param string $path |
|
| 113 | + */ |
|
| 114 | + protected function addActivity($addToFavorite, $fileId, $path) { |
|
| 115 | + $user = $this->userSession->getUser(); |
|
| 116 | + if (!$user instanceof IUser) { |
|
| 117 | + return; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - $eventName = $addToFavorite ? 'addFavorite' : 'removeFavorite'; |
|
| 121 | - $this->dispatcher->dispatch(self::class . '::' . $eventName, new GenericEvent(null, [ |
|
| 122 | - 'userId' => $user->getUID(), |
|
| 123 | - 'fileId' => $fileId, |
|
| 124 | - 'path' => $path, |
|
| 125 | - ])); |
|
| 120 | + $eventName = $addToFavorite ? 'addFavorite' : 'removeFavorite'; |
|
| 121 | + $this->dispatcher->dispatch(self::class . '::' . $eventName, new GenericEvent(null, [ |
|
| 122 | + 'userId' => $user->getUID(), |
|
| 123 | + 'fileId' => $fileId, |
|
| 124 | + 'path' => $path, |
|
| 125 | + ])); |
|
| 126 | 126 | |
| 127 | - $event = $this->activityManager->generateEvent(); |
|
| 128 | - try { |
|
| 129 | - $event->setApp('files') |
|
| 130 | - ->setObject('files', $fileId, $path) |
|
| 131 | - ->setType('favorite') |
|
| 132 | - ->setAuthor($user->getUID()) |
|
| 133 | - ->setAffectedUser($user->getUID()) |
|
| 134 | - ->setTimestamp(time()) |
|
| 135 | - ->setSubject( |
|
| 136 | - $addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED, |
|
| 137 | - ['id' => $fileId, 'path' => $path] |
|
| 138 | - ); |
|
| 139 | - $this->activityManager->publish($event); |
|
| 140 | - } catch (\InvalidArgumentException $e) { |
|
| 141 | - } catch (\BadMethodCallException $e) { |
|
| 142 | - } |
|
| 143 | - } |
|
| 127 | + $event = $this->activityManager->generateEvent(); |
|
| 128 | + try { |
|
| 129 | + $event->setApp('files') |
|
| 130 | + ->setObject('files', $fileId, $path) |
|
| 131 | + ->setType('favorite') |
|
| 132 | + ->setAuthor($user->getUID()) |
|
| 133 | + ->setAffectedUser($user->getUID()) |
|
| 134 | + ->setTimestamp(time()) |
|
| 135 | + ->setSubject( |
|
| 136 | + $addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED, |
|
| 137 | + ['id' => $fileId, 'path' => $path] |
|
| 138 | + ); |
|
| 139 | + $this->activityManager->publish($event); |
|
| 140 | + } catch (\InvalidArgumentException $e) { |
|
| 141 | + } catch (\BadMethodCallException $e) { |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | } |