From 3c77311397e49282cef6e77a4f0c52bd67522edc Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 16 Mar 2016 11:03:26 +0100 Subject: [PATCH] Display share status info in favorite list Returns the shareTypes share status info to the favorites file list. --- apps/files/appinfo/application.php | 4 +- apps/files/controller/apicontroller.php | 54 +++++++++++++++++- apps/files/service/tagservice.php | 13 ++--- .../tests/controller/apicontrollertest.php | 56 ++++++++++++++++++- 4 files changed, 112 insertions(+), 15 deletions(-) diff --git a/apps/files/appinfo/application.php b/apps/files/appinfo/application.php index bc6dc9fb9e..593e0533c8 100644 --- a/apps/files/appinfo/application.php +++ b/apps/files/appinfo/application.php @@ -40,8 +40,10 @@ class Application extends App { return new ApiController( $c->query('AppName'), $c->query('Request'), + $server->getUserSession(), $c->query('TagService'), - $server->getPreviewManager() + $server->getPreviewManager(), + $server->getShareManager() ); }); diff --git a/apps/files/controller/apicontroller.php b/apps/files/controller/apicontroller.php index c96d92a046..43abf2ff45 100644 --- a/apps/files/controller/apicontroller.php +++ b/apps/files/controller/apicontroller.php @@ -34,6 +34,10 @@ use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataDisplayResponse; use OCA\Files\Service\TagService; use OCP\IPreview; +use OCP\Share\IManager; +use OCP\Files\FileInfo; +use OCP\Files\Node; +use OCP\IUserSession; /** * Class ApiController @@ -43,8 +47,12 @@ use OCP\IPreview; class ApiController extends Controller { /** @var TagService */ private $tagService; + /** @var IManager **/ + private $shareManager; /** @var IPreview */ private $previewManager; + /** IUserSession */ + private $userSession; /** * @param string $appName @@ -54,11 +62,15 @@ class ApiController extends Controller { */ public function __construct($appName, IRequest $request, + IUserSession $userSession, TagService $tagService, - IPreview $previewManager){ + IPreview $previewManager, + IManager $shareManager) { parent::__construct($appName, $request); + $this->userSession = $userSession; $this->tagService = $tagService; $this->previewManager = $previewManager; + $this->shareManager = $shareManager; } /** @@ -132,8 +144,10 @@ class ApiController extends Controller { */ public function getFilesByTag($tagName) { $files = array(); - $fileInfos = $this->tagService->getFilesByTag($tagName); - foreach ($fileInfos as &$fileInfo) { + $nodes = $this->tagService->getFilesByTag($tagName); + foreach ($nodes as &$node) { + $shareTypes = $this->getShareTypes($node); + $fileInfo = $node->getFileInfo(); $file = \OCA\Files\Helper::formatFileInfo($fileInfo); $parts = explode('/', dirname($fileInfo->getPath()), 4); if(isset($parts[3])) { @@ -142,9 +156,43 @@ class ApiController extends Controller { $file['path'] = '/'; } $file['tags'] = [$tagName]; + if (!empty($shareTypes)) { + $file['shareTypes'] = $shareTypes; + } $files[] = $file; } return new DataResponse(['files' => $files]); } + /** + * Return a list of share types for outgoing shares + * + * @param Node $node file node + * + * @return int[] array of share types + */ + private function getShareTypes(Node $node) { + $userId = $this->userSession->getUser()->getUID(); + $shareTypes = []; + $requestedShareTypes = [ + \OCP\Share::SHARE_TYPE_USER, + \OCP\Share::SHARE_TYPE_GROUP, + \OCP\Share::SHARE_TYPE_LINK + ]; + foreach ($requestedShareTypes as $requestedShareType) { + // one of each type is enough to find out about the types + $shares = $this->shareManager->getSharesBy( + $userId, + $requestedShareType, + $node, + false, + 1 + ); + if (!empty($shares)) { + $shareTypes[] = $requestedShareType; + } + } + return $shareTypes; + } + } diff --git a/apps/files/service/tagservice.php b/apps/files/service/tagservice.php index e1425c4661..57cad43a53 100644 --- a/apps/files/service/tagservice.php +++ b/apps/files/service/tagservice.php @@ -25,6 +25,7 @@ namespace OCA\Files\Service; use OC\Files\FileInfo; +use OCP\Files\Node; /** * Service class to manage tags on files. @@ -93,7 +94,7 @@ class TagService { * Get all files for the given tag * * @param string $tagName tag name to filter by - * @return FileInfo[] list of matching files + * @return Node[] list of matching files * @throws \Exception if the tag does not exist */ public function getFilesByTag($tagName) { @@ -103,15 +104,11 @@ class TagService { return []; } - $fileInfos = []; + $allNodes = []; foreach ($fileIds as $fileId) { - $nodes = $this->homeFolder->getById((int) $fileId); - foreach ($nodes as $node) { - /** @var \OC\Files\Node\Node $node */ - $fileInfos[] = $node->getFileInfo(); - } + $allNodes = array_merge($allNodes, $this->homeFolder->getById((int) $fileId)); } - return $fileInfos; + return $allNodes; } } diff --git a/apps/files/tests/controller/apicontrollertest.php b/apps/files/tests/controller/apicontrollertest.php index 6fb8340ead..a9b248a36f 100644 --- a/apps/files/tests/controller/apicontrollertest.php +++ b/apps/files/tests/controller/apicontrollertest.php @@ -51,14 +51,27 @@ class ApiControllerTest extends TestCase { private $preview; /** @var ApiController */ private $apiController; + /** @var \OCP\Share\IManager */ + private $shareManager; public function setUp() { $this->request = $this->getMockBuilder('\OCP\IRequest') ->disableOriginalConstructor() ->getMock(); + $user = $this->getMock('\OCP\IUser'); + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('user1')); + $userSession = $this->getMock('\OCP\IUserSession'); + $userSession->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($user)); $this->tagService = $this->getMockBuilder('\OCA\Files\Service\TagService') ->disableOriginalConstructor() ->getMock(); + $this->shareManager = $this->getMockBuilder('\OCP\Share\IManager') + ->disableOriginalConstructor() + ->getMock(); $this->preview = $this->getMockBuilder('\OCP\IPreview') ->disableOriginalConstructor() ->getMock(); @@ -66,8 +79,10 @@ class ApiControllerTest extends TestCase { $this->apiController = new ApiController( $this->appName, $this->request, + $userSession, $this->tagService, - $this->preview + $this->preview, + $this->shareManager ); } @@ -101,10 +116,32 @@ class ApiControllerTest extends TestCase { ->disableOriginalConstructor() ->getMock() ); + $node = $this->getMockBuilder('\OC\Files\Node\File') + ->disableOriginalConstructor() + ->getMock(); + $node->expects($this->once()) + ->method('getFileInfo') + ->will($this->returnValue($fileInfo)); $this->tagService->expects($this->once()) ->method('getFilesByTag') ->with($this->equalTo([$tagName])) - ->will($this->returnValue([$fileInfo])); + ->will($this->returnValue([$node])); + + $this->shareManager->expects($this->any()) + ->method('getSharesBy') + ->with( + $this->equalTo('user1'), + $this->anything(), + $node, + $this->equalTo(false), + $this->equalTo(1) + ) + ->will($this->returnCallback(function($userId, $shareType) { + if ($shareType === \OCP\Share::SHARE_TYPE_USER || $shareType === \OCP\Share::SHARE_TYPE_LINK) { + return ['dummy_share']; + } + return []; + })); $expected = new DataResponse([ 'files' => [ @@ -124,6 +161,7 @@ class ApiControllerTest extends TestCase { 'MyTagName' ] ], + 'shareTypes' => [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK] ], ], ]); @@ -166,10 +204,22 @@ class ApiControllerTest extends TestCase { ->disableOriginalConstructor() ->getMock() ); + $node1 = $this->getMockBuilder('\OC\Files\Node\File') + ->disableOriginalConstructor() + ->getMock(); + $node1->expects($this->once()) + ->method('getFileInfo') + ->will($this->returnValue($fileInfo1)); + $node2 = $this->getMockBuilder('\OC\Files\Node\File') + ->disableOriginalConstructor() + ->getMock(); + $node2->expects($this->once()) + ->method('getFileInfo') + ->will($this->returnValue($fileInfo2)); $this->tagService->expects($this->once()) ->method('getFilesByTag') ->with($this->equalTo([$tagName])) - ->will($this->returnValue([$fileInfo1, $fileInfo2])); + ->will($this->returnValue([$node1, $node2])); $expected = new DataResponse([ 'files' => [