diff options
author | Robin Appelman <robin@icewind.nl> | 2021-03-26 17:34:49 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2021-04-21 13:56:11 +0200 |
commit | 907e997c9914c925fbf86225a98b2262ee798351 (patch) | |
tree | f85c98f32d873e2afb60fd51ae5cc394004b3271 /apps | |
parent | e8221303e9634b6ca4e6c0b9deb60f4dda6b3d2c (diff) | |
download | nextcloud-server-907e997c9914c925fbf86225a98b2262ee798351.tar.gz nextcloud-server-907e997c9914c925fbf86225a98b2262ee798351.zip |
optimize getting share types for recent files
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files/lib/Controller/ApiController.php | 82 |
1 files changed, 49 insertions, 33 deletions
diff --git a/apps/files/lib/Controller/ApiController.php b/apps/files/lib/Controller/ApiController.php index 0cf261af726..20f60cde30d 100644 --- a/apps/files/lib/Controller/ApiController.php +++ b/apps/files/lib/Controller/ApiController.php @@ -177,9 +177,9 @@ class ApiController extends Controller { * @return array */ private function formatNodes(array $nodes) { - return array_values(array_map(function (Node $node) { - /** @var \OC\Files\Node\Node $shareTypes */ - $shareTypes = $this->getShareTypes($node); + $shareTypesForNodes = $this->getShareTypesForNodes($nodes); + return array_values(array_map(function (Node $node) use ($shareTypesForNodes) { + $shareTypes = $shareTypesForNodes[$node->getId()] ?? []; $file = \OCA\Files\Helper::formatFileInfo($node->getFileInfo()); $file['hasPreview'] = $this->previewManager->isAvailable($node); $parts = explode('/', dirname($node->getPath()), 4); @@ -196,28 +196,13 @@ class ApiController extends Controller { } /** - * Returns a list of recently modifed files. - * - * @NoAdminRequired + * Get the share types for each node * - * @return DataResponse - */ - public function getRecentFiles() { - $nodes = $this->userFolder->getRecent(100); - $files = $this->formatNodes($nodes); - return new DataResponse(['files' => $files]); - } - - /** - * Return a list of share types for outgoing shares - * - * @param Node $node file node - * - * @return int[] array of share types + * @param \OCP\Files\Node[] $nodes + * @return array<int, int[]> list of share types for each fileid */ - private function getShareTypes(Node $node) { + private function getShareTypesForNodes(array $nodes): array { $userId = $this->userSession->getUser()->getUID(); - $shareTypes = []; $requestedShareTypes = [ IShare::TYPE_USER, IShare::TYPE_GROUP, @@ -227,23 +212,54 @@ class ApiController extends Controller { IShare::TYPE_ROOM, IShare::TYPE_DECK, ]; - foreach ($requestedShareTypes as $requestedShareType) { - // one of each type is enough to find out about the types - $shares = $this->shareManager->getSharesBy( - $userId, - $requestedShareType, - $node, - false, - 1 - ); - if (!empty($shares)) { - $shareTypes[] = $requestedShareType; + $shareTypes = []; + + $nodeIds = array_map(function (Node $node) { + return $node->getId(); + }, $nodes); + + foreach ($requestedShareTypes as $shareType) { + $nodesLeft = array_combine($nodeIds, array_fill(0, count($nodeIds), true)); + $offset = 0; + + // fetch shares until we've either found shares for all nodes or there are no more shares left + while (count($nodesLeft) > 0) { + $shares = $this->shareManager->getSharesBy($userId, $shareType, null, false, 100, $offset); + foreach ($shares as $share) { + $fileId = $share->getNodeId(); + if (isset($nodesLeft[$fileId])) { + if (!isset($shareTypes[$fileId])) { + $shareTypes[$fileId] = []; + } + $shareTypes[$fileId][] = $shareType; + unset($nodesLeft[$fileId]); + } + } + + if (count($shares) < 100) { + break; + } else { + $offset += count($shares); + } } } return $shareTypes; } /** + * Returns a list of recently modifed files. + * + * @NoAdminRequired + * + * @return DataResponse + */ + public function getRecentFiles() { + $nodes = $this->userFolder->getRecent(100); + $files = $this->formatNodes($nodes); + return new DataResponse(['files' => $files]); + } + + /** * Change the default sort mode * * @NoAdminRequired |