Add a Talk API for OCP

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2021-12-20 17:58:54 +01:00
parent eb1927040f
commit 2c356d0852
No known key found for this signature in database
GPG Key ID: CC42AC2A7F0E56D8
13 changed files with 658 additions and 0 deletions

View File

@ -536,6 +536,11 @@ return array(
'OCP\\SystemTag\\SystemTagsEntityEvent' => $baseDir . '/lib/public/SystemTag/SystemTagsEntityEvent.php',
'OCP\\SystemTag\\TagAlreadyExistsException' => $baseDir . '/lib/public/SystemTag/TagAlreadyExistsException.php',
'OCP\\SystemTag\\TagNotFoundException' => $baseDir . '/lib/public/SystemTag/TagNotFoundException.php',
'OCP\\Talk\\Exceptions\\NoBackendException' => $baseDir . '/lib/public/Talk/Exceptions/NoBackendException.php',
'OCP\\Talk\\IBroker' => $baseDir . '/lib/public/Talk/IBroker.php',
'OCP\\Talk\\IConversation' => $baseDir . '/lib/public/Talk/IConversation.php',
'OCP\\Talk\\IConversationOptions' => $baseDir . '/lib/public/Talk/IConversationOptions.php',
'OCP\\Talk\\ITalkBackend' => $baseDir . '/lib/public/Talk/ITalkBackend.php',
'OCP\\Template' => $baseDir . '/lib/public/Template.php',
'OCP\\UserInterface' => $baseDir . '/lib/public/UserInterface.php',
'OCP\\UserStatus\\IManager' => $baseDir . '/lib/public/UserStatus/IManager.php',
@ -1458,6 +1463,8 @@ return array(
'OC\\Tagging\\Tag' => $baseDir . '/lib/private/Tagging/Tag.php',
'OC\\Tagging\\TagMapper' => $baseDir . '/lib/private/Tagging/TagMapper.php',
'OC\\Tags' => $baseDir . '/lib/private/Tags.php',
'OC\\Talk\\Broker' => $baseDir . '/lib/private/Talk/Broker.php',
'OC\\Talk\\ConversationOptions' => $baseDir . '/lib/private/Talk/ConversationOptions.php',
'OC\\TempManager' => $baseDir . '/lib/private/TempManager.php',
'OC\\TemplateLayout' => $baseDir . '/lib/private/TemplateLayout.php',
'OC\\Template\\Base' => $baseDir . '/lib/private/Template/Base.php',

View File

@ -565,6 +565,11 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\SystemTag\\SystemTagsEntityEvent' => __DIR__ . '/../../..' . '/lib/public/SystemTag/SystemTagsEntityEvent.php',
'OCP\\SystemTag\\TagAlreadyExistsException' => __DIR__ . '/../../..' . '/lib/public/SystemTag/TagAlreadyExistsException.php',
'OCP\\SystemTag\\TagNotFoundException' => __DIR__ . '/../../..' . '/lib/public/SystemTag/TagNotFoundException.php',
'OCP\\Talk\\Exceptions\\NoBackendException' => __DIR__ . '/../../..' . '/lib/public/Talk/Exceptions/NoBackendException.php',
'OCP\\Talk\\IBroker' => __DIR__ . '/../../..' . '/lib/public/Talk/IBroker.php',
'OCP\\Talk\\IConversation' => __DIR__ . '/../../..' . '/lib/public/Talk/IConversation.php',
'OCP\\Talk\\IConversationOptions' => __DIR__ . '/../../..' . '/lib/public/Talk/IConversationOptions.php',
'OCP\\Talk\\ITalkBackend' => __DIR__ . '/../../..' . '/lib/public/Talk/ITalkBackend.php',
'OCP\\Template' => __DIR__ . '/../../..' . '/lib/public/Template.php',
'OCP\\UserInterface' => __DIR__ . '/../../..' . '/lib/public/UserInterface.php',
'OCP\\UserStatus\\IManager' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IManager.php',
@ -1487,6 +1492,8 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Tagging\\Tag' => __DIR__ . '/../../..' . '/lib/private/Tagging/Tag.php',
'OC\\Tagging\\TagMapper' => __DIR__ . '/../../..' . '/lib/private/Tagging/TagMapper.php',
'OC\\Tags' => __DIR__ . '/../../..' . '/lib/private/Tags.php',
'OC\\Talk\\Broker' => __DIR__ . '/../../..' . '/lib/private/Talk/Broker.php',
'OC\\Talk\\ConversationOptions' => __DIR__ . '/../../..' . '/lib/private/Talk/ConversationOptions.php',
'OC\\TempManager' => __DIR__ . '/../../..' . '/lib/private/TempManager.php',
'OC\\TemplateLayout' => __DIR__ . '/../../..' . '/lib/private/TemplateLayout.php',
'OC\\Template\\Base' => __DIR__ . '/../../..' . '/lib/private/Template/Base.php',

View File

@ -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
);
}
};
}
@ -349,6 +361,21 @@ class RegistrationContext {
$this->profileLinkActions[] = new ServiceRegistration($appId, $actionClass);
}
/**
* @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
*/
@ -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;
}
}

109
lib/private/Talk/Broker.php Normal file
View File

@ -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()
);
}
}

View File

@ -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;
}
}

View File

