fix(OCS-API): Add endpoint to list user tasks

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
pull/45094/head
Marcel Klehr 3 weeks ago
parent f3a88f04ec
commit a8afa7f23d

@ -201,16 +201,46 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController {
* with a specific appId and optionally with an identifier
*
* @param string $appId ID of the app
* @param string|null $identifier An arbitrary identifier for the task
* @param string|null $customId An arbitrary identifier for the task
* @return DataResponse<Http::STATUS_OK, array{tasks: CoreTaskProcessingTask[]}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
*
* 200: Task list returned
*/
#[NoAdminRequired]
#[ApiRoute(verb: 'GET', url: '/tasks/app/{appId}', root: '/taskprocessing')]
public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse {
public function listTasksByApp(string $appId, ?string $customId = null): DataResponse {
try {
$tasks = $this->taskProcessingManager->getUserTasksByApp($this->userId, $appId, $identifier);
$tasks = $this->taskProcessingManager->getUserTasksByApp($this->userId, $appId, $customId);
/** @var CoreTaskProcessingTask[] $json */
$json = array_map(static function (Task $task) {
return $task->jsonSerialize();
}, $tasks);
return new DataResponse([
'tasks' => $json,
]);
} catch (Exception $e) {
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
} catch (\JsonException $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
*
* @param string|null $taskType The task type to filter by
* @param string|null $customId An arbitrary identifier for the task
* @return DataResponse<Http::STATUS_OK, array{tasks: CoreTaskProcessingTask[]}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
*
* 200: Task list returned
*/
#[NoAdminRequired]
#[ApiRoute(verb: 'GET', url: '/tasks', root: '/taskprocessing')]
public function listTasksByUser(?string $taskType, ?string $customId = null): DataResponse {
try {
$tasks = $this->taskProcessingManager->getUserTasks($this->userId, $taskType, $customId);
/** @var CoreTaskProcessingTask[] $json */
$json = array_map(static function (Task $task) {
return $task->jsonSerialize();

@ -3954,7 +3954,7 @@
],
"parameters": [
{
"name": "identifier",
"name": "customId",
"in": "query",
"description": "An arbitrary identifier for the task",
"schema": {
@ -4065,6 +4065,134 @@
}
}
},
"/ocs/v2.php/taskprocessing/tasks": {
"get": {
"operationId": "task_processing_api-list-tasks-by-user",
"summary": "This endpoint returns a list of tasks of a user that are related with a specific appId and optionally with an identifier",
"tags": [
"task_processing_api"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "taskType",
"in": "query",
"description": "The task type to filter by",
"schema": {
"type": "string",
"nullable": true
}
},
{
"name": "customId",
"in": "query",
"description": "An arbitrary identifier for the task",
"schema": {
"type": "string",
"nullable": true
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "Task list returned",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"tasks"
],
"properties": {
"tasks": {
"type": "array",
"items": {
"$ref": "#/components/schemas/TaskProcessingTask"
}
}
}
}
}
}
}
}
}
}
},
"500": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/taskprocessing/tasks/{taskId}/file/{fileId}": {
"get": {
"operationId": "task_processing_api-get-file-contents",

@ -100,6 +100,27 @@ class TaskMapper extends QBMapper {
return $this->findEntity($qb);
}
/**
* @param string|null $userId
* @param string|null $taskType
* @param string|null $customId
* @return list<Task>
* @throws Exception
*/
public function findByUserAndTaskType(?string $userId, ?string $taskType = null, ?string $customId = null): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Task::$columns)
->from($this->tableName)
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
if ($taskType !== null) {
$qb->andWhere($qb->expr()->eq('type', $qb->createPositionalParameter($taskType)));
}
if ($customId !== null) {
$qb->andWhere($qb->expr()->eq('custom_id', $qb->createPositionalParameter($customId)));
}
return array_values($this->findEntities($qb));
}
/**
* @param string $userId
* @param string $appId
@ -107,7 +128,7 @@ class TaskMapper extends QBMapper {
* @return list<Task>
* @throws Exception
*/
public function findUserTasksByApp(string $userId, string $appId, ?string $customId = null): array {
public function findUserTasksByApp(?string $userId, string $appId, ?string $customId = null): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Task::$columns)
->from($this->tableName)

@ -806,6 +806,17 @@ class Manager implements IManager {
}
}
public function getUserTasks(?string $userId, ?string $taskTypeId = null, ?string $customId = null): array {
try {
$taskEntities = $this->taskMapper->findUserTasksByApp($userId, $taskTypeId, $customId);
return array_map(fn ($taskEntity): Task => $taskEntity->toPublicTask(), $taskEntities);
} catch (\OCP\DB\Exception $e) {
throw new \OCP\TaskProcessing\Exception\Exception('There was a problem finding the tasks', 0, $e);
} catch (\JsonException $e) {
throw new \OCP\TaskProcessing\Exception\Exception('There was a problem parsing JSON after finding the tasks', 0, $e);
}
}
public function getUserTasksByApp(?string $userId, string $appId, ?string $customId = null): array {
try {
$taskEntities = $this->taskMapper->findUserTasksByApp($userId, $appId, $customId);

@ -135,6 +135,16 @@ interface IManager {
*/
public function getUserTask(int $id, ?string $userId): Task;
/**
* @param string|null $userId The user id that scheduled the task
* @param string|null $taskTypeId The task type id to filter by
* @return list<Task>
* @throws Exception If the query failed
* @throws NotFoundException If the task could not be found
* @since 30.0.0
*/
public function getUserTasks(?string $userId, ?string $taskTypeId = null, ?string $customId = null): array;
/**
* @param string|null $userId
* @param string $appId

Loading…
Cancel
Save