Skip to content

Commit

Permalink
remove child shares
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Apr 6, 2022
1 parent 8772511 commit 3b5789a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
49 changes: 47 additions & 2 deletions lib/Db/ShareWrapperRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,57 @@ public function delete(int $shareId): void {
* @param string $initiator
*/
public function deleteSharesToCircle(string $circleId, string $initiator = ''): void {
$qb = $this->getShareDeleteSql();
$qb->andWhere($qb->exprLimit('share_with', $circleId));
$qb = $this->getShareSelectSql();
$qb->limit('share_with', $circleId);
if ($initiator !== '') {
$qb->limit('uid_initiator', $initiator);
}

$ids = array_map(
function (ShareWrapper $share): string {
return $share->getId();
},
$this->getItemsFromRequest($qb)
);

$this->deleteSharesAndChild($ids);
}


public function removeOrphanShares(): void {
$qb = $this->getShareSelectSql();
$expr = $qb->expr();
$qb->leftJoin(
CoreQueryBuilder::SHARE, CoreRequestBuilder::TABLE_SHARE, 'p',
$expr->andX($expr->eq('p.id', CoreQueryBuilder::SHARE . '.parent'))
);

$qb->filterNull('parent');
$qb->limitNull('id', false, 'p');

$ids = [];
$cursor = $qb->execute();
while ($data = $cursor->fetch()) {
$ids[] = $data['id'];
}
$cursor->closeCursor();

$this->deleteSharesAndChild($ids);
}


/**
* @param array $ids
*/
private function deleteSharesAndChild(array $ids): void {
$qb = $this->getShareDeleteSql();
$qb->andWhere(
$qb->expr()->orX(
$qb->exprLimitInArray('id', $ids),
$qb->exprLimitInArray('parent', $ids)
)
);

$qb->execute();
}
}
2 changes: 1 addition & 1 deletion lib/Listeners/Files/DestroyingCircle.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ public function handle(Event $event): void {
}

$circle = $event->getCircle();
$this->shareWrapperService->deleteSharesToCircle($circle->getSingleId(), '', true);
$this->shareWrapperService->deleteAllSharesToCircle($circle->getSingleId());
}
}
2 changes: 1 addition & 1 deletion lib/Listeners/Files/MembershipsRemoved.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function handle(Event $event): void {
$federatedUser = $this->circlesManager->getFederatedUser($membership->getSingleId());
if ($federatedUser->getUserType() === Member::TYPE_USER
&& $federatedUser->isLocal()) {
$this->shareWrapperService->deleteSharesToCircle(
$this->shareWrapperService->deleteUserSharesToCircle(
$membership->getCircleId(),
$federatedUser->getUserId()
);
Expand Down
17 changes: 16 additions & 1 deletion lib/Service/MaintenanceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ private function runMaintenance5(): void {
// } catch (Exception $e) {
// }

try {
// Can be removed in NC27.
$this->output('Remove orphan shares');
$this->removeOrphanShares();
} catch (Exception $e) {
}

try {
// Can be removed in NC27.
$this->output('fix sub-circle display name');
Expand Down Expand Up @@ -331,6 +338,14 @@ private function removeMembersWithNoCircles(): void {
}


private function removeOrphanShares(): void {
$this->shareWrapperRequest->removeOrphanShares();
}


/**
* @throws RequestBuilderException
*/
private function removeDeprecatedShares(): void {
$probe = new CircleProbe();
$probe->includePersonalCircles()
Expand All @@ -353,7 +368,7 @@ function (ShareWrapper $share) {

foreach ($shares as $share) {
if (!in_array($share, $circles)) {
$this->shareWrapperService->deleteSharesToCircle($share, '', true);
$this->shareWrapperService->deleteAllSharesToCircle($share);
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions lib/Service/ShareWrapperService.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,25 @@ public function delete(ShareWrapper $shareWrapper): void {
*
* @throws Exception
*/
public function deleteSharesToCircle(string $circleId, string $initiator = '', bool $force = false): void {
if ($initiator === '' && !$force) {
throw new Exception('if initiator is empty, you need to set $force as true');
public function deleteUserSharesToCircle(string $circleId, string $initiator): void {
if ($initiator === '') {
throw new Exception('$initiator cannot be empty');
}

$this->cache->clear('');
$this->shareWrapperRequest->deleteSharesToCircle($circleId, $initiator);
}


/**
* @param string $circleId
*/
public function deleteAllSharesToCircle(string $circleId): void {
$this->cache->clear('');
$this->shareWrapperRequest->deleteSharesToCircle($circleId, '');
}


/**
* @param string $circleId
* @param FederatedUser|null $shareRecipient
Expand Down

0 comments on commit 3b5789a

Please sign in to comment.