summaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/Comments
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@gmx.de>2016-05-12 09:42:40 +0200
committerThomas Müller <DeepDiver1975@users.noreply.github.com>2016-05-12 09:42:40 +0200
commitdd9ee10bc0699e5dcc17c3ef2181dcda01d9a69b (patch)
tree2d464fcc20ea330481e56487287aeae462ce5917 /apps/dav/lib/Comments
parent4a3311f430ec6e45c62b2ebde2cae71e943f3c81 (diff)
downloadnextcloud-server-dd9ee10bc0699e5dcc17c3ef2181dcda01d9a69b.tar.gz
nextcloud-server-dd9ee10bc0699e5dcc17c3ef2181dcda01d9a69b.zip
Move dav app to PSR-4 (#24527)
* Move Application to correct namespace and PSR-4 it * Move dav app to PSR-4
Diffstat (limited to 'apps/dav/lib/Comments')
-rw-r--r--apps/dav/lib/Comments/CommentNode.php261
-rw-r--r--apps/dav/lib/Comments/CommentsPlugin.php255
-rw-r--r--apps/dav/lib/Comments/EntityCollection.php199
-rw-r--r--apps/dav/lib/Comments/EntityTypeCollection.php125
-rw-r--r--apps/dav/lib/Comments/RootCollection.php205
5 files changed, 1045 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..339abc6382d
--- /dev/null
+++ b/apps/dav/lib/Comments/CommentNode.php
@@ -0,0 +1,261 @@
+<?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\Comments\MessageTooLongException;
+use OCP\ILogger;
+use OCP\IUserManager;
+use OCP\IUserSession;
+use Sabre\DAV\Exception\BadRequest;
+use Sabre\DAV\Exception\Forbidden;
+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';
+
+ const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}isUnread';
+ const PROPERTY_NAME_MESSAGE = '{http://owncloud.org/ns}message';
+ const PROPERTY_NAME_ACTOR_DISPLAYNAME = '{http://owncloud.org/ns}actorDisplayName';
+
+ /** @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;
+
+ /** @var IUserSession */
+ protected $userSession;
+
+ /**
+ * CommentNode constructor.
+ *
+ * @param ICommentsManager $commentsManager
+ * @param IComment $comment
+ * @param IUserManager $userManager
+ * @param IUserSession $userSession
+ * @param ILogger $logger
+ */
+ public function __construct(
+ ICommentsManager $commentsManager,
+ IComment $comment,
+ IUserManager $userManager,
+ IUserSession $userSession,
+ 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;
+ $this->userSession = $userSession;
+ }
+
+ /**
+ * 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}verb',
+ '{http://owncloud.org/ns}actorType',
+ '{http://owncloud.org/ns}actorId',
+ '{http://owncloud.org/ns}creationDateTime',
+ '{http://owncloud.org/ns}latestChildDateTime',
+ '{http://owncloud.org/ns}objectType',
+ '{http://owncloud.org/ns}objectId',
+ // re-used property names are defined as constants
+ self::PROPERTY_NAME_MESSAGE,
+ self::PROPERTY_NAME_ACTOR_DISPLAYNAME,
+ self::PROPERTY_NAME_UNREAD
+ ];
+ }
+
+ protected function checkWriteAccessOnComment() {
+ $user = $this->userSession->getUser();
+ if( $this->comment->getActorType() !== 'users'
+ || is_null($user)
+ || $this->comment->getActorId() !== $user->getUID()
+ ) {
+ throw new Forbidden('Only authors are allowed to edit their comment.');
+ }
+ }
+
+ /**
+ * Deleted the current node
+ *
+ * @return void
+ */
+ function delete() {
+ $this->checkWriteAccessOnComment();
+ $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() {
+ return null;
+ }
+
+ /**
+ * update the comment's message
+ *
+ * @param $propertyValue
+ * @return bool
+ * @throws BadRequest
+ * @throws Forbidden
+ */
+ public function updateComment($propertyValue) {
+ $this->checkWriteAccessOnComment();
+ try {
+ $this->comment->setMessage($propertyValue);
+ $this->commentsManager->save($this->comment);
+ return true;
+ } catch (\Exception $e) {
+ $this->logger->logException($e, ['app' => 'dav/comments']);
+ if($e instanceof MessageTooLongException) {
+ $msg = 'Message exceeds allowed character limit of ';
+ throw new BadRequest($msg . IComment::MAX_MESSAGE_LENGTH, 0, $e);
+ }
+ 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::PROPERTY_NAME_MESSAGE, [$this, 'updateComment']);
+ }
+
+ /**
+ * 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::PROPERTY_NAME_ACTOR_DISPLAYNAME] = $displayName;
+ }
+
+ $unread = null;
+ $user = $this->userSession->getUser();
+ if(!is_null($user)) {
+ $readUntil = $this->commentsManager->getReadMark(
+ $this->comment->getObjectType(),
+ $this->comment->getObjectId(),
+ $user
+ );
+ if(is_null($readUntil)) {
+ $unread = 'true';
+ } else {
+ $unread = $this->comment->getCreationDateTime() > $readUntil;
+ // re-format for output
+ $unread = $unread ? 'true' : 'false';
+ }
+ }
+ $result[self::PROPERTY_NAME_UNREAD] = $unread;
+
+ return $result;
+ }
+}
diff --git a/apps/dav/lib/Comments/CommentsPlugin.php b/apps/dav/lib/Comments/CommentsPlugin.php
new file mode 100644
index 00000000000..d4a065649ef
--- /dev/null
+++ b/apps/dav/lib/Comments/CommentsPlugin.php
@@ -0,0 +1,255 @@
+<?php
+/**
+ * @author Arthur Schiwon <blizzz@owncloud.com>
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Vincent Petry <pvince81@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\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_NAME = '{http://owncloud.org/ns}filter-comments';
+ 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(\Sabre\HTTP\toDate($value));
+ };
+
+ $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();
+ $node = $this->server->tree->getNodeForPath($path);
+ if (!$node instanceof EntityCollection) {
+ return null;
+ }
+
+ $data = $request->getBodyAsString();
+ $comment = $this->createComment(
+ $node->getName(),
+ $node->getId(),
+ $data,
+ $request->getHeader('Content-Type')
+ );
+
+ // update read marker for the current user/poster to avoid
+ // having their own comments marked as unread
+ $node->setReadMarker(null);
+
+ $url = rtrim($request->getUrl(), '/') . '/' . urlencode($comment->getId());
+
+ $response->setHeader('Content-Location', $url);
+
+ // created
+ $response->setStatus(201);
+ return false;
+ }
+
+ /**
+ * Returns a list of reports this plugin supports.
+ *
+ * This will be used in the {DAV:}supported-report-set property.
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function getSupportedReportSet($uri) {
+ return [self::REPORT_NAME];
+ }
+
+ /**
+ * 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 || $reportName !== self::REPORT_NAME) {
+ 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);
+ $comment->setMessage($data['message']);
+ $comment->setVerb($data['verb']);
+ $this->commentsManager->save($comment);
+ return $comment;
+ } catch (\InvalidArgumentException $e) {
+ throw new BadRequest('Invalid input values', 0, $e);
+ } catch (\OCP\Comments\MessageTooLongException $e) {
+ $msg = 'Message exceeds allowed character limit of ';
+ throw new BadRequest($msg . \OCP\Comments\IComment::MAX_MESSAGE_LENGTH, 0, $e);
+ }
+ }
+
+
+
+}
diff --git a/apps/dav/lib/Comments/EntityCollection.php b/apps/dav/lib/Comments/EntityCollection.php
new file mode 100644
index 00000000000..a55a18c00c0
--- /dev/null
+++ b/apps/dav/lib/Comments/EntityCollection.php
@@ -0,0 +1,199 @@
+<?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 OCP\IUserSession;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\PropPatch;
+
+/**
+ * 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 implements \Sabre\DAV\IProperties {
+ const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker';
+
+ /** @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 IUserSession $userSession
+ * @param ILogger $logger
+ */
+ public function __construct(
+ $id,
+ $name,
+ ICommentsManager $commentsManager,
+ Folder $fileRoot,
+ IUserManager $userManager,
+ IUserSession $userSession,
+ 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;
+ $this->userSession = $userSession;
+ }
+
+ /**
+ * 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->userSession,
+ $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->userSession,
+ $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;
+ }
+ }
+
+ /**
+ * Sets the read marker to the specified date for the logged in user
+ *
+ * @param \DateTime $value
+ * @return bool
+ */
+ public function setReadMarker($value) {
+ $dateTime = new \DateTime($value);
+ $user = $this->userSession->getUser();
+ $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user);
+ return true;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ function propPatch(PropPatch $propPatch) {
+ $propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ function getProperties($properties) {
+ $marker = null;
+ $user = $this->userSession->getUser();
+ if(!is_null($user)) {
+ $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user);
+ }
+ return [self::PROPERTY_NAME_READ_MARKER => $marker];
+ }
+}
+
diff --git a/apps/dav/lib/Comments/EntityTypeCollection.php b/apps/dav/lib/Comments/EntityTypeCollection.php
new file mode 100644
index 00000000000..6bc42484207
--- /dev/null
+++ b/apps/dav/lib/Comments/EntityTypeCollection.php
@@ -0,0 +1,125 @@
+<?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 OCP\IUserSession;
+use Sabre\DAV\Exception\MethodNotAllowed;
+use Sabre\DAV\Exception\NotFound;
+
+/**
+ * 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 IUserSession $userSession
+ * @param ILogger $logger
+ */
+ public function __construct(
+ $name,
+ ICommentsManager $commentsManager,
+ Folder $fileRoot,
+ IUserManager $userManager,
+ IUserSession $userSession,
+ 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;
+ $this->userSession = $userSession;
+ }
+
+ /**
+ * 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) {
+ if(!$this->childExists($name)) {
+ throw new NotFound('Entity does not exist or is not available');
+ }
+ return new EntityCollection(
+ $name,
+ $this->name,
+ $this->commentsManager,
+ $this->fileRoot,
+ $this->userManager,
+ $this->userSession,
+ $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(intval($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..cda666f7162
--- /dev/null
+++ b/apps/dav/lib/Comments/RootCollection.php
@@ -0,0 +1,205 @@
+<?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->userSession,
+ $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;
+ }
+}