Browse Source

Merge pull request #9329 from nextcloud/feat/9270/video-background-capabilities

feat(virtual-background): Add capabilities for predefined virtual bac…
pull/9334/head
Joas Schilling 3 years ago
committed by GitHub
parent
commit
989eed795d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      docs/capabilities.md
  2. 46
      lib/Capabilities.php
  3. 46
      tests/php/CapabilitiesTest.php

2
docs/capabilities.md

@ -116,3 +116,5 @@
## 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.
* `config => call => predefined-backgrounds` - List of predefined virtual backgrounds. The files are in Talks img/ folder, accessible via the normal image path methods. The list is cached for 5 minutes.
* `config => call => can-upload-background` - Boolean flag whether the user can upload a custom virtual background (requires an account and non-zero quota). Uploads should be done to Talk/Backgrounds/ (respecting the user's attachment directory setting).

46
lib/Capabilities.php

@ -29,12 +29,16 @@ use OCA\Talk\Chat\ChatManager;
use OCP\App\IAppManager;
use OCP\Capabilities\IPublicCapability;
use OCP\Comments\ICommentsManager;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Translation\ITranslationManager;
use OCP\Util;
class Capabilities implements IPublicCapability {
protected ICache $talkCache;
public function __construct(
protected IConfig $serverConfig,
@ -43,7 +47,9 @@ class Capabilities implements IPublicCapability {
protected IUserSession $userSession,
protected IAppManager $appManager,
protected ITranslationManager $translationManager,
ICacheFactory $cacheFactory,
) {
$this->talkCache = $cacheFactory->createLocal('talk::');
}
public function getCapabilities(): array {
@ -163,6 +169,46 @@ class Capabilities implements IPublicCapability {
$capabilities['features'][] = 'chat-reference-id';
}
$predefinedBackgrounds = $this->talkCache->get('predefined_backgrounds');
if ($predefinedBackgrounds !== null) {
// Try using cached value
$predefinedBackgrounds = json_decode($predefinedBackgrounds, true);
}
if (!is_array($predefinedBackgrounds)) {
// Cache was empty or invalid, regenerate
$predefinedBackgrounds = [];
if (file_exists(__DIR__ . '/../img/backgrounds')) {
$directoryIterator = new \DirectoryIterator(__DIR__ . '/../img/backgrounds');
foreach ($directoryIterator as $file) {
if (!$file->isFile()) {
continue;
}
if ($file->isDot()) {
continue;
}
if ($file->getFilename() === 'COPYING') {
continue;
}
$predefinedBackgrounds[] = $file->getFilename();
}
sort($predefinedBackgrounds);
}
$this->talkCache->set('predefined_backgrounds', json_encode($predefinedBackgrounds), 300);
}
$capabilities['config']['call']['predefined-backgrounds'] = $predefinedBackgrounds;
if ($user instanceof IUser) {
$quota = $user->getQuota();
if ($quota !== 'none') {
$quota = Util::computerFileSize($quota);
}
$capabilities['config']['call']['can-upload-background'] = $quota === 'none' || $quota > 0;
} else {
$capabilities['config']['call']['can-upload-background'] = false;
}
return [
'spreed' => $capabilities,
];

46
tests/php/CapabilitiesTest.php

@ -32,6 +32,8 @@ use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCP\App\IAppManager;
use OCP\Capabilities\IPublicCapability;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
@ -47,6 +49,8 @@ class CapabilitiesTest extends TestCase {
protected IUserSession|MockObject $userSession;
protected IAppManager|MockObject $appManager;
protected ITranslationManager|MockObject $translationManager;
protected ICacheFactory|MockObject $cacheFactory;
protected ICache|MockObject $talkCache;
protected ?array $baseFeatures = null;
public function setUp(): void {
@ -57,6 +61,12 @@ class CapabilitiesTest extends TestCase {
$this->userSession = $this->createMock(IUserSession::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->translationManager = $this->createMock(ITranslationManager::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->talkCache = $this->createMock(ICache::class);
$this->cacheFactory->method('createLocal')
->with('talk::')
->willReturn($this->talkCache);
$this->commentsManager->expects($this->any())
->method('supportReactions')
@ -139,6 +149,7 @@ class CapabilitiesTest extends TestCase {
$this->userSession,
$this->appManager,
$this->translationManager,
$this->cacheFactory,
);
$this->userSession->expects($this->once())
@ -174,6 +185,15 @@ class CapabilitiesTest extends TestCase {
'enabled' => true,
'breakout-rooms' => false,
'recording' => false,
'predefined-backgrounds' => [
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg',
'5.jpg',
'6.jpg',
],
'can-upload-background' => false,
],
'chat' => [
'max-length' => 32000,
@ -197,8 +217,9 @@ class CapabilitiesTest extends TestCase {
public function dataGetCapabilitiesUserAllowed(): array {
return [
[true, false, Participant::PRIVACY_PRIVATE],
[false, true, Participant::PRIVACY_PUBLIC],
[true, false, 'none', true, Participant::PRIVACY_PRIVATE],
[false, true, '1 MB', true, Participant::PRIVACY_PUBLIC],
[false, true, '0 B', false, Participant::PRIVACY_PUBLIC],
];
}
@ -206,9 +227,11 @@ class CapabilitiesTest extends TestCase {
* @dataProvider dataGetCapabilitiesUserAllowed
* @param bool $isNotAllowed
* @param bool $canCreate
* @param string $quota
* @param bool $canUpload
* @param int $readPrivacy
*/
public function testGetCapabilitiesUserAllowed(bool $isNotAllowed, bool $canCreate, int $readPrivacy): void {
public function testGetCapabilitiesUserAllowed(bool $isNotAllowed, bool $canCreate, string $quota, bool $canUpload, int $readPrivacy): void {
$capabilities = new Capabilities(
$this->serverConfig,
$this->talkConfig,
@ -216,6 +239,7 @@ class CapabilitiesTest extends TestCase {
$this->userSession,
$this->appManager,
$this->translationManager,
$this->cacheFactory,
);
$user = $this->createMock(IUser::class);
@ -250,6 +274,9 @@ class CapabilitiesTest extends TestCase {
->with('uid')
->willReturn($readPrivacy);
$user->method('getQuota')
->willReturn($quota);
$this->serverConfig->expects($this->any())
->method('getAppValue')
->willReturnMap([
@ -278,6 +305,15 @@ class CapabilitiesTest extends TestCase {
'enabled' => false,
'breakout-rooms' => true,
'recording' => false,
'predefined-backgrounds' => [
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg',
'5.jpg',
'6.jpg',
],
'can-upload-background' => $canUpload,
],
'chat' => [
'max-length' => 32000,
@ -322,6 +358,7 @@ class CapabilitiesTest extends TestCase {
$this->userSession,
$this->appManager,
$this->translationManager,
$this->cacheFactory,
);
$user = $this->createMock(IUser::class);
@ -346,6 +383,7 @@ class CapabilitiesTest extends TestCase {
$this->userSession,
$this->appManager,
$this->translationManager,
$this->cacheFactory,
);
$this->talkConfig->expects($this->once())
@ -367,6 +405,7 @@ class CapabilitiesTest extends TestCase {
$this->userSession,
$this->appManager,
$this->translationManager,
$this->cacheFactory,
);
$this->talkConfig->expects($this->once())
@ -392,6 +431,7 @@ class CapabilitiesTest extends TestCase {
$this->userSession,
$this->appManager,
$this->translationManager,
$this->cacheFactory,
);
$translations = [];

Loading…
Cancel
Save