diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2022-02-04 09:54:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-04 09:54:13 +0100 |
commit | 82f602e8575c213c5a49bee2dc89bbf18bce86f1 (patch) | |
tree | ec16f2f0ca27846555d615e87b01e8e50020e0a0 /lib/private | |
parent | 2f2538a57a1bb9aa07de439b41cca64e937f5e56 (diff) | |
parent | 2c356d085236ba17367f25998953b61368078fcd (diff) | |
download | nextcloud-server-82f602e8575c213c5a49bee2dc89bbf18bce86f1.tar.gz nextcloud-server-82f602e8575c213c5a49bee2dc89bbf18bce86f1.zip |
Merge pull request #30354 from nextcloud/enhancement/talk-api
Add a Talk API for OCP
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/AppFramework/Bootstrap/RegistrationContext.php | 35 | ||||
-rw-r--r-- | lib/private/Talk/Broker.php | 109 | ||||
-rw-r--r-- | lib/private/Talk/ConversationOptions.php | 49 |
3 files changed, 193 insertions, 0 deletions
diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index 6a9073b576b..401a967c988 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -30,6 +30,8 @@ declare(strict_types=1); namespace OC\AppFramework\Bootstrap; use Closure; +use OCP\Talk\ITalkBackend; +use RuntimeException; use function array_shift; use OC\Support\CrashReport\Registry; use OCP\AppFramework\App; @@ -65,6 +67,9 @@ class RegistrationContext { /** @var ServiceRegistration<ILinkAction>[] */ private $profileLinkActions = []; + /** @var null|ServiceRegistration<ITalkBackend> */ + private $talkBackendRegistration = null; + /** @var ServiceFactoryRegistration[] */ private $services = []; @@ -259,6 +264,13 @@ class RegistrationContext { $actionClass ); } + + public function registerTalkBackend(string $backend): void { + $this->context->registerTalkBackend( + $this->appId, + $backend + ); + } }; } @@ -350,6 +362,21 @@ class RegistrationContext { } /** + * @psalm-param class-string<ITalkBackend> $backend + */ + public function registerTalkBackend(string $appId, string $backend) { + // Some safeguards for invalid registrations + if ($appId !== 'spreed') { + throw new RuntimeException("Only the Talk app is allowed to register a Talk backend"); + } + if ($this->talkBackendRegistration !== null) { + throw new RuntimeException("There can only be one Talk backend"); + } + + $this->talkBackendRegistration = new ServiceRegistration($appId, $backend); + } + + /** * @param App[] $apps */ public function delegateCapabilityRegistrations(array $apps): void { @@ -600,4 +627,12 @@ class RegistrationContext { public function getProfileLinkActions(): array { return $this->profileLinkActions; } + + /** + * @return ServiceRegistration|null + * @psalm-return ServiceRegistration<ITalkBackend>|null + */ + public function getTalkBackendRegistration(): ?ServiceRegistration { + return $this->talkBackendRegistration; + } } diff --git a/lib/private/Talk/Broker.php b/lib/private/Talk/Broker.php new file mode 100644 index 00000000000..a686adeed04 --- /dev/null +++ b/lib/private/Talk/Broker.php @@ -0,0 +1,109 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OC\Talk; + +use OC\AppFramework\Bootstrap\Coordinator; +use OCP\IServerContainer; +use OCP\Talk\Exceptions\NoBackendException; +use OCP\Talk\IBroker; +use OCP\Talk\IConversation; +use OCP\Talk\IConversationOptions; +use OCP\Talk\ITalkBackend; +use Psr\Log\LoggerInterface; +use RuntimeException; +use Throwable; + +class Broker implements IBroker { + private Coordinator $coordinator; + + private IServerContainer $container; + + private LoggerInterface $logger; + + private ?bool $hasBackend = null; + + private ?ITalkBackend $backend = null; + + public function __construct(Coordinator $coordinator, + IServerContainer $container, + LoggerInterface $logger) { + $this->coordinator = $coordinator; + $this->container = $container; + $this->logger = $logger; + } + + public function hasBackend(): bool { + if ($this->hasBackend !== null) { + return $this->hasBackend; + } + + $context = $this->coordinator->getRegistrationContext(); + if ($context === null) { + // Backend requested too soon, e.g. from the bootstrap `register` method of an app + throw new RuntimeException("Not all apps have been registered yet"); + } + $backendRegistration = $context->getTalkBackendRegistration(); + if ($backendRegistration === null) { + // Nothing to do. Remember and exit. + return $this->hasBackend = false; + } + + try { + $this->backend = $this->container->get( + $backendRegistration->getService() + ); + + // Remember and return + return $this->hasBackend = true; + } catch (Throwable $e) { + $this->logger->error("Talk backend {class} could not be loaded: " . $e->getMessage(), [ + 'class' => $backendRegistration->getService(), + 'exception' => $e, + ]); + + // Temporary result. Maybe the next time the backend is requested it can be loaded. + return false; + } + } + + public function newConversationOptions(): IConversationOptions { + return ConversationOptions::default(); + } + + public function createConversation(string $name, + array $moderators, + IConversationOptions $options = null): IConversation { + if (!$this->hasBackend()) { + throw new NoBackendException("The Talk broker has no registered backend"); + } + + return $this->backend->createConversation( + $name, + $moderators, + $options ?? ConversationOptions::default() + ); + } +} diff --git a/lib/private/Talk/ConversationOptions.php b/lib/private/Talk/ConversationOptions.php new file mode 100644 index 00000000000..f86c8d037d3 --- /dev/null +++ b/lib/private/Talk/ConversationOptions.php @@ -0,0 +1,49 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2022 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OC\Talk; + +use OCP\Talk\IConversationOptions; + +class ConversationOptions implements IConversationOptions { + private bool $isPublic; + + private function __construct(bool $isPublic) { + $this->isPublic = $isPublic; + } + + public static function default(): self { + return new self(false); + } + + public function setPublic(bool $isPublic = true): IConversationOptions { + $this->isPublic = $isPublic; + return $this; + } + + public function isPublic(): bool { + return $this->isPublic; + } +} |