Browse Source

Merge pull request #6308 from nextcloud/feature/1328/job-to-remove-file-room-of-removed-file

Implement remove file room of removed file
pull/6334/head
Joas Schilling 4 years ago
committed by GitHub
parent
commit
c15391c0d7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 51
      lib/BackgroundJob/RemoveEmptyRooms.php
  2. 174
      tests/php/BackgroundJob/RemoveEmptyRoomsTest.php

51
lib/BackgroundJob/RemoveEmptyRooms.php

@ -23,12 +23,12 @@ declare(strict_types=1);
namespace OCA\Talk\BackgroundJob;
use OCA\Talk\Federation\FederationManager;
use OCA\Talk\Service\ParticipantService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use OCP\Files\Config\IUserMountCache;
use Psr\Log\LoggerInterface;
/**
@ -47,15 +47,16 @@ class RemoveEmptyRooms extends TimedJob {
/** @var LoggerInterface */
protected $logger;
/** @var FederationManager */
protected $federationManager;
/** @var IUserMountCache */
protected $userMountCache;
protected $numDeletedRooms = 0;
public function __construct(ITimeFactory $timeFactory,
Manager $manager,
ParticipantService $participantService,
LoggerInterface $logger) {
LoggerInterface $logger,
IUserMountCache $userMountCache) {
parent::__construct($timeFactory);
// Every 5 minutes
@ -64,6 +65,7 @@ class RemoveEmptyRooms extends TimedJob {
$this->manager = $manager;
$this->participantService = $participantService;
$this->logger = $logger;
$this->userMountCache = $userMountCache;
}
protected function run($argument): void {
@ -77,13 +79,46 @@ class RemoveEmptyRooms extends TimedJob {
}
public function callback(Room $room): void {
if ($room->getType() === Room::CHANGELOG_CONVERSATION) {
if ($room->getType() === Room::TYPE_CHANGELOG) {
return;
}
if ($this->participantService->getNumberOfActors($room) === 0 && $room->getObjectType() !== 'file' && $this->federationManager->getNumberOfInvitations($room) === 0) {
$room->deleteRoom();
$this->numDeletedRooms++;
if ($this->deleteIfIsEmpty($room)) {
return;
}
$this->deleteIfFileIsRemoved($room);
}
private function deleteIfIsEmpty(Room $room): bool {
if ($room->getObjectType() === 'file') {
return false;
}
if ($this->participantService->getNumberOfActors($room) !== 0) {
return false;
}
$this->doDeleteRoom($room);
return true;
}
private function deleteIfFileIsRemoved(Room $room): bool {
if ($room->getObjectType() !== 'file') {
return false;
}
$mountsForFile = $this->userMountCache->getMountsForFileId($room->getObjectId());
if (!empty($mountsForFile)) {
return false;
}
$this->doDeleteRoom($room);
return true;
}
private function doDeleteRoom(Room $room): void {
$room->deleteRoom();
$this->numDeletedRooms++;
}
}

174
tests/php/BackgroundJob/RemoveEmptyRoomsTest.php

@ -0,0 +1,174 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Tests\BackgroundJob;
use OCA\Talk\BackgroundJob\RemoveEmptyRooms;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\Config\IUserMountCache;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class RemoveEmptyRoomsTest extends TestCase {
/** @var ITimeFactory|MockObject */
protected $timeFactory;
/** @var Manager|MockObject */
protected $manager;
/** @var ParticipantService|MockObject */
protected $participantService;
/** @var LoggerInterface|MockObject */
protected $loggerInterface;
/** @var IUserMountCache|MockObject */
protected $userMountCache;
public function setUp(): void {
parent::setUp();
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->manager = $this->createMock(Manager::class);
$this->participantService = $this->createMock(ParticipantService::class);
$this->loggerInterface = $this->createMock(LoggerInterface::class);
$this->userMountCache = $this->createMock(IUserMountCache::class);
}
public function getBackgroundJob(): RemoveEmptyRooms {
return new RemoveEmptyRooms(
$this->timeFactory,
$this->manager,
$this->participantService,
$this->loggerInterface,
$this->userMountCache
);
}
public function testDoDeleteRoom(): void {
$backgroundJob = $this->getBackgroundJob();
$room = $this->createMock(Room::class);
$room->method('getType')
->willReturn(Room::TYPE_GROUP);
$numDeletedRooms = $this->invokePrivate($backgroundJob, 'numDeletedRooms');
$this->assertEquals(0, $numDeletedRooms, 'Invalid default quantity of rooms');
$this->invokePrivate($backgroundJob, 'doDeleteRoom', [$room]);
$numDeletedRooms = $this->invokePrivate($backgroundJob, 'numDeletedRooms');
$this->assertEquals(1, $numDeletedRooms, 'Invalid final quantity of rooms');
}
/**
* @dataProvider dataDeleteIfFileIsRemoved
*/
public function testDeleteIfFileIsRemoved(string $objectType, array $fileList, int $numDeletedRoomsExpected): void {
$backgroundJob = $this->getBackgroundJob();
$numDeletedRoomsActual = $this->invokePrivate($backgroundJob, 'numDeletedRooms');
$this->assertEquals(0, $numDeletedRoomsActual, 'Invalid default quantity of rooms');
$room = $this->createMock(Room::class);
$room->method('getType')
->willReturn(Room::TYPE_GROUP);
$room->method('getObjectType')
->willReturn($objectType);
$userMountCache = $this->invokePrivate($backgroundJob, 'userMountCache');
$userMountCache->method('getMountsForFileId')
->willReturn($fileList);
$this->invokePrivate($backgroundJob, 'deleteIfFileIsRemoved', [$room]);
$numDeletedRoomsActual = $this->invokePrivate($backgroundJob, 'numDeletedRooms');
$this->assertEquals($numDeletedRoomsExpected, $numDeletedRoomsActual, 'Invalid final quantity of rooms');
}
public function dataDeleteIfFileIsRemoved(): array {
return [
['', [], 0],
['email', [], 0],
['file', ['fileExists'], 0],
['file', [], 1],
];
}
/**
* @dataProvider dataDeleteIfIsEmpty
*/
public function testDeleteIfIsEmpty(string $objectType, int $actorsCount, int $numDeletedRoomsExpected): void {
$backgroundJob = $this->getBackgroundJob();
$numDeletedRoomsActual = $this->invokePrivate($backgroundJob, 'numDeletedRooms');
$this->assertEquals(0, $numDeletedRoomsActual, 'Invalid default quantity of rooms');
$room = $this->createMock(Room::class);
$room->method('getType')
->willReturn(Room::TYPE_GROUP);
$room->method('getObjectType')
->willReturn($objectType);
$participantService = $this->invokePrivate($backgroundJob, 'participantService');
$participantService->method('getNumberOfActors')
->willReturn($actorsCount);
$this->invokePrivate($backgroundJob, 'deleteIfIsEmpty', [$room]);
$numDeletedRoomsActual = $this->invokePrivate($backgroundJob, 'numDeletedRooms');
$this->assertEquals($numDeletedRoomsExpected, $numDeletedRoomsActual, 'Invalid final quantity of rooms');
}
public function dataDeleteIfIsEmpty(): array {
return [
['', 1, 0],
['file', 1, 0],
['email', 1, 0],
['email', 0, 1]
];
}
/**
* @dataProvider dataCallback
*/
public function testCallback(int $roomType, string $objectType, int $numDeletedRoomsExpected): void {
$backgroundJob = $this->getBackgroundJob();
$room = $this->createMock(Room::class);
$room->method('getType')
->willReturn($roomType);
$room->method('getObjectType')
->willReturn($objectType);
$backgroundJob->callback($room);
$numDeletedRoomsActual = $this->invokePrivate($backgroundJob, 'numDeletedRooms');
$this->assertEquals($numDeletedRoomsExpected, $numDeletedRoomsActual, 'Invalid final quantity of rooms');
}
public function dataCallback(): array {
return [
[Room::TYPE_CHANGELOG, '', 0],
[Room::TYPE_GROUP, 'file', 1],
];
}
}
Loading…
Cancel
Save