fix: clean attachment sharing records after permanent deleted
Signed-off-by: Luka Trovic <luka@nextcloud.com>
This commit is contained in:
@@ -14,6 +14,7 @@ use OCA\Deck\Db\CardMapper;
|
|||||||
use OCA\Deck\Db\StackMapper;
|
use OCA\Deck\Db\StackMapper;
|
||||||
use OCA\Deck\InvalidAttachmentType;
|
use OCA\Deck\InvalidAttachmentType;
|
||||||
use OCA\Deck\Service\AttachmentService;
|
use OCA\Deck\Service\AttachmentService;
|
||||||
|
use OCA\Deck\Sharing\DeckShareProvider;
|
||||||
use OCP\AppFramework\Utility\ITimeFactory;
|
use OCP\AppFramework\Utility\ITimeFactory;
|
||||||
use OCP\BackgroundJob\IJob;
|
use OCP\BackgroundJob\IJob;
|
||||||
use OCP\BackgroundJob\TimedJob;
|
use OCP\BackgroundJob\TimedJob;
|
||||||
@@ -30,14 +31,25 @@ class DeleteCron extends TimedJob {
|
|||||||
private $attachmentMapper;
|
private $attachmentMapper;
|
||||||
/** @var StackMapper */
|
/** @var StackMapper */
|
||||||
private $stackMapper;
|
private $stackMapper;
|
||||||
|
/** @var DeckShareProvider */
|
||||||
|
private $deckShareProvider;
|
||||||
|
|
||||||
public function __construct(ITimeFactory $time, BoardMapper $boardMapper, CardMapper $cardMapper, AttachmentService $attachmentService, AttachmentMapper $attachmentMapper, StackMapper $stackMapper) {
|
public function __construct(
|
||||||
|
ITimeFactory $time,
|
||||||
|
BoardMapper $boardMapper,
|
||||||
|
CardMapper $cardMapper,
|
||||||
|
AttachmentService $attachmentService,
|
||||||
|
AttachmentMapper $attachmentMapper,
|
||||||
|
StackMapper $stackMapper,
|
||||||
|
DeckShareProvider $deckShareProvider,
|
||||||
|
) {
|
||||||
parent::__construct($time);
|
parent::__construct($time);
|
||||||
$this->boardMapper = $boardMapper;
|
$this->boardMapper = $boardMapper;
|
||||||
$this->cardMapper = $cardMapper;
|
$this->cardMapper = $cardMapper;
|
||||||
$this->attachmentService = $attachmentService;
|
$this->attachmentService = $attachmentService;
|
||||||
$this->attachmentMapper = $attachmentMapper;
|
$this->attachmentMapper = $attachmentMapper;
|
||||||
$this->stackMapper = $stackMapper;
|
$this->stackMapper = $stackMapper;
|
||||||
|
$this->deckShareProvider = $deckShareProvider;
|
||||||
|
|
||||||
$this->setInterval(60 * 60 * 24);
|
$this->setInterval(60 * 60 * 24);
|
||||||
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
|
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
|
||||||
@@ -70,6 +82,12 @@ class DeleteCron extends TimedJob {
|
|||||||
$this->attachmentMapper->delete($attachment);
|
$this->attachmentMapper->delete($attachment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete orphaned attachment shares
|
||||||
|
$shares = $this->deckShareProvider->getOrphanedAttachmentShares();
|
||||||
|
foreach ($shares as $share) {
|
||||||
|
$this->deckShareProvider->delete($share);
|
||||||
|
}
|
||||||
|
|
||||||
$stacks = $this->stackMapper->findToDelete();
|
$stacks = $this->stackMapper->findToDelete();
|
||||||
foreach ($stacks as $stack) {
|
foreach ($stacks as $stack) {
|
||||||
$this->stackMapper->delete($stack);
|
$this->stackMapper->delete($stack);
|
||||||
|
|||||||
@@ -645,4 +645,16 @@ class CardMapper extends QBMapper implements IPermissionMapper {
|
|||||||
|
|
||||||
$result->closeCursor();
|
$result->closeCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAllCardIds(): array {
|
||||||
|
$qb = $this->db->getQueryBuilder();
|
||||||
|
$qb->select('id')
|
||||||
|
->from('deck_cards');
|
||||||
|
$result = $qb->executeQuery();
|
||||||
|
$ids = [];
|
||||||
|
while ($row = $result->fetch()) {
|
||||||
|
$ids[] = (int)$row['id'];
|
||||||
|
}
|
||||||
|
return $ids;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1046,4 +1046,21 @@ class DeckShareProvider implements \OCP\Share\IShareProvider {
|
|||||||
}
|
}
|
||||||
$cursor->closeCursor();
|
$cursor->closeCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getOrphanedAttachmentShares(): array {
|
||||||
|
$allCardIds = $this->cardMapper->getAllCardIds();
|
||||||
|
$qb = $this->dbConnection->getQueryBuilder();
|
||||||
|
$qb->select('*')
|
||||||
|
->from('share', 's')
|
||||||
|
->where($qb->expr()->eq('s.share_type', $qb->createNamedParameter(IShare::TYPE_DECK)))
|
||||||
|
->andWhere($qb->expr()->notIn('s.share_with', $qb->createNamedParameter($allCardIds, IQueryBuilder::PARAM_STR_ARRAY)));
|
||||||
|
|
||||||
|
$cursor = $qb->execute();
|
||||||
|
$shares = [];
|
||||||
|
while ($data = $cursor->fetch()) {
|
||||||
|
$shares[] = $this->createShareObject($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $shares;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ use OCA\Deck\Db\StackMapper;
|
|||||||
use OCA\Deck\InvalidAttachmentType;
|
use OCA\Deck\InvalidAttachmentType;
|
||||||
use OCA\Deck\Service\AttachmentService;
|
use OCA\Deck\Service\AttachmentService;
|
||||||
use OCA\Deck\Service\IAttachmentService;
|
use OCA\Deck\Service\IAttachmentService;
|
||||||
|
use OCA\Deck\Sharing\DeckShareProvider;
|
||||||
use OCP\AppFramework\Utility\ITimeFactory;
|
use OCP\AppFramework\Utility\ITimeFactory;
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use Test\TestCase;
|
use Test\TestCase;
|
||||||
@@ -53,6 +54,8 @@ class DeleteCronTest extends TestCase {
|
|||||||
private $attachmentMapper;
|
private $attachmentMapper;
|
||||||
/** @var StackMapper|MockObject */
|
/** @var StackMapper|MockObject */
|
||||||
private $stackMapper;
|
private $stackMapper;
|
||||||
|
/** @var DeckShareProvider */
|
||||||
|
private $deckShareProvider;
|
||||||
/** @var DeleteCron */
|
/** @var DeleteCron */
|
||||||
protected $deleteCron;
|
protected $deleteCron;
|
||||||
|
|
||||||
@@ -64,7 +67,16 @@ class DeleteCronTest extends TestCase {
|
|||||||
$this->attachmentService = $this->createMock(AttachmentService::class);
|
$this->attachmentService = $this->createMock(AttachmentService::class);
|
||||||
$this->attachmentMapper = $this->createMock(AttachmentMapper::class);
|
$this->attachmentMapper = $this->createMock(AttachmentMapper::class);
|
||||||
$this->stackMapper = $this->createMock(StackMapper::class);
|
$this->stackMapper = $this->createMock(StackMapper::class);
|
||||||
$this->deleteCron = new DeleteCron($this->timeFactory, $this->boardMapper, $this->cardMapper, $this->attachmentService, $this->attachmentMapper, $this->stackMapper);
|
$this->deckShareProvider = $this->createMock(DeckShareProvider::class);
|
||||||
|
$this->deleteCron = new DeleteCron(
|
||||||
|
$this->timeFactory,
|
||||||
|
$this->boardMapper,
|
||||||
|
$this->cardMapper,
|
||||||
|
$this->attachmentService,
|
||||||
|
$this->attachmentMapper,
|
||||||
|
$this->stackMapper,
|
||||||
|
$this->deckShareProvider,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getBoard($id) {
|
protected function getBoard($id) {
|
||||||
|
|||||||
Reference in New Issue
Block a user