From 205809a45f7e7aaea7e0bfb082988540381a3838 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 --- apps/dav/appinfo/v1/publicwebdav.php | 1 + apps/dav/appinfo/v1/webdav.php | 1 + apps/dav/appinfo/v2/publicremote.php | 2 + .../dav/lib/Connector/Sabre/ServerFactory.php | 4 +- apps/dav/lib/Connector/Sabre/TagsPlugin.php | 49 ++------------- 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 | 52 ++-------------- apps/files/tests/Service/TagServiceTest.php | 17 +----- lib/private/TagManager.php | 10 ++- lib/private/Tags.php | 61 ++++++++++++++++++- lib/public/ITags.php | 6 +- tests/lib/TagsTest.php | 10 +-- 14 files changed, 102 insertions(+), 134 deletions(-) diff --git a/apps/dav/appinfo/v1/publicwebdav.php b/apps/dav/appinfo/v1/publicwebdav.php index dc74fe214afbb..a8af3156695ef 100644 --- a/apps/dav/appinfo/v1/publicwebdav.php +++ b/apps/dav/appinfo/v1/publicwebdav.php @@ -51,6 +51,7 @@ \OC::$server->getRequest(), \OC::$server->getPreviewManager(), $eventDispatcher, + \OC::$server->getActivityManager(), \OC::$server->getL10N('dav') ); diff --git a/apps/dav/appinfo/v1/webdav.php b/apps/dav/appinfo/v1/webdav.php index 0faed7ccc947f..8de6cb3202ab2 100644 --- a/apps/dav/appinfo/v1/webdav.php +++ b/apps/dav/appinfo/v1/webdav.php @@ -35,6 +35,7 @@ \OC::$server->getRequest(), \OC::$server->getPreviewManager(), $dispatcher, + \OC::$server->getActivityManager(), \OC::$server->getL10N('dav') ); diff --git a/apps/dav/appinfo/v2/publicremote.php b/apps/dav/appinfo/v2/publicremote.php index 91d5a0448eba1..759d265e757cd 100644 --- a/apps/dav/appinfo/v2/publicremote.php +++ b/apps/dav/appinfo/v2/publicremote.php @@ -15,6 +15,7 @@ use OCA\DAV\Storage\PublicOwnerWrapper; use OCA\DAV\Storage\PublicShareWrapper; use OCA\FederatedFileSharing\FederatedShareProvider; +use OCP\Activity\IManager as IActivityManager; use OCP\BeforeSabrePubliclyLoadedEvent; use OCP\Constants; use OCP\EventDispatcher\IEventDispatcher; @@ -68,6 +69,7 @@ $request, Server::get(IPreview::class), $eventDispatcher, + Server::get(IActivityManager::class), $l10nFactory->get('dav'), ); diff --git a/apps/dav/lib/Connector/Sabre/ServerFactory.php b/apps/dav/lib/Connector/Sabre/ServerFactory.php index 3735e6972a4e9..11ea2cdead973 100644 --- a/apps/dav/lib/Connector/Sabre/ServerFactory.php +++ b/apps/dav/lib/Connector/Sabre/ServerFactory.php @@ -14,6 +14,7 @@ use OCA\DAV\DAV\ViewOnlyPlugin; use OCA\DAV\Files\ErrorPagePlugin; use OCA\Theming\ThemingDefaults; +use OCP\Activity\IManager; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Folder; use OCP\Files\IFilenameValidator; @@ -41,6 +42,7 @@ public function __construct( private IRequest $request, private IPreview $previewManager, private IEventDispatcher $eventDispatcher, + private IManager $activityManager, private IL10N $l10n, ) { } @@ -134,7 +136,7 @@ public function createServer(string $baseUri, )); if ($this->userSession->isLoggedIn()) { - $server->addPlugin(new TagsPlugin($objectTree, $this->tagManager, $this->userSession, $this->eventDispatcher, \OCP\Server::get(\OCP\Activity\IManager::class))); + $server->addPlugin(new TagsPlugin($objectTree, $this->tagManager, $this->userSession, $this->eventDispatcher, $this->activityManager)); $server->addPlugin(new SharesPlugin( $objectTree, $this->userSession, diff --git a/apps/dav/lib/Connector/Sabre/TagsPlugin.php b/apps/dav/lib/Connector/Sabre/TagsPlugin.php index 0f2e06d83f066..2b9aa79f6e64c 100644 --- a/apps/dav/lib/Connector/Sabre/TagsPlugin.php +++ b/apps/dav/lib/Connector/Sabre/TagsPlugin.php @@ -27,14 +27,10 @@ * License along with this library. If not, see . * */ -use OCP\ITagManager; -use OCP\ITags; -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\ITagManager; +use OCP\ITags; use OCP\IUserSession; use Sabre\DAV\PropFind; use Sabre\DAV\PropPatch; @@ -266,11 +262,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)) { @@ -282,39 +276,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 e419cae3ef656..9aaaba68c3911 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -75,7 +75,7 @@ use OCP\IConfig; use OCP\IPreview; use OCP\IRequest; -use OCP\IUser; +use OCP\ITagManager; use OCP\IUserSession; use OCP\Mail\IMailer; use OCP\Profiler\IProfiler; @@ -257,10 +257,10 @@ public function __construct( \OC::$server->getUserFolder(), )); // custom properties plugin must be the last one + $eventDispatcher = \OCP\Server::get(IEventDispatcher::class); + $activityManager = \OCP\Server::get(IManager::class); $userSession = \OC::$server->getUserSession(); $user = $userSession->getUser(); - $dispatcher = \OC::$server->get(IEventDispatcher::class); - $activityManager = \OC::$server->get(IManager::class); if ($user !== null) { $view = Filesystem::getView(); $config = \OCP\Server::get(IConfig::class); @@ -294,9 +294,7 @@ public function __construct( new QuotaPlugin($view)); } $this->server->addPlugin( - new TagsPlugin( - $this->server->tree, \OC::$server->getTagManager(), $userSession, $dispatcher, $activityManager - ) + new TagsPlugin($this->server->tree, \OC::$server->get(ITagManager::class), $userSession, $eventDispatcher, $activityManager) ); // 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 013f02a0e429d..0424db3be346d 100644 --- a/apps/dav/tests/unit/Connector/Sabre/TagsPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/TagsPluginTest.php @@ -399,8 +399,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([ @@ -424,8 +424,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 450cd79505d21..6f860923285ac 100644 --- a/apps/files/lib/Service/TagService.php +++ b/apps/files/lib/Service/TagService.php @@ -7,16 +7,9 @@ */ 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; /** * Service class to manage tags on files. @@ -24,11 +17,8 @@ class TagService { public function __construct( - private IUserSession $userSession, - private IManager $activityManager, private ?ITags $tagger, private ?Folder $homeFolder, - private IEventDispatcher $dispatcher, ) { } @@ -61,14 +51,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); } @@ -77,40 +69,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 50c8d586587cf..7bc0dc92f4f30 100644 --- a/apps/files/tests/Service/TagServiceTest.php +++ b/apps/files/tests/Service/TagServiceTest.php @@ -85,11 +85,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(); @@ -107,9 +104,6 @@ public function testUpdateFileTags(): void { $tag1 = 'tag1'; $tag2 = 'tag2'; - $this->tagService->expects($this->never()) - ->method('addActivity'); - $subdir = $this->root->newFolder('subdir'); $testFile = $subdir->newFile('test.txt'); $testFile->putContent('test contents'); @@ -148,19 +142,14 @@ 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'] - ); - // set tags $this->tagService->updateFileTags('subdir/test.txt', [ITags::TAG_FAVORITE]); + $this->assertEquals([$file->getId()], $this->tagger->getFavorites()); + // remove tag $this->tagService->updateFileTags('subdir/test.txt', []); - + $this->assertEquals([], $this->tagger->getFavorites()); $subdir->delete(); } 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 1f22a4c6a337f..58b833f4a3cda 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 0da4522c86c22..1f8a08fe6cc69 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..3ff3f4ead8513 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)); } @@ -268,9 +270,9 @@ public function testUnTag(): void { public function testFavorite(): void { $tagger = $this->tagMgr->load($this->objectType); - $this->assertTrue($tagger->addToFavorites(1)); + $this->assertTrue($tagger->addToFavorites(1, '/dummypath')); $this->assertEquals([1], $tagger->getFavorites()); - $this->assertTrue($tagger->removeFromFavorites(1)); + $this->assertTrue($tagger->removeFromFavorites(1, '/dummypath')); $this->assertEquals([], $tagger->getFavorites()); } }