-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?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\External\MountProvider; | ||
use OCP\Files\Storage\IStorage; | ||
use OCP\Files\Storage\IStorageFactory; | ||
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; | ||
|
||
/** @var IStorageFactory */ | ||
private $loader; | ||
|
||
/** @var MountProvider | null */ | ||
private $externalMountProvider; | ||
|
||
/** | ||
* PollIncomingShares constructor. | ||
* | ||
* @param IDBConnection $dbConnection | ||
* @param IUserManager $userManager | ||
* @param MountProvider $externalMountProvider | ||
* @param IStorageFactory $loader | ||
*/ | ||
public function __construct(IDBConnection $dbConnection, IUserManager $userManager, IStorageFactory $loader, MountProvider $externalMountProvider = null) { | ||
parent::__construct(); | ||
$this->dbConnection = $dbConnection; | ||
$this->userManager = $userManager; | ||
$this->loader = $loader; | ||
$this->externalMountProvider = $externalMountProvider; | ||
} | ||
|
||
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) { | ||
if ($this->externalMountProvider === null) { | ||
$output->writeln("Polling is not possible when files_sharing app is disabled. Please enable it with 'occ app:enable files_sharing'"); | ||
return 1; | ||
} | ||
$cursor = $this->getCursor(); | ||
while ($data = $cursor->fetch()) { | ||
$user = $this->userManager->get($data['user']); | ||
$userMounts = $this->externalMountProvider->getMountsForUser($user, $this->loader); | ||
/** @var \OCA\Files_Sharing\External\Mount $mount */ | ||
foreach ($userMounts as $mount) { | ||
try { | ||
/** @var Storage $storage */ | ||
$storage = $mount->getStorage(); | ||
$this->refreshStorageRoot($storage); | ||
} catch (\Exception $e) { | ||
$output->writeln($e->getMessage()); | ||
} | ||
} | ||
} | ||
$cursor->closeCursor(); | ||
} | ||
|
||
/** | ||
* @param IStorage $storage | ||
* | ||
* @throws LockedException | ||
* @throws ServerNotAvailableException | ||
* @throws StorageInvalidException | ||
* @throws StorageNotAvailableException | ||
*/ | ||
protected function refreshStorageRoot(IStorage $storage) { | ||
$localMtime = $storage->filemtime(''); | ||
/** @var \OCA\Files_Sharing\External\Storage $storage */ | ||
if ($storage->hasUpdated('', $localMtime)) { | ||
$storage->getScanner('')->scan('', false, 0); | ||
} | ||
} | ||
|
||
/** | ||
* @return \Doctrine\DBAL\Driver\Statement | ||
*/ | ||
protected function getCursor() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My taste tells me that it should stay here. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?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 OCA\Files_Sharing\External\MountProvider; | ||
use OCP\DB\QueryBuilder\IExpressionBuilder; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\Files\Storage\IStorageFactory; | ||
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; | ||
|
||
/** @var MountProvider | \PHPUnit_Framework_MockObject_MockObject */ | ||
private $externalMountProvider; | ||
|
||
/** @var IStorageFactory | \PHPUnit_Framework_MockObject_MockObject */ | ||
private $loader; | ||
|
||
protected function setUp() { | ||
parent::setUp(); | ||
$this->dbConnection = $this->createMock(IDBConnection::class); | ||
$this->userManager = $this->createMock(IUserManager::class); | ||
$this->externalMountProvider = $this->createMock(MountProvider::class); | ||
$this->loader = $this->createMock(IStorageFactory::class); | ||
$command = new PollIncomingShares($this->dbConnection, $this->userManager, $this->loader, $this->externalMountProvider); | ||
$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->externalMountProvider->expects($this->once())->method('getMountsForUser') | ||
->willReturn([]); | ||
|
||
$this->dbConnection->method('getQueryBuilder')->willReturn($qbMock); | ||
$this->commandTester->execute([]); | ||
$output = $this->commandTester->getDisplay(); | ||
$this->assertEmpty($output); | ||
} | ||
|
||
public function testWithFilesSharingDisabled() { | ||
$command = new PollIncomingShares($this->dbConnection, $this->userManager, $this->loader, null); | ||
$this->commandTester = new CommandTester($command); | ||
$this->commandTester->execute([]); | ||
$output = $this->commandTester->getDisplay(); | ||
$this->assertContains("Polling is not possible when files_sharing app is disabled. Please enable it with 'occ app:enable files_sharing'", $output); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Problem here, is exceptions never get logged and since this command is designed probably to run in cron, will go unnoticed. Would suggest to log as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomneedham at least some of them are logged by the dav app:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's likely logged on a higher level already ?