diff --git a/apps/dav/lib/Connector/Sabre/TagsPlugin.php b/apps/dav/lib/Connector/Sabre/TagsPlugin.php index 7f05d4d3c03a7..a91cd19fc37ac 100644 --- a/apps/dav/lib/Connector/Sabre/TagsPlugin.php +++ b/apps/dav/lib/Connector/Sabre/TagsPlugin.php @@ -251,11 +251,11 @@ public function handleUpdateProperties($path, PropPatch $propPatch) { return true; }); - $propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node) { + $propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node, $path) { if ((int)$favState === 1 || $favState === 'true') { - $this->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE); + $this->getTagger()->addToFavorites($node->getId(), $path); } else { - $this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE); + $this->getTagger()->removeFromFavorites($node->getId(), $path); } if (is_null($favState)) { diff --git a/apps/files/lib/Service/TagService.php b/apps/files/lib/Service/TagService.php index 450cd79505d21..376101bfc82c1 100644 --- a/apps/files/lib/Service/TagService.php +++ b/apps/files/lib/Service/TagService.php @@ -7,15 +7,11 @@ */ namespace OCA\Files\Service; -use OCA\Files\Activity\FavoriteProvider; use OCP\Activity\IManager; use OCP\EventDispatcher\IEventDispatcher; -use OCP\Files\Events\NodeAddedToFavorite; -use OCP\Files\Events\NodeRemovedFromFavorite; use OCP\Files\Folder; use OCP\Files\NotFoundException; use OCP\ITags; -use OCP\IUser; use OCP\IUserSession; /** @@ -61,56 +57,22 @@ public function updateFileTags($path, $tags) { $newTags = array_diff($tags, $currentTags); foreach ($newTags as $tag) { if ($tag === ITags::TAG_FAVORITE) { - $this->addActivity(true, $fileId, $path); + $this->tagger->addToFavorites($fileId, $path); + } else { + $this->tagger->tagAs($fileId, $tag); } - $this->tagger->tagAs($fileId, $tag); } $deletedTags = array_diff($currentTags, $tags); foreach ($deletedTags as $tag) { if ($tag === ITags::TAG_FAVORITE) { - $this->addActivity(false, $fileId, $path); + $this->tagger->removeFromFavorites($fileId, $path); + } else { + $this->tagger->unTag($fileId, $tag); } - $this->tagger->unTag($fileId, $tag); } // TODO: re-read from tagger to make sure the // list is up to date, in case of concurrent changes ? return $tags; } - - /** - * @param bool $addToFavorite - * @param int $fileId - * @param string $path - */ - protected function addActivity($addToFavorite, $fileId, $path) { - $user = $this->userSession->getUser(); - if (!$user instanceof IUser) { - return; - } - - if ($addToFavorite) { - $event = new NodeAddedToFavorite($user, $fileId, $path); - } else { - $event = new NodeRemovedFromFavorite($user, $fileId, $path); - } - $this->dispatcher->dispatchTyped($event); - - $event = $this->activityManager->generateEvent(); - try { - $event->setApp('files') - ->setObject('files', $fileId, $path) - ->setType('favorite') - ->setAuthor($user->getUID()) - ->setAffectedUser($user->getUID()) - ->setTimestamp(time()) - ->setSubject( - $addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED, - ['id' => $fileId, 'path' => $path] - ); - $this->activityManager->publish($event); - } catch (\InvalidArgumentException $e) { - } catch (\BadMethodCallException $e) { - } - } } diff --git a/lib/private/TagManager.php b/lib/private/TagManager.php index f99653f2c052f..5d38cfcb256d4 100644 --- a/lib/private/TagManager.php +++ b/lib/private/TagManager.php @@ -8,9 +8,11 @@ namespace OC; use OC\Tagging\TagMapper; +use OCP\Activity\IManager; use OCP\Db\Exception as DBException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventListener; use OCP\IDBConnection; use OCP\ITagManager; @@ -28,7 +30,14 @@ class TagManager implements ITagManager, IEventListener { private IDBConnection $connection; private LoggerInterface $logger; - public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection, LoggerInterface $logger) { + public function __construct( + TagMapper $mapper, + IUserSession $userSession, + IDBConnection $connection, + LoggerInterface $logger, + private IEventDispatcher $eventDispatcher, + private IManager $activityManager, + ) { $this->mapper = $mapper; $this->userSession = $userSession; $this->connection = $connection; @@ -57,7 +66,7 @@ public function load($type, $defaultTags = [], $includeShared = false, $userId = } $userId = $this->userSession->getUser()->getUId(); } - return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $defaultTags); + return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $defaultTags, $this->userSession, $this->eventDispatcher, $this->activityManager); } /** diff --git a/lib/private/Tags.php b/lib/private/Tags.php index 1f22a4c6a337f..2e6a3bc1dd805 100644 --- a/lib/private/Tags.php +++ b/lib/private/Tags.php @@ -9,10 +9,17 @@ use OC\Tagging\Tag; use OC\Tagging\TagMapper; +use OCA\Files\Activity\FavoriteProvider; +use OCP\Activity\IManager; use OCP\DB\Exception; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Events\NodeAddedToFavorite; +use OCP\Files\Events\NodeRemovedFromFavorite; use OCP\IDBConnection; use OCP\ITags; +use OCP\IUser; +use OCP\IUserSession; use OCP\Share_Backend; use Psr\Log\LoggerInterface; @@ -62,7 +69,17 @@ class Tags implements ITags { * * since 20.0.0 $includeShared isn't used anymore */ - public function __construct(TagMapper $mapper, string $user, string $type, LoggerInterface $logger, IDBConnection $connection, array $defaultTags = []) { + public function __construct( + TagMapper $mapper, + string $user, + string $type, + LoggerInterface $logger, + IDBConnection $connection, + array $defaultTags = [], + private IUserSession $userSession, + private IEventDispatcher $eventDispatcher, + private IManager $activityManager, + ) { $this->mapper = $mapper; $this->user = $user; $this->type = $type; @@ -478,10 +495,11 @@ public function getFavorites() { * @param int $objid The id of the object * @return boolean */ - public function addToFavorites($objid) { + public function addToFavorites($objid, $path = '') { if (!$this->userHasTag(ITags::TAG_FAVORITE, $this->user)) { $this->add(ITags::TAG_FAVORITE); } + $this->addActivity(true, $objid, $path); return $this->tagAs($objid, ITags::TAG_FAVORITE); } @@ -489,9 +507,11 @@ public function addToFavorites($objid) { * Remove an object from favorites * * @param int $objid The id of the object + * @param string $path The path of the object * @return boolean */ - public function removeFromFavorites($objid) { + public function removeFromFavorites($objid, $path = '') { + $this->addActivity(false, $objid, $path); return $this->unTag($objid, ITags::TAG_FAVORITE); } @@ -684,4 +704,41 @@ private function tagMap(Tag $tag) { 'type' => $tag->getType() ]; } + + /** + * @param bool $addToFavorite + * @param int $fileId + * @param string $path + */ + protected function addActivity($addToFavorite, $fileId, $path) { + $user = $this->userSession->getUser(); + if (!$user instanceof IUser) { + return; + } + + if ($addToFavorite) { + $event = new NodeAddedToFavorite($user, $fileId, $path); + } else { + $event = new NodeRemovedFromFavorite($user, $fileId, $path); + } + $this->eventDispatcher->dispatchTyped($event); + + $event = $this->activityManager->generateEvent(); + try { + $event->setApp('files') + ->setObject('files', $fileId, $path) + ->setType('favorite') + ->setAuthor($user->getUID()) + ->setAffectedUser($user->getUID()) + ->setTimestamp(time()) + ->setSubject( + $addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED, + ['id' => $fileId, 'path' => $path] + ); + $this->activityManager->publish($event); + } catch (\InvalidArgumentException $e) { + } catch (\BadMethodCallException $e) { + } + } + } diff --git a/lib/public/ITags.php b/lib/public/ITags.php index 0da4522c86c22..0cd962da1c95a 100644 --- a/lib/public/ITags.php +++ b/lib/public/ITags.php @@ -165,19 +165,21 @@ public function getFavorites(); * Add an object to favorites * * @param int $objid The id of the object + * @param string $path The path of the object * @return boolean * @since 6.0.0 */ - public function addToFavorites($objid); + public function addToFavorites($objid, $path = ''); /** * Remove an object from favorites * * @param int $objid The id of the object + * @param string $path The path of the object * @return boolean * @since 6.0.0 */ - public function removeFromFavorites($objid); + public function removeFromFavorites($objid, $path = ''); /** * Creates a tag/object relation.