Skip to content

Commit

Permalink
chore(files_sharing): refactor ShareAPIController shared methods
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Sep 20, 2024
1 parent 92f0cfb commit 1cbec70
Show file tree
Hide file tree
Showing 9 changed files with 790 additions and 1,055 deletions.
1 change: 1 addition & 0 deletions apps/files_sharing/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'OCA\\Files_Sharing\\Controller\\RemoteController' => $baseDir . '/../lib/Controller/RemoteController.php',
'OCA\\Files_Sharing\\Controller\\SettingsController' => $baseDir . '/../lib/Controller/SettingsController.php',
'OCA\\Files_Sharing\\Controller\\ShareAPIController' => $baseDir . '/../lib/Controller/ShareAPIController.php',
'OCA\\Files_Sharing\\Controller\\ShareApiControllerFactory' => $baseDir . '/../lib/Controller/ShareApiControllerFactory.php',
'OCA\\Files_Sharing\\Controller\\ShareController' => $baseDir . '/../lib/Controller/ShareController.php',
'OCA\\Files_Sharing\\Controller\\ShareInfoController' => $baseDir . '/../lib/Controller/ShareInfoController.php',
'OCA\\Files_Sharing\\Controller\\ShareesAPIController' => $baseDir . '/../lib/Controller/ShareesAPIController.php',
Expand Down
1 change: 1 addition & 0 deletions apps/files_sharing/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ComposerStaticInitFiles_Sharing
'OCA\\Files_Sharing\\Controller\\RemoteController' => __DIR__ . '/..' . '/../lib/Controller/RemoteController.php',
'OCA\\Files_Sharing\\Controller\\SettingsController' => __DIR__ . '/..' . '/../lib/Controller/SettingsController.php',
'OCA\\Files_Sharing\\Controller\\ShareAPIController' => __DIR__ . '/..' . '/../lib/Controller/ShareAPIController.php',
'OCA\\Files_Sharing\\Controller\\ShareApiControllerFactory' => __DIR__ . '/..' . '/../lib/Controller/ShareApiControllerFactory.php',
'OCA\\Files_Sharing\\Controller\\ShareController' => __DIR__ . '/..' . '/../lib/Controller/ShareController.php',
'OCA\\Files_Sharing\\Controller\\ShareInfoController' => __DIR__ . '/..' . '/../lib/Controller/ShareInfoController.php',
'OCA\\Files_Sharing\\Controller\\ShareesAPIController' => __DIR__ . '/..' . '/../lib/Controller/ShareesAPIController.php',
Expand Down
223 changes: 41 additions & 182 deletions apps/files_sharing/lib/Controller/DeletedShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,160 +8,70 @@
*/
namespace OCA\Files_Sharing\Controller;

use OCA\Files_Sharing\ResponseDefinitions;
use OCP\App\IAppManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\AppFramework\QueryException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IDateTimeZone;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IPreview;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IShare;
use OCP\UserStatus\IManager as UserStatusManager;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

