Make the root collection neutral so it does not only work for files

This commit is contained in:
Joas Schilling 2016-02-24 13:23:44 +01:00
parent 3a8e537946
commit c9fda84841
No known key found for this signature in database
GPG Key ID: 70A0B324C41C0946
5 changed files with 123 additions and 30 deletions

View File

@ -51,3 +51,10 @@ $managerListener = function(\OCP\Comments\CommentsEvent $event) use ($activityMa
};
$eventDispatcher->addListener(\OCP\Comments\CommentsEvent::EVENT_ADD, $managerListener);
$eventDispatcher->addListener(\OCP\Comments\CommentsEntityEvent::EVENT_ENTITY, function(\OCP\Comments\CommentsEntityEvent $event) {
$event->addEntityCollection('files', function($name) {
$nodes = \OC::$server->getUserFolder()->getById(intval($name));
return !empty($nodes);
});
});

View File

@ -44,21 +44,27 @@ class EntityTypeCollection extends RootCollection {
/** @var ILogger */
protected $logger;
/** @var IUserManager */
protected $userManager;
/** @var \Closure */
protected $childExistsFunction;
/**
* @param string $name
* @param ICommentsManager $commentsManager
* @param Folder $fileRoot
* @param IUserManager $userManager
* @param IUserSession $userSession
* @param ILogger $logger
* @param \Closure $childExistsFunction
*/
public function __construct(
$name,
ICommentsManager $commentsManager,
Folder $fileRoot,
IUserManager $userManager,
IUserSession $userSession,
ILogger $logger
ILogger $logger,
\Closure $childExistsFunction
) {
$name = trim($name);
if(empty($name) || !is_string($name)) {
@ -66,10 +72,10 @@ class EntityTypeCollection extends RootCollection {
}
$this->name = $name;
$this->commentsManager = $commentsManager;
$this->fileRoot = $fileRoot;
$this->logger = $logger;
$this->userManager = $userManager;
$this->userSession = $userSession;
$this->childExistsFunction = $childExistsFunction;
}
/**
@ -113,8 +119,7 @@ class EntityTypeCollection extends RootCollection {
* @return bool
*/
function childExists($name) {
$nodes = $this->fileRoot->getById(intval($name));
return !empty($nodes);
return call_user_func($this->childExistsFunction, $name);
}
}

View File

@ -21,8 +21,8 @@
namespace OCA\DAV\Comments;
use OCP\Comments\CommentsEntityEvent;
use OCP\Comments\ICommentsManager;
use OCP\Files\IRootFolder;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\IUserSession;
@ -30,11 +30,12 @@ use Sabre\DAV\Exception\NotAuthenticated;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\ICollection;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class RootCollection implements ICollection {
/** @var EntityTypeCollection[] */
private $entityTypeCollections = [];
/** @var EntityTypeCollection[]|null */
private $entityTypeCollections;
/** @var ICommentsManager */
protected $commentsManager;
@ -47,34 +48,32 @@ class RootCollection implements ICollection {
/** @var IUserManager */
protected $userManager;
/**
* @var IUserSession
*/
/** @var IUserSession */
protected $userSession;
/**
* @var IRootFolder
*/
protected $rootFolder;
/** @var EventDispatcherInterface */
protected $dispatcher;
/**
* @param ICommentsManager $commentsManager
* @param IUserManager $userManager
* @param IUserSession $userSession
* @param IRootFolder $rootFolder
* @param EventDispatcherInterface $dispatcher
* @param ILogger $logger
*/
public function __construct(
ICommentsManager $commentsManager,
IUserManager $userManager,
IUserSession $userSession,
IRootFolder $rootFolder,
EventDispatcherInterface $dispatcher,
ILogger $logger)
{
$this->commentsManager = $commentsManager;
$this->logger = $logger;
$this->userManager = $userManager;
$this->userSession = $userSession;
$this->rootFolder = $rootFolder;
$this->dispatcher = $dispatcher;
}
/**
@ -85,22 +84,28 @@ class RootCollection implements ICollection {
* @throws NotAuthenticated
*/
protected function initCollections() {
if(!empty($this->entityTypeCollections)) {
if($this->entityTypeCollections !== null) {
return;
}
$user = $this->userSession->getUser();
if(is_null($user)) {
throw new NotAuthenticated();
}
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$this->entityTypeCollections['files'] = new EntityTypeCollection(
'files',
$this->commentsManager,
$userFolder,
$this->userManager,
$this->userSession,
$this->logger
);
$event = new CommentsEntityEvent(CommentsEntityEvent::EVENT_ENTITY);
$this->dispatcher->dispatch(CommentsEntityEvent::EVENT_ENTITY, $event);
$this->entityTypeCollections = [];
foreach ($event->getEntityCollections() as $entity => $entityExistsFunction) {
$this->entityTypeCollections[$entity] = new EntityTypeCollection(
$entity,
$this->commentsManager,
$this->userManager,
$this->userSession,
$this->logger,
$entityExistsFunction
);
}
}
/**

View File

@ -77,7 +77,7 @@ class RootCollection extends SimpleCollection {
\OC::$server->getCommentsManager(),
\OC::$server->getUserManager(),
\OC::$server->getUserSession(),
\OC::$server->getRootFolder(),
\OC::$server->getEventDispatcher(),
\OC::$server->getLogger()
);

View File

@ -0,0 +1,76 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCP\Comments;
use Symfony\Component\EventDispatcher\Event;
/**
* Class CommentsEntityEvent
*
* @package OCP\Comments
* @since 9.1.0
*/
class CommentsEntityEvent extends Event {
const EVENT_ENTITY = 'OCP\Comments\ICommentsManager::registerEntity';
/** @var string */
protected $event;
/** @var \Closure[] */
protected $collections;
/**
* DispatcherEvent constructor.
*
* @param string $event
* @since 9.1.0
*/
public function __construct($event) {
$this->event = $event;
$this->collections = [];
}
/**
* @param string $name
* @param \Closure $entityExistsFunction The closure should take one
* argument, which is the id of the entity, that comments
* should be handled for. The return should then be bool,
* depending on whether comments are allowed (true) or not.
* @throws \OutOfBoundsException when the entity name is already taken
* @since 9.1.0
*/
public function addEntityCollection($name, \Closure $entityExistsFunction) {
if (isset($this->collections[$name])) {
throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"');
}
$this->collections[$name] = $entityExistsFunction;
}
/**
* @return \Closure[]
* @since 9.1.0
*/
public function getEntityCollections() {
return $this->collections;
}
}