Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove child shares #1000

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
ArtificialOwl marked this conversation as resolved.
Show resolved Hide resolved
*/
private function deleteSharesAndChild(array $ids): void {
$qb = $this->getShareDeleteSql();
$qb->andWhere(
$qb->expr()->orX(
$qb->exprLimitInArray('id', $ids),
ArtificialOwl marked this conversation as resolved.
Show resolved Hide resolved
$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