diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 2400f815..0951e450 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -15,6 +15,7 @@ use OCA\AppAPI\Listener\DeclarativeSettings\RegisterDeclarativeSettingsListener; use OCA\AppAPI\Listener\DeclarativeSettings\SetValueListener; use OCA\AppAPI\Listener\FileEventsListener; +use OCA\AppAPI\Listener\GetTaskProcessingProvidersListener; use OCA\AppAPI\Listener\LoadFilesPluginListener; use OCA\AppAPI\Listener\LoadMenuEntriesListener; use OCA\AppAPI\Listener\SabrePluginAuthInitListener; @@ -24,7 +25,6 @@ use OCA\AppAPI\Notifications\ExAppNotifier; use OCA\AppAPI\PublicCapabilities; use OCA\AppAPI\Service\ProvidersAI\SpeechToTextService; -use OCA\AppAPI\Service\ProvidersAI\TaskProcessingService; use OCA\AppAPI\Service\ProvidersAI\TextProcessingService; use OCA\AppAPI\Service\ProvidersAI\TranslationService; use OCA\DAV\Events\SabrePluginAuthInitEvent; @@ -40,12 +40,12 @@ use OCP\Files\Events\Node\NodeRenamedEvent; use OCP\Files\Events\Node\NodeTouchedEvent; use OCP\Files\Events\Node\NodeWrittenEvent; -use OCP\IConfig; use OCP\Navigation\Events\LoadAdditionalEntriesEvent; use OCP\SabrePluginEvent; use OCP\Settings\Events\DeclarativeSettingsGetValueEvent; use OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent; use OCP\Settings\Events\DeclarativeSettingsSetValueEvent; +use OCP\TaskProcessing\Events\GetTaskProcessingProvidersEvent; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -64,6 +64,7 @@ public function __construct(array $urlParams = []) { * @psalm-suppress UndefinedClass */ public function register(IRegistrationContext $context): void { + $context->registerEventListener(GetTaskProcessingProvidersEvent::class, GetTaskProcessingProvidersListener::class); $context->registerEventListener(LoadAdditionalEntriesEvent::class, LoadMenuEntriesListener::class); $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadFilesPluginListener::class); $context->registerCapability(Capabilities::class); @@ -91,14 +92,6 @@ public function register(IRegistrationContext $context): void { /** @var TranslationService $translationService */ $translationService = $container->get(TranslationService::class); $translationService->registerExAppTranslationProviders($context, $container->getServer()); - - $config = $this->getContainer()->query(IConfig::class); - if (version_compare($config->getSystemValueString('version', '0.0.0'), '30.0', '>=')) { - /** @var TaskProcessingService $taskProcessingService */ - $taskProcessingService = $container->get(TaskProcessingService::class); - $taskProcessingService->registerExAppTaskProcessingProviders($context, $container->getServer()); - $taskProcessingService->registerExAppTaskProcessingCustomTaskTypes($context); - } } catch (NotFoundExceptionInterface|ContainerExceptionInterface) { } $context->registerEventListener(NodeCreatedEvent::class, FileEventsListener::class); diff --git a/lib/Listener/GetTaskProcessingProvidersListener.php b/lib/Listener/GetTaskProcessingProvidersListener.php new file mode 100644 index 00000000..2709cb82 --- /dev/null +++ b/lib/Listener/GetTaskProcessingProvidersListener.php @@ -0,0 +1,73 @@ +taskProcessingService->getRegisteredTaskProcessingProviders(); + + foreach ($exAppsProviders as $exAppProvider) { + try { + // Decode provider data + $providerData = json_decode($exAppProvider->getProvider(), true, 512, JSON_THROW_ON_ERROR); + $providerInstance = $this->taskProcessingService->getAnonymousExAppProvider($providerData); + $event->addProvider($providerInstance); + + // Decode and add custom task type if it exists + $customTaskTypeDataJson = $exAppProvider->getCustomTaskType(); + if ($customTaskTypeDataJson !== null && $customTaskTypeDataJson !== '' && $customTaskTypeDataJson !== 'null') { + $customTaskTypeData = json_decode($customTaskTypeDataJson, true, 512, JSON_THROW_ON_ERROR); + $taskTypeInstance = $this->taskProcessingService->getAnonymousTaskType($customTaskTypeData); + $event->addTaskType($taskTypeInstance); + } + } catch (JsonException $e) { + $this->logger->error( + 'Failed to decode or process ExApp TaskProcessing provider/task type during event handling', + [ + 'exAppId' => $exAppProvider->getAppId(), + 'providerName' => $exAppProvider->getName(), + 'exception' => $e->getMessage(), + ] + ); + } catch (\Throwable $e) { + $this->logger->error( + 'Unexpected error processing ExApp TaskProcessing provider/task type during event handling', + [ + 'exAppId' => $exAppProvider->getAppId(), + 'providerName' => $exAppProvider->getName(), + 'exception' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ] + ); + } + } + } +} diff --git a/lib/Service/ProvidersAI/TaskProcessingService.php b/lib/Service/ProvidersAI/TaskProcessingService.php index d0fbcdd4..18e6943e 100644 --- a/lib/Service/ProvidersAI/TaskProcessingService.php +++ b/lib/Service/ProvidersAI/TaskProcessingService.php @@ -244,7 +244,7 @@ public function registerExAppTaskProcessingProviders(IRegistrationContext $conte /** * @psalm-suppress UndefinedClass, MissingDependency, InvalidReturnStatement, InvalidReturnType */ - private function getAnonymousExAppProvider( + public function getAnonymousExAppProvider( array $provider, ): IProvider { return new class($provider) implements IProvider { @@ -337,38 +337,7 @@ public function unregisterExAppTaskProcessingProviders(string $appId): int { return $result; } - /** - * @param IRegistrationContext $context - * - * @return void - */ - public function registerExAppTaskProcessingCustomTaskTypes(IRegistrationContext $context): void { - $exAppsProviders = $this->getRegisteredTaskProcessingProviders(); - foreach ($exAppsProviders as $exAppProvider) { - $customTaskType = $exAppProvider->getCustomTaskType(); - if ($customTaskType === null) { - continue; - } - - /** @var class-string $className */ - $className = '\\OCA\\AppAPI\\' . $exAppProvider->getAppId() . '\\' . $exAppProvider->getName() . '\\TaskType'; - try { - $taskType = $this->getAnonymousTaskType(json_decode($customTaskType, true, 512, JSON_THROW_ON_ERROR)); - } catch (JsonException $e) { - $this->logger->debug('Failed to register ExApp TaskProcessing custom task type', ['exAppId' => $exAppProvider->getAppId(), 'taskType' => $exAppProvider->getName(), 'exception' => $e]); - continue; - } catch (\Throwable) { - continue; - } - - $context->registerService($className, function () use ($taskType) { - return $taskType; - }); - $context->registerTaskProcessingTaskType($className); - } - } - - private function getAnonymousTaskType( + public function getAnonymousTaskType( array $customTaskType, ): ITaskType { return new class($customTaskType) implements ITaskType { diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index df171e04..bd8dd127 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -9,6 +9,7 @@ + @@ -81,6 +82,14 @@ + + + + + + + +