Skip to content

Commit

Permalink
Merge pull request #520 from nextcloud/clean-shares-18
Browse files Browse the repository at this point in the history
[nc18] clean deprecated shares
  • Loading branch information
ArtificialOwl authored Nov 12, 2020
2 parents 06ab327 + 123ef7c commit a72ddb7
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 62 deletions.
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Users won't be able to find this Circle using Nextcloud search engine.
</dependencies>

<background-jobs>
<job>OCA\Circles\Cron\Maintenance</job>
<job>OCA\Circles\Cron\ContactsExistingShares</job>
</background-jobs>

Expand Down
72 changes: 10 additions & 62 deletions lib/Command/Clean.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,41 @@

namespace OCA\Circles\Command;

use Exception;
use OC\Core\Command\Base;
use OCA\Circles\Db\CirclesRequest;
use OCA\Circles\Db\CoreRequestBuilder;
use OCA\Circles\Db\MembersRequest;
use OCA\Circles\Exceptions\CircleDoesNotExistException;
use OCA\Circles\Service\CleanService;
use OCP\IDBConnection;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class Clean extends Base {


/** @var IDBConnection */
private $dbConnection;

/** @var CirclesRequest */
private $circlesRequest;

/** @var MembersRequest */
private $membersRequest;
/** @var CleanService */
private $cleanService;


/**
* Clean constructor.
*
* @param IDBConnection $connection
* @param CirclesRequest $circlesRequest
* @param MembersRequest $membersRequest
* @param CleanService $cleanService
*/
public function __construct(
IDBConnection $connection, CirclesRequest $circlesRequest, MembersRequest $membersRequest
IDBConnection $connection, CirclesRequest $circlesRequest, CleanService $cleanService
) {
parent::__construct();
$this->dbConnection = $connection;
$this->circlesRequest = $circlesRequest;
$this->membersRequest = $membersRequest;

$this->cleanService = $cleanService;
}

protected function configure() {
Expand All @@ -73,60 +70,11 @@ protected function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output) {
$this->cleanService->clean();
$output->writeln('done');

try {
$this->fixUserType();
$this->removeCirclesWithNoOwner();
$this->removeMembersWithNoCircles();

$output->writeln('done');
} catch (Exception $e) {
$output->writeln($e->getMessage());
}
}


private function fixUserType() {
$qb = $this->dbConnection->getQueryBuilder();
$qb->update(CoreRequestBuilder::TABLE_MEMBERS)
->set('user_type', $qb->createNamedParameter(1))
->where(
$qb->expr()
->eq('user_type', $qb->createNamedParameter(0))
);

return $qb->execute();
}


private function removeCirclesWithNoOwner() {

$circles = $this->circlesRequest->forceGetCircles();

foreach ($circles as $circle) {
if ($circle->getOwner()
->getUserId() === null) {
$this->circlesRequest->destroyCircle($circle->getUniqueId());
}
}
return 0;
}


private function removeMembersWithNoCircles() {

$members = $this->membersRequest->forceGetAllMembers();

foreach ($members as $member) {
try {
$this->circlesRequest->forceGetCircle($member->getCircleId());

} catch (CircleDoesNotExistException $e) {
$this->membersRequest->removeMember($member);
}
}

}
}



74 changes: 74 additions & 0 deletions lib/Cron/Maintenance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php declare(strict_types=1);


/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2017
* @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\Circles\Cron;


use OC\BackgroundJob\TimedJob;
use OCA\Circles\AppInfo\Application;
use OCA\Circles\Exceptions\GSStatusException;
use OCA\Circles\Service\CirclesService;
use OCA\Circles\Service\CleanService;
use OCA\Circles\Service\GSUpstreamService;
use OCA\Circles\Service\MembersService;
use OCP\AppFramework\QueryException;


/**
* Class Maintenance
*
* @package OCA\Cicles\Cron
*/
class Maintenance extends TimedJob {


/**
* Cache constructor.
*/
public function __construct() {
$this->setInterval(10);
}


/**
* @param $argument
*
* @throws QueryException
*/
protected function run($argument) {
$app = \OC::$server->query(Application::class);
$c = $app->getContainer();

/** @var CleanService $cleanService */
$cleanService = \OC::$server->query(CleanService::class);
$cleanService->clean();
}

}

38 changes: 38 additions & 0 deletions lib/Db/SharesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ public function removeSharesFromMember(Member $member) {
}


/**
* @param string $circleId
*/
public function removeSharesToCircleId(string $circleId) {
$qb = $this->getSharesDeleteSql();
$expr = $qb->expr();

$andX = $expr->andX();
$andX->add($expr->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE)));
$andX->add($expr->eq('share_with', $qb->createNamedParameter($circleId)));
$qb->andWhere($andX);

$qb->execute();
}


/**
* @param string $circleId
*
Expand All @@ -79,5 +95,27 @@ public function getSharesForCircle(string $circleId) {
return $shares;
}


/**
* @return array
*/
public function getShares(): array {
$qb = $this->getSharesSelectSql();

$expr = $qb->expr();

$this->limitToShareType($qb, self::SHARE_TYPE);
$qb->andWhere($expr->isNull($this->default_select_alias . '.parent'));

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

return $shares;
}

}

Loading

0 comments on commit a72ddb7

Please sign in to comment.