|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Whiteboard\Service; |
| 11 | + |
| 12 | +use OCA\AppAPI\Service\ExAppService as AppAPIService; |
| 13 | +use OCA\Whiteboard\Consts\ExAppConsts; |
| 14 | +use OCP\App\IAppManager; |
| 15 | +use OCP\AppFramework\Services\IInitialState; |
| 16 | +use Psr\Container\ContainerInterface; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | +use Throwable; |
| 19 | + |
| 20 | +/** |
| 21 | + * @psalm-suppress UndefinedClass |
| 22 | + * @psalm-suppress MissingDependency |
| 23 | + */ |
| 24 | +final class ExAppService { |
| 25 | + private ?AppAPIService $appAPIService = null; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private IAppManager $appManager, |
| 29 | + private ContainerInterface $container, |
| 30 | + private IInitialState $initialState, |
| 31 | + private LoggerInterface $logger, |
| 32 | + ) { |
| 33 | + $this->initAppAPIService(); |
| 34 | + } |
| 35 | + |
| 36 | + private function initAppAPIService(): void { |
| 37 | + $isAppAPIEnabled = $this->isAppAPIEnabled(); |
| 38 | + |
| 39 | + if (class_exists(AppAPIService::class) && $isAppAPIEnabled) { |
| 40 | + try { |
| 41 | + $this->appAPIService = $this->container->get(AppAPIService::class); |
| 42 | + } catch (Throwable $e) { |
| 43 | + $this->logger->error('exApp', [$e->getMessage()]); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private function isAppAPIEnabled(): bool { |
| 49 | + return $this->appManager->isEnabledForUser(ExAppConsts::APP_API_ID); |
| 50 | + } |
| 51 | + |
| 52 | + public function isExAppEnabled(string $appId): bool { |
| 53 | + if ($this->appAPIService === null) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + return $this->appAPIService->getExApp($appId)?->getEnabled() === 1; |
| 58 | + } |
| 59 | + |
| 60 | + public function isWhiteboardWebsocketEnabled(): bool { |
| 61 | + return $this->isExAppEnabled(ExAppConsts::WHITEBOARD_EX_APP_ID); |
| 62 | + } |
| 63 | + |
| 64 | + public function initFrontendState(): void { |
| 65 | + $this->initialState->provideInitialState( |
| 66 | + ExAppConsts::WHITEBOARD_EX_APP_ENABLED_KEY, |
| 67 | + $this->isWhiteboardWebsocketEnabled() |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
0 commit comments