|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\Assistant\Listener\Text2Image; |
| 9 | + |
| 10 | +use OCA\Assistant\AppInfo\Application; |
| 11 | +use OCP\Collaboration\Reference\RenderReferenceEvent; |
| 12 | +use OCP\EventDispatcher\Event; |
| 13 | +use OCP\EventDispatcher\IEventListener; |
| 14 | +use OCP\IAppConfig; |
| 15 | +use OCP\IConfig; |
| 16 | +use OCP\TaskProcessing\IManager as ITaskProcessingManager; |
| 17 | +use OCP\TaskProcessing\TaskTypes\TextToImage; |
| 18 | +use OCP\Util; |
| 19 | + |
| 20 | +/** |
| 21 | + * @template-implements IEventListener<Event> |
| 22 | + */ |
| 23 | +class Text2StickerListener implements IEventListener { |
| 24 | + public function __construct( |
| 25 | + private IConfig $config, |
| 26 | + private IAppConfig $appConfig, |
| 27 | + private ?string $userId, |
| 28 | + private ITaskProcessingManager $taskProcessingManager, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + public function handle(Event $event): void { |
| 33 | + |
| 34 | + if (!$event instanceof RenderReferenceEvent) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + if ($this->appConfig->getValueString(Application::APP_ID, 'text_to_sticker_picker_enabled', '1') === '1' |
| 39 | + && $this->config->getUserValue($this->userId, Application::APP_ID, 'text_to_sticker_picker_enabled', '1') === '1') { |
| 40 | + |
| 41 | + // Double check that at least one provider is registered |
| 42 | + $availableTaskTypes = $this->taskProcessingManager->getAvailableTaskTypes(); |
| 43 | + $textToImageAvailable = array_key_exists(TextToImage::ID, $availableTaskTypes); |
| 44 | + if ($textToImageAvailable) { |
| 45 | + Util::addScript(Application::APP_ID, Application::APP_ID . '-stickerGeneration'); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments