aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2023-08-07 21:50:14 +0200
committerGitHub <noreply@github.com>2023-08-07 21:50:14 +0200
commiteb2f3bcb64afce19de2c2893f92a71729209105a (patch)
treec947a00f13af7b9b1c15304a8c6fe4b6883fc984 /lib
parentb4a3f8088ab4ccb53c25ee316fe9b575b41b36f8 (diff)
parentf154fe7f8ea5e81f313192555e8c720b2207b9fb (diff)
downloadnextcloud-server-eb2f3bcb64afce19de2c2893f92a71729209105a.tar.gz
nextcloud-server-eb2f3bcb64afce19de2c2893f92a71729209105a.zip
Merge pull request #39680 from nextcloud/enh/tp-list-tasks-by-app-and-identifier
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/TextProcessing/Db/Task.php6
-rw-r--r--lib/private/TextProcessing/Db/TaskMapper.php40
-rw-r--r--lib/private/TextProcessing/Manager.php60
-rw-r--r--lib/public/TextProcessing/FreePromptTaskType.php2
-rw-r--r--lib/public/TextProcessing/IManager.php27
7 files changed, 132 insertions, 5 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 787ae893aa0..2e5c239b6ed 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -1153,6 +1153,7 @@ return array(
'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 e56e20a95b5..48c6701c7c6 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 8c2ddb74f1f..9c6f16d11ae 100644
--- a/lib/private/TextProcessing/Db/Task.php
+++ b/lib/private/TextProcessing/Db/Task.php
@@ -39,11 +39,11 @@ use OCP\TextProcessing\Task as OCPTask;
* @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 624efd042f7..62dabea544f 100644
--- a/lib/private/TextProcessing/Db/TaskMapper.php
+++ b/lib/private/TextProcessing/Db/TaskMapper.php
@@ -60,6 +60,46 @@ class TaskMapper extends QBMapper {
}
/**
+ * @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
+ * @param string|null $identifier
+ * @return array
+ * @throws Exception
+ */
+ public function findUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array {
+ $qb = $this->db->getQueryBuilder();
+ $qb->select(Task::$columns)
+ ->from($this->tableName)
+ ->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)));
+ }
+ return $this->findEntities($qb);
+ }
+
+ /**
* @param int $timeout
* @return int the number of deleted tasks
* @throws Exception
diff --git a/lib/private/TextProcessing/Manager.php b/lib/private/TextProcessing/Manager.php
index 05e046a0049..b9cb06c298e 100644
--- a/lib/private/TextProcessing/Manager.php
+++ b/lib/private/TextProcessing/Manager.php
@@ -28,6 +28,7 @@ namespace OC\TextProcessing;
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;
@@ -178,6 +179,19 @@ class Manager implements IManager {
}
/**
+ * @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
+ *
* @param int $id The id of the task
* @return OCPTask
* @throws RuntimeException If the query failed
@@ -192,7 +206,51 @@ 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);
+ }
+ }
+
+ /**
+ * 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 getUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array {
+ try {
+ $taskEntities = $this->taskMapper->findUserTasksByApp($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/FreePromptTaskType.php b/lib/public/TextProcessing/FreePromptTaskType.php
index a1e52e268a3..2cb8d6b7946 100644
--- a/lib/public/TextProcessing/FreePromptTaskType.php
+++ b/lib/public/TextProcessing/FreePromptTaskType.php
@@ -61,6 +61,6 @@ class FreePromptTaskType implements ITaskType {
* @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 50d012eca68..dec0baba4bb 100644
--- a/lib/public/TextProcessing/IManager.php
+++ b/lib/public/TextProcessing/IManager.php
@@ -73,6 +73,14 @@ interface IManager {
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
* @throws RuntimeException If the query failed
@@ -80,4 +88,23 @@ interface IManager {
* @since 27.1.0
*/
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 getUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array;
}