Skip to content

Commit

Permalink
api adjustments (#36)
Browse files Browse the repository at this point in the history
Resolves: #35.

Return consistent ExApps list structure.

---------

Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
Co-authored-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
andrey18106 and bigcat88 authored Aug 9, 2023
1 parent fbe08c7 commit 859989b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
11 changes: 8 additions & 3 deletions lib/Controller/ExAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
4 changes: 0 additions & 4 deletions lib/Controller/OCSApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +33,6 @@ class OCSApiController extends OCSController {
public function __construct(
IRequest $request,
?string $userId,
IL10N $l,
AppEcosystemV2Service $service,
ExFilesActionsMenuService $exFilesActionsMenuService,
LoggerInterface $logger,
Expand All @@ -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;
Expand Down
33 changes: 17 additions & 16 deletions lib/Service/AppEcosystemV2Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
1 change: 1 addition & 0 deletions lib/Service/ExAppApiScopeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 859989b

Please sign in to comment.