summaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/comments
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@owncloud.com>2016-01-11 18:09:00 +0100
committerArthur Schiwon <blizzz@owncloud.com>2016-01-26 12:10:14 +0100
commited546bd2a591d0d9029e9f3989e159f8b1e4e8c5 (patch)
treef0d0126c7f43cea0191b2dda78d5a832767cc822 /apps/dav/lib/comments
parent3da78c8f1c9355a726f289e834fa237366c3df20 (diff)
downloadnextcloud-server-ed546bd2a591d0d9029e9f3989e159f8b1e4e8c5.tar.gz
nextcloud-server-ed546bd2a591d0d9029e9f3989e159f8b1e4e8c5.zip
Comments DAV implementation
Diffstat (limited to 'apps/dav/lib/comments')
-rw-r--r--apps/dav/lib/comments/commentnode.php211
-rw-r--r--apps/dav/lib/comments/commentsplugin.php243
-rw-r--r--apps/dav/lib/comments/entitycollection.php148
-rw-r--r--apps/dav/lib/comments/entitytypecollection.php120
-rw-r--r--apps/dav/lib/comments/rootcollection.php204
5 files changed, 926 insertions, 0 deletions
diff --git a/apps/dav/lib/comments/commentnode.php b/apps/dav/lib/comments/commentnode.php
new file mode 100644
index 00000000000..b78b4765ca4
--- /dev/null
+++ b/apps/dav/lib/comments/commentnode.php
@@ -0,0 +1,211 @@
+<?php
+/**
+ * @author Arthur Schiwon <blizzz@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 OCA\DAV\Comments;
+
+
+use OCP\Comments\IComment;
+use OCP\Comments\ICommentsManager;
+use OCP\ILogger;
+use OCP\IUserManager;
+use Sabre\DAV\Exception\MethodNotAllowed;
+use Sabre\DAV\PropPatch;
+
+class CommentNode implements \Sabre\DAV\INode, \Sabre\DAV\IProperties {
+ const NS_OWNCLOUD = 'http://owncloud.org/ns';
+
+ /** @var IComment */
+ public $comment;
+
+ /** @var ICommentsManager */
+ protected $commentsManager;
+
+ /** @var ILogger */
+ protected $logger;
+
+ /** @var array list of properties with key being their name and value their setter */
+ protected $properties = [];
+
+ /** @var IUserManager */
+ protected $userManager;
+
+ /**
+ * CommentNode constructor.
+ *
+ * @param ICommentsManager $commentsManager
+ * @param IComment $comment
+ * @param IUserManager $userManager
+ * @param ILogger $logger
+ */
+ public function __construct(
+ ICommentsManager $commentsManager,
+ IComment $comment,
+ IUserManager $userManager,
+ ILogger $logger
+ ) {
+ $this->commentsManager = $commentsManager;
+ $this->comment = $comment;
+ $this->logger = $logger;
+
+ $methods = get_class_methods($this->comment);
+ $methods = array_filter($methods, function($name){
+ return strpos($name, 'get') === 0;
+ });
+ foreach($methods as $getter) {
+ $name = '{'.self::NS_OWNCLOUD.'}' . lcfirst(substr($getter, 3));
+ $this->properties[$name] = $getter;
+ }
+ $this->userManager = $userManager;
+ }
+
+ /**
+ * returns a list of all possible property names
+ *
+ * @return array
+ */
+ static public function getPropertyNames() {
+ return [
+ '{http://owncloud.org/ns}id',
+ '{http://owncloud.org/ns}parentId',
+ '{http://owncloud.org/ns}topmostParentId',
+ '{http://owncloud.org/ns}childrenCount',
+ '{http://owncloud.org/ns}message',
+ '{http://owncloud.org/ns}verb',
+ '{http://owncloud.org/ns}actorType',
+ '{http://owncloud.org/ns}actorId',
+ '{http://owncloud.org/ns}actorDisplayName',
+ '{http://owncloud.org/ns}creationDateTime',
+ '{http://owncloud.org/ns}latestChildDateTime',
+ '{http://owncloud.org/ns}objectType',
+ '{http://owncloud.org/ns}objectId',
+ ];
+ }
+
+ /**
+ * Deleted the current node
+ *
+ * @return void
+ */
+ function delete() {
+ $this->commentsManager->delete($this->comment->getId());
+ }
+
+ /**
+ * Returns the name of the node.
+ *
+ * This is used to generate the url.
+ *
+ * @return string
+ */
+ function getName() {
+ return $this->comment->getId();
+ }
+
+ /**
+ * Renames the node
+ *
+ * @param string $name The new name
+ * @throws MethodNotAllowed
+ */
+ function setName($name) {
+ throw new MethodNotAllowed();
+ }
+
+ /**
+ * Returns the last modification time, as a unix timestamp
+ *
+ * @return int
+ */
+ function getLastModified() {
+ // we do not have a separate "mDateTime" field for updates currently.
+ return $this->comment->getCreationDateTime()->getTimestamp();
+ }
+
+ /**
+ * update the comment's message
+ *
+ * @param $propertyValue
+ * @return bool
+ */
+ public function updateComment($propertyValue) {
+ try {
+ $this->comment->setMessage($propertyValue);
+ $this->commentsManager->save($this->comment);
+ return true;
+ } catch (\Exception $e) {
+ $this->logger->logException($e, ['app' => 'dav/comments']);
+ return false;
+ }
+ }
+
+ /**
+ * Updates properties on this node.
+ *
+ * This method received a PropPatch object, which contains all the
+ * information about the update.
+ *
+ * To update specific properties, call the 'handle' method on this object.
+ * Read the PropPatch documentation for more information.
+ *
+ * @param PropPatch $propPatch
+ * @return void
+ */
+ function propPatch(PropPatch $propPatch) {
+ // other properties than 'message' are read only
+ $propPatch->handle('{'.self::NS_OWNCLOUD.'}message', [$this, 'updateComment']);
+ $propPatch->commit();
+ }
+
+ /**
+ * Returns a list of properties for this nodes.
+ *
+ * The properties list is a list of propertynames the client requested,
+ * encoded in clark-notation {xmlnamespace}tagname
+ *
+ * If the array is empty, it means 'all properties' were requested.
+ *
+ * Note that it's fine to liberally give properties back, instead of
+ * conforming to the list of requested properties.
+ * The Server class will filter out the extra.
+ *
+ * @param array $properties
+ * @return array
+ */
+ function getProperties($properties) {
+ $properties = array_keys($this->properties);
+
+ $result = [];
+ foreach($properties as $property) {
+ $getter = $this->properties[$property];
+ if(method_exists($this->comment, $getter)) {
+ $result[$property] = $this->comment->$getter();
+ }
+ }
+
+ if($this->comment->getActorType() === 'users') {
+ $user = $this->userManager->get($this->comment->getActorId());
+ $displayName = is_null($user) ? null : $user->getDisplayName();
+ $result['{' . self::NS_OWNCLOUD . '}actorDisplayName'] = $displayName;
+ }
+
+ return $result;
+ }
+}
diff --git a/apps/dav/lib/comments/commentsplugin.php b/apps/dav/lib/comments/commentsplugin.php
new file mode 100644
index 00000000000..59ce3f12f6c
--- /dev/null
+++ b/apps/dav/lib/comments/commentsplugin.php
@@ -0,0 +1,243 @@
+<?php
+/**
+ * @author Arthur Schiwon <blizzz@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, 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 OCA\DAV\Comments;
+
+use OCP\Comments\IComment;
+use OCP\Comments\ICommentsManager;
+use OCP\IUserSession;
+use Sabre\DAV\Exception\BadRequest;
+use Sabre\DAV\Exception\ReportNotSupported;
+use Sabre\DAV\Exception\UnsupportedMediaType;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\Server;
+use Sabre\DAV\ServerPlugin;
+use Sabre\DAV\Xml\Element\Response;
+use Sabre\DAV\Xml\Response\MultiStatus;
+use Sabre\HTTP\RequestInterface;
+use Sabre\HTTP\ResponseInterface;
+use Sabre\Xml\Writer;
+
+/**
+ * Sabre plugin to handle comments:
+ */
+class CommentsPlugin extends ServerPlugin {
+ // namespace
+ const NS_OWNCLOUD = 'http://owncloud.org/ns';
+
+ const REPORT_PARAM_LIMIT = '{http://owncloud.org/ns}limit';
+ const REPORT_PARAM_OFFSET = '{http://owncloud.org/ns}offset';
+ const REPORT_PARAM_TIMESTAMP = '{http://owncloud.org/ns}datetime';
+
+ /** @var ICommentsManager */
+ protected $commentsManager;
+
+ /** @var \Sabre\DAV\Server $server */
+ private $server;
+
+ /** @var \OCP\IUserSession */
+ protected $userSession;
+
+ /**
+ * Comments plugin
+ *
+ * @param ICommentsManager $commentsManager
+ * @param IUserSession $userSession
+ */
+ public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) {
+ $this->commentsManager = $commentsManager;
+ $this->userSession = $userSession;
+ }
+
+ /**
+ * This initializes the plugin.
+ *
+ * This function is called by Sabre\DAV\Server, after
+ * addPlugin is called.
+ *
+ * This method should set up the required event subscriptions.
+ *
+ * @param Server $server
+ * @return void
+ */
+ function initialize(Server $server) {
+ $this->server = $server;
+ if(strpos($this->server->getRequestUri(), 'comments/') !== 0) {
+ return;
+ }
+
+ $this->server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
+
+ $this->server->xml->classMap['DateTime'] = function(Writer $writer, \DateTime $value) {
+ $writer->write($value->format('Y-m-d H:m:i'));
+ };
+
+ $this->server->on('report', [$this, 'onReport']);
+ $this->server->on('method:POST', [$this, 'httpPost']);
+ }
+
+ /**
+ * POST operation on Comments collections
+ *
+ * @param RequestInterface $request request object
+ * @param ResponseInterface $response response object
+ * @return null|false
+ */
+ public function httpPost(RequestInterface $request, ResponseInterface $response) {
+ $path = $request->getPath();
+
+ // Making sure the node exists
+ try {
+ $node = $this->server->tree->getNodeForPath($path);
+ } catch (NotFound $e) {
+ return null;
+ }
+
+ if ($node instanceof EntityCollection) {
+ $data = $request->getBodyAsString();
+
+ $comment = $this->createComment(
+ $node->getName(),
+ $node->getId(),
+ $data,
+ $request->getHeader('Content-Type')
+ );
+ $url = $request->getUrl() . '/' . urlencode($comment->getId());
+
+ $response->setHeader('Content-Location', $url);
+
+ // created
+ $response->setStatus(201);
+ return false;
+ }
+ }
+
+ /**
+ * REPORT operations to look for comments
+ *
+ * @param string $reportName
+ * @param [] $report
+ * @param string $uri
+ * @return bool
+ * @throws NotFound
+ * @throws ReportNotSupported
+ */
+ public function onReport($reportName, $report, $uri) {
+ $node = $this->server->tree->getNodeForPath($uri);
+ if(!$node instanceof EntityCollection) {
+ throw new ReportNotSupported();
+ }
+ $args = ['limit' => 0, 'offset' => 0, 'datetime' => null];
+ $acceptableParameters = [
+ $this::REPORT_PARAM_LIMIT,
+ $this::REPORT_PARAM_OFFSET,
+ $this::REPORT_PARAM_TIMESTAMP
+ ];
+ $ns = '{' . $this::NS_OWNCLOUD . '}';
+ foreach($report as $parameter) {
+ if(!in_array($parameter['name'], $acceptableParameters) || empty($parameter['value'])) {
+ continue;
+ }
+ $args[str_replace($ns, '', $parameter['name'])] = $parameter['value'];
+ }
+
+ if(!is_null($args['datetime'])) {
+ $args['datetime'] = new \DateTime($args['datetime']);
+ }
+
+ $results = $node->findChildren($args['limit'], $args['offset'], $args['datetime']);
+
+ $responses = [];
+ foreach($results as $node) {
+ $nodePath = $this->server->getRequestUri() . '/' . $node->comment->getId();
+ $resultSet = $this->server->getPropertiesForPath($nodePath, CommentNode::getPropertyNames());
+ if(isset($resultSet[0]) && isset($resultSet[0][200])) {
+ $responses[] = new Response(
+ $this->server->getBaseUri() . $nodePath,
+ [200 => $resultSet[0][200]],
+ 200
+ );
+ }
+
+ }
+
+ $xml = $this->server->xml->write(
+ '{DAV:}multistatus',
+ new MultiStatus($responses)
+ );
+
+ $this->server->httpResponse->setStatus(207);
+ $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
+ $this->server->httpResponse->setBody($xml);
+
+ return false;
+ }
+
+ /**
+ * Creates a new comment
+ *
+ * @param string $objectType e.g. "files"
+ * @param string $objectId e.g. the file id
+ * @param string $data JSON encoded string containing the properties of the tag to create
+ * @param string $contentType content type of the data
+ * @return IComment newly created comment
+ *
+ * @throws BadRequest if a field was missing
+ * @throws UnsupportedMediaType if the content type is not supported
+ */
+ private function createComment($objectType, $objectId, $data, $contentType = 'application/json') {
+ if (explode(';', $contentType)[0] === 'application/json') {
+ $data = json_decode($data, true);
+ } else {
+ throw new UnsupportedMediaType();
+ }
+
+ $actorType = $data['actorType'];
+ $actorId = null;
+ if($actorType === 'users') {
+ $user = $this->userSession->getUser();
+ if(!is_null($user)) {
+ $actorId = $user->getUID();
+ }
+ }
+ if(is_null($actorId)) {
+ throw new BadRequest('Invalid actor "' . $actorType .'"');
+ }
+
+ try {
+ $comment = $this->commentsManager->create($actorType, $actorId, $objectType, $objectId);
+ $properties = [
+ 'message' => 'setMessage',
+ 'verb' => 'setVerb',
+ ];
+ foreach($properties as $property => $setter) {
+ $comment->$setter($data[$property]);
+ }
+ $this->commentsManager->save($comment);
+ return $comment;
+ } catch (\InvalidArgumentException $e) {
+ throw new BadRequest('Invalid input values', 0, $e);
+ }
+ }
+
+
+
+}
diff --git a/apps/dav/lib/comments/entitycollection.php b/apps/dav/lib/comments/entitycollection.php
new file mode 100644
index 00000000000..a93569f6e4f
--- /dev/null
+++ b/apps/dav/lib/comments/entitycollection.php
@@ -0,0 +1,148 @@
+<?php
+/**
+ * @author Arthur Schiwon <blizzz@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 OCA\DAV\Comments;
+
+use OCP\Comments\ICommentsManager;
+use OCP\Files\Folder;
+use OCP\ILogger;
+use OCP\IUserManager;
+use Sabre\DAV\Exception\NotFound;
+
+/**
+ * Class EntityCollection
+ *
+ * this represents a specific holder of comments, identified by an entity type
+ * (class member $name) and an entity id (class member $id).
+ *
+ * @package OCA\DAV\Comments
+ */
+class EntityCollection extends RootCollection {
+ /** @var Folder */
+ protected $fileRoot;
+
+ /** @var string */
+ protected $id;
+
+ /** @var ILogger */
+ protected $logger;
+
+ /**
+ * @param string $id
+ * @param string $name
+ * @param ICommentsManager $commentsManager
+ * @param Folder $fileRoot
+ * @param IUserManager $userManager
+ * @param ILogger $logger
+ */
+ public function __construct(
+ $id,
+ $name,
+ ICommentsManager $commentsManager,
+ Folder $fileRoot,
+ IUserManager $userManager,
+ ILogger $logger
+ ) {
+ foreach(['id', 'name'] as $property) {
+ $$property = trim($$property);
+ if(empty($$property) || !is_string($$property)) {
+ throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string');
+ }
+ }
+ $this->id = $id;
+ $this->name = $name;
+ $this->commentsManager = $commentsManager;
+ $this->fileRoot = $fileRoot;
+ $this->logger = $logger;
+ $this->userManager = $userManager;
+ }
+
+ /**
+ * returns the ID of this entity
+ *
+ * @return string
+ */
+ public function getId() {
+ return $this->id;
+ }
+
+ /**
+ * Returns a specific child node, referenced by its name
+ *
+ * This method must throw Sabre\DAV\Exception\NotFound if the node does not
+ * exist.
+ *
+ * @param string $name
+ * @return \Sabre\DAV\INode
+ * @throws NotFound
+ */
+ function getChild($name) {
+ try {
+ $comment = $this->commentsManager->get($name);
+ return new CommentNode($this->commentsManager, $comment, $this->userManager, $this->logger);
+ } catch (\OCP\Comments\NotFoundException $e) {
+ throw new NotFound();
+ }
+ }
+
+ /**
+ * Returns an array with all the child nodes
+ *
+ * @return \Sabre\DAV\INode[]
+ */
+ function getChildren() {
+ return $this->findChildren();
+ }
+
+ /**
+ * Returns an array of comment nodes. Result can be influenced by offset,
+ * limit and date time parameters.
+ *
+ * @param int $limit
+ * @param int $offset
+ * @param \DateTime|null $datetime
+ * @return CommentNode[]
+ */
+ function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) {
+ $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime);
+ $result = [];
+ foreach($comments as $comment) {
+ $result[] = new CommentNode($this->commentsManager, $comment, $this->userManager, $this->logger);
+ }
+ return $result;
+ }
+
+ /**
+ * Checks if a child-node with the specified name exists
+ *
+ * @param string $name
+ * @return bool
+ */
+ function childExists($name) {
+ try {
+ $this->commentsManager->get($name);
+ return true;
+ } catch (\OCP\Comments\NotFoundException $e) {
+ return false;
+ }
+ }
+}
+
diff --git a/apps/dav/lib/comments/entitytypecollection.php b/apps/dav/lib/comments/entitytypecollection.php
new file mode 100644
index 00000000000..544838b89e6
--- /dev/null
+++ b/apps/dav/lib/comments/entitytypecollection.php
@@ -0,0 +1,120 @@
+<?php
+/**
+ * @author Arthur Schiwon <blizzz@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 OCA\DAV\Comments;
+
+use OCP\Comments\ICommentsManager;
+use OCP\Files\Folder;
+use OCP\ILogger;
+use OCP\IUserManager;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\MethodNotAllowed;
+
+/**
+ * Class EntityTypeCollection
+ *
+ * This is collection on the type of things a user can leave comments on, for
+ * example: 'files'.
+ *
+ * Its children are instances of EntityCollection (representing a specific
+ * object, for example the file by id).
+ *
+ * @package OCA\DAV\Comments
+ */
+class EntityTypeCollection extends RootCollection {
+ /** @var Folder */
+ protected $fileRoot;
+
+ /** @var ILogger */
+ protected $logger;
+
+ /**
+ * @param string $name
+ * @param ICommentsManager $commentsManager
+ * @param Folder $fileRoot
+ * @param IUserManager $userManager
+ * @param ILogger $logger
+ */
+ public function __construct(
+ $name,
+ ICommentsManager $commentsManager,
+ Folder $fileRoot,
+ IUserManager $userManager,
+ ILogger $logger
+ ) {
+ $name = trim($name);
+ if(empty($name) || !is_string($name)) {
+ throw new \InvalidArgumentException('"name" parameter must be non-empty string');
+ }
+ $this->name = $name;
+ $this->commentsManager = $commentsManager;
+ $this->fileRoot = $fileRoot;
+ $this->logger = $logger;
+ $this->userManager = $userManager;
+ }
+
+ /**
+ * Returns a specific child node, referenced by its name
+ *
+ * This method must throw Sabre\DAV\Exception\NotFound if the node does not
+ * exist.
+ *
+ * @param string $name
+ * @return \Sabre\DAV\INode
+ * @throws Forbidden
+ */
+ function getChild($name) {
+ if(!$this->childExists($name)) {
+ throw new Forbidden('Entity does not exist or is not available');
+ }
+ return new EntityCollection(
+ $name,
+ $this->name,
+ $this->commentsManager,
+ $this->fileRoot,
+ $this->userManager,
+ $this->logger
+ );
+ }
+
+ /**
+ * Returns an array with all the child nodes
+ *
+ * @return \Sabre\DAV\INode[]
+ * @throws MethodNotAllowed
+ */
+ function getChildren() {
+ throw new MethodNotAllowed('No permission to list folder contents');
+ }
+
+ /**
+ * Checks if a child-node with the specified name exists
+ *
+ * @param string $name
+ * @return bool
+ */
+ function childExists($name) {
+ $nodes = $this->fileRoot->getById($name);
+ return !empty($nodes);
+ }
+
+
+}
diff --git a/apps/dav/lib/comments/rootcollection.php b/apps/dav/lib/comments/rootcollection.php
new file mode 100644
index 00000000000..aec8e655667
--- /dev/null
+++ b/apps/dav/lib/comments/rootcollection.php
@@ -0,0 +1,204 @@
+<?php
+/**
+ * @author Arthur Schiwon <blizzz@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 OCA\DAV\Comments;
+
+use OCP\Comments\ICommentsManager;
+use OCP\Files\IRootFolder;
+use OCP\ILogger;
+use OCP\IUserManager;
+use OCP\IUserSession;
+use Sabre\DAV\Exception\NotAuthenticated;
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\ICollection;
+
+class RootCollection implements ICollection {
+
+ /** @var EntityTypeCollection[] */
+ private $entityTypeCollections = [];
+
+ /** @var ICommentsManager */
+ protected $commentsManager;
+
+ /** @var string */
+ protected $name = 'comments';
+
+ /** @var ILogger */
+ protected $logger;
+
+ /** @var IUserManager */
+ protected $userManager;
+ /**
+ * @var IUserSession
+ */
+ protected $userSession;
+ /**
+ * @var IRootFolder
+ */
+ protected $rootFolder;
+
+ /**
+ * @param ICommentsManager $commentsManager
+ * @param IUserManager $userManager
+ * @param IUserSession $userSession
+ * @param IRootFolder $rootFolder
+ * @param ILogger $logger
+ */
+ public function __construct(
+ ICommentsManager $commentsManager,
+ IUserManager $userManager,
+ IUserSession $userSession,
+ IRootFolder $rootFolder,
+ ILogger $logger)
+ {
+ $this->commentsManager = $commentsManager;
+ $this->logger = $logger;
+ $this->userManager = $userManager;
+ $this->userSession = $userSession;
+ $this->rootFolder = $rootFolder;
+ }
+
+ /**
+ * initializes the collection. At this point of time, we need the logged in
+ * user. Since it is not the case when the instance is created, we cannot
+ * have this in the constructor.
+ *
+ * @throws NotAuthenticated
+ */
+ protected function initCollections() {
+ if(!empty($this->entityTypeCollections)) {
+ 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->logger
+ );
+ }
+
+ /**
+ * Creates a new file in the directory
+ *
+ * @param string $name Name of the file
+ * @param resource|string $data Initial payload
+ * @return null|string
+ * @throws Forbidden
+ */
+ function createFile($name, $data = null) {
+ throw new Forbidden('Cannot create comments by id');
+ }
+
+ /**
+ * Creates a new subdirectory
+ *
+ * @param string $name
+ * @throws Forbidden
+ */
+ function createDirectory($name) {
+ throw new Forbidden('Permission denied to create collections');
+ }
+
+ /**
+ * Returns a specific child node, referenced by its name
+ *
+ * This method must throw Sabre\DAV\Exception\NotFound if the node does not
+ * exist.
+ *
+ * @param string $name
+ * @return \Sabre\DAV\INode
+ * @throws NotFound
+ */
+ function getChild($name) {
+ $this->initCollections();
+ if(isset($this->entityTypeCollections[$name])) {
+ return $this->entityTypeCollections[$name];
+ }
+ throw new NotFound('Entity type "' . $name . '" not found."');
+ }
+
+ /**
+ * Returns an array with all the child nodes
+ *
+ * @return \Sabre\DAV\INode[]
+ */
+ function getChildren() {
+ $this->initCollections();
+ return $this->entityTypeCollections;
+ }
+
+ /**
+ * Checks if a child-node with the specified name exists
+ *
+ * @param string $name
+ * @return bool
+ */
+ function childExists($name) {
+ $this->initCollections();
+ return isset($this->entityTypeCollections[$name]);
+ }
+
+ /**
+ * Deleted the current node
+ *
+ * @throws Forbidden
+ */
+ function delete() {
+ throw new Forbidden('Permission denied to delete this collection');
+ }
+
+ /**
+ * Returns the name of the node.
+ *
+ * This is used to generate the url.
+ *
+ * @return string
+ */
+ function getName() {
+ return $this->name;
+ }
+
+ /**
+ * Renames the node
+ *
+ * @param string $name The new name
+ * @throws Forbidden
+ */
+ function setName($name) {
+ throw new Forbidden('Permission denied to rename this collection');
+ }
+
+ /**
+ * Returns the last modification time, as a unix timestamp
+ *
+ * @return int
+ */
+ function getLastModified() {
+ return null;
+ }
+}