|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\Assistant\Reference; |
| 9 | + |
| 10 | +use OCA\Assistant\AppInfo\Application; |
| 11 | +use OCP\Collaboration\Reference\IReference; |
| 12 | +use OCP\Collaboration\Reference\IReferenceManager; |
| 13 | +use OCP\Collaboration\Reference\IReferenceProvider; |
| 14 | +use OCP\Collaboration\Reference\LinkReferenceProvider; |
| 15 | +use OCP\Collaboration\Reference\Reference; |
| 16 | +use OCP\IURLGenerator; |
| 17 | +use OCP\TaskProcessing\IManager as TaskProcessingManager; |
| 18 | + |
| 19 | +class TaskOutputFileReferenceProvider implements IReferenceProvider { |
| 20 | + |
| 21 | + private const RICH_OBJECT_TYPE = Application::APP_ID . '_task-output-file'; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private IReferenceManager $referenceManager, |
| 25 | + private LinkReferenceProvider $linkReferenceProvider, |
| 26 | + private IURLGenerator $urlGenerator, |
| 27 | + private TaskProcessingManager $taskProcessingManager, |
| 28 | + private ?string $userId, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritDoc |
| 34 | + */ |
| 35 | + public function matchReference(string $referenceText): bool { |
| 36 | + return $this->getLinkInfo($referenceText) !== null; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @inheritDoc |
| 41 | + */ |
| 42 | + public function resolveReference(string $referenceText): ?IReference { |
| 43 | + if ($this->matchReference($referenceText)) { |
| 44 | + $linkInfo = $this->getLinkInfo($referenceText); |
| 45 | + if ($linkInfo !== null) { |
| 46 | + $taskId = $linkInfo['taskId']; |
| 47 | + $task = $this->taskProcessingManager->getTask($taskId); |
| 48 | + if ($task->getUserId() === null || $task->getUserId() !== $this->userId) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + $linkInfo['taskTypeId'] = $task->getTaskTypeId(); |
| 53 | + $linkInfo['taskTypeName'] = $this->taskProcessingManager->getAvailableTaskTypes()[$task->getTaskTypeId()]['name'] ?? null; |
| 54 | + $reference = new Reference($referenceText); |
| 55 | + $reference->setRichObject( |
| 56 | + self::RICH_OBJECT_TYPE, |
| 57 | + $linkInfo, |
| 58 | + ); |
| 59 | + return $reference; |
| 60 | + } |
| 61 | + // fallback to opengraph |
| 62 | + return $this->linkReferenceProvider->resolveReference($referenceText); |
| 63 | + } |
| 64 | + |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param string $url |
| 70 | + * @return array|null |
| 71 | + */ |
| 72 | + private function getLinkinfo(string $url): ?array { |
| 73 | + // assistant download link |
| 74 | + // https://nextcloud.local/ocs/v2.php/apps/assistant/api/v1/task/42/output-file/398/download |
| 75 | + |
| 76 | + $start = $this->urlGenerator->linkToOCSRouteAbsolute(Application::APP_ID . '.assistantApi.getOutputFile', [ |
| 77 | + 'apiVersion' => 'v1', |
| 78 | + 'ocpTaskId' => 123, |
| 79 | + 'fileId' => 123, |
| 80 | + ]); |
| 81 | + $start = str_replace('/task/123/output-file/123/download', '/task/', $start); |
| 82 | + if (str_starts_with($url, $start)) { |
| 83 | + preg_match('/\/task\/(\d+)\/output-file\/(\d+)\/download$/i', $url, $matches); |
| 84 | + if (count($matches) > 2) { |
| 85 | + return [ |
| 86 | + 'taskId' => (int)$matches[1], |
| 87 | + 'fileId' => (int)$matches[2], |
| 88 | + ]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // task processing download links |
| 93 | + // https://nextcloud.local/ocs/v2.php/taskprocessing/tasks/42/file/398 |
| 94 | + |
| 95 | + $start = $this->urlGenerator->linkToOCSRouteAbsolute('core.taskProcessingApi.getFileContents', [ |
| 96 | + 'taskId' => 123, |
| 97 | + 'fileId' => 123, |
| 98 | + ]); |
| 99 | + $start = str_replace('/tasks/123/file/123', '/tasks/', $start); |
| 100 | + if (str_starts_with($url, $start)) { |
| 101 | + preg_match('/\/tasks\/(\d+)\/file\/(\d+)$/i', $url, $matches); |
| 102 | + if (count($matches) > 2) { |
| 103 | + return [ |
| 104 | + 'taskId' => (int)$matches[1], |
| 105 | + 'fileId' => (int)$matches[2], |
| 106 | + ]; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * We use the userId here because when connecting/disconnecting from the GitHub account, |
| 115 | + * we want to invalidate all the user cache and this is only possible with the cache prefix |
| 116 | + * @inheritDoc |
| 117 | + */ |
| 118 | + public function getCachePrefix(string $referenceId): string { |
| 119 | + return $this->userId ?? ''; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * We don't use the userId here but rather a reference unique id |
| 124 | + * @inheritDoc |
| 125 | + */ |
| 126 | + public function getCacheKey(string $referenceId): ?string { |
| 127 | + return $referenceId; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param string $userId |
| 132 | + * @return void |
| 133 | + */ |
| 134 | + public function invalidateUserCache(string $userId): void { |
| 135 | + $this->referenceManager->invalidateCache($userId); |
| 136 | + } |
| 137 | +} |
0 commit comments