Browse Source

Add a capability

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/1923/head
Joas Schilling 7 years ago
parent
commit
86164d4838
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 2
      docs/api-v1.md
  2. 24
      lib/Capabilities.php
  3. 46
      tests/php/CapabilitiesTest.php

2
docs/api-v1.md

@ -583,7 +583,7 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1`
+ `201 Created`
+ `400 Bad Request` In case of any other error
+ `404 Not Found` When the conversation could not be found for the participant
+ `413 Payload Too Large` When the message was longer than the allowed limit of 1000 characters
+ `413 Payload Too Large` When the message was longer than the allowed limit of 32000 characters (or 1000 until Nextcloud 16.0.1, check the `spreed => config => chat => max-length` capability for the limit)
- Data:
The full message array of the new message, as defined in [Receive chat messages of a conversation](#receive-chat-messages-of-a-conversation)

24
lib/Capabilities.php

@ -24,29 +24,40 @@ declare(strict_types=1);
namespace OCA\Spreed;
use OCA\Spreed\Chat\ChatManager;
use OCP\Capabilities\IPublicCapability;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
class Capabilities implements IPublicCapability {
/** @var IConfig */
protected $serverConfig;
/** @var Config */
protected $config;
protected $talkConfig;
/** @var IUserSession */
protected $userSession;
public function __construct(Config $config,
public function __construct(IConfig $serverConfig,
Config $talkConfig,
IUserSession $userSession) {
$this->config = $config;
$this->serverConfig = $serverConfig;
$this->talkConfig = $talkConfig;
$this->userSession = $userSession;
}
public function getCapabilities(): array {
$user = $this->userSession->getUser();
if ($user instanceof IUser && $this->config->isDisabledForUser($user)) {
if ($user instanceof IUser && $this->talkConfig->isDisabledForUser($user)) {
return [];
}
$maxChatLength = 1000;
if (version_compare($this->serverConfig->getSystemValueString('version', '0.0.0'), '16.0.2', '>=')) {
$maxChatLength = ChatManager::MAX_CHAT_LENGTH;
}
return [
'spreed' => [
'features' => [
@ -68,6 +79,11 @@ class Capabilities implements IPublicCapability {
'locked-one-to-one-rooms',
'read-only-rooms',
],
'config' => [
'chat' => [
'max-length' => $maxChatLength,
],
],
],
];
}

46
tests/php/CapabilitiesTest.php

@ -27,6 +27,7 @@ namespace OCA\Spreed\Tests\Unit;
use OCA\Spreed\Capabilities;
use OCA\Spreed\Config;
use OCP\Capabilities\IPublicCapability;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
@ -34,20 +35,24 @@ use Test\TestCase;
class CapabilitiesTest extends TestCase {
/** @var IConfig|MockObject */
protected $serverConfig;
/** @var Config|MockObject */
protected $config;
protected $talkConfig;
/** @var IUserSession|MockObject */
protected $userSession;
public function setUp() {
parent::setUp();
$this->config = $this->createMock(Config::class);
$this->serverConfig = $this->createMock(IConfig::class);
$this->talkConfig = $this->createMock(Config::class);
$this->userSession = $this->createMock(IUserSession::class);
}
public function testGetCapabilitiesGuest(): void {
$capabilities = new Capabilities(
$this->config,
$this->serverConfig,
$this->talkConfig,
$this->userSession
);
@ -55,9 +60,14 @@ class CapabilitiesTest extends TestCase {
->method('getUser')
->willReturn(null);
$this->config->expects($this->never())
$this->talkConfig->expects($this->never())
->method('isDisabledForUser');
$this->serverConfig->expects($this->once())
->method('getSystemValueString')
->with('version', '0.0.0')
->willReturn('16.0.1');
$this->assertInstanceOf(IPublicCapability::class, $capabilities);
$this->assertSame([
'spreed' => [
@ -80,13 +90,19 @@ class CapabilitiesTest extends TestCase {
'locked-one-to-one-rooms',
'read-only-rooms',
],
'config' => [
'chat' => [
'max-length' => 1000,
],
],
],
], $capabilities->getCapabilities());
}
public function testGetCapabilitiesUserAllowed(): void {
$capabilities = new Capabilities(
$this->config,
$this->serverConfig,
$this->talkConfig,
$this->userSession
);
@ -95,11 +111,16 @@ class CapabilitiesTest extends TestCase {
->method('getUser')
->willReturn($user);
$this->config->expects($this->once())
$this->talkConfig->expects($this->once())
->method('isDisabledForUser')
->with($user)
->willReturn(false);
$this->serverConfig->expects($this->once())
->method('getSystemValueString')
->with('version', '0.0.0')
->willReturn('16.0.2');
$this->assertInstanceOf(IPublicCapability::class, $capabilities);
$this->assertSame([
'spreed' => [
@ -122,13 +143,19 @@ class CapabilitiesTest extends TestCase {
'locked-one-to-one-rooms',
'read-only-rooms',
],
'config' => [
'chat' => [
'max-length' => 32000,
],
],
],
], $capabilities->getCapabilities());
}
public function testGetCapabilitiesUserDisallowed(): void {
$capabilities = new Capabilities(
$this->config,
$this->serverConfig,
$this->talkConfig,
$this->userSession
);
@ -137,11 +164,14 @@ class CapabilitiesTest extends TestCase {
->method('getUser')
->willReturn($user);
$this->config->expects($this->once())
$this->talkConfig->expects($this->once())
->method('isDisabledForUser')
->with($user)
->willReturn(true);
$this->serverConfig->expects($this->never())
->method('getSystemValueString');
$this->assertInstanceOf(IPublicCapability::class, $capabilities);
$this->assertSame([], $capabilities->getCapabilities());
}

Loading…
Cancel
Save