2016-01-11 20:09:00 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2016-01-11 20:09:00 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-01-11 20:09:00 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Comments;
|
|
|
|
|
|
|
|
use OCP\Comments\IComment;
|
|
|
|
use OCP\Comments\ICommentsManager;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use Sabre\DAV\Exception\BadRequest;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Sabre\DAV\Exception\NotFound;
|
2016-01-11 20:09:00 +03:00
|
|
|
use Sabre\DAV\Exception\ReportNotSupported;
|
|
|
|
use Sabre\DAV\Exception\UnsupportedMediaType;
|
|
|
|
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
|
2020-04-10 17:54:27 +03:00
|
|
|
public const NS_OWNCLOUD = 'http://owncloud.org/ns';
|
2016-01-11 20:09:00 +03:00
|
|
|
|
2020-10-05 16:12:57 +03:00
|
|
|
public const REPORT_NAME = '{http://owncloud.org/ns}filter-comments';
|
|
|
|
public const REPORT_PARAM_LIMIT = '{http://owncloud.org/ns}limit';
|
|
|
|
public const REPORT_PARAM_OFFSET = '{http://owncloud.org/ns}offset';
|
2020-04-10 17:54:27 +03:00
|
|
|
public const REPORT_PARAM_TIMESTAMP = '{http://owncloud.org/ns}datetime';
|
2016-01-11 20:09:00 +03:00
|
|
|
|
|
|
|
/** @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
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function initialize(Server $server) {
|
2016-01-11 20:09:00 +03:00
|
|
|
$this->server = $server;
|
2020-04-10 15:19:56 +03:00
|
|
|
if (strpos($this->server->getRequestUri(), 'comments/') !== 0) {
|
2016-01-11 20:09:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->server->xml->classMap['DateTime'] = function (Writer $writer, \DateTime $value) {
|
2016-01-28 23:45:04 +03:00
|
|
|
$writer->write(\Sabre\HTTP\toDate($value));
|
2016-01-11 20:09:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
$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();
|
2016-01-26 14:08:51 +03:00
|
|
|
$node = $this->server->tree->getNodeForPath($path);
|
|
|
|
if (!$node instanceof EntityCollection) {
|
2016-01-11 20:09:00 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-01-26 14:08:51 +03:00
|
|
|
$data = $request->getBodyAsString();
|
|
|
|
$comment = $this->createComment(
|
|
|
|
$node->getName(),
|
|
|
|
$node->getId(),
|
|
|
|
$data,
|
|
|
|
$request->getHeader('Content-Type')
|
|
|
|
);
|
2016-02-05 18:30:37 +03:00
|
|
|
|
|
|
|
// update read marker for the current user/poster to avoid
|
|
|
|
// having their own comments marked as unread
|
|
|
|
$node->setReadMarker(null);
|
|
|
|
|
2016-02-15 12:54:56 +03:00
|
|
|
$url = rtrim($request->getUrl(), '/') . '/' . urlencode($comment->getId());
|
2016-01-11 20:09:00 +03:00
|
|
|
|
2016-01-26 14:08:51 +03:00
|
|
|
$response->setHeader('Content-Location', $url);
|
2016-01-11 20:09:00 +03:00
|
|
|
|
2016-01-26 14:08:51 +03:00
|
|
|
// created
|
|
|
|
$response->setStatus(201);
|
|
|
|
return false;
|
2016-01-11 20:09:00 +03:00
|
|
|
}
|
|
|
|
|
2016-01-26 23:19:44 +03:00
|
|
|
/**
|
|
|
|
* 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];
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:09:00 +03:00
|
|
|
/**
|
|
|
|
* REPORT operations to look for comments
|
|
|
|
*
|
|
|
|
* @param string $reportName
|
2016-08-16 21:28:23 +03:00
|
|
|
* @param array $report
|
2016-01-11 20:09:00 +03:00
|
|
|
* @param string $uri
|
|
|
|
* @return bool
|
|
|
|
* @throws NotFound
|
|
|
|
* @throws ReportNotSupported
|
|
|
|
*/
|
|
|
|
public function onReport($reportName, $report, $uri) {
|
|
|
|
$node = $this->server->tree->getNodeForPath($uri);
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$node instanceof EntityCollection || $reportName !== self::REPORT_NAME) {
|
2016-01-11 20:09:00 +03:00
|
|
|
throw new ReportNotSupported();
|
|
|
|
}
|
|
|
|
$args = ['limit' => 0, 'offset' => 0, 'datetime' => null];
|
|
|
|
$acceptableParameters = [
|
|
|
|
$this::REPORT_PARAM_LIMIT,
|
|
|
|
$this::REPORT_PARAM_OFFSET,
|
2016-01-26 15:04:41 +03:00
|
|
|
$this::REPORT_PARAM_TIMESTAMP
|
2016-01-11 20:09:00 +03:00
|
|
|
];
|
|
|
|
$ns = '{' . $this::NS_OWNCLOUD . '}';
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($report as $parameter) {
|
|
|
|
if (!in_array($parameter['name'], $acceptableParameters) || empty($parameter['value'])) {
|
2016-01-11 20:09:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$args[str_replace($ns, '', $parameter['name'])] = $parameter['value'];
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($args['datetime'])) {
|
2016-01-26 15:04:41 +03:00
|
|
|
$args['datetime'] = new \DateTime($args['datetime']);
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:09:00 +03:00
|
|
|
$results = $node->findChildren($args['limit'], $args['offset'], $args['datetime']);
|
|
|
|
|
|
|
|
$responses = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($results as $node) {
|
2016-01-11 20:09:00 +03:00
|
|
|
$nodePath = $this->server->getRequestUri() . '/' . $node->comment->getId();
|
|
|
|
$resultSet = $this->server->getPropertiesForPath($nodePath, CommentNode::getPropertyNames());
|
2020-04-10 15:19:56 +03:00
|
|
|
if (isset($resultSet[0]) && isset($resultSet[0][200])) {
|
2016-01-11 20:09:00 +03:00
|
|
|
$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;
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($actorType === 'users') {
|
2016-01-11 20:09:00 +03:00
|
|
|
$user = $this->userSession->getUser();
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($user)) {
|
2016-01-11 20:09:00 +03:00
|
|
|
$actorId = $user->getUID();
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if (is_null($actorId)) {
|
2016-01-11 20:09:00 +03:00
|
|
|
throw new BadRequest('Invalid actor "' . $actorType .'"');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$comment = $this->commentsManager->create($actorType, $actorId, $objectType, $objectId);
|
2016-01-26 14:08:51 +03:00
|
|
|
$comment->setMessage($data['message']);
|
|
|
|
$comment->setVerb($data['verb']);
|
2016-01-11 20:09:00 +03:00
|
|
|
$this->commentsManager->save($comment);
|
|
|
|
return $comment;
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new BadRequest('Invalid input values', 0, $e);
|
2016-02-09 15:59:13 +03:00
|
|
|
} catch (\OCP\Comments\MessageTooLongException $e) {
|
|
|
|
$msg = 'Message exceeds allowed character limit of ';
|
|
|
|
throw new BadRequest($msg . \OCP\Comments\IComment::MAX_MESSAGE_LENGTH, 0, $e);
|
2016-01-11 20:09:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|