-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rooms): add option to automatically lock public and group rooms …
…if they are inactive Signed-off-by: Anna Larch <anna@nextcloud.com>
- Loading branch information
1 parent
df0efdb
commit f11783e
Showing
7 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Talk\BackgroundJob; | ||
|
||
use OCA\Talk\Config; | ||
use OCA\Talk\Room; | ||
use OCA\Talk\Service\RoomService; | ||
use OCA\Talk\Webinary; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\IJob; | ||
use OCP\BackgroundJob\TimedJob; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class LockInactiveRooms extends TimedJob { | ||
|
||
public function __construct( | ||
ITimeFactory $timeFactory, | ||
private RoomService $roomService, | ||
private Config $appConfig, | ||
private LoggerInterface $logger, | ||
) { | ||
parent::__construct($timeFactory); | ||
|
||
// Every hour | ||
$this->setInterval(60 * 60 * 24); | ||
$this->setTimeSensitivity(IJob::TIME_SENSITIVE); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function run($argument): void { | ||
$interval = $this->appConfig->getInactiveLockTime(); | ||
$forceLobby = $this->appConfig->enableLobbyOnLockedRooms(); | ||
if ($interval === 0) { | ||
return; | ||
} | ||
$timestamp = $this->time->getTime() - $interval * 60 * 60 * 24; | ||
$time = $this->time->getDateTime('@' . $timestamp); | ||
$rooms = $this->roomService->getInactiveRooms($time); | ||
array_map(function (Room $room) use ($forceLobby) { | ||
$this->roomService->setReadOnly($room, Room::READ_ONLY); | ||
$this->logger->debug("Locking room {$room->getId()} due to inactivity"); | ||
if ($forceLobby) { | ||
$this->roomService->setLobby($room, Webinary::LOBBY_NON_MODERATORS, $this->time->getDateTime()); | ||
$this->logger->debug("Enabling lobby for room {$room->getId()}"); | ||
} | ||
}, $rooms); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Talk\Tests\BackgroundJob; | ||
|
||
use OCA\Talk\BackgroundJob\LockInactiveRooms; | ||
use OCA\Talk\Config; | ||
use OCA\Talk\Room; | ||
use OCA\Talk\Service\RoomService; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Psr\Log\LoggerInterface; | ||
use Test\TestCase; | ||
|
||
class LockInactiveRoomsTest extends TestCase { | ||
protected ITimeFactory&MockObject $timeFactory; | ||
protected RoomService&MockObject $roomService; | ||
private Config&MockObject $appConfig; | ||
protected LoggerInterface&MockObject $logger; | ||
private LockInactiveRooms $job; | ||
|
||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->timeFactory = $this->createMock(ITimeFactory::class); | ||
$this->roomService = $this->createMock(RoomService::class); | ||
$this->appConfig = $this->createMock(Config::class); | ||
$this->logger = $this->createMock(LoggerInterface::class); | ||
$this->job = new LockInactiveRooms($this->timeFactory, | ||
$this->roomService, | ||
$this->appConfig, | ||
$this->logger | ||
); | ||
} | ||
|
||
public function testNotEnabled(): void { | ||
$this->appConfig->expects(self::once()) | ||
->method('getInactiveLockTime') | ||
->willReturn(0); | ||
$this->appConfig->expects(self::once()) | ||
->method('enableLobbyOnLockedRooms') | ||
->willReturn(false); | ||
$this->timeFactory->expects(self::never()) | ||
->method(self::anything()); | ||
$this->roomService->expects(self::never()) | ||
->method(self::anything()); | ||
$this->logger->expects(self::never()) | ||
->method(self::anything()); | ||
|
||
$this->job->run('t'); | ||
} | ||
|
||
public function testNoRooms(): void { | ||
$this->appConfig->expects(self::once()) | ||
->method('getInactiveLockTime') | ||
->willReturn(123); | ||
$this->appConfig->expects(self::once()) | ||
->method('enableLobbyOnLockedRooms') | ||
->willReturn(false); | ||
$this->timeFactory->expects(self::once()) | ||
->method('getTime'); | ||
$this->timeFactory->expects(self::once()) | ||
->method('getDateTime'); | ||
$this->roomService->expects(self::once()) | ||
->method('getInactiveRooms') | ||
->willReturn([]); | ||
$this->roomService->expects(self::never()) | ||
->method('setReadOnly'); | ||
$this->roomService->expects(self::never()) | ||
->method('setLobby'); | ||
$this->logger->expects(self::never()) | ||
->method(self::anything()); | ||
|
||
$this->job->run('t'); | ||
|
||
} | ||
|
||
public function testLockRooms(): void { | ||
$rooms = [ | ||
$this->createConfiguredMock(Room::class, [ | ||
'getReadOnly' => 0, | ||
'getType' => Room::TYPE_PUBLIC, | ||
]), | ||
$this->createConfiguredMock(Room::class, [ | ||
'getReadOnly' => 0, | ||
'getType' => Room::TYPE_GROUP, | ||
]), | ||
]; | ||
|
||
$this->appConfig->expects(self::once()) | ||
->method('getInactiveLockTime') | ||
->willReturn(123); | ||
$this->appConfig->expects(self::once()) | ||
->method('enableLobbyOnLockedRooms') | ||
->willReturn(false); | ||
$this->timeFactory->expects(self::once()) | ||
->method('getTime'); | ||
$this->timeFactory->expects(self::once()) | ||
->method('getDateTime'); | ||
$this->roomService->expects(self::once()) | ||
->method('getInactiveRooms') | ||
->willReturn($rooms); | ||
$this->roomService->expects(self::exactly(2)) | ||
->method('setReadOnly'); | ||
$this->roomService->expects(self::never()) | ||
->method('setLobby'); | ||
$this->logger->expects(self::exactly(2)) | ||
->method('debug'); | ||
|
||
$this->job->run('t'); | ||
|
||
} | ||
|
||
public function testLockRoomsAndEnableLobby(): void { | ||
$rooms = [ | ||
$this->createConfiguredMock(Room::class, [ | ||
'getReadOnly' => 0, | ||
'getType' => Room::TYPE_PUBLIC, | ||
]), | ||
$this->createConfiguredMock(Room::class, [ | ||
'getReadOnly' => 0, | ||
'getType' => Room::TYPE_GROUP, | ||
]), | ||
]; | ||
|
||
$this->appConfig->expects(self::once()) | ||
->method('getInactiveLockTime') | ||
->willReturn(123); | ||
$this->appConfig->expects(self::once()) | ||
->method('enableLobbyOnLockedRooms') | ||
->willReturn(true); | ||
$this->timeFactory->expects(self::once()) | ||
->method('getTime'); | ||
$this->timeFactory->expects(self::any()) | ||
->method('getDateTime'); | ||
$this->roomService->expects(self::once()) | ||
->method('getInactiveRooms') | ||
->willReturn($rooms); | ||
$this->roomService->expects(self::exactly(2)) | ||
->method('setReadOnly'); | ||
$this->roomService->expects(self::exactly(2)) | ||
->method('setLobby'); | ||
$this->logger->expects(self::exactly(4)) | ||
->method('debug'); | ||
|
||
$this->job->run('t'); | ||
} | ||
} |