Skip to content

Commit

Permalink
Merge pull request #82 from nextcloud/enh/rename-listener
Browse files Browse the repository at this point in the history
enh: add node rename listener
  • Loading branch information
marcelklehr authored Dec 16, 2024
2 parents 43332f8 + bf4cfcd commit 6c33b75
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\Files\Cache\CacheEntryInsertedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\Events\NodeRemovedFromCache;
use OCP\IConfig;
Expand Down Expand Up @@ -72,6 +73,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(BeforeNodeDeletedEvent::class, FileListener::class);
$context->registerEventListener(NodeCreatedEvent::class, FileListener::class);
$context->registerEventListener(CacheEntryInsertedEvent::class, FileListener::class);
$context->registerEventListener(NodeRenamedEvent::class, FileListener::class);
$context->registerEventListener(NodeRemovedFromCache::class, FileListener::class);
$context->registerEventListener(NodeWrittenEvent::class, FileListener::class);
$context->registerEventListener(AppDisableEvent::class, AppDisableListener::class);
Expand Down
32 changes: 32 additions & 0 deletions lib/Listener/FileListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
use OCA\ContextChat\Service\ActionService;
use OCA\ContextChat\Service\ProviderConfigService;
use OCA\ContextChat\Service\QueueService;
use OCA\ContextChat\Service\StorageService;
use OCP\DB\Exception;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Cache\CacheEntryInsertedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\Events\NodeRemovedFromCache;
use OCP\Files\File;
Expand All @@ -30,6 +32,7 @@
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -42,6 +45,8 @@ public function __construct(
private QueueService $queue,
private IRootFolder $rootFolder,
private ActionService $actionService,
private StorageService $storageService,
private IManager $shareManager,
) {
}

Expand Down Expand Up @@ -76,6 +81,33 @@ public function handle(Event $event): void {
return;
}

if ($event instanceof NodeRenamedEvent) {
$targetNode = $event->getTarget();

if ($targetNode instanceof Folder) {
$files = $this->storageService->getAllFilesInFolder($targetNode);
} else {
$files = [$targetNode];
}

foreach ($files as $file) {
if (!$file instanceof File) {
continue;
}
$shareAccessList = $this->shareManager->getAccessList($file, true, true);
/**
* @var string[] $shareUserIds
*/
$shareUserIds = array_keys($shareAccessList['users']);
$fileUserIds = $this->storageService->getUsersForFileId($file->getId());

$userIds = array_unique(array_merge($shareUserIds, $fileUserIds));
$fileRef = ProviderConfigService::getSourceId($file->getId());
$this->actionService->updateAccessDeclSource($userIds, $fileRef);
}
return;
}

if ($event instanceof NodeRemovedFromCache) {
$cacheEntry = $event->getStorage()->getCache()->get($event->getPath());
if ($cacheEntry === false) {
Expand Down
25 changes: 3 additions & 22 deletions lib/Listener/ShareListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ public function handle(Event $event): void {
}

if ($node->getType() === FileInfo::TYPE_FOLDER) {
$mount = $node->getMountPoint();
if ($mount->getNumericStorageId() === null) {
return;
}
$files = $this->storageService->getFilesInMount($mount->getNumericStorageId(), $node->getId(), 0, 0);
foreach ($files as $fileId) {
$file = current($this->rootFolder->getById($fileId));
$files = $this->storageService->getAllFilesInFolder($node);
foreach ($files as $file) {
if (!$file instanceof File) {
continue;
}
Expand Down Expand Up @@ -133,21 +128,7 @@ public function handle(Event $event): void {
$userIds = array_unique(array_merge($realFileUserIds, $shareUserIds));

if ($node instanceof Folder) {
$mount = $node->getMountPoint();
if ($mount->getNumericStorageId() === null) {
return;
}
$files = $this->storageService->getFilesInMount($mount->getNumericStorageId(), $node->getId(), 0, 0);
$files = [];

foreach ($files as $fileId) {
$node = current($this->rootFolder->getById($fileId));
if (!$node instanceof File) {
continue;
}
$files[] = $node;
}

$files = $this->storageService->getAllFilesInFolder($node);
foreach ($files as $file) {
$this->actionService->updateAccessDeclSource(
$userIds,
Expand Down
31 changes: 31 additions & 0 deletions lib/Service/StorageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;
Expand All @@ -42,6 +46,7 @@ public function __construct(
private IMimeTypeLoader $mimeTypes,
private IUserMountCache $userMountCache,
private IFilesMetadataManager $metadataManager,
private IRootFolder $rootFolder,
) {
}

Expand Down Expand Up @@ -162,6 +167,32 @@ public function getUsersForFileId(int $fileId): array {
}, $mountInfos);
}

/**
* @param Node $node
* @return array<File>
*/
public function getAllFilesInFolder(Node $node): array {
if (!$node instanceof Folder) {
return [];
}
$mount = $node->getMountPoint();
if ($mount->getNumericStorageId() === null) {
return [];
}
$filesGen = $this->getFilesInMount($mount->getNumericStorageId(), $node->getId(), 0, 0);
$files = [];

foreach ($filesGen as $fileId) {
$node = current($this->rootFolder->getById($fileId));
if (!$node instanceof File) {
continue;
}
$files[] = $node;
}

return $files;
}

private function getCacheQueryBuilder(): CacheQueryBuilder {
return new CacheQueryBuilder($this->db->getQueryBuilder(), $this->metadataManager);
}
Expand Down

0 comments on commit 6c33b75

Please sign in to comment.