Skip to content

Commit 8d953ae

Browse files
committed
refactor(tags): move favorite event dispatching to tags.php
Signed-off-by: grnd-alt <[email protected]>
1 parent 2d02d83 commit 8d953ae

File tree

7 files changed

+34
-46
lines changed

7 files changed

+34
-46
lines changed

apps/dav/lib/Connector/Sabre/TagsPlugin.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
*
2929
*/
3030
use OCP\EventDispatcher\IEventDispatcher;
31-
use OCP\Files\Events\NodeAddedToFavorite;
32-
use OCP\Files\Events\NodeRemovedFromFavorite;
3331
use OCP\ITagManager;
3432
use OCP\ITags;
3533
use OCP\IUserSession;
@@ -260,10 +258,8 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
260258
$propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node, $path) {
261259
if ((int)$favState === 1 || $favState === 'true') {
262260
$this->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE);
263-
$this->eventDispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $node->getId(), $path));
264261
} else {
265262
$this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE);
266-
$this->eventDispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $node->getId(), $path));
267263
}
268264

269265
if (is_null($favState)) {

apps/files/lib/AppInfo/Application.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use OCP\AppFramework\Bootstrap\IRegistrationContext;
3636
use OCP\Collaboration\Reference\RenderReferenceEvent;
3737
use OCP\Collaboration\Resources\IProviderManager;
38-
use OCP\EventDispatcher\IEventDispatcher;
3938
use OCP\Files\Cache\CacheEntryRemovedEvent;
4039
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
4140
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
@@ -100,7 +99,6 @@ public function register(IRegistrationContext $context): void {
10099
$c->get(IActivityManager::class),
101100
$c->get(ITagManager::class)->load(self::APP_ID),
102101
$server->getUserFolder(),
103-
$c->get(IEventDispatcher::class),
104102
);
105103
});
106104

apps/files/lib/Service/TagService.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
namespace OCA\Files\Service;
99

1010
use OCP\Activity\IManager;
11-
use OCP\EventDispatcher\IEventDispatcher;
12-
use OCP\Files\Events\NodeAddedToFavorite;
13-
use OCP\Files\Events\NodeRemovedFromFavorite;
1411
use OCP\Files\Folder;
1512
use OCP\Files\NotFoundException;
1613
use OCP\ITags;
@@ -26,7 +23,6 @@ public function __construct(
2623
private IManager $activityManager,
2724
private ?ITags $tagger,
2825
private ?Folder $homeFolder,
29-
private IEventDispatcher $dispatcher,
3026
) {
3127
}
3228

@@ -58,16 +54,10 @@ public function updateFileTags($path, $tags) {
5854

5955
$newTags = array_diff($tags, $currentTags);
6056
foreach ($newTags as $tag) {
61-
if ($tag === ITags::TAG_FAVORITE) {
62-
$this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $fileId, $path));
63-
}
6457
$this->tagger->tagAs($fileId, $tag);
6558
}
6659
$deletedTags = array_diff($currentTags, $tags);
6760
foreach ($deletedTags as $tag) {
68-
if ($tag === ITags::TAG_FAVORITE) {
69-
$this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $fileId, $path));
70-
}
7161
$this->tagger->unTag($fileId, $tag);
7262
}
7363

apps/files/tests/Service/TagServiceTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ protected function getTagService(array $methods = []) {
9191
$this->activityManager,
9292
$this->tagger,
9393
$this->root,
94-
$this->dispatcher,
9594
])
9695
->setMethods($methods)
9796
->getMock();

