diff --git a/lib/Service/CalendarInitialStateService.php b/lib/Service/CalendarInitialStateService.php index 82c725fbec..836861f620 100644 --- a/lib/Service/CalendarInitialStateService.php +++ b/lib/Service/CalendarInitialStateService.php @@ -18,6 +18,7 @@ use OCP\IConfig; use OCP\IGroupManager; use OCP\IUserManager; +use OCP\Talk\IBroker; use function in_array; class CalendarInitialStateService { @@ -36,6 +37,7 @@ public function __construct( private ?IQueue $queue, private IGroupManager $groupManager, private IUserManager $userManager, + private IBroker $talkBroker, ) { } @@ -151,34 +153,10 @@ private function getView(string $view): string { } private function isTalkEnabledForUser(): bool { - $userId = $this->userId; - if ($userId === null) { - return false; - } - - $talkEnabled = $this->appManager->isEnabledForUser('spreed'); - $user = $this->userManager->get($userId); - - if ($user === null) { - return false; - } - - $userGroups = $userGroups = $this->groupManager->getUserGroupIds($user); - - - //groups allowed to start a conversation - $startConversation = $this->config->getAppValue('spreed', 'start_conversations', '[]'); - $startConversation = json_decode($startConversation, true); - - $canStartConversation = !empty(array_intersect($startConversation, $userGroups)); - - //groups allowed to use talk - $allowedGroups = $this->config->getAppValue('spreed', 'allowed_groups', '[]'); - $allowedGroups = json_decode($allowedGroups, true); - - $canUseTalk = !empty(array_intersect($allowedGroups, $userGroups)); + $canStartConversation= !$this->talkBroker->isDisabledForUser(); + $canUseTalk= !$this->talkBroker->isNotAllowedToCreateConversations(); - return $talkEnabled && $canStartConversation && $canUseTalk; + return $canStartConversation && $canUseTalk; } diff --git a/tests/php/unit/Service/CalendarInitialStateServiceTest.php b/tests/php/unit/Service/CalendarInitialStateServiceTest.php index 99dcc4fd57..d2f3647117 100755 --- a/tests/php/unit/Service/CalendarInitialStateServiceTest.php +++ b/tests/php/unit/Service/CalendarInitialStateServiceTest.php @@ -22,6 +22,7 @@ use OCP\IConfig; use OCP\IGroupManager; use OCP\IUserManager; +use OCP\Talk\IBroker; use PHPUnit\Framework\MockObject\MockObject; class CalendarInitialStateServiceTest extends TestCase { @@ -59,6 +60,7 @@ class CalendarInitialStateServiceTest extends TestCase { private IGroupManager&MockObject $groupManager; private IUserManager&MockObject $userManager; + private IBroker&MockObject $talkBroker; protected function setUp(): void { $this->appName = 'calendar'; @@ -77,6 +79,7 @@ protected function setUp(): void { } $this->groupManager = $this->createMock(IGroupManager::class); $this->userManager = $this->createMock(IUserManager::class); + $this->talkBroker = $this->createMock(IBroker::class); } public function testRun(): void { @@ -94,6 +97,7 @@ public function testRun(): void { $this->queue, $this->groupManager, $this->userManager, + $this->talkBroker, ); $this->config->expects(self::exactly(17)) ->method('getAppValue') @@ -137,10 +141,9 @@ public function testRun(): void { ->willReturnMap([ ['dav', 'enableCalendarFederation', true, false, true], ]); - $this->appManager->expects(self::exactly(3)) + $this->appManager->expects(self::exactly(2)) ->method('isEnabledForUser') ->willReturnMap([ - ['spreed', null, true], ['tasks', null, true], ['circles', null, false], ]); @@ -150,6 +153,12 @@ public function testRun(): void { ['spreed', true, '12.0.0'], ['circles', true, '22.0.0'], ]); + $this->talkBroker->expects(self::once()) + ->method('isDisabledForUser') + ->willReturn(false); + $this->talkBroker->expects(self::once()) + ->method('isNotAllowedToCreateConversations') + ->willReturn(false); $this->appointmentContfigService->expects(self::once()) ->method('getAllAppointmentConfigurations') ->with($this->userId) @@ -209,6 +218,7 @@ public function testRunAnonymously(): void { $this->queue, $this->groupManager, $this->userManager, + $this->talkBroker, ); $this->config->expects(self::exactly(17)) ->method('getAppValue') @@ -265,6 +275,12 @@ public function testRunAnonymously(): void { ['spreed', true, '12.0.0'], ['circles', true, '22.0.0'], ]); + $this->talkBroker->expects(self::once()) + ->method('isDisabledForUser') + ->willReturn(true); + $this->talkBroker->expects(self::once()) + ->method('isNotAllowedToCreateConversations') + ->willReturn(true); $this->resourceManager->expects(self::once()) ->method('getBackends') ->willReturn([]); @@ -326,6 +342,7 @@ public function testIndexViewFix(string $savedView, string $expectedView): void $this->queue, $this->groupManager, $this->userManager, + $this->talkBroker, ); $this->config->expects(self::exactly(17)) ->method('getAppValue') @@ -369,10 +386,9 @@ public function testIndexViewFix(string $savedView, string $expectedView): void ->willReturnMap([ ['dav', 'enableCalendarFederation', true, false, true], ]); - $this->appManager->expects(self::exactly(3)) + $this->appManager->expects(self::exactly(2)) ->method('isEnabledForUser') ->willReturnMap([ - ['spreed', null, false], ['tasks', null, false], ['circles', null, false], ]); @@ -382,6 +398,12 @@ public function testIndexViewFix(string $savedView, string $expectedView): void ['spreed', true, '11.3.0'], ['circles', true, '22.0.0'], ]); + $this->talkBroker->expects(self::once()) + ->method('isDisabledForUser') + ->willReturn(true); + $this->talkBroker->expects(self::once()) + ->method('isNotAllowedToCreateConversations') + ->willReturn(true); $this->appointmentContfigService->expects(self::once()) ->method('getAllAppointmentConfigurations') ->with($this->userId)