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

Add a command to poll incoming federated shares for updates #34903

Merged
merged 3 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions apps/federatedfilesharing/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@
<personal>OCA\FederatedFileSharing\Panels\GeneralPersonalPanel</personal>
<personal>OCA\FederatedFileSharing\Panels\SharingPersonalPanel</personal>
</settings>
<commands>
<command>OCA\FederatedFileSharing\Command\PollIncomingShares</command>
</commands>
</info>
132 changes: 132 additions & 0 deletions apps/federatedfilesharing/lib/Command/PollIncomingShares.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* @author Viktar Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2019, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\FederatedFileSharing\Command;

use OC\ServerNotAvailableException;
use OCA\Files_Sharing\AppInfo\Application;
use OCP\Files\Storage\IStorage;
use OCP\Files\StorageInvalidException;
use OCP\Files\StorageNotAvailableException;
use OCP\IDBConnection;
use OCP\IUserManager;
use OCP\Lock\LockedException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class PollIncomingShares extends Command {
/** @var IDBConnection */
private $dbConnection;

/** @var IUserManager */
private $userManager;

/**
* PollIncomingShares constructor.
*
* @param IDBConnection $dbConnection
* @param IUserManager $userManager
*/
public function __construct(IDBConnection $dbConnection, IUserManager $userManager) {
parent::__construct();
$this->dbConnection = $dbConnection;
$this->userManager = $userManager;
}

protected function configure() {
$this->setName('incoming-shares:poll')
->setDescription('Poll incoming federated shares manually to detect updates');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int|null|void
*/
public function execute(InputInterface $input, OutputInterface $output) {
$mountProvider = $this->getExternalMountProvider();
$loader = $this->getLoader();
$cursor = $this->getCursor();
while ($data = $cursor->fetch()) {
$user = $this->userManager->get($data['user']);
$userMounts = $mountProvider->getMountsForUser($user, $loader);
/** @var \OCA\Files_Sharing\External\Mount $mount */
foreach ($userMounts as $mount) {
/** @var Storage $storage */
$storage = $mount->getStorage();
$this->refreshStorageRoot($storage);
}
}
$cursor->closeCursor();
}

/**
* @param IStorage $storage
*/
protected function refreshStorageRoot(IStorage $storage) {
try {
$localMtime = $storage->filemtime('');
/** @var \OCA\Files_Sharing\External\Storage $storage */
if ($storage->hasUpdated('', $localMtime)) {
try {
$storage->getScanner('')->scan('', false, 0);
} catch (LockedException $e) {
// it can be locked, let's skip it then
VicDeo marked this conversation as resolved.
Show resolved Hide resolved
} catch (ServerNotAvailableException $e) {
// remote server hasn't responded
}
}
} catch (StorageNotAvailableException $e) {
// pass
} catch (StorageInvalidException $e) {
// pass
}
}

/**
* @return \Doctrine\DBAL\Driver\Statement
*/
protected function getCursor() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to the FederatedShareProvider? It should be fine to inject the provider directly in order to use a public method that isn't in the interface.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My taste tells me that it should stay here.
Could be moved to the provider but it needs to be renamed into getUniqueRecipients and return them as an array of uids (and this array may be huuuge)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to leak DB stuff, you may return a generator instead of an array. As long as it's clear that the function returns a generator of uids I think it's fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current code is good enough

$qb = $this->dbConnection->getQueryBuilder();
$qb->selectDistinct('user')
->from('share_external')
->where($qb->expr()->eq('accepted', $qb->expr()->literal('1')));

return $qb->execute();
}

/**
* @return \OCP\Files\Storage\IStorageFactory
*/
protected function getLoader() {
return \OC\Files\Filesystem::getLoader();
VicDeo marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @return \OCA\Files_Sharing\External\MountProvider
*/
protected function getExternalMountProvider() {
$app = new Application();
VicDeo marked this conversation as resolved.
Show resolved Hide resolved
return $app->getContainer()->query('ExternalMountProvider');
}
}
79 changes: 79 additions & 0 deletions apps/federatedfilesharing/tests/Command/PollIncomingSharesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* @author Viktar Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2019, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\FederatedFileSharing\Tests\Command;

use Doctrine\DBAL\Driver\Statement;
use OCA\FederatedFileSharing\Tests\TestCase;
use OCA\FederatedFileSharing\Command\PollIncomingShares;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Tester\CommandTester;

/**
* Class PollIncomingSharesTest
*
* @group DB
* @package OCA\FederatedFileSharing\Tests\Command
*/
class PollIncomingSharesTest extends TestCase {
/** @var CommandTester */
private $commandTester;

/** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject */
private $dbConnection;

/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
private $userManager;

protected function setUp() {
parent::setUp();
$this->dbConnection = $this->createMock(IDBConnection::class);
$this->userManager = $this->createMock(IUserManager::class);
$command = new PollIncomingShares($this->dbConnection, $this->userManager);
$this->commandTester = new CommandTester($command);
}

public function testNoSharesPoll() {
$uid = 'foo';
$exprBuilder = $this->createMock(IExpressionBuilder::class);
$statementMock = $this->createMock(Statement::class);
$statementMock->method('fetch')->willReturnOnConsecutiveCalls(['user' => $uid], false);
$qbMock = $this->createMock(IQueryBuilder::class);
$qbMock->method('selectDistinct')->willReturnSelf();
$qbMock->method('from')->willReturnSelf();
$qbMock->method('where')->willReturnSelf();
$qbMock->method('expr')->willReturn($exprBuilder);
$qbMock->method('execute')->willReturn($statementMock);

$userMock = $this->createMock(IUser::class);
$this->userManager->expects($this->once())->method('get')
->with($uid)->willReturn($userMock);

$this->dbConnection->method('getQueryBuilder')->willReturn($qbMock);
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
$this->assertEmpty($output);
}
}