lib/private/TagManager.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCP\Db\Exception as DBException;
1212
use OCP\DB\QueryBuilder\IQueryBuilder;
1313
use OCP\EventDispatcher\Event;
14+
use OCP\EventDispatcher\IEventDispatcher;
1415
use OCP\EventDispatcher\IEventListener;
1516
use OCP\IDBConnection;
1617
use OCP\ITagManager;
@@ -23,16 +24,14 @@
2324
* @template-implements IEventListener<UserDeletedEvent>
2425
*/
2526
class TagManager implements ITagManager, IEventListener {
26-
private TagMapper $mapper;
27-
private IUserSession $userSession;
28-
private IDBConnection $connection;
29-
private LoggerInterface $logger;
3027

31-
public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection, LoggerInterface $logger) {
32-
$this->mapper = $mapper;
33-
$this->userSession = $userSession;
34-
$this->connection = $connection;
35-
$this->logger = $logger;
28+
public function __construct(
29+
private TagMapper $mapper,
30+
private IUserSession $userSession,
31+
private IDBConnection $connection,
32+
private LoggerInterface $logger,
33+
private IEventDispatcher $dispatcher,
34+
) {
3635
}
3736

3837
/**
@@ -57,7 +56,7 @@ public function load($type, $defaultTags = [], $includeShared = false, $userId =
5756
}
5857
$userId = $this->userSession->getUser()->getUId();
5958
}
60-
return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $defaultTags);
59+
return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $this->dispatcher, $this->userSession, $defaultTags);
6160
}
6261

6362
/**

lib/private/Tags.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
use OC\Tagging\TagMapper;
1212
use OCP\DB\Exception;
1313
use OCP\DB\QueryBuilder\IQueryBuilder;
14+
use OCP\EventDispatcher\IEventDispatcher;
15+
use OCP\Files\Events\NodeAddedToFavorite;
16+
use OCP\Files\Events\NodeRemovedFromFavorite;
1417
use OCP\IDBConnection;
1518
use OCP\ITags;
19+
use OCP\IUserSession;
1620
use OCP\Share_Backend;
1721
use Psr\Log\LoggerInterface;
1822

@@ -21,10 +25,6 @@ class Tags implements ITags {
2125
* Used for storing objectid/categoryname pairs while rescanning.
2226
*/
2327
private static array $relations = [];
24-
private string $type;
25-
private string $user;
26-
private IDBConnection $db;
27-
private LoggerInterface $logger;
2828
private array $tags = [];
2929

3030
/**
@@ -38,11 +38,6 @@ class Tags implements ITags {
3838
*/
3939
private array $owners = [];
4040

41-
/**
42-
* The Mapper we are using to communicate our Tag objects to the database.
43-
*/
44-
private TagMapper $mapper;
45-
4641
/**
4742
* The sharing backend for objects of $this->type. Required if
4843
* $this->includeShared === true to determine ownership of items.
@@ -62,14 +57,18 @@ class Tags implements ITags {
6257
*
6358
* since 20.0.0 $includeShared isn't used anymore
6459
*/
65-
public function __construct(TagMapper $mapper, string $user, string $type, LoggerInterface $logger, IDBConnection $connection, array $defaultTags = []) {
66-
$this->mapper = $mapper;
67-
$this->user = $user;
68-
$this->type = $type;
60+
public function __construct(
61+
private TagMapper $mapper,
62+
private string $user,
63+
private string $type,
64+
private LoggerInterface $logger,
65+
private IDBConnection $db,
66+
private IEventDispatcher $dispatcher,
67+
private IUserSession $userSession,
68+
array $defaultTags = [],
69+
) {
6970
$this->owners = [$this->user];
7071
$this->tags = $this->mapper->loadTags($this->owners, $this->type);
71-
$this->db = $connection;
72-
$this->logger = $logger;
7372

7473
if (count($defaultTags) > 0 && count($this->tags) === 0) {
7574
$this->addMultiple($defaultTags, true);
@@ -502,7 +501,7 @@ public function removeFromFavorites($objid) {
502501
* @param string $tag The id or name of the tag
503502
* @return boolean Returns false on error.
504503
*/
505-
public function tagAs($objid, $tag) {
504+
public function tagAs($objid, $tag, string $path = '') {
506505
if (is_string($tag) && !is_numeric($tag)) {
507506
$tag = trim($tag);
508507
if ($tag === '') {
@@ -532,6 +531,9 @@ public function tagAs($objid, $tag) {
532531
]);
533532
return false;
534533
}
534+
if ($tag === ITags::TAG_FAVORITE) {
535+
$this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $objid, $path));
536+
}
535537
return true;
536538
}
537539

@@ -542,7 +544,7 @@ public function tagAs($objid, $tag) {
542544
* @param string $tag The id or name of the tag
543545
* @return boolean
544546
*/
545-
public function unTag($objid, $tag) {
547+
public function unTag($objid, $tag, string $path = '') {
546548
if (is_string($tag) && !is_numeric($tag)) {
547549
$tag = trim($tag);
548550
if ($tag === '') {
@@ -569,6 +571,9 @@ public function unTag($objid, $tag) {
569571
]);
570572
return false;
571573
}
574+
if ($tag === ITags::TAG_FAVORITE) {
575+
$this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $objid, $path));
576+
}
572577
return true;
573578
}
574579

tests/lib/TagsTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Test;
99

10+
use OCP\EventDispatcher\IEventDispatcher;
1011
use OCP\IDBConnection;
1112
use OCP\IUser;
1213
use OCP\IUserSession;
@@ -48,7 +49,7 @@ protected function setUp(): void {
4849

4950
$this->objectType = $this->getUniqueID('type_');
5051
$this->tagMapper = new \OC\Tagging\TagMapper(\OC::$server->get(IDBConnection::class));
51-
$this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class));
52+
$this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(IEventDispatcher::class));
5253
}
5354

5455
protected function tearDown(): void {
@@ -65,7 +66,7 @@ public function testTagManagerWithoutUserReturnsNull(): void {
6566
->expects($this->any())
6667
->method('getUser')
6768
->willReturn(null);
68-
$this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection(), \OC::$server->get(LoggerInterface::class));
69+
$this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection(), \OC::$server->get(LoggerInterface::class), \OC::$server->get(IEventDispatcher::class));
6970
$this->assertNull($this->tagMgr->load($this->objectType));
7071
}
7172

0 commit comments

Comments
 (0)