diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 0173a627..10331d50 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; @@ -23,7 +24,6 @@ use OCA\AppAPI\Middleware\ExAppUiMiddleware; use OCA\AppAPI\Notifications\ExAppNotifier; use OCA\AppAPI\PublicCapabilities; -use OCA\AppAPI\Service\ProvidersAI\TaskProcessingService; use OCA\AppAPI\SetupChecks\DaemonCheck; use OCA\DAV\Events\SabrePluginAuthInitEvent; use OCA\Files\Event\LoadAdditionalScriptsEvent; @@ -43,8 +43,7 @@ use OCP\Settings\Events\DeclarativeSettingsGetValueEvent; use OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent; use OCP\Settings\Events\DeclarativeSettingsSetValueEvent; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; +use OCP\TaskProcessing\Events\GetTaskProcessingProvidersEvent; class Application extends App implements IBootstrap { public const APP_ID = 'app_api'; @@ -61,6 +60,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); @@ -75,14 +75,6 @@ public function register(IRegistrationContext $context): void { $context->registerEventListener(DeclarativeSettingsGetValueEvent::class, GetValueListener::class); $context->registerEventListener(DeclarativeSettingsSetValueEvent::class, SetValueListener::class); - $container = $this->getContainer(); - try { - /** @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); $context->registerEventListener(NodeTouchedEvent::class, FileEventsListener::class); $context->registerEventListener(NodeWrittenEvent::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 e47aaee1..c6d2d1c9 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -9,6 +9,7 @@ + @@ -81,6 +82,14 @@ + + + + + + + +