Skip to content

Commit

Permalink
start implementing ocs endpoint to get task list from user+appId+iden…
Browse files Browse the repository at this point in the history
…tifier

Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
  • Loading branch information
julien-nc committed Aug 2, 2023
1 parent 114cad3 commit b4baed2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
32 changes: 32 additions & 0 deletions core/Controller/TextProcessingApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,36 @@ public function getTask(int $id): DataResponse {
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}

/**
* This endpoint returns a list of tasks related with a specific appId and identifier
*
* @PublicPage
* @UserRateThrottle(limit=20, period=120)
*
* @param string $appId
* @param string|null $identifier
* @return DataResponse<Http::STATUS_OK, array{tasks: array{CoreTextProcessingTask}}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>

Check failure on line 169 in core/Controller/TextProcessingApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidReturnType

core/Controller/TextProcessingApiController.php:169:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{message?: string, tasks?: list{array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: string, userId: null|string}}}, array<never, never>>' for OC\Core\Controller\TextProcessingApiController::listTasksByApp is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|500, array{message?: string, tasks?: array<array-key, array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: class-string<T:OCP\TextProcessing\Task as OCP\TextProcessing\ITaskType>, userId: null|string}>}, array<never, never>>' (see https://psalm.dev/011)

Check failure

Code scanning / Psalm

InvalidReturnType Error

The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{message?: string, tasks?: list{array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: string, userId: null|string}}}, array<never, never>>' for OC\Core\Controller\TextProcessingApiController::listTasksByApp is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|500, array{message?: string, tasks?: array<array-key, array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: class-string<T:OCP\TextProcessing\Task as OCP\TextProcessing\ITaskType>, userId: null|string}>}, array<never, never>>'
*
* 200: Task list returned
*/
public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse {
if ($this->userId === null) {
return new DataResponse([

Check failure on line 175 in core/Controller/TextProcessingApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidReturnStatement

core/Controller/TextProcessingApiController.php:175:11: InvalidReturnStatement: The inferred type 'OCP\AppFramework\Http\DataResponse<200, array{tasks: array<never, never>}, array<never, never>>' does not match the declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{message?: string, tasks?: list{array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: string, userId: null|string}}}, array<never, never>>' for OC\Core\Controller\TextProcessingApiController::listTasksByApp (see https://psalm.dev/128)
'tasks' => [],
]);

Check failure

Code scanning / Psalm

InvalidReturnStatement Error

The inferred type 'OCP\AppFramework\Http\DataResponse<200, array{tasks: array<never, never>}, array<never, never>>' does not match the declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{message?: string, tasks?: list{array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: string, userId: null|string}}}, array<never, never>>' for OC\Core\Controller\TextProcessingApiController::listTasksByApp
}
try {
$tasks = $this->languageModelManager->getTasksByApp($this->userId, $appId, $identifier);
$json = array_map(static function (Task $task) {
return $task->jsonSerialize();
}, $tasks);

return new DataResponse([

Check failure on line 185 in core/Controller/TextProcessingApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidReturnStatement

core/Controller/TextProcessingApiController.php:185:11: InvalidReturnStatement: The inferred type 'OCP\AppFramework\Http\DataResponse<200, array{tasks: array<array-key, array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: class-string<T:OCP\TextProcessing\Task as OCP\TextProcessing\ITaskType>, userId: null|string}>}, array<never, never>>' does not match the declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{message?: string, tasks?: list{array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: string, userId: null|string}}}, array<never, never>>' for OC\Core\Controller\TextProcessingApiController::listTasksByApp (see https://psalm.dev/128)
'tasks' => $json,
]);

Check failure

Code scanning / Psalm

InvalidReturnStatement Error

The inferred type 'OCP\AppFramework\Http\DataResponse<200, array{tasks: array<array-key, array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: class-string<T:OCP\TextProcessing\Task as OCP\TextProcessing\ITaskType>, userId: null|string}>}, array<never, never>>' does not match the declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{message?: string, tasks?: list{array{appId: string, id: int|null, identifier: string, input: string, output: null|string, status: 0|1|2|3|4, type: string, userId: null|string}}}, array<never, never>>' for OC\Core\Controller\TextProcessingApiController::listTasksByApp
} catch (\RuntimeException $e) {
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
}
1 change: 1 addition & 0 deletions core/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
['root' => '/textprocessing', 'name' => 'TextProcessingApi#taskTypes', 'url' => '/tasktypes', 'verb' => 'GET'],
['root' => '/textprocessing', 'name' => 'TextProcessingApi#schedule', 'url' => '/schedule', 'verb' => 'POST'],
['root' => '/textprocessing', 'name' => 'TextProcessingApi#getTask', 'url' => '/task/{id}', 'verb' => 'GET'],
['root' => '/textprocessing', 'name' => 'TextProcessingApi#listTasksByApp', 'url' => '/tasks/app/{appId}', 'verb' => 'GET'],
],
]);

Expand Down
19 changes: 19 additions & 0 deletions lib/private/TextProcessing/Db/TaskMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ public function find(int $id): Task {
return $this->findEntity($qb);
}

/**
* @param string $userId
* @param string $appId
* @param string|null $identifier
* @return array
* @throws Exception
*/
public function findByApp(string $userId, string $appId, ?string $identifier = null): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Task::$columns)
->from($this->tableName)
->where($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)))
->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
if ($identifier !== null) {
$qb->andWhere($qb->expr()->eq('identifier', $qb->createPositionalParameter($identifier)));
}
return $this->findEntities($qb);
}

/**
* @param int $timeout
* @return int the number of deleted tasks
Expand Down
19 changes: 18 additions & 1 deletion lib/private/TextProcessing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,24 @@ public function getTask(int $id): OCPTask {
} catch (MultipleObjectsReturnedException $e) {
throw new RuntimeException('Could not uniquely identify task with given id', 0, $e);
} catch (Exception $e) {
throw new RuntimeException('Failure while trying to find task by id: '.$e->getMessage(), 0, $e);
throw new RuntimeException('Failure while trying to find task by id: ' . $e->getMessage(), 0, $e);
}
}

/**
* @param string $userId
* @param string $appId
* @param string|null $identifier
* @return array
*/
public function getTasksByApp(string $userId, string $appId, ?string $identifier = null): array {
try {
$taskEntities = $this->taskMapper->findByApp($userId, $appId, $identifier);
return array_map(static function (DbTask $taskEntity) {
return $taskEntity->toPublicTask();
}, $taskEntities);
} catch (Exception $e) {
throw new RuntimeException('Failure while trying to find tasks by appId and identifier: ' . $e->getMessage(), 0, $e);
}
}
}
8 changes: 8 additions & 0 deletions lib/public/TextProcessing/IManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ public function scheduleTask(Task $task) : void;
* @since 27.1.0
*/
public function getTask(int $id): Task;

/**
* @param string $userId
* @param string $appId
* @param string|null $identifier
* @return array
*/
public function getTasksByApp(string $userId, string $appId, ?string $identifier = null): array;

Check failure on line 90 in lib/public/TextProcessing/IManager.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/TextProcessing/IManager.php:90:2: InvalidDocblock: @SInCE is required for methods in OCP. (see https://psalm.dev/008)
}

0 comments on commit b4baed2

Please sign in to comment.