Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
['name' => 'assistantApi#getAvailableTaskTypes', 'url' => '/api/{apiVersion}/task-types', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'assistantApi#getUserTasks', 'url' => '/api/{apiVersion}/tasks', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'assistantApi#parseTextFromFile', 'url' => '/api/{apiVersion}/parse-file', 'verb' => 'POST', 'requirements' => $requirements],
['name' => 'assistantApi#getNotifyWhenReady', 'url' => '/api/{apiVersion}/task/{ocpTaskId}/notify', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'assistantApi#notifyWhenReady', 'url' => '/api/{apiVersion}/task/{ocpTaskId}/notify', 'verb' => 'POST', 'requirements' => $requirements],
['name' => 'assistantApi#cancelNotifyWhenReady', 'url' => '/api/{apiVersion}/task/{ocpTaskId}/notify', 'verb' => 'DELETE', 'requirements' => $requirements],
['name' => 'assistantApi#uploadInputFile', 'url' => '/api/{apiVersion}/input-file', 'verb' => 'POST', 'requirements' => $requirements],
['name' => 'assistantApi#displayUserFile', 'url' => '/api/{apiVersion}/file/{fileId}/display', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'assistantApi#getUserFileInfo', 'url' => '/api/{apiVersion}/file/{fileId}/info', 'verb' => 'GET', 'requirements' => $requirements],
Expand Down
56 changes: 56 additions & 0 deletions lib/Controller/AssistantApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ public function __construct(
parent::__construct($appName, $request);
}

/**
* Get the notification request for a task when it has finished
*
* Does not need bruteforce protection since we respond with success anyways
* as we don't want to keep the front-end waiting.
* However, we still use rate limiting to prevent timing attacks.
*
* @param int $ocpTaskId ID of the target task
* @return DataResponse<Http::STATUS_OK, array{id: int, ocp_task_id: int, timestamp: int}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{error: string}, array{}>
* @throws MultipleObjectsReturnedException
*
* 200: Task notification request retrieved successfully
*/
#[NoAdminRequired]
#[NoCSRFRequired]
#[AnonRateLimit(limit: 10, period: 60)]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['assistant_api'])]
public function getNotifyWhenReady(int $ocpTaskId): DataResponse {
if ($this->userId === null) {
return new DataResponse(['error' => $this->l10n->t('Failed to notify when ready; unknown user')], Http::STATUS_INTERNAL_SERVER_ERROR);
}

$notification = $this->assistantService->getNotifyWhenReady($ocpTaskId, $this->userId);
return new DataResponse($notification, Http::STATUS_OK);
}

/**
* Notify when the task has finished
*
Expand Down Expand Up @@ -77,6 +103,36 @@ public function notifyWhenReady(int $ocpTaskId): DataResponse {
return new DataResponse('', Http::STATUS_OK);
}

/**
* Cancel an existing notification when a task has finished
*
* Does not need bruteforce protection since we respond with success anyways
* as we don't want to keep the front-end waiting.
* However, we still use rate limiting to prevent timing attacks.
*
* @param int $ocpTaskId ID of the target task
* @return DataResponse<Http::STATUS_OK, '', array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{error: string}, array{}>
* @throws MultipleObjectsReturnedException
*
* 200: Ready notification deleted successfully
*/
#[NoAdminRequired]
#[NoCSRFRequired]
#[AnonRateLimit(limit: 10, period: 60)]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['assistant_api'])]
public function cancelNotifyWhenReady(int $ocpTaskId): DataResponse {
if ($this->userId === null) {
return new DataResponse(['error' => $this->l10n->t('Failed to cancel notification; unknown user')], Http::STATUS_INTERNAL_SERVER_ERROR);
}

try {
$this->assistantService->cancelNotifyWhenReady($ocpTaskId, $this->userId);
} catch (Exception $e) {
// Ignore
}
return new DataResponse('', Http::STATUS_OK);
}

/**
* Get available task types
*
Expand Down
57 changes: 57 additions & 0 deletions lib/Service/AssistantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Html2Text\Html2Text;
use OC\User\NoUserException;
use OCA\Assistant\AppInfo\Application;
use OCA\Assistant\Db\TaskNotification;
use OCA\Assistant\Db\TaskNotificationMapper;
use OCA\Assistant\ResponseDefinitions;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
Expand Down Expand Up @@ -123,6 +124,34 @@ public function __construct(
];
}

/**
* Get notification request for a task
*
* @param int $taskId
* @param string $userId
* @throws Exception
* @throws MultipleObjectsReturnedException
*/
public function getNotifyWhenReady(int $taskId, string $userId): array {
try {
$task = $this->taskProcessingManager->getTask($taskId);
} catch (NotFoundException $e) {
// task may already be deleted, return an empty notification
return (new TaskNotification())->jsonSerialize();
} catch (TaskProcessingException $e) {
$this->logger->debug('Task request error : ' . $e->getMessage());
throw new Exception('Internal server error.', Http::STATUS_INTERNAL_SERVER_ERROR);
}

if ($task->getUserId() !== $userId) {
$this->logger->info('A user attempted viewing notifications of another user\'s task');
throw new Exception('Unauthorized', Http::STATUS_UNAUTHORIZED);
}

$notification = $this->taskNotificationMapper->getByTaskId($taskId) ?: new TaskNotification();
return $notification->jsonSerialize();
}

/**
* Notify when image generation is ready
*
Expand Down Expand Up @@ -155,6 +184,34 @@ public function notifyWhenReady(int $taskId, string $userId): void {
}
}

/**
* Cancel notification when task is finished
*
* @param int $taskId
* @param string $userId
* @throws Exception
* @throws MultipleObjectsReturnedException
*/
public function cancelNotifyWhenReady(int $taskId, string $userId): void {
try {
$task = $this->taskProcessingManager->getTask($taskId);
} catch (NotFoundException $e) {
// task may be already deleted, so delete any dangling notifications
$this->taskNotificationMapper->deleteByTaskId($taskId);
return;
} catch (TaskProcessingException $e) {
$this->logger->debug('Task request error : ' . $e->getMessage());
throw new Exception('Internal server error.', Http::STATUS_INTERNAL_SERVER_ERROR);
}

if ($task->getUserId() !== $userId) {
$this->logger->info('A user attempted deleting notifications of another user\'s task');
throw new Exception('Unauthorized', Http::STATUS_UNAUTHORIZED);
}

$this->taskNotificationMapper->deleteByTaskId($taskId);
}

/**
* @return array<AssistantTaskProcessingTaskType>
*/
Expand Down
Loading
Loading