Skip to content

Commit 9986e02

Browse files
committed
start implementing ocs endpoint to get task list from user+appId+identifier
Signed-off-by: Julien Veyssier <[email protected]>
1 parent b4a3f80 commit 9986e02

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

core/Controller/TextProcessingApiController.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,36 @@ public function getTask(int $id): DataResponse {
157157
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
158158
}
159159
}
160+
161+
/**
162+
* This endpoint returns a list of tasks related with a specific appId and identifier
163+
*
164+
* @PublicPage
165+
* @UserRateThrottle(limit=20, period=120)
166+
*
167+
* @param string $appId
168+
* @param string|null $identifier
169+
* @return DataResponse<Http::STATUS_OK, array{tasks: array{CoreTextProcessingTask}}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
170+
*
171+
* 200: Task list returned
172+
*/
173+
public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse {
174+
if ($this->userId === null) {
175+
return new DataResponse([
176+
'tasks' => [],
177+
]);
178+
}
179+
try {
180+
$tasks = $this->languageModelManager->getTasksByApp($this->userId, $appId, $identifier);
181+
$json = array_map(static function (Task $task) {
182+
return $task->jsonSerialize();
183+
}, $tasks);
184+
185+
return new DataResponse([
186+
'tasks' => $json,
187+
]);
188+
} catch (\RuntimeException $e) {
189+
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
190+
}
191+
}
160192
}

core/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
['root' => '/textprocessing', 'name' => 'TextProcessingApi#taskTypes', 'url' => '/tasktypes', 'verb' => 'GET'],
150150
['root' => '/textprocessing', 'name' => 'TextProcessingApi#schedule', 'url' => '/schedule', 'verb' => 'POST'],
151151
['root' => '/textprocessing', 'name' => 'TextProcessingApi#getTask', 'url' => '/task/{id}', 'verb' => 'GET'],
152+
['root' => '/textprocessing', 'name' => 'TextProcessingApi#listTasksByApp', 'url' => '/tasks/app/{appId}', 'verb' => 'GET'],
152153
],
153154
]);
154155

lib/private/TextProcessing/Db/TaskMapper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ public function find(int $id): Task {
5959
return $this->findEntity($qb);
6060
}
6161

62+
/**
63+
* @param string $userId
64+
* @param string $appId
65+
* @param string|null $identifier
66+
* @return array
67+
* @throws Exception
68+
*/
69+
public function findByApp(string $userId, string $appId, ?string $identifier = null): array {
70+
$qb = $this->db->getQueryBuilder();
71+
$qb->select(Task::$columns)
72+
->from($this->tableName)
73+
->where($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)))
74+
->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
75+
if ($identifier !== null) {
76+
$qb->andWhere($qb->expr()->eq('identifier', $qb->createPositionalParameter($identifier)));
77+
}
78+
return $this->findEntities($qb);
79+
}
80+
6281
/**
6382
* @param int $timeout
6483
* @return int the number of deleted tasks

lib/private/TextProcessing/Manager.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,24 @@ public function getTask(int $id): OCPTask {
192192
} catch (MultipleObjectsReturnedException $e) {
193193
throw new RuntimeException('Could not uniquely identify task with given id', 0, $e);
194194
} catch (Exception $e) {
195-
throw new RuntimeException('Failure while trying to find task by id: '.$e->getMessage(), 0, $e);
195+
throw new RuntimeException('Failure while trying to find task by id: ' . $e->getMessage(), 0, $e);
196+
}
197+
}
198+
199+
/**
200+
* @param string $userId
201+
* @param string $appId
202+
* @param string|null $identifier
203+
* @return array
204+
*/
205+
public function getTasksByApp(string $userId, string $appId, ?string $identifier = null): array {
206+
try {
207+
$taskEntities = $this->taskMapper->findByApp($userId, $appId, $identifier);
208+
return array_map(static function (DbTask $taskEntity) {
209+
return $taskEntity->toPublicTask();
210+
}, $taskEntities);
211+
} catch (Exception $e) {
212+
throw new RuntimeException('Failure while trying to find tasks by appId and identifier: ' . $e->getMessage(), 0, $e);
196213
}
197214
}
198215
}

lib/public/TextProcessing/IManager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,12 @@ public function scheduleTask(Task $task) : void;
8080
* @since 27.1.0
8181
*/
8282
public function getTask(int $id): Task;
83+
84+
/**
85+
* @param string $userId
86+
* @param string $appId
87+
* @param string|null $identifier
88+
* @return array
89+
*/
90+
public function getTasksByApp(string $userId, string $appId, ?string $identifier = null): array;
8391
}

0 commit comments

Comments
 (0)