]> source.dussan.org Git - nextcloud-server.git/commitdiff
start implementing ocs endpoint to get task list from user+appId+identifier
authorJulien Veyssier <julien-nc@posteo.net>
Wed, 2 Aug 2023 14:47:41 +0000 (16:47 +0200)
committerJulien Veyssier <julien-nc@posteo.net>
Wed, 9 Aug 2023 12:58:02 +0000 (14:58 +0200)
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
(cherry picked from commit 9986e02097e5c82e1bb21ba3f897b14d82a50c15)
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
core/Controller/TextProcessingApiController.php
core/routes.php
lib/private/TextProcessing/Db/TaskMapper.php
lib/private/TextProcessing/Manager.php
lib/public/TextProcessing/IManager.php

index 9ed332644e14548b43ccad8c9a885ff7e640b452..0f4c8c40b293020177dedd5648d8d2aaf757a425 100644 (file)
@@ -134,4 +134,36 @@ class TextProcessingApiController extends \OCP\AppFramework\OCSController {
                        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{}>
+        *
+        *  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);
+               }
+       }
 }
index 4790f32af32306bfc02291259be2d00ec4fe04a0..90578ee6f0d2fe0967fe8e3e7437ff306cc5e6c0 100644 (file)
@@ -149,6 +149,7 @@ $application->registerRoutes($this, [
                ['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'],
        ],
 ]);
 
index 624efd042f74bd6a2e931cbb3beb596f5c4fc518..cf78006d6d9e85f7214ebecc79f015b87fd64e27 100644 (file)
@@ -59,6 +59,25 @@ class TaskMapper extends QBMapper {
                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
index 05e046a004978080d10a6710b6974bc9bd3fe916..da38dc876a786853b1533a4d5b43ea7e144ae3c4 100644 (file)
@@ -192,7 +192,24 @@ class Manager implements IManager {
                } 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);
                }
        }
 }
index 50d012eca6862c0108fc0ea1a6f58d1e422fecee..c7b101ad51082b71157ed345475637b24a6024b8 100644 (file)
@@ -80,4 +80,12 @@ interface IManager {
         * @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;
 }