summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2016-06-18 22:04:56 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2016-10-31 15:55:40 +0100
commit3692769b0a0713fa9e3976692736da76707986c5 (patch)
tree7749b3fcc18a8027301635519794a44b9bdc683e
parent357a823457397d3e93ec8cd4dc01fb6859eb0049 (diff)
downloadnextcloud-server-3692769b0a0713fa9e3976692736da76707986c5.tar.gz
nextcloud-server-3692769b0a0713fa9e3976692736da76707986c5.zip
Add getShareTypesInFolder to optimize folder listening
Signed-off-by: Robin Appelman <icewind@owncloud.com>
-rw-r--r--apps/dav/lib/Connector/Sabre/SharesPlugin.php32
-rw-r--r--apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php19
-rw-r--r--apps/federatedfilesharing/lib/FederatedShareProvider.php5
-rw-r--r--lib/private/Share20/DefaultShareProvider.php43
-rw-r--r--lib/private/Share20/Manager.php10
-rw-r--r--lib/private/Share20/ProviderFactory.php4
-rw-r--r--lib/public/Share/IManager.php11
-rw-r--r--lib/public/Share/IProviderFactory.php6
-rw-r--r--lib/public/Share/IShareProvider.php11
-rw-r--r--tests/lib/Share20/ManagerTest.php9
10 files changed, 146 insertions, 4 deletions
diff --git a/apps/dav/lib/Connector/Sabre/SharesPlugin.php b/apps/dav/lib/Connector/Sabre/SharesPlugin.php
index cd58f050dda..ebb7fefff33 100644
--- a/apps/dav/lib/Connector/Sabre/SharesPlugin.php
+++ b/apps/dav/lib/Connector/Sabre/SharesPlugin.php
@@ -136,6 +136,30 @@ 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()] : [];
+ return array_map(function(IShare $share) {
+ return $share->getShareType();
+ }, $shares);
+ }, $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 +180,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..86204782d19 100644
--- a/apps/federatedfilesharing/lib/FederatedShareProvider.php
+++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php
@@ -563,6 +563,11 @@ class FederatedShareProvider implements IShareProvider {
return;
}
+
+ public function getSharesInFolder($userId, $node, $reshares) {
+ return [];//TODO
+ }
+
/**
* @inheritdoc
*/
diff --git a/lib/private/Share20/DefaultShareProvider.php b/lib/private/Share20/DefaultShareProvider.php
index 56b9d5b1ee8..2002e3bf8cb 100644
--- a/lib/private/Share20/DefaultShareProvider.php
+++ b/lib/private/Share20/DefaultShareProvider.php
@@ -454,6 +454,49 @@ class DefaultShareProvider implements IShareProvider {
return $share;
}
+ public function getSharesInFolder($userId, $node, $reshares) {
+ $qb = $this->dbConn->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'))
+ ));
+
+ $qb->andWhere($qb->expr()->orX(
+ $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
+ $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
+ ));
+
+ /**
+ * 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->createShare($data);
+ }
+ $cursor->closeCursor();
+
+ return $shares;
+ }
+
/**
* @inheritdoc
*/
diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php
index 22cf5a3f65a..f2f4acf8d00 100644
--- a/lib/private/Share20/Manager.php
+++ b/lib/private/Share20/Manager.php
@@ -34,6 +34,7 @@ use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountManager;
+use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IGroupManager;
@@ -48,6 +49,7 @@ use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
+use OCP\Share\IShareProvider;
/**
* This class is the communication hub for all sharing related operations.
@@ -881,6 +883,14 @@ class Manager implements IManager {
$provider->move($share, $recipientId);
}
+ public function getSharesInFolder($userId, Node $node, $reshares = false) {
+ $providers = $this->factory->getAllProviders();
+
+ return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) {
+ return $shares + $provider->getSharesInFolder($userId, $node, $reshares);
+ }, []);
+ }
+
/**
* @inheritdoc
*/
diff --git a/lib/private/Share20/ProviderFactory.php b/lib/private/Share20/ProviderFactory.php
index e3de10a8aee..5cdc9a51a22 100644
--- a/lib/private/Share20/ProviderFactory.php
+++ b/lib/private/Share20/ProviderFactory.php
@@ -163,4 +163,8 @@ class ProviderFactory implements IProviderFactory {
return $provider;
}
+
+ public function getAllProviders() {
+ return [$this->defaultShareProvider(), $this->federatedShareProvider()];
+ }
}
diff --git a/lib/public/Share/IManager.php b/lib/public/Share/IManager.php
index 37751911883..e8c69e06f9f 100644
--- a/lib/public/Share/IManager.php
+++ b/lib/public/Share/IManager.php
@@ -88,6 +88,17 @@ interface IManager {
public function moveShare(IShare $share, $recipientId);
/**
+ * Get all shares shared by (initiated) by the provided user in a folder.
+ *
+ * @param string $userId
+ * @param Node|null $node
+ * @param bool $reshares
+ * @return IShare[]
+ * @since 9.2.0
+ */
+ public function getSharesInFolder($userId, Node $node, $reshares = false);
+
+ /**
* Get shares shared by (initiated) by the provided user.
*
* @param string $userId
diff --git a/lib/public/Share/IProviderFactory.php b/lib/public/Share/IProviderFactory.php
index 8d9ea5bfdd0..928298a7860 100644
--- a/lib/public/Share/IProviderFactory.php
+++ b/lib/public/Share/IProviderFactory.php
@@ -55,4 +55,10 @@ interface IProviderFactory {
* @since 9.0.0
*/
public function getProviderForType($shareType);
+
+ /**
+ * @return IShareProvider[]
+ * @since 9.2.0
+ */
+ public function getAllProviders();
}
diff --git a/lib/public/Share/IShareProvider.php b/lib/public/Share/IShareProvider.php
index c4e116ac7fd..db444d36935 100644
--- a/lib/public/Share/IShareProvider.php
+++ b/lib/public/Share/IShareProvider.php
@@ -92,6 +92,17 @@ interface IShareProvider {
public function move(\OCP\Share\IShare $share, $recipient);
/**
+ * Get all shares by the given user in a folder
+ *
+ * @param string $userId
+ * @param Node|null $node
+ * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator
+ * @return \OCP\Share\IShare[]
+ * @since 9.2.0
+ */
+ public function getSharesInFolder($userId, $node, $reshares);
+
+ /**
* Get all shares by the given user
*
* @param string $userId
diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php
index 72ba4da6e5d..7ed6c6ca153 100644
--- a/tests/lib/Share20/ManagerTest.php
+++ b/tests/lib/Share20/ManagerTest.php
@@ -2564,4 +2564,13 @@ class DummyFactory implements IProviderFactory {
public function getProviderForType($shareType) {
return $this->provider;
}
+
+ /**
+ * @return IShareProvider[]
+ */
+ public function getAllProviders() {
+ return [$this->provider];
+ }
+
+
}