Skip to content

Commit

Permalink
switch places that always use the first getById result to getFirstNod…
Browse files Browse the repository at this point in the history
…eById

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Feb 9, 2024
1 parent a983470 commit 2e8fb89
Show file tree
Hide file tree
Showing 37 changed files with 99 additions and 148 deletions.
2 changes: 1 addition & 1 deletion apps/dav/lib/BulkUpload/BulkUploadPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)

$node = $this->userFolder->newFile($headers['x-file-path'], $content);
$node->touch($mtime);
$node = $this->userFolder->getById($node->getId())[0];
$node = $this->userFolder->getFirstNodeById($node->getId());

$writtenFiles[$headers['x-file-path']] = [
"error" => false,
Expand Down
3 changes: 1 addition & 2 deletions apps/dav/lib/Connector/Sabre/FilesReportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,8 @@ public function findNodesByFileIds(Node $rootNode, array $fileIds): array {

$results = [];
foreach ($fileIds as $fileId) {
$entry = $folder->getById($fileId);
$entry = $folder->getFirstNodeById($fileId);

Check notice

Code scanning / Psalm

UndefinedInterfaceMethod Note

Method OCP\Files\Node::getFirstNodeById does not exist
if ($entry) {
$entry = current($entry);
$results[] = $this->wrapNode($entry);
}
}
Expand Down
5 changes: 2 additions & 3 deletions apps/dav/lib/Controller/DirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,16 @@ public function __construct(string $appName,
public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
$userFolder = $this->rootFolder->getUserFolder($this->userId);

$files = $userFolder->getById($fileId);
$file = $userFolder->getFirstNodeById($fileId);

if ($files === []) {
if (!$file) {
throw new OCSNotFoundException();
}

if ($expirationTime <= 0 || $expirationTime > (60 * 60 * 24)) {
throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24));
}

$file = array_shift($files);
if (!($file instanceof File)) {
throw new OCSBadRequestException('Direct download only works for files');
}
Expand Down
6 changes: 3 additions & 3 deletions apps/dav/lib/Direct/DirectFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ public function getLastModified() {
private function getFile() {
if ($this->file === null) {
$userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId());
$files = $userFolder->getById($this->direct->getFileId());
$file = $userFolder->getFirstNodeById($this->direct->getFileId());

if ($files === []) {
if (!$file) {
throw new NotFound();
}

$this->file = array_shift($files);
$this->file = $file;

Check notice

Code scanning / Psalm

PropertyTypeCoercion Note

$this->file expects 'OCP\Files\File', parent type 'OCP\Files\Node' provided
}

return $this->file;
Expand Down
4 changes: 2 additions & 2 deletions apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public function __construct(
$userSession,
$groupManager,
function ($name) {
$nodes = \OC::$server->getUserFolder()->getById((int)$name);
return !empty($nodes);
$node = \OC::$server->getUserFolder()->getFirstNodeById((int)$name);

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OC\Server::getUserFolder has been marked as deprecated

Check notice

Code scanning / Psalm

PossiblyNullReference Note

Cannot call method getFirstNodeById on possibly null value
return $node !== null;
}
),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ protected function setUp(): void {
$userFolder = $this->userFolder;

$closure = function ($name) use ($userFolder) {
$nodes = $userFolder->getById(intval($name));
return !empty($nodes);
$node = $userFolder->getFirstNodeById(intval($name));
return $node !== null;
};

$this->node = new \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection(
Expand All @@ -98,14 +98,14 @@ protected function setUp(): void {
);
}


public function testForbiddenCreateFile(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);

$this->node->createFile('555');
}


public function testForbiddenCreateDirectory(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);

Expand All @@ -123,7 +123,7 @@ public function testGetChild(): void {
$this->assertEquals('555', $childNode->getName());
}


public function testGetChildWithoutAccess(): void {
$this->expectException(\Sabre\DAV\Exception\NotFound::class);

Expand All @@ -134,7 +134,7 @@ public function testGetChildWithoutAccess(): void {
$this->node->getChild('555');
}


public function testGetChildren(): void {
$this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);

Expand All @@ -157,14 +157,14 @@ public function testChildExistsWithoutAccess(): void {
$this->assertFalse($this->node->childExists('555'));
}


public function testDelete(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);

$this->node->delete();
}


public function testSetName(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);

Expand Down
6 changes: 3 additions & 3 deletions apps/federatedfilesharing/lib/FederatedShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -890,13 +890,13 @@ private function getNode($userId, $id) {
throw new InvalidShare();
}

$nodes = $userFolder->getById($id);
$node = $userFolder->getFirstNodeById($id);

if (empty($nodes)) {
if (!$node) {
throw new InvalidShare();
}

return $nodes[0];
return $node;

Check notice

Code scanning / Psalm

LessSpecificReturnStatement Note

The type 'OCP\Files\Node' is more general than the declared return type 'OCP\Files\File|OCP\Files\Folder' for OCA\FederatedFileSharing\FederatedShareProvider::getNode
}

/**
Expand Down
5 changes: 2 additions & 3 deletions apps/files/lib/Activity/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ public function getFavoriteNodes(string $user, bool $foldersOnly = false): array
$userFolder = $this->rootFolder->getUserFolder($user);
$favoriteNodes = [];
foreach ($favorites as $favorite) {
$nodes = $userFolder->getById($favorite);
if (!empty($nodes)) {
$node = array_shift($nodes);
$node = $userFolder->getFirstNodeById($favorite);
if ($node) {
if (!$foldersOnly || $node instanceof Folder) {
$favoriteNodes[] = $node;
}
Expand Down
6 changes: 2 additions & 4 deletions apps/files/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ protected function getEndToEndEncryptionContainer($fileId, $path) {
}

$userFolder = $this->rootFolder->getUserFolder($this->activityManager->getCurrentUserId());
$files = $userFolder->getById($fileId);
if (empty($files)) {
$file = $userFolder->getFirstNodeById($fileId);
if (!$file) {
try {
// Deleted, try with parent
$file = $this->findExistingParent($userFolder, dirname($path));
Expand All @@ -450,8 +450,6 @@ protected function getEndToEndEncryptionContainer($fileId, $path) {
return $file;
}

$file = array_shift($files);

if ($file instanceof Folder && $file->isEncrypted()) {
// If the folder is encrypted, it is the Container,
// but can be the name is just fine.
Expand Down
6 changes: 3 additions & 3 deletions apps/files/lib/BackgroundJob/TransferOwnership.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ protected function run($argument) {
$fileId = $transfer->getFileId();

$userFolder = $this->rootFolder->getUserFolder($sourceUser);
$nodes = $userFolder->getById($fileId);
$node = $userFolder->getFirstNodeById($fileId);

if (empty($nodes)) {
if (!$node) {
$this->logger->alert('Could not transfer ownership: Node not found');
$this->failedNotication($transfer);
return;
}
$path = $userFolder->getRelativePath($nodes[0]->getPath());
$path = $userFolder->getRelativePath($node->getPath());

$sourceUserObject = $this->userManager->get($sourceUser);
$destinationUserObject = $this->userManager->get($destinationUser);
Expand Down
12 changes: 6 additions & 6 deletions apps/files/lib/Collaboration/Resources/ResourceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ private function getNode(IResource $resource): ?Node {
if (isset($this->nodes[(int) $resource->getId()])) {
return $this->nodes[(int) $resource->getId()];
}
$nodes = $this->rootFolder->getById((int) $resource->getId());
if (!empty($nodes)) {
$this->nodes[(int) $resource->getId()] = array_shift($nodes);
$node = $this->rootFolder->getFirstNodeById((int) $resource->getId());
if ($node) {
$this->nodes[(int) $resource->getId()] = $node;
return $this->nodes[(int) $resource->getId()];
}
return null;
Expand Down Expand Up @@ -113,10 +113,10 @@ public function canAccessResource(IResource $resource, IUser $user = null): bool
}

$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$nodes = $userFolder->getById((int) $resource->getId());
$node = $userFolder->getById((int) $resource->getId());

if (!empty($nodes)) {
$this->nodes[(int) $resource->getId()] = array_shift($nodes);
if ($node) {
$this->nodes[(int) $resource->getId()] = $node;
return true;
}

Expand Down
17 changes: 7 additions & 10 deletions apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ private function provideInitialState(string $dir, ?string $fileid): void {

$uid = $user->getUID();
$userFolder = $this->rootFolder->getUserFolder($uid);
$nodes = $userFolder->getById((int) $fileid);
$node = array_shift($nodes);
$node = $userFolder->getFirstNodeById((int) $fileid);

if ($node === null) {
return;
Expand Down Expand Up @@ -363,17 +362,16 @@ private function provideInitialState(string $dir, ?string $fileid): void {
private function redirectToFileIfInTrashbin($fileId): RedirectResponse {
$uid = $this->userSession->getUser()->getUID();
$baseFolder = $this->rootFolder->getUserFolder($uid);
$nodes = $baseFolder->getById($fileId);
$node = $baseFolder->getFirstNodeById($fileId);
$params = [];

if (empty($nodes) && $this->appManager->isEnabledForUser('files_trashbin')) {
if (!$node && $this->appManager->isEnabledForUser('files_trashbin')) {
/** @var Folder */
$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
$nodes = $baseFolder->getById($fileId);
$node = $baseFolder->getFirstNodeById($fileId);
$params['view'] = 'trashbin';

if (!empty($nodes)) {
$node = current($nodes);
if ($node) {
$params['fileid'] = $fileId;
if ($node instanceof Folder) {
// set the full path to enter the folder
Expand All @@ -398,16 +396,15 @@ private function redirectToFileIfInTrashbin($fileId): RedirectResponse {
private function redirectToFile(int $fileId) {
$uid = $this->userSession->getUser()->getUID();
$baseFolder = $this->rootFolder->getUserFolder($uid);
$nodes = $baseFolder->getById($fileId);
$node = $baseFolder->getFirstNodeById($fileId);
$params = ['view' => 'files'];

try {
$this->redirectToFileIfInTrashbin($fileId);
} catch (NotFoundException $e) {
}

if (!empty($nodes)) {
$node = current($nodes);
if ($node) {
$params['fileid'] = $fileId;
if ($node instanceof Folder) {
// set the full path to enter the folder
Expand Down
6 changes: 3 additions & 3 deletions apps/files/lib/Listener/SyncLivePhotosListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ private function getLivePhotoPeer(int $nodeId): ?Node {
$peerFileId = (int)$metadata->getString('files-live-photo');

// Check the user's folder.
$nodes = $this->userFolder->getById($peerFileId);
if (count($nodes) !== 0) {
return $nodes[0];
$node = $this->userFolder->getFirstNodeById($peerFileId);
if ($node) {
return $node;
}

// Check the user's trashbin.
Expand Down
5 changes: 2 additions & 3 deletions apps/files_reminders/lib/Model/RichReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ public function __construct(
* @throws NodeNotFoundException
*/
public function getNode(): Node {
$nodes = $this->root->getUserFolder($this->getUserId())->getById($this->getFileId());
if (empty($nodes)) {
$node = $this->root->getUserFolder($this->getUserId())->getFirstNodeById($this->getFileId());
if (!$node) {
throw new NodeNotFoundException();
}
$node = reset($nodes);
return $node;
}

Expand Down
5 changes: 2 additions & 3 deletions apps/files_reminders/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ public function prepare(INotification $notification, string $languageCode): INot
$params = $notification->getSubjectParameters();
$fileId = $params['fileId'];

$nodes = $this->root->getUserFolder($notification->getUser())->getById($fileId);
if (empty($nodes)) {
$node = $this->root->getUserFolder($notification->getUser())->getFirstNodeById($fileId);
if (!$node) {
throw new InvalidArgumentException();
}
$node = reset($nodes);

$path = rtrim($node->getPath(), '/');
if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) {
Expand Down
4 changes: 2 additions & 2 deletions apps/files_reminders/lib/Service/ReminderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): boo
$this->reminderMapper->update($reminder);
return false;
} catch (DoesNotExistException $e) {
$nodes = $this->root->getUserFolder($user->getUID())->getById($fileId);
if (empty($nodes)) {
$node = $this->root->getUserFolder($user->getUID())->getFirstNodeById($fileId);
if (!$node) {
throw new NodeNotFoundException();
}
// Create new reminder if no reminder is found
Expand Down
6 changes: 3 additions & 3 deletions apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public function sort(array &$sortArray, array $context) {
}
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
/** @var Node[] $nodes */
$nodes = $userFolder->getById((int)$context['itemId']);
if (count($nodes) === 0) {
$node = $userFolder->getFirstNodeById((int)$context['itemId']);
if (!$node) {
return;
}
$al = $this->shareManager->getAccessList($nodes[0]);
$al = $this->shareManager->getAccessList($node);

foreach ($sortArray as $type => &$byType) {
if (!isset($al[$type]) || !is_array($al[$type])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,13 @@ private function formatShare(IShare $share): array {
'path' => $share->getTarget(),
];
$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
$nodes = $userFolder->getById($share->getNodeId());
if (empty($nodes)) {
$node = $userFolder->getFirstNodeById($share->getNodeId());
if (!$node) {
// fallback to guessing the path
$node = $userFolder->get($share->getTarget());
if ($node === null || $share->getTarget() === '') {
throw new NotFoundException();
}
} else {
$node = $nodes[0];
}

$result['path'] = $userFolder->getRelativePath($node->getPath());
Expand Down
Loading

0 comments on commit 2e8fb89

Please sign in to comment.