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
14 changes: 3 additions & 11 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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';
Expand All @@ -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);
Expand All @@ -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);
Expand Down
73 changes: 73 additions & 0 deletions lib/Listener/GetTaskProcessingProvidersListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\AppAPI\Listener;

use JsonException;
use OCA\AppAPI\Service\ProvidersAI\TaskProcessingService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\TaskProcessing\Events\GetTaskProcessingProvidersEvent;
use Psr\Log\LoggerInterface;

class GetTaskProcessingProvidersListener implements IEventListener {
public function __construct(
private readonly TaskProcessingService $taskProcessingService,
private readonly LoggerInterface $logger,
) {
}

/**
* @param Event $event
* @return void
*/
public function handle(Event $event): void {
if (!$event instanceof GetTaskProcessingProvidersEvent) {
return;
}

$exAppsProviders = $this->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(),
]
);
}
}
}
}
35 changes: 2 additions & 33 deletions lib/Service/ProvidersAI/TaskProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<ITaskType> $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 {
Expand Down
9 changes: 9 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<code><![CDATA[LoadFilesPluginListener::class]]></code>
<code><![CDATA[LoadMenuEntriesListener::class]]></code>
<code><![CDATA[SabrePluginAuthInitListener::class]]></code>
<code><![CDATA[registerEventListener]]></code>
</InvalidArgument>
<MissingDependency>
<code><![CDATA[DavPlugin]]></code>
Expand Down Expand Up @@ -81,6 +82,14 @@
<code><![CDATA[private]]></code>
</MissingDependency>
</file>
<file src="lib/Listener/GetTaskProcessingProvidersListener.php">
<MissingTemplateParam>
<code><![CDATA[IEventListener]]></code>
</MissingTemplateParam>
<UndefinedClass>
<code><![CDATA[GetTaskProcessingProvidersEvent]]></code>
</UndefinedClass>
</file>
<file src="lib/Listener/LoadFilesPluginListener.php">
<ImplementedParamTypeMismatch>
<code><![CDATA[$event]]></code>
Expand Down