From 66a63e1877a739dd95460dbe2526ba375ab6d8cf Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 10 Nov 2026 15:15:11 +0100 Subject: [PATCH] refactor: use public server API instead of private classes Replace OC\User\NoUserException with OCP\User\Exceptions\UserNotFoundException. The private exception extends the public one since server 35, and IRootFolder::getUserFolder() documents the public one, so the catch blocks behave the same. Replace OC\Files\Node\File with OCP\Files\File in WorkspacePlugin. The private class was only needed for getFileInfo(); the public node exposes getId() and getEtag() directly. Drop the now unused NoUserException psalm stub. Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5-5 --- lib/DAV/WorkspacePlugin.php | 6 ++--- lib/Middleware/SessionMiddleware.php | 4 +-- lib/Notification/Notifier.php | 4 +-- lib/Service/AttachmentService.php | 34 +++++++++++++------------- lib/Service/FileService.php | 4 +-- tests/stub.php | 5 ---- tests/unit/DAV/WorkspacePluginTest.php | 9 +++---- 7 files changed, 29 insertions(+), 37 deletions(-) diff --git a/lib/DAV/WorkspacePlugin.php b/lib/DAV/WorkspacePlugin.php index d3bbc8d6345..313e5184d76 100644 --- a/lib/DAV/WorkspacePlugin.php +++ b/lib/DAV/WorkspacePlugin.php @@ -9,12 +9,12 @@ namespace OCA\Text\DAV; use Exception; -use OC\Files\Node\File; use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Files\FilesHome; use OCA\Text\Service\ConfigService; use OCA\Text\Service\FileService; use OCA\Text\Service\WorkspaceService; +use OCP\Files\File; use OCP\Files\GenericFileException; use OCP\Files\NotPermittedException; use OCP\ICacheFactory; @@ -106,7 +106,7 @@ public function propFind(PropFind $propFind, INode $node) { $cachedContent = ''; if ($file instanceof File && $this->fileService->canSeeContent($file)) { $cache = $this->cacheFactory->createDistributed('text_workspace'); - $cacheKey = $file->getFileInfo()->getId() . '_' . $file->getFileInfo()->getEtag(); + $cacheKey = $file->getId() . '_' . $file->getEtag(); if (($cachedContent = $cache->get($cacheKey)) !== null) { return $cachedContent; } @@ -130,7 +130,7 @@ public function propFind(PropFind $propFind, INode $node) { $workspaceFileCallback = function () use ($file) { if ($file instanceof File) { - return $file->getFileInfo()->getId(); + return $file->getId(); } return ''; }; diff --git a/lib/Middleware/SessionMiddleware.php b/lib/Middleware/SessionMiddleware.php index d9537f13990..942ff68ce33 100644 --- a/lib/Middleware/SessionMiddleware.php +++ b/lib/Middleware/SessionMiddleware.php @@ -7,7 +7,6 @@ namespace OCA\Text\Middleware; -use OC\User\NoUserException; use OCA\Text\Controller\ISessionAwareController; use OCA\Text\Db\Document; use OCA\Text\Exception\AccountDisabledException; @@ -30,6 +29,7 @@ use OCP\IRequest; use OCP\IUserManager; use OCP\IUserSession; +use OCP\User\Exceptions\UserNotFoundException; use ReflectionException; class SessionMiddleware extends Middleware { @@ -134,7 +134,7 @@ private function assertDocumentSession(ISessionAwareController $controller): voi * Fallback for loading attachments without a session. * * @throws NotPermittedException - * @throws NoUserException + * @throws UserNotFoundException * @throws InvalidSessionException */ private function assertUserOrShareToken(ISessionAwareController $controller): void { diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php index a8c979adeb4..8456a3441c1 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -9,7 +9,6 @@ namespace OCA\Text\Notification; -use OC\User\NoUserException; use OCA\Text\Event\MentionEvent; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; @@ -22,6 +21,7 @@ use OCP\Notification\INotification; use OCP\Notification\INotifier; use OCP\Notification\UnknownNotificationException; +use OCP\User\Exceptions\UserNotFoundException; class Notifier implements INotifier { public const string TYPE_MENTIONED = 'mentioned'; @@ -86,7 +86,7 @@ public function prepare(INotification $notification, string $languageCode): INot try { $userFolder = $this->rootFolder->getUserFolder($targetUser); - } catch (NotPermittedException|NoUserException) { + } catch (NotPermittedException|UserNotFoundException) { throw new UnknownNotificationException(); } $node = $userFolder->getFirstNodeById($fileId); diff --git a/lib/Service/AttachmentService.php b/lib/Service/AttachmentService.php index 13345885622..38c20707b26 100755 --- a/lib/Service/AttachmentService.php +++ b/lib/Service/AttachmentService.php @@ -9,7 +9,6 @@ namespace OCA\Text\Service; -use OC\User\NoUserException; use OCA\DAV\Connector\Sabre\PublicAuth; use OCA\Text\Context\IContext; use OCA\Text\Controller\AttachmentController; @@ -32,6 +31,7 @@ use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager as ShareManager; use OCP\Share\IShare; +use OCP\User\Exceptions\UserNotFoundException; use OCP\Util; readonly class AttachmentService { @@ -52,7 +52,7 @@ public function __construct( * Get image content or preview from file name * * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException * @throws NotFoundException * @throws NotPermittedException */ @@ -64,7 +64,7 @@ public function getImageFile(IContext $context, string $imageFileName, bool $pre /** * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException * @throws NotFoundException * @throws NotPermittedException */ @@ -95,7 +95,7 @@ private function getImageFileContent(string $imageFileName, File $textFile, bool * @throws NotFoundException * @throws InvalidPathException * @throws NotPermittedException - * @throws NoUserException + * @throws UserNotFoundException */ public function getMediaFile(IContext $context, string $mediaFileName, IShare|IUser $auth): ?File { $textFile = $this->getTextFile($context); @@ -116,7 +116,7 @@ private function canDownload(File $textFile, IShare|IUser $auth): bool { * @throws NotFoundException * @throws NotPermittedException * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException */ private function getMediaFullFile(string $mediaFileName, File $textFile): ?File { $attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true); @@ -131,7 +131,7 @@ private function getMediaFullFile(string $mediaFileName, File $textFile): ?File * @throws NotFoundException * @throws NotPermittedException * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException */ public function getMediaFilePreview(IContext $context, string $mediaFileName): ?array { $textFile = $this->getTextFile($context); @@ -144,7 +144,7 @@ public function getMediaFilePreview(IContext $context, string $mediaFileName): ? * @throws NotFoundException * @throws NotPermittedException * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException */ private function getMediaFilePreviewFile(string $mediaFileName, File $textFile): ?array { $attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true); @@ -171,7 +171,7 @@ private function getMediaFilePreviewFile(string $mediaFileName, File $textFile): /** * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException * @throws NotFoundException * @throws NotPermittedException */ @@ -247,7 +247,7 @@ public function getAttachmentList(IContext $context, IShare|IUser $auth, ?Sessio * @throws NotFoundException * @throws NotPermittedException * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException */ public function uploadAttachment(IContext $context, string $newFileName, $newFileResource, IShare|IUser $auth): array { if ($auth instanceof IShare && $auth->getPassword() !== null) { @@ -293,7 +293,7 @@ public function uploadAttachment(IContext $context, string $newFileName, $newFil * @throws NotFoundException * @throws NotPermittedException * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException */ public function insertAttachmentFile(IContext $context, string $path, IUser $user): array { $textFile = $this->getTextFile($context); @@ -319,7 +319,7 @@ public function insertAttachmentFile(IContext $context, string $path, IUser $use * @throws NotFoundException * @throws NotPermittedException * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException */ public function createAttachmentFile(IContext $context, string $newFileName, IUser $user): array { $textFile = $this->getTextFile($context); @@ -364,7 +364,7 @@ public static function getUniqueFileName(Folder $dir, string $fileName): string * @throws NotFoundException * @throws NotPermittedException * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException */ private function getAttachmentDirectoryForFile(File $textFile, bool $create = false): Folder { $owner = $textFile->getOwner(); @@ -394,7 +394,7 @@ private function getAttachmentDirectoryForFile(File $textFile, bool $create = fa * * @throws NotFoundException * @throws NotPermittedException - * @throws NoUserException + * @throws UserNotFoundException */ private function getFileFromPath(string $filePath, string $userId): File { $userFolder = $this->rootFolder->getUserFolder($userId); @@ -410,7 +410,7 @@ private function getFileFromPath(string $filePath, string $userId): File { /** * Get a user file from file ID * - * @throws NoUserException + * @throws UserNotFoundException * @throws NotFoundException * @throws NotPermittedException */ @@ -459,7 +459,7 @@ private function getShareFolder(string $shareToken): ?Folder { * @throws NotPermittedException * @throws InvalidPathException * @throws LockedException - * @throws NoUserException + * @throws UserNotFoundException */ public function cleanupAttachments(int $fileId): int { $textFile = $this->rootFolder->getFirstNodeById($fileId); @@ -526,7 +526,7 @@ public static function getAttachmentNamesFromContent(string $content, int $fileI /** * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException * @throws NotFoundException * @throws NotPermittedException * @throws LockedException @@ -625,7 +625,7 @@ public function moveAttachments(File $source, File $target): void { * @throws NotFoundException * @throws NotPermittedException * @throws InvalidPathException - * @throws NoUserException + * @throws UserNotFoundException */ public function deleteAttachments(File $source): void { // if there is an attachment dir for this file diff --git a/lib/Service/FileService.php b/lib/Service/FileService.php index 0803c1fcb05..c578e6e2721 100644 --- a/lib/Service/FileService.php +++ b/lib/Service/FileService.php @@ -21,6 +21,7 @@ use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager as ShareManager; use OCP\Share\IShare; +use OCP\User\Exceptions\UserNotFoundException; use Psr\Log\LoggerInterface; class FileService { @@ -57,8 +58,7 @@ public function getFileById(int $fileId, string $userId): File { try { $userFolder = $this->rootFolder->getUserFolder($userId); - } catch (\OC\User\NoUserException) { - // It is a bit hacky to depend on internal exceptions here. But it is the best we can do for now + } catch (UserNotFoundException) { throw new NotFoundException(); } diff --git a/tests/stub.php b/tests/stub.php index 0ca6b52e163..c694ab91430 100644 --- a/tests/stub.php +++ b/tests/stub.php @@ -41,11 +41,6 @@ abstract public function getShare(): \OCP\Share\IShare; } } -namespace OC\User { - class NoUserException extends \Exception { - } -} - namespace OCA\Files_Sharing { abstract class SharedStorage implements \OCP\Files\Storage\IStorage { abstract public function getShare(): \OCP\Share\IShare; diff --git a/tests/unit/DAV/WorkspacePluginTest.php b/tests/unit/DAV/WorkspacePluginTest.php index f5fea2def8d..06f293d6c6f 100644 --- a/tests/unit/DAV/WorkspacePluginTest.php +++ b/tests/unit/DAV/WorkspacePluginTest.php @@ -9,13 +9,12 @@ namespace OCA\Text\Tests\DAV; -use OC\Files\Node\File; use OCA\DAV\Connector\Sabre\Directory; use OCA\Text\DAV\WorkspacePlugin; use OCA\Text\Service\ConfigService; use OCA\Text\Service\FileService; use OCA\Text\Service\WorkspaceService; -use OCP\Files\FileInfo; +use OCP\Files\File; use OCP\Files\Folder; use OCP\ICache; use OCP\ICacheFactory; @@ -31,11 +30,9 @@ class WorkspacePluginTest extends TestCase { private WorkspacePlugin $plugin; protected function setUp(): void { - $fileInfo = $this->createMock(FileInfo::class); - $fileInfo->method('getId')->willReturn(42); - $fileInfo->method('getEtag')->willReturn('etag'); $file = $this->createMock(File::class); - $file->method('getFileInfo')->willReturn($fileInfo); + $file->method('getId')->willReturn(42); + $file->method('getEtag')->willReturn('etag'); $file->method('getContent')->willReturn('## Readme'); $workspaceService = $this->createMock(WorkspaceService::class);