Skip to content
Merged
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
Prev Previous commit
Next Next commit
Unify share fetching
Signed-off-by: Roeland Jago Douma <[email protected]>
  • Loading branch information
rullzer committed Jul 30, 2019
commit 21477bca775987d898735485eeb4fb6424eb04e6
126 changes: 27 additions & 99 deletions apps/dav/lib/Connector/Sabre/SharesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
private $userFolder;

/** @var IShare[] */
private $cachedShares;

/**
* @var IShare[]
*/
private $cachedShareTypes;
private $cachedShares = [];

private $cachedFolders = [];

Expand All @@ -94,7 +89,6 @@ public function __construct(
$this->shareManager = $shareManager;
$this->userFolder = $userFolder;
$this->userId = $userSession->getUser()->getUID();
$this->cachedShareTypes = [];
}

/**
Expand All @@ -117,61 +111,6 @@ public function initialize(\Sabre\DAV\Server $server) {
$this->server->on('propFind', array($this, 'handleGetProperties'));
}

/**
* Return a list of share types for outgoing shares
*
* @param \OCP\Files\Node $node file node
*
* @return int[] array of share types
*/
private function getShareTypes(\OCP\Files\Node $node) {
$shareTypes = [];
$requestedShareTypes = [
\OCP\Share::SHARE_TYPE_USER,
\OCP\Share::SHARE_TYPE_GROUP,
\OCP\Share::SHARE_TYPE_LINK,
\OCP\Share::SHARE_TYPE_REMOTE,
\OCP\Share::SHARE_TYPE_EMAIL,
\OCP\Share::SHARE_TYPE_ROOM,
\OCP\Share::SHARE_TYPE_CIRCLE,
];
foreach ($requestedShareTypes as $requestedShareType) {
// one of each type is enough to find out about the types
$shares = $this->shareManager->getSharesBy(
$this->userId,
$requestedShareType,
$node,
false,
1
);
if (!empty($shares)) {
$shareTypes[] = $requestedShareType;
}
}
return $shareTypes;
}

private function getSharesTypesInFolder(\OCP\Files\Folder $node) {
$shares = $this->shareManager->getSharesInFolder(
$this->userId,
$node,
true
);

$shareTypesByFileId = [];

foreach($shares as $fileId => $sharesForFile) {
$types = array_map(function(IShare $share) {
return $share->getShareType();
}, $sharesForFile);
$types = array_unique($types);
sort($types);
$shareTypesByFileId[$fileId] = $types;
}

return $shareTypesByFileId;
}

private function getShare(\OCP\Files\Node $node): array {
$result = [];
$requestedShareTypes = [
Expand Down Expand Up @@ -207,6 +146,26 @@ private function getSharesFolder(\OCP\Files\Folder $node): array {
);
}

private function getShares(\Sabre\DAV\INode $sabreNode): array {
if (isset($this->cachedShares[$sabreNode->getId()])) {
$shares = $this->cachedShares[$sabreNode->getId()];
} else {
list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath());
if ($parentPath === '') {
$parentPath = '/';
}
// if we already cached the folder this file is in we know there are no shares for this file
if (array_search($parentPath, $this->cachedFolders) === false) {
$node = $this->userFolder->get($sabreNode->getPath());
$shares = $this->getShare($node);
} else {
return [];
}
}

return $shares;
}

/**
* Adds shares to propfind response
*
Expand All @@ -231,55 +190,24 @@ public function handleGetProperties(
) {
$folderNode = $this->userFolder->get($sabreNode->getPath());

$childShares = $this->getSharesTypesInFolder($folderNode);
$this->cachedFolders[] = $sabreNode->getPath();
$this->cachedShareTypes[$folderNode->getId()] = $this->getShareTypes($folderNode);
foreach ($childShares as $id => $shares) {
$this->cachedShareTypes[$id] = $shares;
}

$childShares = $this->getSharesFolder($folderNode);
foreach ($childShares as $id => $shares) {
$this->cachedShares[$id] = $shares;
}
}

$propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) {
if (isset($this->cachedShareTypes[$sabreNode->getId()])) {
$shareTypes = $this->cachedShareTypes[$sabreNode->getId()];
} else {
list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath());
if ($parentPath === '') {
$parentPath = '/';
}
// if we already cached the folder this file is in we know there are no shares for this file
if (array_search($parentPath, $this->cachedFolders) === false) {
$node = $this->userFolder->get($sabreNode->getPath());
$shareTypes = $this->getShareTypes($node);
} else {
return [];
}
}
$shares = $this->getShares($sabreNode);

$shareTypes = array_unique(array_map(function(IShare $share) {
return $share->getShareType();
}, $shares));

return new ShareTypeList($shareTypes);
});

$propFind->handle(self::SHAREES_PROPERTYNAME, function() use ($sabreNode) {
if (isset($this->cachedShares[$sabreNode->getId()])) {
$shares = $this->cachedShares[$sabreNode->getId()];
} else {
list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath());
if ($parentPath === '') {
$parentPath = '/';
}
// if we already cached the folder this file is in we know there are no shares for this file
if (array_search($parentPath, $this->cachedFolders) === false) {
$node = $this->userFolder->get($sabreNode->getPath());
$shares = $this->getShare($node);
} else {
return [];
}
}
$shares = $this->getShares($sabreNode);

return new ShareeList($shares);
});
Expand Down