/**
* @psalm-import-type Files_SharingDeletedShare from ResponseDefinitions
* @psalm-import-type Files_SharingShare from ResponseDefinitions

Check failure

Code scanning / Psalm

UndefinedDocblockClass Error

Docblock-defined class, interface or enum named OCA\Files_Sharing\Controller\ResponseDefinitions does not exist

Check failure on line 34 in apps/files_sharing/lib/Controller/DeletedShareAPIController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedDocblockClass

apps/files_sharing/lib/Controller/DeletedShareAPIController.php:34:23: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCA\Files_Sharing\Controller\ResponseDefinitions does not exist (see https://psalm.dev/200)
*/
class DeletedShareAPIController extends OCSController {

/** @var ShareManager */
private $shareManager;

/** @var string */
private $userId;

/** @var IUserManager */
private $userManager;

/** @var IGroupManager */
private $groupManager;

/** @var IRootFolder */
private $rootFolder;

/** @var IAppManager */
private $appManager;

/** @var IServerContainer */
private $serverContainer;

public function __construct(string $appName,
class DeletedShareAPIController extends ShareApiControllerFactory {

public function __construct(
IRequest $request,
ShareManager $shareManager,
string $UserId,
IUserManager $userManager,
IGroupManager $groupManager,
IRootFolder $rootFolder,
IAppManager $appManager,
IServerContainer $serverContainer) {
parent::__construct($appName, $request);

$this->shareManager = $shareManager;
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->rootFolder = $rootFolder;
$this->appManager = $appManager;
$this->serverContainer = $serverContainer;
}

/**
* @suppress PhanUndeclaredClassMethod
*
* @return Files_SharingDeletedShare
*/
private function formatShare(IShare $share): array {
$result = [
'id' => $share->getFullId(),
'share_type' => $share->getShareType(),
'uid_owner' => $share->getSharedBy(),
'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(),
'permissions' => 0,
'stime' => $share->getShareTime()->getTimestamp(),
'parent' => null,
'expiration' => null,
'token' => null,
'uid_file_owner' => $share->getShareOwner(),
'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(),
'path' => $share->getTarget(),
];
$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
$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();
}
}

$result['path'] = $userFolder->getRelativePath($node->getPath());
if ($node instanceof \OCP\Files\Folder) {
$result['item_type'] = 'folder';
} else {
$result['item_type'] = 'file';
}
$result['mimetype'] = $node->getMimetype();
$result['storage_id'] = $node->getStorage()->getId();
$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
$result['item_source'] = $node->getId();
$result['file_source'] = $node->getId();
$result['file_parent'] = $node->getParent()->getId();
$result['file_target'] = $share->getTarget();
$result['item_size'] = $node->getSize();
$result['item_mtime'] = $node->getMTime();

$expiration = $share->getExpirationDate();
if ($expiration !== null) {
$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
}

if ($share->getShareType() === IShare::TYPE_GROUP) {
$group = $this->groupManager->get($share->getSharedWith());
$result['share_with'] = $share->getSharedWith();
$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
} elseif ($share->getShareType() === IShare::TYPE_ROOM) {
$result['share_with'] = $share->getSharedWith();
$result['share_with_displayname'] = '';

try {
$result = array_merge($result, $this->getRoomShareHelper()->formatShare($share));
} catch (QueryException $e) {
}
} elseif ($share->getShareType() === IShare::TYPE_DECK) {
$result['share_with'] = $share->getSharedWith();
$result['share_with_displayname'] = '';

try {
$result = array_merge($result, $this->getDeckShareHelper()->formatShare($share));
} catch (QueryException $e) {
}
} elseif ($share->getShareType() === IShare::TYPE_SCIENCEMESH) {
$result['share_with'] = $share->getSharedWith();
$result['share_with_displayname'] = '';

try {
$result = array_merge($result, $this->getSciencemeshShareHelper()->formatShare($share));
} catch (QueryException $e) {
}
}

return $result;
protected ShareManager $shareManager,
protected ?string $userId,
protected IUserManager $userManager,
protected IGroupManager $groupManager,
protected IRootFolder $rootFolder,
protected IAppManager $appManager,
protected ContainerInterface $serverContainer,
protected UserStatusManager $userStatusManager,
protected IPreview $previewManager,
protected IDateTimeZone $dateTimeZone,
protected IURLGenerator $urlGenerator,
protected IL10N $l,
protected LoggerInterface $logger,
) {
parent::__construct(
$request, $shareManager,
$userId,
$userManager,
$groupManager,
$rootFolder,
$appManager,
$serverContainer,
$userStatusManager,
$previewManager,
$dateTimeZone,
$urlGenerator,
$l,
$logger,
);
}

/**
* Get a list of all deleted shares
*
* @return DataResponse<Http::STATUS_OK, Files_SharingDeletedShare[], array{}>
* @return DataResponse<Http::STATUS_OK, Files_SharingShare[], array{}>

Check failure

Code scanning / Psalm

InvalidReturnType Error

The declared return type 'OCP\AppFramework\Http\DataResponse<200, array<array-key, type-alias(OCA\Files_Sharing\Controller\ResponseDefinitions::Files_SharingShare)>, array<never, never>>' for OCA\Files_Sharing\Controller\DeletedShareAPIController::index is incorrect, got 'OCP\AppFramework\Http\DataResponse<200, array<array-key, array{'is-mount-root': bool, 'mount-type': string, attributes: null|string, can_delete: bool, can_edit: bool, displayname_file_owner: string, displayname_owner: string, expiration: null|string, file_parent: int, file_source: int, file_target: string, has_preview: bool, hide_download: 0|1, id: string, item_mtime: int, item_permissions?: int, item_size: float|int, item_source: int, item_type: 'file'|'folder', label: null|string, mail_send: 0|1, mimetype: string, note: string, parent: null, password?: null|string, password_expiration_time?: null|string, path: null|string, permissions: int, send_password_by_talk?: bool, share_type: int, share_with?: null|string, share_with_avatar?: string, share_with_displayname?: string, share_with_displayname_unique?: null|string, share_with_link?: string, status?: array{clearAt: int|null, icon: null|string, message: null|string, status: string}, stime: int, storage: int, storage_id: string, token: null|string, uid_file_owner: string, uid_owner: string, url?: string}>, array<never, never>>'

Check failure on line 74 in apps/files_sharing/lib/Controller/DeletedShareAPIController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidReturnType

apps/files_sharing/lib/Controller/DeletedShareAPIController.php:74:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200, array<array-key, type-alias(OCA\Files_Sharing\Controller\ResponseDefinitions::Files_SharingShare)>, array<never, never>>' for OCA\Files_Sharing\Controller\DeletedShareAPIController::index is incorrect, got 'OCP\AppFramework\Http\DataResponse<200, array<array-key, array{'is-mount-root': bool, 'mount-type': string, attributes: null|string, can_delete: bool, can_edit: bool, displayname_file_owner: string, displayname_owner: string, expiration: null|string, file_parent: int, file_source: int, file_target: string, has_preview: bool, hide_download: 0|1, id: string, item_mtime: int, item_permissions?: int, item_size: float|int, item_source: int, item_type: 'file'|'folder', label: null|string, mail_send: 0|1, mimetype: string, note: string, parent: null, password?: null|string, password_expiration_time?: null|string, path: null|string, permissions: int, send_password_by_talk?: bool, share_type: int, share_with?: null|string, share_with_avatar?: string, share_with_displayname?: string, share_with_displayname_unique?: null|string, share_with_link?: string, status?: array{clearAt: int|null, icon: null|string, message: null|string, status: string}, stime: int, storage: int, storage_id: string, token: null|string, uid_file_owner: string, uid_owner: string, url?: string}>, array<never, never>>' (see https://psalm.dev/011)
*
* 200: Deleted shares returned
*/
Expand Down Expand Up @@ -211,55 +121,4 @@ public function undelete(string $id): DataResponse {

return new DataResponse([]);
}

/**
* Returns the helper of DeletedShareAPIController for room shares.
*
* If the Talk application is not enabled or the helper is not available
* a QueryException is thrown instead.
*
* @return \OCA\Talk\Share\Helper\DeletedShareAPIController
* @throws QueryException
*/
private function getRoomShareHelper() {
if (!$this->appManager->isEnabledForUser('spreed')) {
throw new QueryException();
}

return $this->serverContainer->get('\OCA\Talk\Share\Helper\DeletedShareAPIController');
}

/**
* Returns the helper of DeletedShareAPIHelper for deck shares.
*
* If the Deck application is not enabled or the helper is not available
* a QueryException is thrown instead.
*
* @return \OCA\Deck\Sharing\ShareAPIHelper
* @throws QueryException
*/
private function getDeckShareHelper() {
if (!$this->appManager->isEnabledForUser('deck')) {
throw new QueryException();
}

return $this->serverContainer->get('\OCA\Deck\Sharing\ShareAPIHelper');
}

/**
* Returns the helper of DeletedShareAPIHelper for sciencemesh shares.
*
* If the sciencemesh application is not enabled or the helper is not available
* a QueryException is thrown instead.
*
* @return \OCA\Deck\Sharing\ShareAPIHelper
* @throws QueryException
*/
private function getSciencemeshShareHelper() {
if (!$this->appManager->isEnabledForUser('sciencemesh')) {
throw new QueryException();
}

return $this->serverContainer->get('\OCA\ScienceMesh\Sharing\ShareAPIHelper');
}
}
Loading

0 comments on commit 1cbec70

Please sign in to comment.