From 9986e02097e5c82e1bb21ba3f897b14d82a50c15 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Wed, 2 Aug 2023 16:47:41 +0200 Subject: [PATCH 1/6] 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; } From 41b19cf969956fe57fcb35e3dee0200d5c29b6d7 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Thu, 3 Aug 2023 23:11:29 +0200 Subject: [PATCH 2/6] allow anon text processing scheduling add a textprocessing_tasks index convert anotations to method attributes refactor TP manager add mapper methods Signed-off-by: Julien Veyssier --- .../TextProcessingApiController.php | 52 +++++++-------- .../Version28000Date20230803221055.php | 66 +++++++++++++++++++ lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/TextProcessing/Db/Task.php | 6 +- lib/private/TextProcessing/Db/TaskMapper.php | 23 ++++++- lib/private/TextProcessing/Manager.php | 33 +++++++++- .../TextProcessing/FreePromptTaskType.php | 2 +- lib/public/TextProcessing/IManager.php | 13 +++- 9 files changed, 159 insertions(+), 38 deletions(-) create mode 100644 core/Migrations/Version28000Date20230803221055.php diff --git a/core/Controller/TextProcessingApiController.php b/core/Controller/TextProcessingApiController.php index 21cf9d58c333b..c713a70481c07 100644 --- a/core/Controller/TextProcessingApiController.php +++ b/core/Controller/TextProcessingApiController.php @@ -29,6 +29,10 @@ use InvalidArgumentException; use OCA\Core\ResponseDefinitions; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\Attribute\AnonRateLimit; +use OCP\AppFramework\Http\Attribute\NoAdminRequired; +use OCP\AppFramework\Http\Attribute\PublicPage; +use OCP\AppFramework\Http\Attribute\UserRateLimit; use OCP\AppFramework\Http\DataResponse; use OCP\Common\Exception\NotFoundException; use OCP\IL10N; @@ -47,13 +51,13 @@ */ class TextProcessingApiController extends \OCP\AppFramework\OCSController { public function __construct( - string $appName, - IRequest $request, - private IManager $languageModelManager, - private IL10N $l, - private ?string $userId, + string $appName, + IRequest $request, + private IManager $textProcessingManager, + private IL10N $l, + private ?string $userId, private ContainerInterface $container, - private LoggerInterface $logger, + private LoggerInterface $logger, ) { parent::__construct($appName, $request); } @@ -61,12 +65,11 @@ public function __construct( /** * This endpoint returns all available LanguageModel task types * - * @PublicPage - * * @return DataResponse */ + #[PublicPage] public function taskTypes(): DataResponse { - $typeClasses = $this->languageModelManager->getAvailableTaskTypes(); + $typeClasses = $this->textProcessingManager->getAvailableTaskTypes(); $types = []; /** @var string $typeClass */ foreach ($typeClasses as $typeClass) { @@ -92,10 +95,6 @@ public function taskTypes(): DataResponse { /** * This endpoint allows scheduling a language model task * - * @PublicPage - * @UserRateThrottle(limit=20, period=120) - * @AnonRateThrottle(limit=5, period=120) - * * @param string $input Input text * @param string $type Type of the task * @param string $appId ID of the app that will execute the task @@ -107,6 +106,9 @@ public function taskTypes(): DataResponse { * 400: Scheduling task is not possible * 412: Scheduling task is not possible */ + #[PublicPage] + #[UserRateLimit(limit: 20, period: 120)] + #[AnonRateLimit(limit: 5, period: 120)] public function schedule(string $input, string $type, string $appId, string $identifier = ''): DataResponse { try { $task = new Task($type, $input, $appId, $this->userId, $identifier); @@ -114,7 +116,7 @@ public function schedule(string $input, string $type, string $appId, string $ide return new DataResponse(['message' => $this->l->t('Requested task type does not exist')], Http::STATUS_BAD_REQUEST); } try { - $this->languageModelManager->scheduleTask($task); + $this->textProcessingManager->scheduleTask($task); $json = $task->jsonSerialize(); @@ -130,7 +132,6 @@ public function schedule(string $input, string $type, string $appId, string $ide * This endpoint allows checking the status and results of a task. * Tasks are removed 1 week after receiving their last update. * - * @PublicPage * @param int $id The id of the task * * @return DataResponse|DataResponse @@ -138,13 +139,10 @@ public function schedule(string $input, string $type, string $appId, string $ide * 200: Task returned * 404: Task not found */ + #[PublicPage] public function getTask(int $id): DataResponse { try { - $task = $this->languageModelManager->getTask($id); - - if ($this->userId !== $task->getUserId()) { - return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND); - } + $task = $this->textProcessingManager->getUserTask($id, $this->userId); $json = $task->jsonSerialize(); @@ -159,10 +157,8 @@ public function getTask(int $id): DataResponse { } /** - * This endpoint returns a list of tasks related with a specific appId and identifier - * - * @PublicPage - * @UserRateThrottle(limit=20, period=120) + * This endpoint returns a list of tasks of a user that are related + * with a specific appId and optionally with an identifier * * @param string $appId * @param string|null $identifier @@ -170,14 +166,10 @@ public function getTask(int $id): DataResponse { * * 200: Task list returned */ + #[NoAdminRequired] 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); + $tasks = $this->textProcessingManager->getUserTasksByApp($this->userId, $appId, $identifier); $json = array_map(static function (Task $task) { return $task->jsonSerialize(); }, $tasks); diff --git a/core/Migrations/Version28000Date20230803221055.php b/core/Migrations/Version28000Date20230803221055.php new file mode 100644 index 0000000000000..8878a6f6cce9b --- /dev/null +++ b/core/Migrations/Version28000Date20230803221055.php @@ -0,0 +1,66 @@ + + * + * @author Julien Veyssier + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OC\Core\Migrations; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +/** + * Adjust textprocessing_tasks table + */ +class Version28000Date20230803221055 extends SimpleMigrationStep { + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + $changed = false; + + if ($schema->hasTable('textprocessing_tasks')) { + $table = $schema->getTable('textprocessing_tasks'); + + $column = $table->getColumn('user_id'); + $column->setNotnull(false); + + $table->addIndex(['user_id', 'app_id', 'identifier'], 'tp_tasks_uid_appid_ident'); + + $changed = true; + } + + if ($changed) { + return $schema; + } + + return null; + } +} diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 787ae893aa0ac..2e5c239b6ed1b 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1153,6 +1153,7 @@ 'OC\\Core\\Migrations\\Version27000Date20230309104802' => $baseDir . '/core/Migrations/Version27000Date20230309104802.php', 'OC\\Core\\Migrations\\Version28000Date20230616104802' => $baseDir . '/core/Migrations/Version28000Date20230616104802.php', 'OC\\Core\\Migrations\\Version28000Date20230728104802' => $baseDir . '/core/Migrations/Version28000Date20230728104802.php', + 'OC\\Core\\Migrations\\Version28000Date20230803221055' => $baseDir . '/core/Migrations/Version28000Date20230803221055.php', 'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php', 'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php', 'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index e56e20a95b50e..48c6701c7c605 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1186,6 +1186,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Core\\Migrations\\Version27000Date20230309104802' => __DIR__ . '/../../..' . '/core/Migrations/Version27000Date20230309104802.php', 'OC\\Core\\Migrations\\Version28000Date20230616104802' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20230616104802.php', 'OC\\Core\\Migrations\\Version28000Date20230728104802' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20230728104802.php', + 'OC\\Core\\Migrations\\Version28000Date20230803221055' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20230803221055.php', 'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php', 'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php', 'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php', diff --git a/lib/private/TextProcessing/Db/Task.php b/lib/private/TextProcessing/Db/Task.php index 8c2ddb74f1fb7..9c6f16d11ae3b 100644 --- a/lib/private/TextProcessing/Db/Task.php +++ b/lib/private/TextProcessing/Db/Task.php @@ -39,11 +39,11 @@ * @method string getOutput() * @method setStatus(int $type) * @method int getStatus() - * @method setUserId(string $type) - * @method string getuserId() + * @method setUserId(?string $userId) + * @method string|null getUserId() * @method setAppId(string $type) * @method string getAppId() - * @method setIdentifier(string $type) + * @method setIdentifier(string $identifier) * @method string getIdentifier() */ class Task extends Entity { diff --git a/lib/private/TextProcessing/Db/TaskMapper.php b/lib/private/TextProcessing/Db/TaskMapper.php index cf78006d6d9e8..6fde1e510b58e 100644 --- a/lib/private/TextProcessing/Db/TaskMapper.php +++ b/lib/private/TextProcessing/Db/TaskMapper.php @@ -59,6 +59,27 @@ public function find(int $id): Task { return $this->findEntity($qb); } + /** + * @param int $id + * @param string|null $userId + * @return Task + * @throws DoesNotExistException + * @throws Exception + * @throws MultipleObjectsReturnedException + */ + public function findByIdAndUser(int $id, ?string $userId): Task { + $qb = $this->db->getQueryBuilder(); + $qb->select(Task::$columns) + ->from($this->tableName) + ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id))); + if ($userId === null) { + $qb->andWhere($qb->expr()->isNull('user_id')); + } else { + $qb->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId))); + } + return $this->findEntity($qb); + } + /** * @param string $userId * @param string $appId @@ -66,7 +87,7 @@ public function find(int $id): Task { * @return array * @throws Exception */ - public function findByApp(string $userId, string $appId, ?string $identifier = null): array { + public function findUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array { $qb = $this->db->getQueryBuilder(); $qb->select(Task::$columns) ->from($this->tableName) diff --git a/lib/private/TextProcessing/Manager.php b/lib/private/TextProcessing/Manager.php index da38dc876a786..c8302f1e8df58 100644 --- a/lib/private/TextProcessing/Manager.php +++ b/lib/private/TextProcessing/Manager.php @@ -178,6 +178,8 @@ public function scheduleTask(OCPTask $task): void { } /** + * Get a task from its id + * * @param int $id The id of the task * @return OCPTask * @throws RuntimeException If the query failed @@ -197,14 +199,41 @@ public function getTask(int $id): OCPTask { } /** + * Get a task from its user id and task id + * If userId is null, this can only get a task that was scheduled anonymously + * + * @param int $id The id of the task + * @param string|null $userId The user id that scheduled the task + * @return OCPTask + * @throws RuntimeException If the query failed + * @throws NotFoundException If the task could not be found + */ + public function getUserTask(int $id, ?string $userId): OCPTask { + try { + $taskEntity = $this->taskMapper->findByIdAndUser($id, $userId); + return $taskEntity->toPublicTask(); + } catch (DoesNotExistException $e) { + throw new NotFoundException('Could not find task with the provided id and user id'); + } catch (MultipleObjectsReturnedException $e) { + throw new RuntimeException('Could not uniquely identify task with given id and user id', 0, $e); + } catch (Exception $e) { + throw new RuntimeException('Failure while trying to find task by id and user id: ' . $e->getMessage(), 0, $e); + } + } + + /** + * Get a list of tasks scheduled by a specific user for a specific app + * and optionally with a specific identifier. + * This cannot be used to get anonymously scheduled tasks + * * @param string $userId * @param string $appId * @param string|null $identifier * @return array */ - public function getTasksByApp(string $userId, string $appId, ?string $identifier = null): array { + public function getUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array { try { - $taskEntities = $this->taskMapper->findByApp($userId, $appId, $identifier); + $taskEntities = $this->taskMapper->findUserTasksByApp($userId, $appId, $identifier); return array_map(static function (DbTask $taskEntity) { return $taskEntity->toPublicTask(); }, $taskEntities); diff --git a/lib/public/TextProcessing/FreePromptTaskType.php b/lib/public/TextProcessing/FreePromptTaskType.php index a1e52e268a37c..2cb8d6b794647 100644 --- a/lib/public/TextProcessing/FreePromptTaskType.php +++ b/lib/public/TextProcessing/FreePromptTaskType.php @@ -61,6 +61,6 @@ public function getName(): string { * @since 27.1.0 */ public function getDescription(): string { - return $this->l->t('Runs an arbitrary prompt through the built-in language model.'); + return $this->l->t('Runs an arbitrary prompt through the language model.'); } } diff --git a/lib/public/TextProcessing/IManager.php b/lib/public/TextProcessing/IManager.php index c7b101ad51082..00646528e68ac 100644 --- a/lib/public/TextProcessing/IManager.php +++ b/lib/public/TextProcessing/IManager.php @@ -81,11 +81,22 @@ public function scheduleTask(Task $task) : void; */ public function getTask(int $id): Task; + /** + * @param int $id The id of the task + * @param string|null $userId The user id that scheduled the task + * @return Task + * @throws RuntimeException If the query failed + * @throws NotFoundException If the task could not be found + * @since 27.1.0 + */ + public function getUserTask(int $id, ?string $userId): Task; + /** * @param string $userId * @param string $appId * @param string|null $identifier * @return array + * @since 27.1.0 */ - public function getTasksByApp(string $userId, string $appId, ?string $identifier = null): array; + public function getUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array; } From 05a6a799a7e05f90c9d1eb10b5a41800457e1749 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Fri, 4 Aug 2023 10:43:54 +0200 Subject: [PATCH 3/6] change sql where order to match index order Signed-off-by: Julien Veyssier --- lib/private/TextProcessing/Db/TaskMapper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/private/TextProcessing/Db/TaskMapper.php b/lib/private/TextProcessing/Db/TaskMapper.php index 6fde1e510b58e..62dabea544f8f 100644 --- a/lib/private/TextProcessing/Db/TaskMapper.php +++ b/lib/private/TextProcessing/Db/TaskMapper.php @@ -91,8 +91,8 @@ public function findUserTasksByApp(string $userId, string $appId, ?string $ident $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))); + ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId))) + ->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId))); if ($identifier !== null) { $qb->andWhere($qb->expr()->eq('identifier', $qb->createPositionalParameter($identifier))); } From fca1c309a025e34cc5d635766796d9e5b4f9386b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 4 Aug 2023 15:41:35 +0200 Subject: [PATCH 4/6] feat: Add delete task API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../TextProcessingApiController.php | 30 +++++++++++++++++++ core/routes.php | 1 + lib/private/TextProcessing/Manager.php | 12 ++++++++ lib/public/TextProcessing/IManager.php | 8 +++++ 4 files changed, 51 insertions(+) diff --git a/core/Controller/TextProcessingApiController.php b/core/Controller/TextProcessingApiController.php index c713a70481c07..e1d7788be2701 100644 --- a/core/Controller/TextProcessingApiController.php +++ b/core/Controller/TextProcessingApiController.php @@ -156,6 +156,36 @@ public function getTask(int $id): DataResponse { } } + /** + * This endpoint allows to delete a scheduled task for a user + * + * @param int $id The id of the task + * + * @return DataResponse|DataResponse + * + * 200: Task returned + * 404: Task not found + */ + #[NoAdminRequired] + public function deleteTask(int $id): DataResponse { + try { + $task = $this->textProcessingManager->getUserTask($id, $this->userId); + + $this->textProcessingManager->deleteTask($task); + + $json = $task->jsonSerialize(); + + return new DataResponse([ + 'task' => $json, + ]); + } catch (NotFoundException $e) { + return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND); + } catch (\RuntimeException $e) { + return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } + + /** * This endpoint returns a list of tasks of a user that are related * with a specific appId and optionally with an identifier diff --git a/core/routes.php b/core/routes.php index 90578ee6f0d2f..ad8638e0b1e4e 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#deleteTask', 'url' => '/task/{id}', 'verb' => 'DELETE'], ['root' => '/textprocessing', 'name' => 'TextProcessingApi#listTasksByApp', 'url' => '/tasks/app/{appId}', 'verb' => 'GET'], ], ]); diff --git a/lib/private/TextProcessing/Manager.php b/lib/private/TextProcessing/Manager.php index c8302f1e8df58..b9cb06c298e25 100644 --- a/lib/private/TextProcessing/Manager.php +++ b/lib/private/TextProcessing/Manager.php @@ -28,6 +28,7 @@ use OC\AppFramework\Bootstrap\Coordinator; use OC\TextProcessing\Db\Task as DbTask; use OCP\IConfig; +use OCP\TextProcessing\Task; use OCP\TextProcessing\Task as OCPTask; use OC\TextProcessing\Db\TaskMapper; use OCP\AppFramework\Db\DoesNotExistException; @@ -177,6 +178,17 @@ public function scheduleTask(OCPTask $task): void { ]); } + /** + * @inheritDoc + */ + public function deleteTask(Task $task): void { + $taskEntity = DbTask::fromPublicTask($task); + $this->taskMapper->delete($taskEntity); + $this->jobList->remove(TaskBackgroundJob::class, [ + 'taskId' => $task->getId() + ]); + } + /** * Get a task from its id * diff --git a/lib/public/TextProcessing/IManager.php b/lib/public/TextProcessing/IManager.php index 00646528e68ac..dec0baba4bb74 100644 --- a/lib/public/TextProcessing/IManager.php +++ b/lib/public/TextProcessing/IManager.php @@ -72,6 +72,14 @@ public function runTask(Task $task): string; */ public function scheduleTask(Task $task) : void; + /** + * Delete a task that has been scheduled before + * + * @param Task $task The task to delete + * @since 27.1.0 + */ + public function deleteTask(Task $task): void; + /** * @param int $id The id of the task * @return Task From e9e7db997a689fa1a1794cc9f294b3b224c9d06f Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Mon, 7 Aug 2023 13:29:14 +0200 Subject: [PATCH 5/6] bump server version to 28.0.0.2 to trigger new migration step Signed-off-by: Julien Veyssier --- version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.php b/version.php index 386c676fa363b..012b016eb5bd4 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ // between betas, final and RCs. This is _not_ the public version number. Reset minor/patch level // when updating major/minor version number. -$OC_Version = [28, 0, 0, 1]; +$OC_Version = [28, 0, 0, 2]; // The human-readable string $OC_VersionString = '28.0.0 dev'; From f154fe7f8ea5e81f313192555e8c720b2207b9fb Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Mon, 7 Aug 2023 17:49:58 +0200 Subject: [PATCH 6/6] fix psalm issue Signed-off-by: Julien Veyssier --- core/Controller/TextProcessingApiController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/Controller/TextProcessingApiController.php b/core/Controller/TextProcessingApiController.php index e1d7788be2701..0035750b4b0f9 100644 --- a/core/Controller/TextProcessingApiController.php +++ b/core/Controller/TextProcessingApiController.php @@ -192,7 +192,7 @@ public function deleteTask(int $id): DataResponse { * * @param string $appId * @param string|null $identifier - * @return DataResponse|DataResponse + * @return DataResponse|DataResponse * * 200: Task list returned */ @@ -200,6 +200,7 @@ public function deleteTask(int $id): DataResponse { public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse { try { $tasks = $this->textProcessingManager->getUserTasksByApp($this->userId, $appId, $identifier); + /** @var CoreTextProcessingTask[] $json */ $json = array_map(static function (Task $task) { return $task->jsonSerialize(); }, $tasks);