From 859989b54911898ad0780d108c5f146ad7ef7ab8 Mon Sep 17 00:00:00 2001 From: Andrey Borysenko Date: Wed, 9 Aug 2023 17:25:53 +0300 Subject: [PATCH] api adjustments (#36) Resolves: #35. Return consistent ExApps list structure. --------- Signed-off-by: Alexander Piskun Co-authored-by: Alexander Piskun --- appinfo/routes.php | 2 +- lib/Controller/ExAppController.php | 11 ++++++--- lib/Controller/OCSApiController.php | 4 ---- lib/Service/AppEcosystemV2Service.php | 33 ++++++++++++++------------- lib/Service/ExAppApiScopeService.php | 1 + 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 82422e36..5863970f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -42,7 +42,7 @@ ['name' => 'OCSApi#getExAppUsers', 'url' => '/api/v1/users', 'verb' => 'GET'], // ExApps - ['name' => 'exApp#getExApps', 'url' => '/api/v1/ex-app/all', 'verb' => 'GET'], + ['name' => 'exApp#getExApps', 'url' => '/api/v1/ex-app/{list}', 'verb' => 'GET'], // Ex Apps registration (for Admin GUI, not implemented yet) // ['name' => 'OCSApi#registerExternalApp', 'url' => '/api/v1/ex-app', 'verb' => 'POST'], diff --git a/lib/Controller/ExAppController.php b/lib/Controller/ExAppController.php index cc0a2d43..ce0da3f2 100644 --- a/lib/Controller/ExAppController.php +++ b/lib/Controller/ExAppController.php @@ -10,6 +10,7 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\Attribute\NoCSRFRequired; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\AppFramework\OCSController; use OCP\IRequest; @@ -28,12 +29,16 @@ public function __construct( /** * @NoCSRFRequired * - * @param bool $extended + * @param string $list * + * @throws OCSBadRequestException * @return DataResponse */ #[NoCSRFRequired] - public function getExApps(bool $extended = false): DataResponse { - return new DataResponse($this->service->getExAppsList($extended), Http::STATUS_OK); + public function getExApps(string $list = 'enabled'): DataResponse { + if (!in_array($list, ['all', 'enabled'])) { + throw new OCSBadRequestException(); + } + return new DataResponse($this->service->getExAppsList($list), Http::STATUS_OK); } } diff --git a/lib/Controller/OCSApiController.php b/lib/Controller/OCSApiController.php index 07a633b3..aff4b133 100644 --- a/lib/Controller/OCSApiController.php +++ b/lib/Controller/OCSApiController.php @@ -19,13 +19,11 @@ use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCSController; -use OCP\IL10N; use OCP\IRequest; use Psr\Log\LoggerInterface; class OCSApiController extends OCSController { private LoggerInterface $logger; - private IL10N $l; private AppEcosystemV2Service $service; private ExFilesActionsMenuService $exFilesActionsMenuService; private ExAppApiScopeService $exAppApiScopeService; @@ -35,7 +33,6 @@ class OCSApiController extends OCSController { public function __construct( IRequest $request, ?string $userId, - IL10N $l, AppEcosystemV2Service $service, ExFilesActionsMenuService $exFilesActionsMenuService, LoggerInterface $logger, @@ -45,7 +42,6 @@ public function __construct( $this->request = $request; $this->logger = $logger; - $this->l = $l; $this->service = $service; $this->exFilesActionsMenuService = $exFilesActionsMenuService; $this->userId = $userId; diff --git a/lib/Service/AppEcosystemV2Service.php b/lib/Service/AppEcosystemV2Service.php index 757ee191..6d7b1c28 100644 --- a/lib/Service/AppEcosystemV2Service.php +++ b/lib/Service/AppEcosystemV2Service.php @@ -605,25 +605,26 @@ public function updateExAppLastCheckTime(ExApp &$exApp): void { } } - public function getExAppsList(bool $extended = false): array { + public function getExAppsList(string $list = 'enabled'): array { try { $exApps = $this->exAppMapper->findAll(); - if ($extended) { - $exApps = array_map(function (ExApp $exApp) { - return [ - 'id' => $exApp->getAppid(), - 'name' => $exApp->getName(), - 'version' => $exApp->getVersion(), - 'enabled' => $exApp->getEnabled(), - 'last_check_time' => $exApp->getLastCheckTime(), - 'system' => $this->exAppUsersService->exAppUserExists($exApp->getAppid(), ''), - ]; - }, $exApps); - } else { - $exApps = array_map(function (ExApp $exApp) { - return $exApp->getAppid(); - }, $exApps); + + if ($list === 'enabled') { + $exApps = array_filter($exApps, function (ExApp $exApp) { + return $exApp->getEnabled() === 1; + }); } + + $exApps = array_map(function (ExApp $exApp) { + return [ + 'id' => $exApp->getAppid(), + 'name' => $exApp->getName(), + 'version' => $exApp->getVersion(), + 'enabled' => filter_var($exApp->getEnabled(), FILTER_VALIDATE_BOOLEAN), + 'last_check_time' => $exApp->getLastCheckTime(), + 'system' => $this->exAppUsersService->exAppUserExists($exApp->getAppid(), ''), + ]; + }, $exApps); } catch (Exception $e) { $this->logger->error(sprintf('Error while getting ExApps list. Error: %s', $e->getMessage()), ['exception' => $e]); $exApps = []; diff --git a/lib/Service/ExAppApiScopeService.php b/lib/Service/ExAppApiScopeService.php index 55143cdf..03abbc16 100644 --- a/lib/Service/ExAppApiScopeService.php +++ b/lib/Service/ExAppApiScopeService.php @@ -81,6 +81,7 @@ public function registerInitScopes(): bool { ['api_route' => $aeApiV1Prefix . '/ex-app/preference', 'scope_group' => 1, 'name' => 'BASIC'], ['api_route' => $aeApiV1Prefix . '/users', 'scope_group' => 2, 'name' => 'SYSTEM'], ['api_route' => $aeApiV1Prefix . '/ex-app/all', 'scope_group' => 2, 'name' => 'SYSTEM'], + ['api_route' => $aeApiV1Prefix . '/ex-app/enabled', 'scope_group' => 2, 'name' => 'SYSTEM'], ['api_route' => $aeApiV1Prefix . '/notification', 'scope_group' => 12, 'name' => 'NOTIFICATIONS'], // Cloud scopes