diff options
author | Lukas Reschke <lukas@statuscode.ch> | 2016-11-01 18:44:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-01 18:44:00 +0100 |
commit | ef467c3195731cb1e4fff504b39c06b017deb5ec (patch) | |
tree | 4c74374891448f9243cc0fa7ead2c6e7419ffbf0 /apps | |
parent | 20f45e6fe4626e64df8383d8250acd59c3b290c5 (diff) | |
parent | 7e2159e9bb1769455f56099c13f864edfb624ac1 (diff) | |
download | nextcloud-server-ef467c3195731cb1e4fff504b39c06b017deb5ec.tar.gz nextcloud-server-ef467c3195731cb1e4fff504b39c06b017deb5ec.zip |
Merge pull request #339 from nextcloud/share-types-by-folder
Add getShareTypesInFolder to optimize folder listening
Diffstat (limited to 'apps')
4 files changed, 138 insertions, 4 deletions
diff --git a/apps/dav/lib/Connector/Sabre/SharesPlugin.php b/apps/dav/lib/Connector/Sabre/SharesPlugin.php index cd58f050dda..56d76e66184 100644 --- a/apps/dav/lib/Connector/Sabre/SharesPlugin.php +++ b/apps/dav/lib/Connector/Sabre/SharesPlugin.php @@ -136,6 +136,33 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin { return $shareTypes; } + private function getSharesTypesInFolder(\OCP\Files\Folder $node) { + $shares = $this->shareManager->getSharesInFolder( + $this->userId, + $node, + false + ); + + $children = $node->getDirectoryListing(); + + $values = array_map(function (\OCP\Files\Node $node) use ($shares) { + /** @var IShare[] $shares */ + $shares = (isset($shares[$node->getId()])) ? $shares[$node->getId()] : []; + $types = array_map(function(IShare $share) { + return $share->getShareType(); + }, $shares); + $types = array_unique($types); + sort($types); + return $types; + }, $children); + + $keys = array_map(function (\OCP\Files\Node $node) { + return $node->getId(); + }, $children); + + return array_combine($keys, $values); + } + /** * Adds shares to propfind response * @@ -156,15 +183,15 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin { && !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) ) { $folderNode = $this->userFolder->get($sabreNode->getPath()); - $children = $folderNode->getDirectoryListing(); + $childShares = $this->getSharesTypesInFolder($folderNode); $this->cachedShareTypes[$folderNode->getId()] = $this->getShareTypes($folderNode); - foreach ($children as $childNode) { - $this->cachedShareTypes[$childNode->getId()] = $this->getShareTypes($childNode); + foreach ($childShares as $id => $shares) { + $this->cachedShareTypes[$id] = $shares; } } - $propFind->handle(self::SHARETYPES_PROPERTYNAME, function() use ($sabreNode) { + $propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) { if (isset($this->cachedShareTypes[$sabreNode->getId()])) { $shareTypes = $this->cachedShareTypes[$sabreNode->getId()]; } else { diff --git a/apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php index 433ef4ba63a..2e17c7d0b38 100644 --- a/apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php @@ -206,6 +206,14 @@ class SharesPluginTest extends \Test\TestCase { ->method('get') ->with('/subdir') ->will($this->returnValue($node)); + + $dummyShares = array_map(function($type) { + $share = $this->getMock('\OCP\Share\IShare'); + $share->expects($this->any()) + ->method('getShareType') + ->will($this->returnValue($type)); + return $share; + }, $shareTypes); $this->shareManager->expects($this->any()) ->method('getSharesBy') @@ -224,6 +232,17 @@ class SharesPluginTest extends \Test\TestCase { return []; })); + $this->shareManager->expects($this->any()) + ->method('getSharesInFolder') + ->with( + $this->equalTo('user1'), + $this->anything(), + $this->equalTo(false) + ) + ->will($this->returnCallback(function ($userId, $node, $flag) use ($shareTypes, $dummyShares) { + return [111 => $dummyShares]; + })); + // simulate sabre recursive PROPFIND traversal $propFindRoot = new \Sabre\DAV\PropFind( '/subdir', diff --git a/apps/federatedfilesharing/lib/FederatedShareProvider.php b/apps/federatedfilesharing/lib/FederatedShareProvider.php index c567b92c3e4..270bf86daf7 100644 --- a/apps/federatedfilesharing/lib/FederatedShareProvider.php +++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php @@ -27,6 +27,7 @@ namespace OCA\FederatedFileSharing; use OC\Share20\Share; +use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\IConfig; use OCP\IL10N; @@ -563,6 +564,48 @@ class FederatedShareProvider implements IShareProvider { return; } + + public function getSharesInFolder($userId, Folder $node, $reshares) { + $qb = $this->dbConnection->getQueryBuilder(); + $qb->select('*') + ->from('share', 's') + ->andWhere($qb->expr()->orX( + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) + )) + ->andWhere( + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE)) + ); + + /** + * Reshares for this user are shares where they are the owner. + */ + if ($reshares === false) { + $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); + } else { + $qb->andWhere( + $qb->expr()->orX( + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) + ) + ); + } + + $qb->innerJoin('s', 'filecache' ,'f', 's.file_source = f.fileid'); + $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); + + $qb->orderBy('id'); + + $cursor = $qb->execute(); + $shares = []; + while ($data = $cursor->fetch()) { + $shares[$data['fileid']][] = $this->createShareObject($data); + } + $cursor->closeCursor(); + + return $shares; + } + /** * @inheritdoc */ diff --git a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php index e4c234fd038..92f6ac5e996 100644 --- a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php +++ b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php @@ -702,4 +702,49 @@ class FederatedShareProviderTest extends \Test\TestCase { ['no', false] ]; } + + public function testGetSharesInFolder() { + $userManager = \OC::$server->getUserManager(); + $rootFolder = \OC::$server->getRootFolder(); + + $u1 = $userManager->createUser('testFed', 'test'); + $u2 = $userManager->createUser('testFed2', 'test'); + + $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo'); + $file1 = $folder1->newFile('bar1'); + $file2 = $folder1->newFile('bar2'); + + $this->tokenHandler->method('generateToken')->willReturn('token'); + $this->notifications + ->method('sendRemoteShare') + ->willReturn(true); + + $share1 = $this->shareManager->newShare(); + $share1->setSharedWith('user@server.com') + ->setSharedBy($u1->getUID()) + ->setShareOwner($u1->getUID()) + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1); + $this->provider->create($share1); + + $share2 = $this->shareManager->newShare(); + $share2->setSharedWith('user@server.com') + ->setSharedBy($u2->getUID()) + ->setShareOwner($u1->getUID()) + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file2); + $this->provider->create($share2); + + $result = $this->provider->getSharesInFolder($u1->getUID(), $folder1, false); + $this->assertCount(1, $result); + $this->assertCount(1, $result[$file1->getId()]); + + $result = $this->provider->getSharesInFolder($u1->getUID(), $folder1, true); + $this->assertCount(2, $result); + $this->assertCount(1, $result[$file1->getId()]); + $this->assertCount(1, $result[$file2->getId()]); + + $u1->delete(); + $u2->delete(); + } } |