diff options
author | Julien Veyssier <julien-nc@posteo.net> | 2023-08-03 23:11:29 +0200 |
---|---|---|
committer | Julien Veyssier <julien-nc@posteo.net> | 2023-08-07 13:27:53 +0200 |
commit | 41b19cf969956fe57fcb35e3dee0200d5c29b6d7 (patch) | |
tree | c04657d2585e75624db61e179cf390f1f0ee6c1f /lib/private/TextProcessing | |
parent | 9986e02097e5c82e1bb21ba3f897b14d82a50c15 (diff) | |
download | nextcloud-server-41b19cf969956fe57fcb35e3dee0200d5c29b6d7.tar.gz nextcloud-server-41b19cf969956fe57fcb35e3dee0200d5c29b6d7.zip |
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 <julien-nc@posteo.net>
Diffstat (limited to 'lib/private/TextProcessing')
-rw-r--r-- | lib/private/TextProcessing/Db/Task.php | 6 | ||||
-rw-r--r-- | lib/private/TextProcessing/Db/TaskMapper.php | 23 | ||||
-rw-r--r-- | lib/private/TextProcessing/Manager.php | 33 |
3 files changed, 56 insertions, 6 deletions
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 cf78006d6d9..6fde1e510b5 100644 --- a/lib/private/TextProcessing/Db/TaskMapper.php +++ b/lib/private/TextProcessing/Db/TaskMapper.php @@ -60,13 +60,34 @@ 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 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 da38dc876a7..c8302f1e8df 100644 --- a/lib/private/TextProcessing/Manager.php +++ b/lib/private/TextProcessing/Manager.php @@ -178,6 +178,8 @@ class Manager implements IManager { } /** + * 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 @@ class Manager implements IManager { } /** + * 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); |