@ -264,4 +264,15 @@ interface IRegistrationContext {
* @since 23.0.0
*/
public function registerProfileLinkAction(string $actionClass): void;
/**
* Register the backend of the Talk app
*
* This service must only be used by the Talk app
*
* @param string $backend
* @return void
* @since 24.0.0
*/
public function registerTalkBackend(string $backend): void;
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
/*
* @copyright 2021 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 OCP\Talk\Exceptions;
use RuntimeException;
/**
* Thrown when the Talk API is accessed but there is no registered backend
*
* @since 24.0.0
*/
final class NoBackendException extends RuntimeException {
}

View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
/*
* @copyright 2021 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 OCP\Talk;
use OCP\IUser;
use OCP\Talk\Exceptions\NoBackendException;
/**
* Abstraction over the optional Talk backend
*
* http://software-pattern.org/Broker
*
* @since 24.0.0
*/
interface IBroker {
/**
* Check if the Talk backend is available
*
* @return bool
* @since 24.0.0
*/
public function hasBackend(): bool;
/**
* Create a new instance of the objects object for specifics of a new conversation
*
* @return IConversationOptions
* @throws NoBackendException when Talk is not available
* @since 24.0.0
*/
public function newConversationOptions(): IConversationOptions;
/**
* Create a new conversation
*
* The conversation is private by default. Use the options parameter to make
* it public.
*
* @param string $name
* @param IUser[] $moderators
* @param IConversationOptions|null $options optional configuration for the conversation
*
* @return IConversation
* @throws NoBackendException when Talk is not available
* @since 24.0.0
*/
public function createConversation(string $name,
array $moderators,
IConversationOptions $options = null): IConversation;
}

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/*
* @copyright 2021 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 OCP\Talk;
/**
* @since 24.0.0
*/
interface IConversation {
/**
* Get the absolute URL to this conversation
*
* @return string
* @since 24.0.0
*/
public function getAbsoluteUrl(): string;
}

View File

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
/*
* @copyright 2021 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 OCP\Talk;
/**
* @since 24.0.0
*/
interface IConversationOptions {
/**
* Will the conversation be public?
*
* @return bool
* @since 24.0.0
*/
public function isPublic(): bool;
/**
* Make the new conversation public
*
* @param bool $isPublic
*
* @return $this
* @since 24.0.0
*/
public function setPublic(bool $isPublic = true): self;
}

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/*
* @copyright 2021 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 OCP\Talk;
use OCP\IUser;
/**
* Interface for the Talk app to implement
*
* Other apps must not implement nor use this interface in any way. Use the
* broker instead
*
* @see IBroker
* @since 24.0.0
*/
interface ITalkBackend {
/**
* @param string $name
* @param IUser[] $moderators
* @param IConversationOptions $options configuration for the conversation
*
* @return IConversation
* @since 24.0.0
*/
public function createConversation(string $name,
array $moderators,
IConversationOptions $options): IConversation;
}

View File

@ -0,0 +1,151 @@
<?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 Test\Talk;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\AppFramework\Bootstrap\RegistrationContext;
use OC\AppFramework\Bootstrap\ServiceRegistration;
use OC\Talk\Broker;
use OCP\AppFramework\QueryException;
use OCP\IServerContainer;
use OCP\Talk\IConversationOptions;
use OCP\Talk\ITalkBackend;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Test\TestCase;
class BrokerTest extends TestCase {
private Coordinator $coordinator;
private IServerContainer $container;
private LoggerInterface $logger;
private Broker $broker;
protected function setUp(): void {
parent::setUp();
$this->coordinator = $this->createMock(Coordinator::class);
$this->container = $this->createMock(IServerContainer::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->broker = new Broker(
$this->coordinator,
$this->container,
$this->logger,
);
}
public function testHasNoBackendCalledTooEarly(): void {
$this->expectException(RuntimeException::class);
$this->broker->hasBackend();
}
public function testHasNoBackend(): void {
$this->coordinator->expects(self::once())
->method('getRegistrationContext')
->willReturn($this->createMock(RegistrationContext::class));
self::assertFalse(
$this->broker->hasBackend()
);
}
public function testHasFaultyBackend(): void {
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
$registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator->expects(self::once())
->method('getRegistrationContext')
->willReturn($registrationContext);
$registrationContext->expects(self::once())
->method('getTalkBackendRegistration')
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
$this->container->expects(self::once())
->method('get')
->willThrowException(new QueryException());
$this->logger->expects(self::once())
->method('error');
self::assertFalse(
$this->broker->hasBackend()
);
}
public function testHasBackend(): void {
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
$registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator->expects(self::once())
->method('getRegistrationContext')
->willReturn($registrationContext);
$registrationContext->expects(self::once())
->method('getTalkBackendRegistration')
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
$talkService = $this->createMock(ITalkBackend::class);
$this->container->expects(self::once())
->method('get')
->with($fakeTalkServiceClass)
->willReturn($talkService);
self::assertTrue(
$this->broker->hasBackend()
);
}
public function testNewConversationOptions(): void {
$this->broker->newConversationOptions();
// Nothing to assert
$this->addToAssertionCount(1);
}
public function testCreateConversation(): void {
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
$registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator->expects(self::once())
->method('getRegistrationContext')
->willReturn($registrationContext);
$registrationContext->expects(self::once())
->method('getTalkBackendRegistration')
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
$talkService = $this->createMock(ITalkBackend::class);
$this->container->expects(self::once())
->method('get')
->with($fakeTalkServiceClass)
->willReturn($talkService);
$options = $this->createMock(IConversationOptions::class);
$talkService->expects(self::once())
->method('createConversation')
->with('Watercooler', [], $options);
$this->broker->createConversation(
'Watercooler',
[],
$options
);
}
}

View File

@ -0,0 +1,37 @@
<?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 Test\Talk;
use OC\Talk\ConversationOptions;
use Test\TestCase;
class ConversationOptionsTest extends TestCase {
public function testDefaults(): void {
ConversationOptions::default();
$this->addToAssertionCount(1);
}
}