Skip to content

Commit

Permalink
fixed cyclic imports, talkbots -> Proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
bigcat88 committed Jan 21, 2024
1 parent a4c5952 commit 517de9e
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 68 deletions.
4 changes: 2 additions & 2 deletions lib/Command/ExApp/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
class Deploy extends Command {

public function __construct(
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly DaemonConfigService $daemonConfigService,
private readonly DockerActions $dockerActions,
private readonly IConfig $config,
Expand Down
5 changes: 2 additions & 3 deletions lib/Command/ExApp/Disable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace OCA\AppAPI\Command\ExApp;

use OCA\AppAPI\Service\AppAPIService;

use OCA\AppAPI\Service\ExAppService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -14,8 +13,8 @@

class Disable extends Command {
public function __construct(
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
) {
parent::__construct();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Command/ExApp/DispatchInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
class DispatchInit extends Command {

public function __construct(
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
) {
parent::__construct();
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Command/ExApp/Enable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace OCA\AppAPI\Command\ExApp;

use OCA\AppAPI\Service\AppAPIService;

use OCA\AppAPI\Service\ExAppService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -15,8 +14,8 @@
class Enable extends Command {

public function __construct(
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
) {
parent::__construct();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Command/ExApp/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
class Register extends Command {

public function __construct(
private readonly AppAPIService $service,
private readonly AppAPIService $service,
private readonly DaemonConfigService $daemonConfigService,
private readonly ExAppApiScopeService $exAppApiScopeService,
private readonly ExAppScopesService $exAppScopesService,
private readonly ExAppUsersService $exAppUsersService,
private readonly DockerActions $dockerActions,
private readonly ManualActions $manualActions,
private readonly IConfig $config,
private readonly ExAppService $exAppService,
private readonly ExAppService $exAppService,
) {
parent::__construct();
}
Expand Down
1 change: 0 additions & 1 deletion lib/Command/ExApp/Scopes/ListScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace OCA\AppAPI\Command\ExApp\Scopes;

use OCA\AppAPI\Db\ExAppScope;
use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\ExAppApiScopeService;
use OCA\AppAPI\Service\ExAppScopesService;

Expand Down
6 changes: 3 additions & 3 deletions lib/Command/ExApp/Unregister.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace OCA\AppAPI\Command\ExApp;

use OCA\AppAPI\DeployActions\DockerActions;
use OCA\AppAPI\Service\AppAPIService;

use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\DaemonConfigService;
use OCA\AppAPI\Service\ExAppService;
use Symfony\Component\Console\Command\Command;
Expand All @@ -18,10 +18,10 @@
class Unregister extends Command {

public function __construct(
private readonly AppAPIService $service,
private readonly AppAPIService $service,
private readonly DaemonConfigService $daemonConfigService,
private readonly DockerActions $dockerActions,
private readonly ExAppService $exAppService,
private readonly ExAppService $exAppService,
) {
parent::__construct();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Command/ExApp/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
class Update extends Command {

public function __construct(
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly ExAppScopesService $exAppScopeService,
private readonly ExAppApiScopeService $exAppApiScopeService,
private readonly DaemonConfigService $daemonConfigService,
Expand Down
6 changes: 3 additions & 3 deletions lib/DeployActions/DockerActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use OCA\AppAPI\AppInfo\Application;
use OCA\AppAPI\Db\DaemonConfig;
use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\AppAPICommonService;

use OCA\AppAPI\Service\DaemonConfigService;
use OCA\AppAPI\Service\ExAppService;
Expand Down Expand Up @@ -48,8 +48,8 @@ public function __construct(
private readonly IAppManager $appManager,
private readonly ISecureRandom $random,
private readonly IURLGenerator $urlGenerator,
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly AppAPICommonService $service,
private readonly ExAppService $exAppService,
private readonly DaemonConfigService $daemonConfigService,
) {
}
Expand Down
43 changes: 43 additions & 0 deletions lib/Service/AppAPICommonService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace OCA\AppAPI\Service;

use OCA\AppAPI\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\IRequest;

class AppAPICommonService {

public function __construct(
private readonly IAppManager $appManager,
) {
}

public function buildAppAPIAuthHeaders(?IRequest $request, ?string $userId, string $appId, string $appVersion, string $appSecret): array {
return [
'AA-VERSION' => $this->appManager->getAppVersion(Application::APP_ID, false),
'EX-APP-ID' => $appId,
'EX-APP-VERSION' => $appVersion,
'AUTHORIZATION-APP-API' => base64_encode($userId . ':' . $appSecret),
'AA-REQUEST-ID' => $request instanceof IRequest ? $request->getId() : 'CLI',
];
}

public function isAppHostNameLocal(string $hostname): bool {
return $hostname === '127.0.0.1' || $hostname === 'localhost' || $hostname === '::1';
}

public function buildExAppHost(array $deployConfig): string {
if (isset($deployConfig['net'])) {
if (($deployConfig['net'] === 'host') &&
(isset($deployConfig['host']) && $this->isAppHostNameLocal($deployConfig['host']))
) {
return '127.0.0.1'; # ExApp using this host network, it is visible for Nextcloud on loop-back adapter
}
return '0.0.0.0';
}
return '127.0.0.1'; # fallback to loop-back adapter
}
}
35 changes: 4 additions & 31 deletions lib/Service/AppAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use OCA\AppAPI\DeployActions\DockerActions;
use OCA\AppAPI\DeployActions\ManualActions;
use OCA\AppAPI\Notifications\ExNotificationsManager;
use OCP\App\IAppManager;
use OCP\AppFramework\Http;
use OCP\DB\Exception;
use OCP\Http\Client\IClient;
Expand Down Expand Up @@ -37,7 +36,6 @@ public function __construct(
private readonly IUserSession $userSession,
private readonly ISession $session,
private readonly IUserManager $userManager,
private readonly IAppManager $appManager,
private readonly ExNotificationsManager $exNotificationsManager,
private readonly ExAppService $exAppService,
private readonly ExAppUsersService $exAppUsersService,
Expand All @@ -46,6 +44,7 @@ public function __construct(
private readonly ExAppConfigService $exAppConfigService,
private readonly DockerActions $dockerActions,
private readonly ManualActions $manualActions,
private readonly AppAPICommonService $commonService,
) {
$this->client = $clientService->newClient();
}
Expand Down Expand Up @@ -94,9 +93,9 @@ public function requestToExApp(
}

if (isset($options['headers']) && is_array($options['headers'])) {
$options['headers'] = [...$options['headers'], ...$this->buildAppAPIAuthHeaders($request, $userId, $exApp->getAppid(), $exApp->getVersion(), $exApp->getSecret())];
$options['headers'] = [...$options['headers'], ...$this->commonService->buildAppAPIAuthHeaders($request, $userId, $exApp->getAppid(), $exApp->getVersion(), $exApp->getSecret())];
} else {
$options['headers'] = $this->buildAppAPIAuthHeaders($request, $userId, $exApp->getAppid(), $exApp->getVersion(), $exApp->getSecret());
$options['headers'] = $this->commonService->buildAppAPIAuthHeaders($request, $userId, $exApp->getAppid(), $exApp->getVersion(), $exApp->getSecret());
}
$options['nextcloud'] = [
'allow_local_address' => true, // it's required as we are using ExApp appid as hostname (usually local)
Expand Down Expand Up @@ -173,22 +172,6 @@ private function getUriEncodedParams(array $params): string {
return $paramsContent . http_build_query($params);
}

public function isAppHostNameLocal(string $hostname): bool {
return $hostname === '127.0.0.1' || $hostname === 'localhost' || $hostname === '::1';
}

public function buildExAppHost(array $deployConfig): string {
if (isset($deployConfig['net'])) {
if (($deployConfig['net'] === 'host') &&
(isset($deployConfig['host']) && $this->isAppHostNameLocal($deployConfig['host']))
) {
return '127.0.0.1'; # ExApp using this host network, it is visible for Nextcloud on loop-back adapter
}
return '0.0.0.0';
}
return '127.0.0.1'; # fallback to loop-back adapter
}

/**
* AppAPI authentication request validation for Nextcloud:
* - checks if ExApp exists and is enabled
Expand Down Expand Up @@ -314,16 +297,6 @@ private function finalizeRequestToNC(string $userId, IRequest $request): bool {
return true;
}

private function buildAppAPIAuthHeaders(?IRequest $request, ?string $userId, string $appId, string $appVersion, string $appSecret): array {
return [
'AA-VERSION' => $this->appManager->getAppVersion(Application::APP_ID, false),
'EX-APP-ID' => $appId,
'EX-APP-VERSION' => $appVersion,
'AUTHORIZATION-APP-API' => base64_encode($userId . ':' . $appSecret),
'AA-REQUEST-ID' => $request instanceof IRequest ? $request->getId() : 'CLI',
];
}

private function buildRequestInfo(IRequest $request): array {
$headers = [];
$aeHeadersList = [
Expand Down Expand Up @@ -421,7 +394,7 @@ public function dispatchExAppInitInternal(ExApp $exApp): void {
$auth = [];
$initUrl = $this->getExAppUrl($exApp, $exApp->getPort(), $auth) . '/init';
$options = [
'headers' => $this->buildAppAPIAuthHeaders(null, null, $exApp->getAppid(), $exApp->getVersion(), $exApp->getSecret()),
'headers' => $this->commonService->buildAppAPIAuthHeaders(null, null, $exApp->getAppid(), $exApp->getVersion(), $exApp->getSecret()),
'nextcloud' => [
'allow_local_address' => true,
],
Expand Down
6 changes: 3 additions & 3 deletions lib/Service/ProvidersAI/SpeechToTextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use OCA\AppAPI\Db\SpeechToText\SpeechToTextProviderMapper;
use OCA\AppAPI\Db\SpeechToText\SpeechToTextProviderQueue;
use OCA\AppAPI\Db\SpeechToText\SpeechToTextProviderQueueMapper;
use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\AppAPICommonService;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
Expand Down Expand Up @@ -169,8 +169,8 @@ public function getName(): string {
}

public function transcribeFile(File $file, float $maxExecutionTime = 0): string {
/** @var AppAPIService $service */
$service = $this->serverContainer->get(AppAPIService::class);
/** @var AppAPICommonService $service */
$service = $this->serverContainer->get(AppAPICommonService::class);
$mapper = $this->serverContainer->get(SpeechToTextProviderQueueMapper::class);
$route = $this->provider->getActionHandler();
$queueRecord = $mapper->insert(new SpeechToTextProviderQueue(['created_time' => time()]));
Expand Down
6 changes: 3 additions & 3 deletions lib/Service/ProvidersAI/TextProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use OCA\AppAPI\Db\TextProcessing\TextProcessingProviderMapper;
use OCA\AppAPI\Db\TextProcessing\TextProcessingProviderQueue;
use OCA\AppAPI\Db\TextProcessing\TextProcessingProviderQueueMapper;
use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\AppAPICommonService;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
Expand Down Expand Up @@ -182,8 +182,8 @@ public function getName(): string {
}

public function process(string $prompt, float $maxExecutionTime = 0): string {
/** @var AppAPIService $service */
$service = $this->serverContainer->get(AppAPIService::class);
/** @var AppAPICommonService $service */
$service = $this->serverContainer->get(AppAPICommonService::class);
$mapper = $this->serverContainer->get(TextProcessingProviderQueueMapper::class);
$route = $this->provider->getActionHandler();
$queueRecord = $mapper->insert(new TextProcessingProviderQueue(['created_time' => time()]));
Expand Down
6 changes: 3 additions & 3 deletions lib/Service/ProvidersAI/TranslationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use OCA\AppAPI\Db\Translation\TranslationProviderMapper;
use OCA\AppAPI\Db\Translation\TranslationQueue;
use OCA\AppAPI\Db\Translation\TranslationQueueMapper;
use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\AppAPICommonService;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
Expand Down Expand Up @@ -199,8 +199,8 @@ public function getAvailableLanguages(): array {
}

public function translate(?string $fromLanguage, string $toLanguage, string $text): string {
/** @var AppAPIService $service */
$service = $this->serverContainer->get(AppAPIService::class);
/** @var AppAPICommonService $service */
$service = $this->serverContainer->get(AppAPICommonService::class);
/** @var TranslationQueueMapper $mapper */
$mapper = $this->serverContainer->get(TranslationQueueMapper::class);
$route = $this->provider->getActionHandler();
Expand Down
19 changes: 12 additions & 7 deletions lib/Service/TalkBotsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
use OCA\Talk\Events\BotInstallEvent;
use OCA\Talk\Events\BotUninstallEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IURLGenerator;
use OCP\Security\ISecureRandom;

class TalkBotsService {

public function __construct(
private readonly ExAppConfigService $exAppConfigService,
private readonly IEventDispatcher $dispatcher,
private readonly ISecureRandom $random,
private readonly AppAPIService $service,
private readonly ExAppConfigService $exAppConfigService,
private readonly IEventDispatcher $dispatcher,
private readonly ISecureRandom $random,
private readonly IURLGenerator $urlGenerator,
) {
}

Expand Down Expand Up @@ -57,13 +58,11 @@ public function unregisterExAppBot(ExApp $exApp, string $route): ?bool {
if ($this->exAppConfigService->deleteAppConfigValues([$id], $exApp->getAppid()) !== 1) {
return null;
}

return true;
}

private function getExAppTalkBotConfig(ExApp $exApp, string $route): array {
$auth = [];
$url = $this->service->getExAppUrl($exApp, $exApp->getPort(), $auth) . $route;
$url = $this->getProxyUrl($exApp, $route);
$id = $this->getExAppTalkBotHash($exApp, $route);

$exAppConfig = $this->exAppConfigService->getAppConfig($exApp->getAppid(), $id);
Expand Down Expand Up @@ -93,4 +92,10 @@ public function unregisterExAppTalkBots(ExApp $exApp): void {
private function getExAppTalkBotHash(ExApp $exApp, string $route): string {
return sha1($exApp->getAppid() . '_' . $route);
}

private function getProxyUrl(ExApp $exApp, string $route): string {
return sprintf(
$this->urlGenerator->getAbsoluteURL('') . '/index.php/apps/app_api/proxy/%s%s', $exApp->getAppid(), $route
);
}
}

0 comments on commit 517de9e

Please sign in to comment.