Browse Source

Merge pull request #9321 from nextcloud/feature/9273/translations-capabilities

feat(chat): Add capabilities for translation options
pull/9330/head
Joas Schilling 3 years ago
committed by GitHub
parent
commit
4c023ef657
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      docs/capabilities.md
  2. 23
      lib/Capabilities.php
  3. 60
      tests/php/CapabilitiesTest.php

1
docs/capabilities.md

@ -115,3 +115,4 @@
## 17
* `avatar` - Avatar of conversation
* `config => chat => translations` - List of translations tuples, JSON encoded sample `{"from":"de","fromLabel":"German","to":"en","toLabel":"English"}`. Those tuples should be provided as options when translating chat messages.

23
lib/Capabilities.php

@ -32,26 +32,18 @@ use OCP\Comments\ICommentsManager;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Translation\ITranslationManager;
class Capabilities implements IPublicCapability {
protected IConfig $serverConfig;
protected Config $talkConfig;
protected ICommentsManager $commentsManager;
protected IUserSession $userSession;
private IAppManager $appManager;
public function __construct(
IConfig $serverConfig,
Config $talkConfig,
ICommentsManager $commentsManager,
IUserSession $userSession,
IAppManager $appManager,
protected IConfig $serverConfig,
protected Config $talkConfig,
protected ICommentsManager $commentsManager,
protected IUserSession $userSession,
protected IAppManager $appManager,
protected ITranslationManager $translationManager,
) {
$this->serverConfig = $serverConfig;
$this->talkConfig = $talkConfig;
$this->commentsManager = $commentsManager;
$this->userSession = $userSession;
$this->appManager = $appManager;
}
public function getCapabilities(): array {
@ -134,6 +126,7 @@ class Capabilities implements IPublicCapability {
'max-length' => ChatManager::MAX_CHAT_LENGTH,
'read-privacy' => Participant::PRIVACY_PUBLIC,
//'legacy' => true, // Temporary A-B switch to opt-out of the new context loading
'translations' => $this->translationManager->getLanguages(),
],
'conversations' => [
'can-create' => $user instanceof IUser && !$this->talkConfig->isNotAllowedToCreateConversations($user)

60
tests/php/CapabilitiesTest.php

@ -35,20 +35,18 @@ use OCP\Capabilities\IPublicCapability;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Translation\ITranslationManager;
use OCP\Translation\LanguageTuple;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class CapabilitiesTest extends TestCase {
/** @var IConfig|MockObject */
protected $serverConfig;
/** @var Config|MockObject */
protected $talkConfig;
/** @var CommentsManager|MockObject */
protected $commentsManager;
/** @var IUserSession|MockObject */
protected $userSession;
/** @var IAppManager|MockObject */
protected $appManager;
protected IConfig|MockObject $serverConfig;
protected Config|MockObject $talkConfig;
protected CommentsManager|MockObject $commentsManager;
protected IUserSession|MockObject $userSession;
protected IAppManager|MockObject $appManager;
protected ITranslationManager|MockObject $translationManager;
protected ?array $baseFeatures = null;
public function setUp(): void {
@ -58,6 +56,7 @@ class CapabilitiesTest extends TestCase {
$this->commentsManager = $this->createMock(CommentsManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->translationManager = $this->createMock(ITranslationManager::class);
$this->commentsManager->expects($this->any())
->method('supportReactions')
@ -138,7 +137,8 @@ class CapabilitiesTest extends TestCase {
$this->talkConfig,
$this->commentsManager,
$this->userSession,
$this->appManager
$this->appManager,
$this->translationManager,
);
$this->userSession->expects($this->once())
@ -178,6 +178,7 @@ class CapabilitiesTest extends TestCase {
'chat' => [
'max-length' => 32000,
'read-privacy' => 0,
'translations' => [],
],
'conversations' => [
'can-create' => false,
@ -213,7 +214,8 @@ class CapabilitiesTest extends TestCase {
$this->talkConfig,
$this->commentsManager,
$this->userSession,
$this->appManager
$this->appManager,
$this->translationManager,
);
$user = $this->createMock(IUser::class);
@ -280,6 +282,7 @@ class CapabilitiesTest extends TestCase {
'chat' => [
'max-length' => 32000,
'read-privacy' => $readPrivacy,
'translations' => [],
],
'conversations' => [
'can-create' => $canCreate,
@ -317,7 +320,8 @@ class CapabilitiesTest extends TestCase {
$this->talkConfig,
$this->commentsManager,
$this->userSession,
$this->appManager
$this->appManager,
$this->translationManager,
);
$user = $this->createMock(IUser::class);
@ -340,7 +344,8 @@ class CapabilitiesTest extends TestCase {
$this->talkConfig,
$this->commentsManager,
$this->userSession,
$this->appManager
$this->appManager,
$this->translationManager,
);
$this->talkConfig->expects($this->once())
@ -360,7 +365,8 @@ class CapabilitiesTest extends TestCase {
$this->talkConfig,
$this->commentsManager,
$this->userSession,
$this->appManager
$this->appManager,
$this->translationManager,
);
$this->talkConfig->expects($this->once())
@ -377,4 +383,28 @@ class CapabilitiesTest extends TestCase {
[false],
];
}
public function testCapabilitiesTranslations(): void {
$capabilities = new Capabilities(
$this->serverConfig,
$this->talkConfig,
$this->commentsManager,
$this->userSession,
$this->appManager,
$this->translationManager,
);
$translations = [];
$translations[] = new LanguageTuple('de', 'de Label', 'en', 'en Label');
$translations[] = new LanguageTuple('de_DE', 'de_DE Label', 'en', 'en Label');
$this->translationManager->method('getLanguages')
->willReturn($translations);
$data = json_decode(json_encode($capabilities->getCapabilities(), JSON_THROW_ON_ERROR), true);
$this->assertEquals([
['from' => 'de', 'fromLabel' => 'de Label', 'to' => 'en', 'toLabel' => 'en Label'],
['from' => 'de_DE', 'fromLabel' => 'de_DE Label', 'to' => 'en', 'toLabel' => 'en Label'],
], $data['spreed']['config']['chat']['translations']);
}
}
Loading…
Cancel
Save