Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions lib/Listener/TagListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,47 @@

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\SystemTag\MapperEvent;
use OCP\SystemTag\TagAssignedEvent;
use OCP\SystemTag\TagUnassignedEvent;
use Override;

/**
* @template-implements IEventListener<MapperEvent>
* @template-implements IEventListener<TagAssignedEvent|TagUnassignedEvent>
*/
class TagListener implements IEventListener {
private readonly ICache $tagCountsCache;
private readonly IRootFolder $rootFolder;
private readonly IUserMountCache $userMountCache;

public function __construct(ICacheFactory $cacheFactory, IRootFolder $rootFolder, IUserMountCache $userMountCache) {
public function __construct(
ICacheFactory $cacheFactory,
private readonly IRootFolder $rootFolder,
private readonly IUserMountCache $userMountCache,
) {
$this->tagCountsCache = $cacheFactory->createLocal('photos:tag-counts');
$this->rootFolder = $rootFolder;
$this->userMountCache = $userMountCache;
}

/**
* @inheritDoc
*/
#[Override]
public function handle(Event $event): void {
if ($event instanceof MapperEvent) {
if ($event->getObjectType() !== 'files') {
return;
}
$node = $this->rootFolder->getFirstNodeById((int)$event->getObjectId());
if (!$event instanceof TagAssignedEvent && !$event instanceof TagUnassignedEvent) {
return;
}

if ($event->getObjectType() !== 'files') {
return;
}

foreach ($event->getObjectIds() as $objectId) {
$node = $this->rootFolder->getFirstNodeById((int)$objectId);
if (!$node) {
return;
continue;
}

$mounts = $this->userMountCache->getMountsForRootId($node->getMountPoint()->getStorageRootId());
$userIds = array_map(static fn (ICachedMountInfo $mount) => $mount->getUser()->getUID(), $mounts);
foreach ($userIds as $userId) {
$this->tagCountsCache->remove($userId);
foreach ($mounts as $mount) {
$this->tagCountsCache->remove($mount->getUser()->getUID());
}
}
}
Expand Down
Loading