Skip to content

Commit 607c897

Browse files
luka-nextcloudbackportbot[bot]
authored andcommitted
fix: clean attachment sharing records after permanent deleted
Signed-off-by: Luka Trovic <[email protected]>
1 parent bac6841 commit 607c897

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

lib/Cron/DeleteCron.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCA\Deck\Db\StackMapper;
1414
use OCA\Deck\InvalidAttachmentType;
1515
use OCA\Deck\Service\AttachmentService;
16+
use OCA\Deck\Sharing\DeckShareProvider;
1617
use OCP\AppFramework\Utility\ITimeFactory;
1718
use OCP\BackgroundJob\IJob;
1819
use OCP\BackgroundJob\TimedJob;
@@ -29,14 +30,25 @@ class DeleteCron extends TimedJob {
2930
private $attachmentMapper;
3031
/** @var StackMapper */
3132
private $stackMapper;
33+
/** @var DeckShareProvider */
34+
private $deckShareProvider;
3235

33-
public function __construct(ITimeFactory $time, BoardMapper $boardMapper, CardMapper $cardMapper, AttachmentService $attachmentService, AttachmentMapper $attachmentMapper, StackMapper $stackMapper) {
36+
public function __construct(
37+
ITimeFactory $time,
38+
BoardMapper $boardMapper,
39+
CardMapper $cardMapper,
40+
AttachmentService $attachmentService,
41+
AttachmentMapper $attachmentMapper,
42+
StackMapper $stackMapper,
43+
DeckShareProvider $deckShareProvider,
44+
) {
3445
parent::__construct($time);
3546
$this->boardMapper = $boardMapper;
3647
$this->cardMapper = $cardMapper;
3748
$this->attachmentService = $attachmentService;
3849
$this->attachmentMapper = $attachmentMapper;
3950
$this->stackMapper = $stackMapper;
51+
$this->deckShareProvider = $deckShareProvider;
4052

4153
$this->setInterval(60 * 60 * 24);
4254
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
@@ -69,6 +81,12 @@ protected function run($argument) {
6981
$this->attachmentMapper->delete($attachment);
7082
}
7183

84+
// Delete orphaned attachment shares
85+
$shares = $this->deckShareProvider->getOrphanedAttachmentShares();
86+
foreach ($shares as $share) {
87+
$this->deckShareProvider->delete($share);
88+
}
89+
7290
$stacks = $this->stackMapper->findToDelete();
7391
foreach ($stacks as $stack) {
7492
$this->stackMapper->delete($stack);

lib/Db/CardMapper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,4 +644,16 @@ public function remapCardOwner(int $boardId, string $userId, string $newUserId):
644644

645645
$result->closeCursor();
646646
}
647+
648+
public function getAllCardIds(): array {
649+
$qb = $this->db->getQueryBuilder();
650+
$qb->select('id')
651+
->from('deck_cards');
652+
$result = $qb->executeQuery();
653+
$ids = [];
654+
while ($row = $result->fetch()) {
655+
$ids[] = (int)$row['id'];
656+
}
657+
return $ids;
658+
}
647659
}

lib/Sharing/DeckShareProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,4 +1045,21 @@ public function getAllShares(): iterable {
10451045
}
10461046
$cursor->closeCursor();
10471047
}
1048+
1049+
public function getOrphanedAttachmentShares(): array {
1050+
$allCardIds = $this->cardMapper->getAllCardIds();
1051+
$qb = $this->dbConnection->getQueryBuilder();
1052+
$qb->select('*')
1053+
->from('share', 's')
1054+
->where($qb->expr()->eq('s.share_type', $qb->createNamedParameter(IShare::TYPE_DECK)))
1055+
->andWhere($qb->expr()->notIn('s.share_with', $qb->createNamedParameter($allCardIds, IQueryBuilder::PARAM_STR_ARRAY)));
1056+
1057+
$cursor = $qb->execute();
1058+
$shares = [];
1059+
while ($data = $cursor->fetch()) {
1060+
$shares[] = $this->createShareObject($data);
1061+
}
1062+
1063+
return $shares;
1064+
}
10481065
}

tests/unit/Cron/DeleteCronTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCA\Deck\InvalidAttachmentType;
3535
use OCA\Deck\Service\AttachmentService;
3636
use OCA\Deck\Service\IAttachmentService;
37+
use OCA\Deck\Sharing\DeckShareProvider;
3738
use OCP\AppFramework\Utility\ITimeFactory;
3839
use PHPUnit\Framework\MockObject\MockObject;
3940
use Test\TestCase;
@@ -52,6 +53,8 @@ class DeleteCronTest extends TestCase {
5253
private $attachmentMapper;
5354
/** @var StackMapper|MockObject */
5455
private $stackMapper;
56+
/** @var DeckShareProvider */
57+
private $deckShareProvider;
5558
/** @var DeleteCron */
5659
protected $deleteCron;
5760

@@ -63,7 +66,16 @@ public function setUp(): void {
6366
$this->attachmentService = $this->createMock(AttachmentService::class);
6467
$this->attachmentMapper = $this->createMock(AttachmentMapper::class);
6568
$this->stackMapper = $this->createMock(StackMapper::class);
66-
$this->deleteCron = new DeleteCron($this->timeFactory, $this->boardMapper, $this->cardMapper, $this->attachmentService, $this->attachmentMapper, $this->stackMapper);
69+
$this->deckShareProvider = $this->createMock(DeckShareProvider::class);
70+
$this->deleteCron = new DeleteCron(
71+
$this->timeFactory,
72+
$this->boardMapper,
73+
$this->cardMapper,
74+
$this->attachmentService,
75+
$this->attachmentMapper,
76+
$this->stackMapper,
77+
$this->deckShareProvider,
78+
);
6779
}
6880

6981
protected function getBoard($id) {

0 commit comments

Comments
 (0)