1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Joas Schilling <coding@schilljs.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Vincent Petry <pvince81@owncloud.com>
*
* @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\NotFound;
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
public const NS_OWNCLOUD = 'http://owncloud.org/ns';
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';
public 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
*/
public 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 array $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);
}
}
}
|