From 8c447eaba600fc16bfaeb558aa5a572c7e4ffbb3 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Wed, 3 Sep 2025 17:10:12 +0200 Subject: [PATCH] feat(taskprocessing): add manager method to get the list of available task type IDs Signed-off-by: Julien Veyssier --- lib/private/TaskProcessing/Manager.php | 51 +++++++++++++++++++++++++- lib/public/TaskProcessing/IManager.php | 10 ++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/private/TaskProcessing/Manager.php b/lib/private/TaskProcessing/Manager.php index e288f2981a8..df804c33c8e 100644 --- a/lib/private/TaskProcessing/Manager.php +++ b/lib/private/TaskProcessing/Manager.php @@ -81,6 +81,9 @@ class Manager implements IManager { public const MAX_TASK_AGE_SECONDS = 60 * 60 * 24 * 30 * 4; // 4 months + private const TASK_TYPES_CACHE_KEY = 'available_task_types_v2'; + private const TASK_TYPE_IDS_CACHE_KEY = 'available_task_type_ids'; + /** @var list|null */ private ?array $providers = null; @@ -89,6 +92,9 @@ class Manager implements IManager { */ private ?array $availableTaskTypes = null; + /** @var list|null */ + private ?array $availableTaskTypeIds = null; + private IAppData $appData; private ?array $preferences = null; private ?array $providersById = null; @@ -834,7 +840,7 @@ class Manager implements IManager { return []; } if ($this->availableTaskTypes === null) { - $cachedValue = $this->distributedCache->get('available_task_types_v2'); + $cachedValue = $this->distributedCache->get(self::TASK_TYPES_CACHE_KEY); if ($cachedValue !== null) { $this->availableTaskTypes = unserialize($cachedValue); } @@ -880,12 +886,53 @@ class Manager implements IManager { } $this->availableTaskTypes = $availableTaskTypes; - $this->distributedCache->set('available_task_types_v2', serialize($this->availableTaskTypes), 60); + $this->distributedCache->set(self::TASK_TYPES_CACHE_KEY, serialize($this->availableTaskTypes), 60); } return $this->availableTaskTypes; } + public function getAvailableTaskTypeIds(bool $showDisabled = false, ?string $userId = null): array { + // userId will be obtained from the session if left to null + if (!$this->checkGuestAccess($userId)) { + return []; + } + if ($this->availableTaskTypeIds === null) { + $cachedValue = $this->distributedCache->get(self::TASK_TYPE_IDS_CACHE_KEY); + if ($cachedValue !== null) { + $this->availableTaskTypeIds = $cachedValue; + } + } + // Either we have no cache or showDisabled is turned on, which we don't want to cache, ever. + if ($this->availableTaskTypeIds === null || $showDisabled) { + $taskTypes = $this->_getTaskTypes(); + $taskTypeSettings = $this->_getTaskTypeSettings(); + + $availableTaskTypeIds = []; + foreach ($taskTypes as $taskType) { + if ((!$showDisabled) && isset($taskTypeSettings[$taskType->getId()]) && !$taskTypeSettings[$taskType->getId()]) { + continue; + } + try { + $provider = $this->getPreferredProvider($taskType->getId()); + } catch (\OCP\TaskProcessing\Exception\Exception $e) { + continue; + } + $availableTaskTypeIds[] = $taskType->getId(); + } + + if ($showDisabled) { + // Do not cache showDisabled, ever. + return $availableTaskTypeIds; + } + + $this->availableTaskTypeIds = $availableTaskTypeIds; + $this->distributedCache->set(self::TASK_TYPE_IDS_CACHE_KEY, $this->availableTaskTypeIds, 60); + } + + + return $this->availableTaskTypeIds; + } public function canHandleTask(Task $task): bool { return isset($this->getAvailableTaskTypes()[$task->getTaskTypeId()]); diff --git a/lib/public/TaskProcessing/IManager.php b/lib/public/TaskProcessing/IManager.php index 731250d7aa1..28beeafc884 100644 --- a/lib/public/TaskProcessing/IManager.php +++ b/lib/public/TaskProcessing/IManager.php @@ -47,7 +47,7 @@ interface IManager { public function getPreferredProvider(string $taskTypeId); /** - * @param bool $showDisabled if false, disabled task types will be filtered + * @param bool $showDisabled if false, disabled task types will be filtered out * @param ?string $userId to check if the user is a guest. Will be obtained from session if left to default * @return array, optionalInputShape: ShapeDescriptor[], optionalInputShapeEnumValues: ShapeEnumValue[][], optionalInputShapeDefaults: array, outputShape: ShapeDescriptor[], outputShapeEnumValues: ShapeEnumValue[][], optionalOutputShape: ShapeDescriptor[], optionalOutputShapeEnumValues: ShapeEnumValue[][]}> * @since 30.0.0 @@ -56,6 +56,14 @@ interface IManager { */ public function getAvailableTaskTypes(bool $showDisabled = false, ?string $userId = null): array; + /** + * @param bool $showDisabled if false, disabled task types will be filtered out + * @param ?string $userId to check if the user is a guest. Will be obtained from session if left to default + * @return list + * @since 32.0.0 + */ + public function getAvailableTaskTypeIds(bool $showDisabled = false, ?string $userId = null): array; + /** * @param Task $task The task to run * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called