Skip to content

Commit

Permalink
Merge pull request #12119 from nextcloud/add_circle_to_caldav_and_fil…
Browse files Browse the repository at this point in the history
…epanel-15

Add Circle to Caldav and Filepanel
  • Loading branch information
MorrisJobke authored Mar 14, 2019
2 parents 804a151 + de8e026 commit 6331f17
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 11 deletions.
39 changes: 38 additions & 1 deletion apps/dav/lib/Connector/Sabre/FilesReportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace OCA\DAV\Connector\Sabre;

use OC\Files\View;
use OCP\App\IAppManager;
use Sabre\DAV\Exception\PreconditionFailed;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\ServerPlugin;
Expand All @@ -46,6 +47,7 @@ class FilesReportPlugin extends ServerPlugin {
const NS_OWNCLOUD = 'http://owncloud.org/ns';
const REPORT_NAME = '{http://owncloud.org/ns}filter-files';
const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag';
const CIRCLE_PROPERTYNAME = '{http://owncloud.org/ns}circle';

/**
* Reference to main server object
Expand Down Expand Up @@ -96,6 +98,11 @@ class FilesReportPlugin extends ServerPlugin {
*/
private $userFolder;

/**
* @var IAppManager
*/
private $appManager;

/**
* @param Tree $tree
* @param View $view
Expand All @@ -105,6 +112,7 @@ class FilesReportPlugin extends ServerPlugin {
* @param IUserSession $userSession
* @param IGroupManager $groupManager
* @param Folder $userFolder
* @param IAppManager $appManager
*/
public function __construct(Tree $tree,
View $view,
Expand All @@ -113,7 +121,8 @@ public function __construct(Tree $tree,
ITagManager $fileTagger,
IUserSession $userSession,
IGroupManager $groupManager,
Folder $userFolder
Folder $userFolder,
IAppManager $appManager
) {
$this->tree = $tree;
$this->fileView = $view;
Expand All @@ -123,6 +132,7 @@ public function __construct(Tree $tree,
$this->userSession = $userSession;
$this->groupManager = $groupManager;
$this->userFolder = $userFolder;
$this->appManager = $appManager;
}

/**
Expand Down Expand Up @@ -256,14 +266,19 @@ protected function processFilterRules($filterRules) {
$ns = '{' . $this::NS_OWNCLOUD . '}';
$resultFileIds = null;
$systemTagIds = [];
$circlesIds = [];
$favoriteFilter = null;
foreach ($filterRules as $filterRule) {
if ($filterRule['name'] === $ns . 'systemtag') {
$systemTagIds[] = $filterRule['value'];
}
if ($filterRule['name'] === self::CIRCLE_PROPERTYNAME) {
$circlesIds[] = $filterRule['value'];
}
if ($filterRule['name'] === $ns . 'favorite') {
$favoriteFilter = true;
}

}

if ($favoriteFilter !== null) {
Expand All @@ -282,6 +297,15 @@ protected function processFilterRules($filterRules) {
}
}

if (!empty($circlesIds)) {
$fileIds = $this->getCirclesFileIds($circlesIds);
if (empty($resultFileIds)) {
$resultFileIds = $fileIds;
} else {
$resultFileIds = array_intersect($fileIds, $resultFileIds);
}
}

return $resultFileIds;
}

Expand Down Expand Up @@ -328,6 +352,19 @@ private function getSystemTagFileIds($systemTagIds) {
return $resultFileIds;
}

/**
* @suppress PhanUndeclaredClassMethod
* @param array $circlesIds
* @return array
*/
private function getCirclesFileIds(array $circlesIds) {
if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) {
return [];
}
return \OCA\Circles\Api\v1\Circles::getFilesForCircles($circlesIds);
}


/**
* Prepare propfind response for the given nodes
*
Expand Down
15 changes: 10 additions & 5 deletions apps/dav/lib/Connector/Sabre/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
use OCP\IUserSession;
use OCP\Share\IManager as IShareManager;
use Sabre\DAV\Exception;
use \Sabre\DAV\PropPatch;
use Sabre\DAV\PropPatch;
use Sabre\DAVACL\PrincipalBackend\BackendInterface;

class Principal implements BackendInterface {
Expand Down Expand Up @@ -145,7 +145,11 @@ public function getPrincipalByPath($path) {
return $this->userToPrincipal($user);
}
} else if ($prefix === 'principals/circles') {
return $this->circleToPrincipal($name);
try {
return $this->circleToPrincipal($name);
} catch (QueryException $e) {
return null;
}
}
return null;
}
Expand Down Expand Up @@ -406,6 +410,7 @@ public function getPrincipalPrefix() {
/**
* @param string $circleUniqueId
* @return array|null
* @throws \OCP\AppFramework\QueryException
* @suppress PhanUndeclaredClassMethod
* @suppress PhanUndeclaredClassCatch
*/
Expand Down Expand Up @@ -438,9 +443,9 @@ protected function circleToPrincipal($circleUniqueId) {
* Returns the list of circles a principal is a member of
*
* @param string $principal
* @param bool $needGroups
* @return array
* @throws Exception
* @throws \OCP\AppFramework\QueryException
* @suppress PhanUndeclaredClassMethod
*/
public function getCircleMembership($principal):array {
Expand All @@ -458,13 +463,13 @@ public function getCircleMembership($principal):array {
$circles = \OCA\Circles\Api\v1\Circles::joinedCircles($name, true);

$circles = array_map(function($circle) {
/** @var \OCA\Circles\Model\Circle $group */
/** @var \OCA\Circles\Model\Circle $circle */
return 'principals/circles/' . urlencode($circle->getUniqueId());
}, $circles);

return $circles;

}

return [];
}

Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Connector/Sabre/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ public function createServer($baseUri,
\OC::$server->getTagManager(),
$this->userSession,
\OC::$server->getGroupManager(),
$userFolder
$userFolder,
\OC::$server->getAppManager()
));
// custom properties plugin must be the last one
$server->addPlugin(
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ public function __construct(IRequest $request, $baseUri) {
\OC::$server->getTagManager(),
$userSession,
\OC::$server->getGroupManager(),
$userFolder
$userFolder,
\OC::$server->getAppManager()
));
$lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend(
$this->server->tree,
Expand Down
9 changes: 9 additions & 0 deletions apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public function setUp() {
->withAnyParameters()
->willReturn([]);

$this->principal->expects($this->any())->method('getCircleMembership')
->withAnyParameters()
->willReturn([]);

$this->backend = new CalDavBackend(
$db,
$this->principal,
Expand All @@ -112,6 +116,11 @@ public function tearDown() {
$this->principal->expects($this->any())->method('getGroupMembership')
->withAnyParameters()
->willReturn([]);

$this->principal->expects($this->any())->method('getCircleMembership')
->withAnyParameters()
->willReturn([]);

$books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
foreach ($books as $book) {
$this->backend->deleteCalendar($book['id']);
Expand Down
11 changes: 10 additions & 1 deletion apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\FilesReportPlugin as FilesReportPluginImplementation;
use OCP\App\IAppManager;
use OCP\Files\File;
use OCP\IConfig;
use OCP\IPreview;
Expand Down Expand Up @@ -81,6 +82,9 @@ class FilesReportPluginTest extends \Test\TestCase {
/** @var IPreview|\PHPUnit_Framework_MockObject_MockObject * */
private $previewManager;

/** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject * */
private $appManager;

public function setUp() {
parent::setUp();
$this->tree = $this->getMockBuilder(Tree::class)
Expand Down Expand Up @@ -112,6 +116,10 @@ public function setUp() {
->disableOriginalConstructor()
->getMock();

$this->appManager = $this->getMockBuilder(IAppManager::class)
->disableOriginalConstructor()
->getMock();

$this->tagManager = $this->createMock(ISystemTagManager::class);
$this->tagMapper = $this->createMock(ISystemTagObjectMapper::class);
$this->userSession = $this->createMock(IUserSession::class);
Expand Down Expand Up @@ -140,7 +148,8 @@ public function setUp() {
$privateTagManager,
$this->userSession,
$this->groupManager,
$this->userFolder
$this->userFolder,
$this->appManager
);
}

Expand Down
8 changes: 6 additions & 2 deletions core/js/files/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@

/**
* Fetches a flat list of files filtered by a given filter criteria.
* (currently only system tags is supported)
* (currently system tags and circles are supported)
*
* @param {Object} filter filter criteria
* @param {Object} [filter.systemTagIds] list of system tag ids to filter by
Expand All @@ -525,7 +525,8 @@
properties = options.properties;
}

if (!filter || (!filter.systemTagIds && _.isUndefined(filter.favorite))) {
if (!filter ||
(!filter.systemTagIds && _.isUndefined(filter.favorite) && !filter.circlesIds) ) {
throw 'Missing filter argument';
}

Expand All @@ -551,6 +552,9 @@
_.each(filter.systemTagIds, function(systemTagIds) {
body += ' <oc:systemtag>' + escapeHTML(systemTagIds) + '</oc:systemtag>\n';
});
_.each(filter.circlesIds, function(circlesIds) {
body += ' <oc:circle>' + escapeHTML(circlesIds) + '</oc:circle>\n';
});
if (filter.favorite) {
body += ' <oc:favorite>' + (filter.favorite ? '1': '0') + '</oc:favorite>\n';
}
Expand Down

0 comments on commit 6331f17

Please sign in to comment.