From dabc5375d975f459376a2e49cd267b664e140c63 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Wed, 2 Aug 2023 16:47:41 +0200 Subject: [PATCH] start implementing ocs endpoint to get task list from user+appId+identifier Signed-off-by: Julien Veyssier --- .../TextProcessingApiController.php | 32 +++++++++++++++++++ core/routes.php | 1 + lib/private/TextProcessing/Db/TaskMapper.php | 19 +++++++++++ lib/private/TextProcessing/Manager.php | 19 ++++++++++- lib/public/TextProcessing/IManager.php | 8 +++++ 5 files changed, 78 insertions(+), 1 deletion(-) diff --git a/core/Controller/TextProcessingApiController.php b/core/Controller/TextProcessingApiController.php index 122595bb1517e..21cf9d58c333b 100644 --- a/core/Controller/TextProcessingApiController.php +++ b/core/Controller/TextProcessingApiController.php @@ -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|DataResponse + * + * 200: Task list returned + */ + public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse { + if ($this->userId === null) { + return new DataResponse([ + 'tasks' => [], + ]); + } + try { + $tasks = $this->languageModelManager->getTasksByApp($this->userId, $appId, $identifier); + $json = array_map(static function (Task $task) { + return $task->jsonSerialize(); + }, $tasks); + + return new DataResponse([ + 'tasks' => $json, + ]); + } catch (\RuntimeException $e) { + return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } } diff --git a/core/routes.php b/core/routes.php index 4790f32af3230..90578ee6f0d2f 100644 --- a/core/routes.php +++ b/core/routes.php @@ -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'], ], ]); diff --git a/lib/private/TextProcessing/Db/TaskMapper.php b/lib/private/TextProcessing/Db/TaskMapper.php index 624efd042f74b..cf78006d6d9e8 100644 --- a/lib/private/TextProcessing/Db/TaskMapper.php +++ b/lib/private/TextProcessing/Db/TaskMapper.php @@ -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 diff --git a/lib/private/TextProcessing/Manager.php b/lib/private/TextProcessing/Manager.php index 05e046a004978..da38dc876a786 100644 --- a/lib/private/TextProcessing/Manager.php +++ b/lib/private/TextProcessing/Manager.php @@ -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); } } } diff --git a/lib/public/TextProcessing/IManager.php b/lib/public/TextProcessing/IManager.php index 50d012eca6862..c7b101ad51082 100644 --- a/lib/public/TextProcessing/IManager.php +++ b/lib/public/TextProcessing/IManager.php @@ -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; }