|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OCA\AppAPI\Controller; |
| 6 | + |
| 7 | +use OCA\AppAPI\AppInfo\Application; |
| 8 | +use OCA\AppAPI\Attribute\AppAPIAuth; |
| 9 | +use OCA\AppAPI\Service\TextProcessingService; |
| 10 | +use OCP\AppFramework\Http; |
| 11 | +use OCP\AppFramework\Http\Attribute\NoCSRFRequired; |
| 12 | +use OCP\AppFramework\Http\Attribute\PublicPage; |
| 13 | +use OCP\AppFramework\Http\DataResponse; |
| 14 | +use OCP\AppFramework\Http\Response; |
| 15 | +use OCP\AppFramework\OCSController; |
| 16 | +use OCP\IConfig; |
| 17 | +use OCP\IRequest; |
| 18 | + |
| 19 | +class TextProcessingController extends OCSController { |
| 20 | + protected $request; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + IRequest $request, |
| 24 | + private readonly IConfig $config, |
| 25 | + private readonly TextProcessingService $textProcessingService, |
| 26 | + ) { |
| 27 | + parent::__construct(Application::APP_ID, $request); |
| 28 | + |
| 29 | + $this->request = $request; |
| 30 | + } |
| 31 | + |
| 32 | + #[NoCSRFRequired] |
| 33 | + #[PublicPage] |
| 34 | + #[AppAPIAuth] |
| 35 | + public function registerProvider( |
| 36 | + string $name, |
| 37 | + string $displayName, |
| 38 | + string $actionHandler, |
| 39 | + string $taskType |
| 40 | + ): DataResponse { |
| 41 | + $ncVersion = $this->config->getSystemValueString('version', '0.0.0'); |
| 42 | + if (version_compare($ncVersion, '29.0', '<')) { |
| 43 | + return new DataResponse([], Http::STATUS_NOT_IMPLEMENTED); |
| 44 | + } |
| 45 | + |
| 46 | + $provider = $this->textProcessingService->registerTextProcessingProvider( |
| 47 | + $this->request->getHeader('EX-APP-ID'), $name, $displayName, $actionHandler, $taskType, |
| 48 | + ); |
| 49 | + |
| 50 | + if ($provider === null) { |
| 51 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
| 52 | + } |
| 53 | + |
| 54 | + return new DataResponse(); |
| 55 | + } |
| 56 | + |
| 57 | + #[NoCSRFRequired] |
| 58 | + #[PublicPage] |
| 59 | + #[AppAPIAuth] |
| 60 | + public function unregisterProvider(string $name): Response { |
| 61 | + $ncVersion = $this->config->getSystemValueString('version', '0.0.0'); |
| 62 | + if (version_compare($ncVersion, '29.0', '<')) { |
| 63 | + return new DataResponse([], Http::STATUS_NOT_IMPLEMENTED); |
| 64 | + } |
| 65 | + |
| 66 | + $unregistered = $this->textProcessingService->unregisterTextProcessingProvider( |
| 67 | + $this->request->getHeader('EX-APP-ID'), $name |
| 68 | + ); |
| 69 | + |
| 70 | + if ($unregistered === null) { |
| 71 | + return new DataResponse([], Http::STATUS_NOT_FOUND); |
| 72 | + } |
| 73 | + |
| 74 | + return new DataResponse(); |
| 75 | + } |
| 76 | + |
| 77 | + #[NoCSRFRequired] |
| 78 | + #[PublicPage] |
| 79 | + #[AppAPIAuth] |
| 80 | + public function getProvider(string $name): DataResponse { |
| 81 | + $ncVersion = $this->config->getSystemValueString('version', '0.0.0'); |
| 82 | + if (version_compare($ncVersion, '29.0', '<')) { |
| 83 | + return new DataResponse([], Http::STATUS_NOT_IMPLEMENTED); |
| 84 | + } |
| 85 | + $result = $this->textProcessingService->getExAppTextProcessingProvider( |
| 86 | + $this->request->getHeader('EX-APP-ID'), $name |
| 87 | + ); |
| 88 | + if (!$result) { |
| 89 | + return new DataResponse([], Http::STATUS_NOT_FOUND); |
| 90 | + } |
| 91 | + return new DataResponse($result, Http::STATUS_OK); |
| 92 | + } |
| 93 | +} |
0 commit comments