Skip to content

Commit

Permalink
Split in getReadablePathByUserForFileId and getReadableNodesByUserFor…
Browse files Browse the repository at this point in the history
…FileId

Optimises further and avoid duplicate code for all activity listeners

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Jun 27, 2023
1 parent 287a5a8 commit 80e469c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
7 changes: 3 additions & 4 deletions apps/comments/lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ public function commentEvent(CommentsEvent $event): void {

$cache = $this->mountCollection->getMountCache();

$users = [];
$filesPerUser = $cache->getReadableNodesByUserForFileId((int)$event->getComment()->getObjectId());
foreach ($filesPerUser as $user => $files) {
$users[$user] = $this->rootFolder->getUserFolder($user)->getRelativePath(reset($files)?->getPath() ?? '');
$users = $cache->getReadablePathByUserForFileId((int)$event->getComment()->getObjectId());
if (empty($users)) {
return;
}

$actor = $this->session->getUser();
Expand Down
8 changes: 2 additions & 6 deletions apps/systemtags/lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,10 @@ public function mapperEvent(MapperEvent $event) {
// Get all mount point owners
$cache = $this->mountCollection->getMountCache();

$users = [];
$filesPerUser = $cache->getReadableNodesByUserForFileId((int)$event->getObjectId());
if (empty($filesPerUser)) {
$users = $cache->getReadablePathByUserForFileId((int)$event->getObjectId());
if (empty($users)) {
return;
}
foreach ($filesPerUser as $user => $files) {
$users[$user] = $this->rootFolder->getUserFolder($user)->getRelativePath(reset($files)?->getPath() ?? '');
}

$actor = $this->session->getUser();
if ($actor instanceof IUser) {
Expand Down
36 changes: 33 additions & 3 deletions lib/private/Files/Config/UserMountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,42 @@ public function getReadableNodesByUserForFileId(int $fileId): array {
$rootFolder = Server::get(IRootFolder::class);
$result = [];
foreach ($mounts as $mount) {
if (isset($result[$mount->getUser()->getUID()])) {
$uid = $mount->getUser()->getUID();
if (!isset($result[$uid])) {
$result[$uid] = [];
}

$userFolder = $rootFolder->getUserFolder($uid);
$result[$uid] = array_merge($result[$uid], $userFolder->getById($fileId));
}

return array_filter($result);
}

/**
* Get all users having read access to a file, with a path they see it as.
* They may see the same file under other paths,
* to get this information use the exhaustive getReadableNodesByUserForFileId
*
* @return array<string,string> Paths giving access to the given fileId, indexed by user ID
* @since 28.0.0
*/
public function getReadablePathByUserForFileId(int $fileId): array {
$mounts = $this->getMountsForFileId($fileId);
$rootFolder = Server::get(IRootFolder::class);
$result = [];
foreach ($mounts as $mount) {
$uid = $mount->getUser()->getUID();
if (isset($result[$uid])) {
continue;
}

$userFolder = $rootFolder->getUserFolder($mount->getUser()->getUID());
$result[$mount->getUser()->getUID()] = $userFolder->getById($fileId);
$userFolder = $rootFolder->getUserFolder($uid);
$nodes = $userFolder->getById($fileId);
$node = reset($nodes);
if ($node) {
$result[$uid] = $userFolder->getRelativePath($node->getPath());
}
}

return $result;
Expand Down
9 changes: 9 additions & 0 deletions lib/public/Files/Config/IUserMountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public function getMountsForFileId($fileId, $user = null);
*/
public function getReadableNodesByUserForFileId(int $fileId): array;

/**
* Get all users having read access to a file, with a path they see it as.
* They may see the same file under other paths, to get this information use exhaustive getReadableNodesByUserForFileId
*
* @return array<string, string> Paths giving access to the given fileId, indexed by user ID
* @since 28.0.0
*/
public function getReadablePathByUserForFileId(int $fileId): array;

/**
* Remove all cached mounts for a user
*
Expand Down

0 comments on commit 80e469c

Please sign in to comment.