From e61f3947ea91d47b7809cd8dd383004a4edb412d Mon Sep 17 00:00:00 2001 From: grnd-alt Date: Thu, 10 Oct 2024 16:54:49 +0200 Subject: [PATCH] fix: move favorite activity logging to tags Signed-off-by: grnd-alt --- .../dav/lib/Connector/Sabre/ServerFactory.php | 2 +- apps/dav/lib/Connector/Sabre/TagsPlugin.php | 63 +------------------ apps/dav/lib/Server.php | 10 +-- .../unit/Connector/Sabre/TagsPluginTest.php | 8 +-- apps/files/lib/AppInfo/Application.php | 5 -- apps/files/lib/Service/TagService.php | 61 ++---------------- apps/files/tests/Service/TagServiceTest.php | 15 ++--- lib/private/TagManager.php | 10 ++- lib/private/Tags.php | 61 +++++++++++++++++- lib/public/ITags.php | 6 +- tests/lib/TagsTest.php | 6 +- 11 files changed, 92 insertions(+), 155 deletions(-) diff --git a/apps/dav/lib/Connector/Sabre/ServerFactory.php b/apps/dav/lib/Connector/Sabre/ServerFactory.php index b3fedb7f8b309..4235a02831a01 100644 --- a/apps/dav/lib/Connector/Sabre/ServerFactory.php +++ b/apps/dav/lib/Connector/Sabre/ServerFactory.php @@ -151,7 +151,7 @@ public function createServer(string $baseUri, )); if ($this->userSession->isLoggedIn()) { - $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager, $this->userSession, $this->eventDispatcher, \OCP\Server::get(\OCP\Activity\IManager::class))); + $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager)); $server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin( $objectTree, $this->userSession, diff --git a/apps/dav/lib/Connector/Sabre/TagsPlugin.php b/apps/dav/lib/Connector/Sabre/TagsPlugin.php index dc9005fae68d7..259a64a65e1c1 100644 --- a/apps/dav/lib/Connector/Sabre/TagsPlugin.php +++ b/apps/dav/lib/Connector/Sabre/TagsPlugin.php @@ -28,13 +28,6 @@ * */ -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\IUser; -use OCP\IUserSession; use Sabre\DAV\PropFind; use Sabre\DAV\PropPatch; @@ -58,15 +51,6 @@ class TagsPlugin extends \Sabre\DAV\ServerPlugin { */ private $tagManager; - /** @var \OCP\IUserSession */ - private $userSession; - - /** @var IEventDispatcher */ - private $dispatcher; - - /** @var IManager */ - private $activityManager; - /** * @var \OCP\ITags */ @@ -92,17 +76,11 @@ class TagsPlugin extends \Sabre\DAV\ServerPlugin { public function __construct( \Sabre\DAV\Tree $tree, \OCP\ITagManager $tagManager, - IUserSession $userSession, - IEventDispatcher $dispatcher, - IManager $activityManager, ) { $this->tree = $tree; $this->tagManager = $tagManager; $this->tagger = null; $this->cachedTags = []; - $this->userSession = $userSession; - $this->dispatcher = $dispatcher; - $this->activityManager = $activityManager; } /** @@ -286,11 +264,9 @@ public function handleUpdateProperties(string $path, PropPatch $propPatch) { $propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node, $path) { if ((int)$favState === 1 || $favState === 'true') { - $this->addActivity(true, $node->getId(), $path); - $this->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE); + $this->getTagger()->addToFavorites($node->getId(), $path); } else { - $this->addActivity(false, $node->getId(), $path); - $this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE); + $this->getTagger()->removeFromFavorites($node->getId(), $path); } if (is_null($favState)) { @@ -302,39 +278,4 @@ public function handleUpdateProperties(string $path, PropPatch $propPatch) { }); } - /** - * @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/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index d122b13f66743..ef8d0759219a9 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -52,7 +52,6 @@ use OCA\DAV\Upload\ChunkingPlugin; use OCA\DAV\Upload\ChunkingV2Plugin; use OCA\Theming\ThemingDefaults; -use OCP\Activity\IManager; use OCP\AppFramework\Http\Response; use OCP\Diagnostics\IEventLogger; use OCP\EventDispatcher\IEventDispatcher; @@ -62,7 +61,6 @@ use OCP\IConfig; use OCP\IPreview; use OCP\IRequest; -use OCP\IUser; use OCP\IUserSession; use OCP\Profiler\IProfiler; use OCP\SabrePluginEvent; @@ -246,8 +244,6 @@ public function __construct(IRequest $request, string $baseUri) { // custom properties plugin must be the last one $userSession = \OC::$server->getUserSession(); $user = $userSession->getUser(); - $dispatcher = \OC::$server->get(IEventDispatcher::class); - $activityManager = \OC::$server->get(IManager::class); if ($user !== null) { $view = \OC\Files\Filesystem::getView(); $config = \OCP\Server::get(IConfig::class); @@ -280,12 +276,8 @@ public function __construct(IRequest $request, string $baseUri) { $this->server->addPlugin( new QuotaPlugin($view)); } - - // if (!$user instanceOf IUser ) $this->server->addPlugin( - new TagsPlugin( - $this->server->tree, \OC::$server->getTagManager(), $userSession, $dispatcher, $activityManager - ) + new TagsPlugin($this->server->tree, \OC::$server->getTagManager()) ); // TODO: switch to LazyUserFolder diff --git a/apps/dav/tests/unit/Connector/Sabre/TagsPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/TagsPluginTest.php index da1c4d5f2ac27..dd3f060580e3c 100644 --- a/apps/dav/tests/unit/Connector/Sabre/TagsPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/TagsPluginTest.php @@ -397,8 +397,8 @@ public function testUpdateFav(): void { // set favorite tag $this->tagger->expects($this->once()) - ->method('tagAs') - ->with(123, self::TAG_FAVORITE); + ->method('addToFavorites') + ->with(123, '/dummypath'); // properties to set $propPatch = new \Sabre\DAV\PropPatch([ @@ -422,8 +422,8 @@ public function testUpdateFav(): void { // unfavorite now // set favorite tag $this->tagger->expects($this->once()) - ->method('unTag') - ->with(123, self::TAG_FAVORITE); + ->method('removeFromFavorites') + ->with(123, 'dummypath'); // properties to set $propPatch = new \Sabre\DAV\PropPatch([ diff --git a/apps/files/lib/AppInfo/Application.php b/apps/files/lib/AppInfo/Application.php index 3d2d0527072a7..618cf7d1f6cb3 100644 --- a/apps/files/lib/AppInfo/Application.php +++ b/apps/files/lib/AppInfo/Application.php @@ -28,14 +28,12 @@ use OCA\Files\Service\TagService; use OCA\Files\Service\UserConfig; use OCA\Files\Service\ViewConfig; -use OCP\Activity\IManager as IActivityManager; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Collaboration\Reference\RenderReferenceEvent; use OCP\Collaboration\Resources\IProviderManager; -use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Cache\CacheEntryRemovedEvent; use OCP\Files\Events\Node\BeforeNodeCopiedEvent; use OCP\Files\Events\Node\BeforeNodeDeletedEvent; @@ -97,11 +95,8 @@ public function register(IRegistrationContext $context): void { $server = $c->get(IServerContainer::class); return new TagService( - $c->get(IUserSession::class), - $c->get(IActivityManager::class), $c->get(ITagManager::class)->load(self::APP_ID), $server->getUserFolder(), - $c->get(IEventDispatcher::class), ); }); diff --git a/apps/files/lib/Service/TagService.php b/apps/files/lib/Service/TagService.php index 4bb43145e4a12..841248db4b786 100644 --- a/apps/files/lib/Service/TagService.php +++ b/apps/files/lib/Service/TagService.php @@ -7,44 +7,25 @@ */ 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\ITags; -use OCP\IUser; -use OCP\IUserSession; /** * Service class to manage tags on files. */ class TagService { - /** @var IUserSession */ - private $userSession; - /** @var IManager */ - private $activityManager; /** @var ITags|null */ private $tagger; /** @var Folder|null */ private $homeFolder; - /** @var IEventDispatcher */ - private $dispatcher; public function __construct( - IUserSession $userSession, - IManager $activityManager, ?ITags $tagger, ?Folder $homeFolder, - IEventDispatcher $dispatcher, ) { - $this->userSession = $userSession; - $this->activityManager = $activityManager; $this->tagger = $tagger; $this->homeFolder = $homeFolder; - $this->dispatcher = $dispatcher; } /** @@ -76,14 +57,16 @@ 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); + continue; } $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); + continue; } $this->tagger->unTag($fileId, $tag); } @@ -92,40 +75,4 @@ public function updateFileTags($path, $tags) { // 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/apps/files/tests/Service/TagServiceTest.php b/apps/files/tests/Service/TagServiceTest.php index 7922c45a63983..7345a4b544407 100644 --- a/apps/files/tests/Service/TagServiceTest.php +++ b/apps/files/tests/Service/TagServiceTest.php @@ -83,11 +83,8 @@ protected function setUp(): void { protected function getTagService(array $methods = []) { return $this->getMockBuilder(TagService::class) ->setConstructorArgs([ - $this->userSession, - $this->activityManager, $this->tagger, $this->root, - $this->dispatcher, ]) ->setMethods($methods) ->getMock(); @@ -146,12 +143,12 @@ public function testFavoriteActivity(): void { $subdir = $this->root->newFolder('subdir'); $file = $subdir->newFile('test.txt'); - $this->tagService->expects($this->exactly(2)) - ->method('addActivity') - ->withConsecutive( - [true, $file->getId(), 'subdir/test.txt'], - [false, $file->getId(), 'subdir/test.txt'] - ); + $this->tagService->expects($this->exactly(1)) + ->method('addToFavorites') + ->with($file->getId(), 'subdir/test.txt'); + $this->tagService->expects($this->exactly(1)) + ->method('removeFromFavorites') + ->with($file->getId(), 'subdir/test.txt'); // set tags $this->tagService->updateFileTags('subdir/test.txt', [ITags::TAG_FAVORITE]); diff --git a/lib/private/TagManager.php b/lib/private/TagManager.php index f99653f2c052f..8cf5e564a16ea 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; @@ -25,12 +27,16 @@ class TagManager implements ITagManager, IEventListener { private TagMapper $mapper; private IUserSession $userSession; + private IEventDispatcher $dispatcher; + private IManager $activityManager; private IDBConnection $connection; private LoggerInterface $logger; - public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection, LoggerInterface $logger) { + public function __construct(TagMapper $mapper, IUserSession $userSession, IEventDispatcher $dispatcher, IManager $activityManager, IDBConnection $connection, LoggerInterface $logger) { $this->mapper = $mapper; $this->userSession = $userSession; + $this->dispatcher = $dispatcher; + $this->activityManager = $activityManager; $this->connection = $connection; $this->logger = $logger; } @@ -57,7 +63,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, $this->userSession, $this->dispatcher, $this->activityManager, $userId, $type, $this->logger, $this->connection, $defaultTags); } /** diff --git a/lib/private/Tags.php b/lib/private/Tags.php index 350c49bca2cd3..830164bd79bbd 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; @@ -43,6 +50,14 @@ class Tags implements ITags { */ private TagMapper $mapper; + /** @var \OCP\IUserSession */ + private $userSession; + + /** @var IEventDispatcher */ + private $dispatcher; + + /** @var IManager */ + private $activityManager; /** * The sharing backend for objects of $this->type. Required if * $this->includeShared === true to determine ownership of items. @@ -62,8 +77,11 @@ 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, IUserSession $userSession, IEventDispatcher $dispatcher, IManager $activityManager, string $user, string $type, LoggerInterface $logger, IDBConnection $connection, array $defaultTags = []) { $this->mapper = $mapper; + $this->userSession = $userSession; + $this->dispatcher = $dispatcher; + $this->activityManager = $activityManager; $this->user = $user; $this->type = $type; $this->owners = [$this->user]; @@ -478,10 +496,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); } @@ -491,7 +510,8 @@ public function addToFavorites($objid) { * @param int $objid The id 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,39 @@ 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->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/public/ITags.php b/lib/public/ITags.php index 1ba6abae5a28e..ff384ffbc73ec 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 file to add to favorites * @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 file to remove from favorites * @return boolean * @since 6.0.0 */ - public function removeFromFavorites($objid); + public function removeFromFavorites($objid, $path); /** * Creates a tag/object relation. diff --git a/tests/lib/TagsTest.php b/tests/lib/TagsTest.php index 1876897095486..023cb81bd6b76 100644 --- a/tests/lib/TagsTest.php +++ b/tests/lib/TagsTest.php @@ -7,6 +7,8 @@ namespace Test; +use OC\EventDispatcher\EventDispatcher; +use OCP\Activity\IManager; use OCP\IDBConnection; use OCP\IUser; use OCP\IUserSession; @@ -48,7 +50,7 @@ protected function setUp(): void { $this->objectType = $this->getUniqueID('type_'); $this->tagMapper = new \OC\Tagging\TagMapper(\OC::$server->get(IDBConnection::class)); - $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); + $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->get(EventDispatcher::class), \OC::$server->get(IManager::class), \OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); } protected function tearDown(): void { @@ -65,7 +67,7 @@ public function testTagManagerWithoutUserReturnsNull(): void { ->expects($this->any()) ->method('getUser') ->willReturn(null); - $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection(), \OC::$server->get(LoggerInterface::class)); + $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->get(EventDispatcher::class), \OC::$server->get(IManager::class), \OC::$server->getDatabaseConnection(), \OC::$server->get(LoggerInterface::class)); $this->assertNull($this->tagMgr->load($this->objectType)); }