diff --git a/docs/capabilities.md b/docs/capabilities.md index d2c12bc495..a75ea8b30a 100644 --- a/docs/capabilities.md +++ b/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. diff --git a/lib/Capabilities.php b/lib/Capabilities.php index 5693d66acb..0afde79879 100644 --- a/lib/Capabilities.php +++ b/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) diff --git a/tests/php/CapabilitiesTest.php b/tests/php/CapabilitiesTest.php index cdd618ed9b..e264d34522 100644 --- a/tests/php/CapabilitiesTest.php +++ b/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']); + } }