From 95b06309f3b55d0ac6412c8f07879d55bdfeb95e Mon Sep 17 00:00:00 2001
From: Andrey Borysenko <andrey18106x@gmail.com>
Date: Wed, 9 Oct 2024 21:02:29 +0300
Subject: feat(app_api): add initial state data for AppAPI UI part

Signed-off-by: Andrey Borysenko <andrey18106x@gmail.com>

# Conflicts:
#	apps/settings/lib/Controller/AppSettingsController.php

# Conflicts:
#	apps/settings/lib/Controller/AppSettingsController.php
---
 .../lib/Controller/AppSettingsController.php       | 34 ++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/apps/settings/lib/Controller/AppSettingsController.php b/apps/settings/lib/Controller/AppSettingsController.php
index a27de4e9673..e4b37a33c5d 100644
--- a/apps/settings/lib/Controller/AppSettingsController.php
+++ b/apps/settings/lib/Controller/AppSettingsController.php
@@ -43,6 +43,7 @@ use OCP\INavigationManager;
 use OCP\IRequest;
 use OCP\IURLGenerator;
 use OCP\L10N\IFactory;
+use OCP\Server;
 use OCP\Util;
 use Psr\Log\LoggerInterface;
 
@@ -89,6 +90,8 @@ class AppSettingsController extends Controller {
 		$this->initialState->provideInitialState('appstoreDeveloperDocs', $this->urlGenerator->linkToDocs('developer-manual'));
 		$this->initialState->provideInitialState('appstoreUpdateCount', count($this->getAppsWithUpdates()));
 
+		$this->provideAppApiState();
+
 		$policy = new ContentSecurityPolicy();
 		$policy->addAllowedImageDomain('https://usercontent.apps.nextcloud.com');
 
@@ -101,6 +104,37 @@ class AppSettingsController extends Controller {
 		return $templateResponse;
 	}
 
+	private function provideAppApiState(): void {
+		$appApiEnabled = $this->appManager->isInstalled('app_api');
+		$this->initialState->provideInitialState('appApiEnabled', $appApiEnabled);
+		$daemonConfigAccessible = false;
+		$defaultDaemonConfig = null;
+
+		if ($appApiEnabled) {
+			$exAppFetcher = Server::get(\OCA\AppAPI\Fetcher\ExAppFetcher::class);
+			$this->initialState->provideInitialState('appstoreExAppUpdateCount', count($exAppFetcher->getExAppsWithUpdates()));
+
+			$defaultDaemonConfigName = $this->config->getAppValue('app_api', 'default_daemon_config');
+			if ($defaultDaemonConfigName !== '') {
+				$daemonConfigService = Server::get(\OCA\AppAPI\Service\DaemonConfigService::class);
+				$daemonConfig = $daemonConfigService->getDaemonConfigByName($defaultDaemonConfigName);
+				if ($daemonConfig !== null) {
+					$defaultDaemonConfig = $daemonConfig->jsonSerialize();
+					unset($defaultDaemonConfig['deploy_config']['haproxy_password']);
+					$dockerActions = Server::get(\OCA\AppAPI\DeployActions\DockerActions::class);
+					$dockerActions->initGuzzleClient($daemonConfig);
+					$daemonConfigAccessible = $dockerActions->ping($dockerActions->buildDockerUrl($daemonConfig));
+					if (!$daemonConfigAccessible) {
+						$this->logger->warning(sprintf('Deploy daemon "%s" is not accessible by Nextcloud. Please verify its configuration', $daemonConfig->getName()));
+					}
+				}
+			}
+		}
+
+		$this->initialState->provideInitialState('defaultDaemonConfigAccessible', $daemonConfigAccessible);
+		$this->initialState->provideInitialState('defaultDaemonConfig', $defaultDaemonConfig);
+	}
+
 	/**
 	 * Get all active entries for the app discover section
 	 */
-- 
cgit v1.2.3