1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Arthur Schiwon <[email protected]> |
6
|
|
|
* @author Joas Schilling <[email protected]> |
7
|
|
|
* @author Lukas Reschke <[email protected]> |
8
|
|
|
* @author Morris Jobke <[email protected]> |
9
|
|
|
* @author Robin Appelman <[email protected]> |
10
|
|
|
* @author Roeland Jago Douma <[email protected]> |
11
|
|
|
* @author Thomas Müller <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @license AGPL-3.0 |
14
|
|
|
* |
15
|
|
|
* This code is free software: you can redistribute it and/or modify |
16
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
17
|
|
|
* as published by the Free Software Foundation. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU Affero General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace OC\Comments; |
30
|
|
|
|
31
|
|
|
use Doctrine\DBAL\Exception\DriverException; |
32
|
|
|
use Doctrine\DBAL\Exception\InvalidFieldNameException; |
33
|
|
|
use OCP\Comments\CommentsEvent; |
34
|
|
|
use OCP\Comments\IComment; |
35
|
|
|
use OCP\Comments\ICommentsEventHandler; |
36
|
|
|
use OCP\Comments\ICommentsManager; |
37
|
|
|
use OCP\Comments\NotFoundException; |
38
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
39
|
|
|
use OCP\IConfig; |
40
|
|
|
use OCP\IDBConnection; |
41
|
|
|
use OCP\ILogger; |
42
|
|
|
use OCP\IUser; |
43
|
|
|
|
44
|
|
|
class Manager implements ICommentsManager { |
45
|
|
|
|
46
|
|
|
/** @var IDBConnection */ |
47
|
|
|
protected $dbConn; |
48
|
|
|
|
49
|
|
|
/** @var ILogger */ |
50
|
|
|
protected $logger; |
51
|
|
|
|
52
|
|
|
/** @var IConfig */ |
53
|
|
|
protected $config; |
54
|
|
|
|
55
|
|
|
/** @var IComment[] */ |
56
|
|
|
protected $commentsCache = []; |
57
|
|
|
|
58
|
|
|
/** @var \Closure[] */ |
59
|
|
|
protected $eventHandlerClosures = []; |
60
|
|
|
|
61
|
|
|
/** @var ICommentsEventHandler[] */ |
62
|
|
|
protected $eventHandlers = []; |
63
|
|
|
|
64
|
|
|
/** @var \Closure[] */ |
65
|
|
|
protected $displayNameResolvers = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Manager constructor. |
69
|
|
|
* |
70
|
|
|
* @param IDBConnection $dbConn |
71
|
|
|
* @param ILogger $logger |
72
|
|
|
* @param IConfig $config |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
IDBConnection $dbConn, |
76
|
|
|
ILogger $logger, |
77
|
|
|
IConfig $config |
78
|
|
|
) { |
79
|
|
|
$this->dbConn = $dbConn; |
80
|
|
|
$this->logger = $logger; |
81
|
|
|
$this->config = $config; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* converts data base data into PHP native, proper types as defined by |
86
|
|
|
* IComment interface. |
87
|
|
|
* |
88
|
|
|
* @param array $data |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function normalizeDatabaseData(array $data) { |
92
|
|
|
$data['id'] = (string)$data['id']; |
93
|
|
|
$data['parent_id'] = (string)$data['parent_id']; |
94
|
|
|
$data['topmost_parent_id'] = (string)$data['topmost_parent_id']; |
95
|
|
|
$data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
96
|
|
|
if (!is_null($data['latest_child_timestamp'])) { |
97
|
|
|
$data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
98
|
|
|
} |
99
|
|
|
$data['children_count'] = (int)$data['children_count']; |
100
|
|
|
$data['reference_id'] = $data['reference_id'] ?? null; |
101
|
|
|
return $data; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $data |
107
|
|
|
* @return IComment |
108
|
|
|
*/ |
109
|
|
|
public function getCommentFromData(array $data): IComment { |
110
|
|
|
return new Comment($this->normalizeDatabaseData($data)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* prepares a comment for an insert or update operation after making sure |
115
|
|
|
* all necessary fields have a value assigned. |
116
|
|
|
* |
117
|
|
|
* @param IComment $comment |
118
|
|
|
* @return IComment returns the same updated IComment instance as provided |
119
|
|
|
* by parameter for convenience |
120
|
|
|
* @throws \UnexpectedValueException |
121
|
|
|
*/ |
122
|
|
|
protected function prepareCommentForDatabaseWrite(IComment $comment) { |
123
|
|
|
if (!$comment->getActorType() |
124
|
|
|
|| $comment->getActorId() === '' |
125
|
|
|
|| !$comment->getObjectType() |
126
|
|
|
|| $comment->getObjectId() === '' |
127
|
|
|
|| !$comment->getVerb() |
128
|
|
|
) { |
129
|
|
|
throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($comment->getId() === '') { |
133
|
|
|
$comment->setChildrenCount(0); |
134
|
|
|
$comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC'))); |
135
|
|
|
$comment->setLatestChildDateTime(null); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (is_null($comment->getCreationDateTime())) { |
139
|
|
|
$comment->setCreationDateTime(new \DateTime()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($comment->getParentId() !== '0') { |
143
|
|
|
$comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId())); |
144
|
|
|
} else { |
145
|
|
|
$comment->setTopmostParentId('0'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->cache($comment); |
149
|
|
|
|
150
|
|
|
return $comment; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* returns the topmost parent id of a given comment identified by ID |
155
|
|
|
* |
156
|
|
|
* @param string $id |
157
|
|
|
* @return string |
158
|
|
|
* @throws NotFoundException |
159
|
|
|
*/ |
160
|
|
|
protected function determineTopmostParentId($id) { |
161
|
|
|
$comment = $this->get($id); |
162
|
|
|
if ($comment->getParentId() === '0') { |
163
|
|
|
return $comment->getId(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->determineTopmostParentId($comment->getParentId()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* updates child information of a comment |
171
|
|
|
* |
172
|
|
|
* @param string $id |
173
|
|
|
* @param \DateTime $cDateTime the date time of the most recent child |
174
|
|
|
* @throws NotFoundException |
175
|
|
|
*/ |
176
|
|
|
protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
177
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
178
|
|
|
$query = $qb->select($qb->func()->count('id')) |
179
|
|
|
->from('comments') |
180
|
|
|
->where($qb->expr()->eq('parent_id', $qb->createParameter('id'))) |
181
|
|
|
->setParameter('id', $id); |
182
|
|
|
|
183
|
|
|
$resultStatement = $query->execute(); |
184
|
|
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
185
|
|
|
$resultStatement->closeCursor(); |
186
|
|
|
$children = (int)$data[0]; |
187
|
|
|
|
188
|
|
|
$comment = $this->get($id); |
189
|
|
|
$comment->setChildrenCount($children); |
190
|
|
|
$comment->setLatestChildDateTime($cDateTime); |
191
|
|
|
$this->save($comment); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Tests whether actor or object type and id parameters are acceptable. |
196
|
|
|
* Throws exception if not. |
197
|
|
|
* |
198
|
|
|
* @param string $role |
199
|
|
|
* @param string $type |
200
|
|
|
* @param string $id |
201
|
|
|
* @throws \InvalidArgumentException |
202
|
|
|
*/ |
203
|
|
|
protected function checkRoleParameters($role, $type, $id) { |
204
|
|
|
if ( |
205
|
|
|
!is_string($type) || empty($type) |
|
|
|
|
206
|
|
|
|| !is_string($id) || empty($id) |
207
|
|
|
) { |
208
|
|
|
throw new \InvalidArgumentException($role . ' parameters must be string and not empty'); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* run-time caches a comment |
214
|
|
|
* |
215
|
|
|
* @param IComment $comment |
216
|
|
|
*/ |
217
|
|
|
protected function cache(IComment $comment) { |
218
|
|
|
$id = $comment->getId(); |
219
|
|
|
if (empty($id)) { |
220
|
|
|
return; |
221
|
|
|
} |
222
|
|
|
$this->commentsCache[(string)$id] = $comment; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* removes an entry from the comments run time cache |
227
|
|
|
* |
228
|
|
|
* @param mixed $id the comment's id |
229
|
|
|
*/ |
230
|
|
|
protected function uncache($id) { |
231
|
|
|
$id = (string)$id; |
232
|
|
|
if (isset($this->commentsCache[$id])) { |
233
|
|
|
unset($this->commentsCache[$id]); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* returns a comment instance |
239
|
|
|
* |
240
|
|
|
* @param string $id the ID of the comment |
241
|
|
|
* @return IComment |
242
|
|
|
* @throws NotFoundException |
243
|
|
|
* @throws \InvalidArgumentException |
244
|
|
|
* @since 9.0.0 |
245
|
|
|
*/ |
246
|
|
|
public function get($id) { |
247
|
|
|
if ((int)$id === 0) { |
248
|
|
|
throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if (isset($this->commentsCache[$id])) { |
252
|
|
|
return $this->commentsCache[$id]; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
256
|
|
|
$resultStatement = $qb->select('*') |
257
|
|
|
->from('comments') |
258
|
|
|
->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
259
|
|
|
->setParameter('id', $id, IQueryBuilder::PARAM_INT) |
260
|
|
|
->execute(); |
261
|
|
|
|
262
|
|
|
$data = $resultStatement->fetch(); |
263
|
|
|
$resultStatement->closeCursor(); |
264
|
|
|
if (!$data) { |
265
|
|
|
throw new NotFoundException(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
$comment = $this->getCommentFromData($data); |
270
|
|
|
$this->cache($comment); |
271
|
|
|
return $comment; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* returns the comment specified by the id and all it's child comments. |
276
|
|
|
* At this point of time, we do only support one level depth. |
277
|
|
|
* |
278
|
|
|
* @param string $id |
279
|
|
|
* @param int $limit max number of entries to return, 0 returns all |
280
|
|
|
* @param int $offset the start entry |
281
|
|
|
* @return array |
282
|
|
|
* @since 9.0.0 |
283
|
|
|
* |
284
|
|
|
* The return array looks like this |
285
|
|
|
* [ |
286
|
|
|
* 'comment' => IComment, // root comment |
287
|
|
|
* 'replies' => |
288
|
|
|
* [ |
289
|
|
|
* 0 => |
290
|
|
|
* [ |
291
|
|
|
* 'comment' => IComment, |
292
|
|
|
* 'replies' => [] |
293
|
|
|
* ] |
294
|
|
|
* 1 => |
295
|
|
|
* [ |
296
|
|
|
* 'comment' => IComment, |
297
|
|
|
* 'replies'=> [] |
298
|
|
|
* ], |
299
|
|
|
* … |
300
|
|
|
* ] |
301
|
|
|
* ] |
302
|
|
|
*/ |
303
|
|
|
public function getTree($id, $limit = 0, $offset = 0) { |
304
|
|
|
$tree = []; |
305
|
|
|
$tree['comment'] = $this->get($id); |
306
|
|
|
$tree['replies'] = []; |
307
|
|
|
|
308
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
309
|
|
|
$query = $qb->select('*') |
310
|
|
|
->from('comments') |
311
|
|
|
->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id'))) |
312
|
|
|
->orderBy('creation_timestamp', 'DESC') |
313
|
|
|
->setParameter('id', $id); |
314
|
|
|
|
315
|
|
|
if ($limit > 0) { |
316
|
|
|
$query->setMaxResults($limit); |
317
|
|
|
} |
318
|
|
|
if ($offset > 0) { |
319
|
|
|
$query->setFirstResult($offset); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
$resultStatement = $query->execute(); |
323
|
|
|
while ($data = $resultStatement->fetch()) { |
324
|
|
|
$comment = $this->getCommentFromData($data); |
325
|
|
|
$this->cache($comment); |
326
|
|
|
$tree['replies'][] = [ |
327
|
|
|
'comment' => $comment, |
328
|
|
|
'replies' => [] |
329
|
|
|
]; |
330
|
|
|
} |
331
|
|
|
$resultStatement->closeCursor(); |
332
|
|
|
|
333
|
|
|
return $tree; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* returns comments for a specific object (e.g. a file). |
338
|
|
|
* |
339
|
|
|
* The sort order is always newest to oldest. |
340
|
|
|
* |
341
|
|
|
* @param string $objectType the object type, e.g. 'files' |
342
|
|
|
* @param string $objectId the id of the object |
343
|
|
|
* @param int $limit optional, number of maximum comments to be returned. if |
344
|
|
|
* not specified, all comments are returned. |
345
|
|
|
* @param int $offset optional, starting point |
346
|
|
|
* @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
347
|
|
|
* that may be returned |
348
|
|
|
* @return IComment[] |
349
|
|
|
* @since 9.0.0 |
350
|
|
|
*/ |
351
|
|
|
public function getForObject( |
352
|
|
|
$objectType, |
353
|
|
|
$objectId, |
354
|
|
|
$limit = 0, |
355
|
|
|
$offset = 0, |
356
|
|
|
\DateTime $notOlderThan = null |
357
|
|
|
) { |
358
|
|
|
$comments = []; |
359
|
|
|
|
360
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
361
|
|
|
$query = $qb->select('*') |
362
|
|
|
->from('comments') |
363
|
|
|
->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
364
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
365
|
|
|
->orderBy('creation_timestamp', 'DESC') |
366
|
|
|
->setParameter('type', $objectType) |
367
|
|
|
->setParameter('id', $objectId); |
368
|
|
|
|
369
|
|
|
if ($limit > 0) { |
370
|
|
|
$query->setMaxResults($limit); |
371
|
|
|
} |
372
|
|
|
if ($offset > 0) { |
373
|
|
|
$query->setFirstResult($offset); |
374
|
|
|
} |
375
|
|
|
if (!is_null($notOlderThan)) { |
376
|
|
|
$query |
377
|
|
|
->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
378
|
|
|
->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
$resultStatement = $query->execute(); |
382
|
|
|
while ($data = $resultStatement->fetch()) { |
383
|
|
|
$comment = $this->getCommentFromData($data); |
384
|
|
|
$this->cache($comment); |
385
|
|
|
$comments[] = $comment; |
386
|
|
|
} |
387
|
|
|
$resultStatement->closeCursor(); |
388
|
|
|
|
389
|
|
|
return $comments; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* @param string $objectType the object type, e.g. 'files' |
394
|
|
|
* @param string $objectId the id of the object |
395
|
|
|
* @param int $lastKnownCommentId the last known comment (will be used as offset) |
396
|
|
|
* @param string $sortDirection direction of the comments (`asc` or `desc`) |
397
|
|
|
* @param int $limit optional, number of maximum comments to be returned. if |
398
|
|
|
* set to 0, all comments are returned. |
399
|
|
|
* @return IComment[] |
400
|
|
|
* @return array |
401
|
|
|
*/ |
402
|
|
|
public function getForObjectSince( |
403
|
|
|
string $objectType, |
404
|
|
|
string $objectId, |
405
|
|
|
int $lastKnownCommentId, |
406
|
|
|
string $sortDirection = 'asc', |
407
|
|
|
int $limit = 30 |
408
|
|
|
): array { |
409
|
|
|
$comments = []; |
410
|
|
|
|
411
|
|
|
$query = $this->dbConn->getQueryBuilder(); |
412
|
|
|
$query->select('*') |
413
|
|
|
->from('comments') |
414
|
|
|
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType))) |
415
|
|
|
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))) |
416
|
|
|
->orderBy('creation_timestamp', $sortDirection === 'desc' ? 'DESC' : 'ASC') |
417
|
|
|
->addOrderBy('id', $sortDirection === 'desc' ? 'DESC' : 'ASC'); |
418
|
|
|
|
419
|
|
|
if ($limit > 0) { |
420
|
|
|
$query->setMaxResults($limit); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
$lastKnownComment = $lastKnownCommentId > 0 ? $this->getLastKnownComment( |
424
|
|
|
$objectType, |
425
|
|
|
$objectId, |
426
|
|
|
$lastKnownCommentId |
427
|
|
|
) : null; |
428
|
|
|
if ($lastKnownComment instanceof IComment) { |
429
|
|
|
$lastKnownCommentDateTime = $lastKnownComment->getCreationDateTime(); |
430
|
|
|
if ($sortDirection === 'desc') { |
431
|
|
|
$query->andWhere( |
432
|
|
|
$query->expr()->orX( |
433
|
|
|
$query->expr()->lt( |
434
|
|
|
'creation_timestamp', |
435
|
|
|
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
436
|
|
|
IQueryBuilder::PARAM_DATE |
437
|
|
|
), |
438
|
|
|
$query->expr()->andX( |
439
|
|
|
$query->expr()->eq( |
440
|
|
|
'creation_timestamp', |
441
|
|
|
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
442
|
|
|
IQueryBuilder::PARAM_DATE |
443
|
|
|
), |
444
|
|
|
$query->expr()->lt('id', $query->createNamedParameter($lastKnownCommentId)) |
445
|
|
|
) |
446
|
|
|
) |
447
|
|
|
); |
448
|
|
|
} else { |
449
|
|
|
$query->andWhere( |
450
|
|
|
$query->expr()->orX( |
451
|
|
|
$query->expr()->gt( |
452
|
|
|
'creation_timestamp', |
453
|
|
|
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
454
|
|
|
IQueryBuilder::PARAM_DATE |
455
|
|
|
), |
456
|
|
|
$query->expr()->andX( |
457
|
|
|
$query->expr()->eq( |
458
|
|
|
'creation_timestamp', |
459
|
|
|
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
460
|
|
|
IQueryBuilder::PARAM_DATE |
461
|
|
|
), |
462
|
|
|
$query->expr()->gt('id', $query->createNamedParameter($lastKnownCommentId)) |
463
|
|
|
) |
464
|
|
|
) |
465
|
|
|
); |
466
|
|
|
} |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
$resultStatement = $query->execute(); |
470
|
|
|
while ($data = $resultStatement->fetch()) { |
471
|
|
|
$comment = $this->getCommentFromData($data); |
472
|
|
|
$this->cache($comment); |
473
|
|
|
$comments[] = $comment; |
474
|
|
|
} |
475
|
|
|
$resultStatement->closeCursor(); |
476
|
|
|
|
477
|
|
|
return $comments; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* @param string $objectType the object type, e.g. 'files' |
482
|
|
|
* @param string $objectId the id of the object |
483
|
|
|
* @param int $id the comment to look for |
484
|
|
|
* @return Comment|null |
485
|
|
|
*/ |
486
|
|
|
protected function getLastKnownComment(string $objectType, |
487
|
|
|
string $objectId, |
488
|
|
|
int $id) { |
489
|
|
|
$query = $this->dbConn->getQueryBuilder(); |
490
|
|
|
$query->select('*') |
491
|
|
|
->from('comments') |
492
|
|
|
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType))) |
493
|
|
|
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))) |
494
|
|
|
->andWhere($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
495
|
|
|
|
496
|
|
|
$result = $query->execute(); |
497
|
|
|
$row = $result->fetch(); |
498
|
|
|
$result->closeCursor(); |
499
|
|
|
|
500
|
|
|
if ($row) { |
501
|
|
|
$comment = $this->getCommentFromData($row); |
502
|
|
|
$this->cache($comment); |
503
|
|
|
return $comment; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
return null; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Search for comments with a given content |
511
|
|
|
* |
512
|
|
|
* @param string $search content to search for |
513
|
|
|
* @param string $objectType Limit the search by object type |
514
|
|
|
* @param string $objectId Limit the search by object id |
515
|
|
|
* @param string $verb Limit the verb of the comment |
516
|
|
|
* @param int $offset |
517
|
|
|
* @param int $limit |
518
|
|
|
* @return IComment[] |
519
|
|
|
*/ |
520
|
|
|
public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { |
521
|
|
|
$query = $this->dbConn->getQueryBuilder(); |
522
|
|
|
|
523
|
|
|
$query->select('*') |
524
|
|
|
->from('comments') |
525
|
|
|
->where($query->expr()->iLike('message', $query->createNamedParameter( |
526
|
|
|
'%' . $this->dbConn->escapeLikeParameter($search). '%' |
527
|
|
|
))) |
528
|
|
|
->orderBy('creation_timestamp', 'DESC') |
529
|
|
|
->addOrderBy('id', 'DESC') |
530
|
|
|
->setMaxResults($limit); |
531
|
|
|
|
532
|
|
|
if ($objectType !== '') { |
533
|
|
|
$query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType))); |
534
|
|
|
} |
535
|
|
|
if ($objectId !== '') { |
536
|
|
|
$query->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))); |
537
|
|
|
} |
538
|
|
|
if ($verb !== '') { |
539
|
|
|
$query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb))); |
540
|
|
|
} |
541
|
|
|
if ($offset !== 0) { |
542
|
|
|
$query->setFirstResult($offset); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
$comments = []; |
546
|
|
|
$result = $query->execute(); |
547
|
|
|
while ($data = $result->fetch()) { |
548
|
|
|
$comment = $this->getCommentFromData($data); |
549
|
|
|
$this->cache($comment); |
550
|
|
|
$comments[] = $comment; |
551
|
|
|
} |
552
|
|
|
$result->closeCursor(); |
553
|
|
|
|
554
|
|
|
return $comments; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* @param $objectType string the object type, e.g. 'files' |
559
|
|
|
* @param $objectId string the id of the object |
560
|
|
|
* @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
561
|
|
|
* that may be returned |
562
|
|
|
* @param string $verb Limit the verb of the comment - Added in 14.0.0 |
563
|
|
|
* @return Int |
564
|
|
|
* @since 9.0.0 |
565
|
|
|
*/ |
566
|
|
|
public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null, $verb = '') { |
567
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
568
|
|
|
$query = $qb->select($qb->func()->count('id')) |
569
|
|
|
->from('comments') |
570
|
|
|
->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
571
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
572
|
|
|
->setParameter('type', $objectType) |
573
|
|
|
->setParameter('id', $objectId); |
574
|
|
|
|
575
|
|
|
if (!is_null($notOlderThan)) { |
576
|
|
|
$query |
577
|
|
|
->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
578
|
|
|
->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
if ($verb !== '') { |
582
|
|
|
$query->andWhere($qb->expr()->eq('verb', $qb->createNamedParameter($verb))); |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
$resultStatement = $query->execute(); |
586
|
|
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
587
|
|
|
$resultStatement->closeCursor(); |
588
|
|
|
return (int)$data[0]; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* Get the number of unread comments for all files in a folder |
593
|
|
|
* |
594
|
|
|
* @param int $folderId |
595
|
|
|
* @param IUser $user |
596
|
|
|
* @return array [$fileId => $unreadCount] |
597
|
|
|
* |
598
|
|
|
* @suppress SqlInjectionChecker |
599
|
|
|
*/ |
600
|
|
|
public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { |
601
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
602
|
|
|
|
603
|
|
|
$query = $qb->select('f.fileid') |
604
|
|
|
->addSelect($qb->func()->count('c.id', 'num_ids')) |
605
|
|
|
->from('filecache', 'f') |
606
|
|
|
->leftJoin('f', 'comments', 'c', $qb->expr()->andX( |
|
|
|
|
607
|
|
|
$qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)), |
608
|
|
|
$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')) |
609
|
|
|
)) |
610
|
|
|
->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX( |
611
|
|
|
$qb->expr()->eq('c.object_id', 'm.object_id'), |
612
|
|
|
$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')) |
613
|
|
|
)) |
614
|
|
|
->where( |
615
|
|
|
$qb->expr()->andX( |
616
|
|
|
$qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId)), |
617
|
|
|
$qb->expr()->orX( |
618
|
|
|
$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')), |
619
|
|
|
$qb->expr()->isNull('c.object_type') |
620
|
|
|
), |
621
|
|
|
$qb->expr()->orX( |
622
|
|
|
$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')), |
623
|
|
|
$qb->expr()->isNull('m.object_type') |
624
|
|
|
), |
625
|
|
|
$qb->expr()->orX( |
626
|
|
|
$qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())), |
627
|
|
|
$qb->expr()->isNull('m.user_id') |
628
|
|
|
), |
629
|
|
|
$qb->expr()->orX( |
630
|
|
|
$qb->expr()->gt('c.creation_timestamp', 'm.marker_datetime'), |
631
|
|
|
$qb->expr()->isNull('m.marker_datetime') |
632
|
|
|
) |
633
|
|
|
) |
634
|
|
|
)->groupBy('f.fileid'); |
635
|
|
|
|
636
|
|
|
$resultStatement = $query->execute(); |
637
|
|
|
|
638
|
|
|
$results = []; |
639
|
|
|
while ($row = $resultStatement->fetch()) { |
640
|
|
|
$results[$row['fileid']] = (int) $row['num_ids']; |
641
|
|
|
} |
642
|
|
|
$resultStatement->closeCursor(); |
643
|
|
|
return $results; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* creates a new comment and returns it. At this point of time, it is not |
648
|
|
|
* saved in the used data storage. Use save() after setting other fields |
649
|
|
|
* of the comment (e.g. message or verb). |
650
|
|
|
* |
651
|
|
|
* @param string $actorType the actor type (e.g. 'users') |
652
|
|
|
* @param string $actorId a user id |
653
|
|
|
* @param string $objectType the object type the comment is attached to |
654
|
|
|
* @param string $objectId the object id the comment is attached to |
655
|
|
|
* @return IComment |
656
|
|
|
* @since 9.0.0 |
657
|
|
|
*/ |
658
|
|
|
public function create($actorType, $actorId, $objectType, $objectId) { |
659
|
|
|
$comment = new Comment(); |
660
|
|
|
$comment |
661
|
|
|
->setActor($actorType, $actorId) |
662
|
|
|
->setObject($objectType, $objectId); |
663
|
|
|
return $comment; |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
/** |
667
|
|
|
* permanently deletes the comment specified by the ID |
668
|
|
|
* |
669
|
|
|
* When the comment has child comments, their parent ID will be changed to |
670
|
|
|
* the parent ID of the item that is to be deleted. |
671
|
|
|
* |
672
|
|
|
* @param string $id |
673
|
|
|
* @return bool |
674
|
|
|
* @throws \InvalidArgumentException |
675
|
|
|
* @since 9.0.0 |
676
|
|
|
*/ |
677
|
|
|
public function delete($id) { |
678
|
|
|
if (!is_string($id)) { |
|
|
|
|
679
|
|
|
throw new \InvalidArgumentException('Parameter must be string'); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
try { |
683
|
|
|
$comment = $this->get($id); |
684
|
|
|
} catch (\Exception $e) { |
685
|
|
|
// Ignore exceptions, we just don't fire a hook then |
686
|
|
|
$comment = null; |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
690
|
|
|
$query = $qb->delete('comments') |
691
|
|
|
->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
692
|
|
|
->setParameter('id', $id); |
693
|
|
|
|
694
|
|
|
try { |
695
|
|
|
$affectedRows = $query->execute(); |
696
|
|
|
$this->uncache($id); |
697
|
|
|
} catch (DriverException $e) { |
698
|
|
|
$this->logger->logException($e, ['app' => 'core_comments']); |
699
|
|
|
return false; |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
if ($affectedRows > 0 && $comment instanceof IComment) { |
703
|
|
|
$this->sendEvent(CommentsEvent::EVENT_DELETE, $comment); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
return ($affectedRows > 0); |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* saves the comment permanently |
711
|
|
|
* |
712
|
|
|
* if the supplied comment has an empty ID, a new entry comment will be |
713
|
|
|
* saved and the instance updated with the new ID. |
714
|
|
|
* |
715
|
|
|
* Otherwise, an existing comment will be updated. |
716
|
|
|
* |
717
|
|
|
* Throws NotFoundException when a comment that is to be updated does not |
718
|
|
|
* exist anymore at this point of time. |
719
|
|
|
* |
720
|
|
|
* @param IComment $comment |
721
|
|
|
* @return bool |
722
|
|
|
* @throws NotFoundException |
723
|
|
|
* @since 9.0.0 |
724
|
|
|
*/ |
725
|
|
|
public function save(IComment $comment) { |
726
|
|
|
if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') { |
727
|
|
|
$result = $this->insert($comment); |
728
|
|
|
} else { |
729
|
|
|
$result = $this->update($comment); |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
if ($result && !!$comment->getParentId()) { |
733
|
|
|
$this->updateChildrenInformation( |
734
|
|
|
$comment->getParentId(), |
735
|
|
|
$comment->getCreationDateTime() |
736
|
|
|
); |
737
|
|
|
$this->cache($comment); |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
return $result; |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* inserts the provided comment in the database |
745
|
|
|
* |
746
|
|
|
* @param IComment $comment |
747
|
|
|
* @return bool |
748
|
|
|
*/ |
749
|
|
|
protected function insert(IComment $comment): bool { |
750
|
|
|
|
751
|
|
|
try { |
752
|
|
|
$result = $this->insertQuery($comment, true); |
753
|
|
|
} catch (InvalidFieldNameException $e) { |
754
|
|
|
// The reference id field was only added in Nextcloud 19. |
755
|
|
|
// In order to not cause too long waiting times on the update, |
756
|
|
|
// it was decided to only add it lazy, as it is also not a critical |
757
|
|
|
// feature, but only helps to have a better experience while commenting. |
758
|
|
|
// So in case the reference_id field is missing, |
759
|
|
|
// we simply save the comment without that field. |
760
|
|
|
$result = $this->insertQuery($comment, false); |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
return $result; |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
protected function insertQuery(IComment $comment, bool $tryWritingReferenceId): bool { |
767
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
768
|
|
|
|
769
|
|
|
$values = [ |
770
|
|
|
'parent_id' => $qb->createNamedParameter($comment->getParentId()), |
771
|
|
|
'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()), |
772
|
|
|
'children_count' => $qb->createNamedParameter($comment->getChildrenCount()), |
773
|
|
|
'actor_type' => $qb->createNamedParameter($comment->getActorType()), |
774
|
|
|
'actor_id' => $qb->createNamedParameter($comment->getActorId()), |
775
|
|
|
'message' => $qb->createNamedParameter($comment->getMessage()), |
776
|
|
|
'verb' => $qb->createNamedParameter($comment->getVerb()), |
777
|
|
|
'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'), |
778
|
|
|
'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'), |
779
|
|
|
'object_type' => $qb->createNamedParameter($comment->getObjectType()), |
780
|
|
|
'object_id' => $qb->createNamedParameter($comment->getObjectId()), |
781
|
|
|
]; |
782
|
|
|
|
783
|
|
|
if ($tryWritingReferenceId) { |
784
|
|
|
$values['reference_id'] = $qb->createNamedParameter($comment->getReferenceId()); |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
$affectedRows = $qb->insert('comments') |
788
|
|
|
->values($values) |
789
|
|
|
->execute(); |
790
|
|
|
|
791
|
|
|
if ($affectedRows > 0) { |
792
|
|
|
$comment->setId((string)$qb->getLastInsertId()); |
793
|
|
|
$this->sendEvent(CommentsEvent::EVENT_ADD, $comment); |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
return $affectedRows > 0; |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
/** |
800
|
|
|
* updates a Comment data row |
801
|
|
|
* |
802
|
|
|
* @param IComment $comment |
803
|
|
|
* @return bool |
804
|
|
|
* @throws NotFoundException |
805
|
|
|
*/ |
806
|
|
|
protected function update(IComment $comment) { |
807
|
|
|
// for properly working preUpdate Events we need the old comments as is |
808
|
|
|
// in the DB and overcome caching. Also avoid that outdated information stays. |
809
|
|
|
$this->uncache($comment->getId()); |
810
|
|
|
$this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId())); |
811
|
|
|
$this->uncache($comment->getId()); |
812
|
|
|
|
813
|
|
|
try { |
814
|
|
|
$result = $this->updateQuery($comment, true); |
815
|
|
|
} catch (InvalidFieldNameException $e) { |
816
|
|
|
// See function insert() for explanation |
817
|
|
|
$result = $this->updateQuery($comment, false); |
818
|
|
|
} |
819
|
|
|
|
820
|
|
|
$this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment); |
821
|
|
|
|
822
|
|
|
return $result; |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
protected function updateQuery(IComment $comment, bool $tryWritingReferenceId): bool { |
826
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
827
|
|
|
$qb |
828
|
|
|
->update('comments') |
829
|
|
|
->set('parent_id', $qb->createNamedParameter($comment->getParentId())) |
830
|
|
|
->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId())) |
831
|
|
|
->set('children_count', $qb->createNamedParameter($comment->getChildrenCount())) |
832
|
|
|
->set('actor_type', $qb->createNamedParameter($comment->getActorType())) |
833
|
|
|
->set('actor_id', $qb->createNamedParameter($comment->getActorId())) |
834
|
|
|
->set('message', $qb->createNamedParameter($comment->getMessage())) |
835
|
|
|
->set('verb', $qb->createNamedParameter($comment->getVerb())) |
836
|
|
|
->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime')) |
837
|
|
|
->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime')) |
838
|
|
|
->set('object_type', $qb->createNamedParameter($comment->getObjectType())) |
839
|
|
|
->set('object_id', $qb->createNamedParameter($comment->getObjectId())); |
840
|
|
|
|
841
|
|
|
if ($tryWritingReferenceId) { |
842
|
|
|
$qb->set('reference_id', $qb->createNamedParameter($comment->getReferenceId())); |
843
|
|
|
} |
844
|
|
|
|
845
|
|
|
$affectedRows = $qb->where($qb->expr()->eq('id', $qb->createNamedParameter($comment->getId()))) |
846
|
|
|
->execute(); |
847
|
|
|
|
848
|
|
|
if ($affectedRows === 0) { |
849
|
|
|
throw new NotFoundException('Comment to update does ceased to exist'); |
850
|
|
|
} |
851
|
|
|
|
852
|
|
|
return $affectedRows > 0; |
853
|
|
|
} |
854
|
|
|
|
855
|
|
|
/** |
856
|
|
|
* removes references to specific actor (e.g. on user delete) of a comment. |
857
|
|
|
* The comment itself must not get lost/deleted. |
858
|
|
|
* |
859
|
|
|
* @param string $actorType the actor type (e.g. 'users') |
860
|
|
|
* @param string $actorId a user id |
861
|
|
|
* @return boolean |
862
|
|
|
* @since 9.0.0 |
863
|
|
|
*/ |
864
|
|
|
public function deleteReferencesOfActor($actorType, $actorId) { |
865
|
|
|
$this->checkRoleParameters('Actor', $actorType, $actorId); |
866
|
|
|
|
867
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
868
|
|
|
$affectedRows = $qb |
869
|
|
|
->update('comments') |
870
|
|
|
->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
871
|
|
|
->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
872
|
|
|
->where($qb->expr()->eq('actor_type', $qb->createParameter('type'))) |
873
|
|
|
->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id'))) |
874
|
|
|
->setParameter('type', $actorType) |
875
|
|
|
->setParameter('id', $actorId) |
876
|
|
|
->execute(); |
877
|
|
|
|
878
|
|
|
$this->commentsCache = []; |
879
|
|
|
|
880
|
|
|
return is_int($affectedRows); |
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
/** |
884
|
|
|
* deletes all comments made of a specific object (e.g. on file delete) |
885
|
|
|
* |
886
|
|
|
* @param string $objectType the object type (e.g. 'files') |
887
|
|
|
* @param string $objectId e.g. the file id |
888
|
|
|
* @return boolean |
889
|
|
|
* @since 9.0.0 |
890
|
|
|
*/ |
891
|
|
|
public function deleteCommentsAtObject($objectType, $objectId) { |
892
|
|
|
$this->checkRoleParameters('Object', $objectType, $objectId); |
893
|
|
|
|
894
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
895
|
|
|
$affectedRows = $qb |
896
|
|
|
->delete('comments') |
897
|
|
|
->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
898
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
899
|
|
|
->setParameter('type', $objectType) |
900
|
|
|
->setParameter('id', $objectId) |
901
|
|
|
->execute(); |
902
|
|
|
|
903
|
|
|
$this->commentsCache = []; |
904
|
|
|
|
905
|
|
|
return is_int($affectedRows); |
906
|
|
|
} |
907
|
|
|
|
908
|
|
|
/** |
909
|
|
|
* deletes the read markers for the specified user |
910
|
|
|
* |
911
|
|
|
* @param \OCP\IUser $user |
912
|
|
|
* @return bool |
913
|
|
|
* @since 9.0.0 |
914
|
|
|
*/ |
915
|
|
|
public function deleteReadMarksFromUser(IUser $user) { |
916
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
917
|
|
|
$query = $qb->delete('comments_read_markers') |
918
|
|
|
->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
919
|
|
|
->setParameter('user_id', $user->getUID()); |
920
|
|
|
|
921
|
|
|
try { |
922
|
|
|
$affectedRows = $query->execute(); |
923
|
|
|
} catch (DriverException $e) { |
924
|
|
|
$this->logger->logException($e, ['app' => 'core_comments']); |
925
|
|
|
return false; |
926
|
|
|
} |
927
|
|
|
return ($affectedRows > 0); |
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
/** |
931
|
|
|
* sets the read marker for a given file to the specified date for the |
932
|
|
|
* provided user |
933
|
|
|
* |
934
|
|
|
* @param string $objectType |
935
|
|
|
* @param string $objectId |
936
|
|
|
* @param \DateTime $dateTime |
937
|
|
|
* @param IUser $user |
938
|
|
|
* @since 9.0.0 |
939
|
|
|
* @suppress SqlInjectionChecker |
940
|
|
|
*/ |
941
|
|
|
public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) { |
942
|
|
|
$this->checkRoleParameters('Object', $objectType, $objectId); |
943
|
|
|
|
944
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
945
|
|
|
$values = [ |
946
|
|
|
'user_id' => $qb->createNamedParameter($user->getUID()), |
947
|
|
|
'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'), |
948
|
|
|
'object_type' => $qb->createNamedParameter($objectType), |
949
|
|
|
'object_id' => $qb->createNamedParameter($objectId), |
950
|
|
|
]; |
951
|
|
|
|
952
|
|
|
// Strategy: try to update, if this does not return affected rows, do an insert. |
953
|
|
|
$affectedRows = $qb |
954
|
|
|
->update('comments_read_markers') |
955
|
|
|
->set('user_id', $values['user_id']) |
956
|
|
|
->set('marker_datetime', $values['marker_datetime']) |
957
|
|
|
->set('object_type', $values['object_type']) |
958
|
|
|
->set('object_id', $values['object_id']) |
959
|
|
|
->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
960
|
|
|
->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
961
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
962
|
|
|
->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
963
|
|
|
->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
964
|
|
|
->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
965
|
|
|
->execute(); |
966
|
|
|
|
967
|
|
|
if ($affectedRows > 0) { |
968
|
|
|
return; |
969
|
|
|
} |
970
|
|
|
|
971
|
|
|
$qb->insert('comments_read_markers') |
972
|
|
|
->values($values) |
973
|
|
|
->execute(); |
974
|
|
|
} |
975
|
|
|
|
976
|
|
|
/** |
977
|
|
|
* returns the read marker for a given file to the specified date for the |
978
|
|
|
* provided user. It returns null, when the marker is not present, i.e. |
979
|
|
|
* no comments were marked as read. |
980
|
|
|
* |
981
|
|
|
* @param string $objectType |
982
|
|
|
* @param string $objectId |
983
|
|
|
* @param IUser $user |
984
|
|
|
* @return \DateTime|null |
985
|
|
|
* @since 9.0.0 |
986
|
|
|
*/ |
987
|
|
|
public function getReadMark($objectType, $objectId, IUser $user) { |
988
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
989
|
|
|
$resultStatement = $qb->select('marker_datetime') |
990
|
|
|
->from('comments_read_markers') |
991
|
|
|
->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
992
|
|
|
->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
993
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
994
|
|
|
->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
995
|
|
|
->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
996
|
|
|
->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
997
|
|
|
->execute(); |
998
|
|
|
|
999
|
|
|
$data = $resultStatement->fetch(); |
1000
|
|
|
$resultStatement->closeCursor(); |
1001
|
|
|
if (!$data || is_null($data['marker_datetime'])) { |
1002
|
|
|
return null; |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
return new \DateTime($data['marker_datetime']); |
1006
|
|
|
} |
1007
|
|
|
|
1008
|
|
|
/** |
1009
|
|
|
* deletes the read markers on the specified object |
1010
|
|
|
* |
1011
|
|
|
* @param string $objectType |
1012
|
|
|
* @param string $objectId |
1013
|
|
|
* @return bool |
1014
|
|
|
* @since 9.0.0 |
1015
|
|
|
*/ |
1016
|
|
|
public function deleteReadMarksOnObject($objectType, $objectId) { |
1017
|
|
|
$this->checkRoleParameters('Object', $objectType, $objectId); |
1018
|
|
|
|
1019
|
|
|
$qb = $this->dbConn->getQueryBuilder(); |
1020
|
|
|
$query = $qb->delete('comments_read_markers') |
1021
|
|
|
->where($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
1022
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
1023
|
|
|
->setParameter('object_type', $objectType) |
1024
|
|
|
->setParameter('object_id', $objectId); |
1025
|
|
|
|
1026
|
|
|
try { |
1027
|
|
|
$affectedRows = $query->execute(); |
1028
|
|
|
} catch (DriverException $e) { |
1029
|
|
|
$this->logger->logException($e, ['app' => 'core_comments']); |
1030
|
|
|
return false; |
1031
|
|
|
} |
1032
|
|
|
return ($affectedRows > 0); |
1033
|
|
|
} |
1034
|
|
|
|
1035
|
|
|
/** |
1036
|
|
|
* registers an Entity to the manager, so event notifications can be send |
1037
|
|
|
* to consumers of the comments infrastructure |
1038
|
|
|
* |
1039
|
|
|
* @param \Closure $closure |
1040
|
|
|
*/ |
1041
|
|
|
public function registerEventHandler(\Closure $closure) { |
1042
|
|
|
$this->eventHandlerClosures[] = $closure; |
1043
|
|
|
$this->eventHandlers = []; |
1044
|
|
|
} |
1045
|
|
|
|
1046
|
|
|
/** |
1047
|
|
|
* registers a method that resolves an ID to a display name for a given type |
1048
|
|
|
* |
1049
|
|
|
* @param string $type |
1050
|
|
|
* @param \Closure $closure |
1051
|
|
|
* @throws \OutOfBoundsException |
1052
|
|
|
* @since 11.0.0 |
1053
|
|
|
* |
1054
|
|
|
* Only one resolver shall be registered per type. Otherwise a |
1055
|
|
|
* \OutOfBoundsException has to thrown. |
1056
|
|
|
*/ |
1057
|
|
|
public function registerDisplayNameResolver($type, \Closure $closure) { |
1058
|
|
|
if (!is_string($type)) { |
|
|
|
|
1059
|
|
|
throw new \InvalidArgumentException('String expected.'); |
1060
|
|
|
} |
1061
|
|
|
if (isset($this->displayNameResolvers[$type])) { |
1062
|
|
|
throw new \OutOfBoundsException('Displayname resolver for this type already registered'); |
1063
|
|
|
} |
1064
|
|
|
$this->displayNameResolvers[$type] = $closure; |
1065
|
|
|
} |
1066
|
|
|
|
1067
|
|
|
/** |
1068
|
|
|
* resolves a given ID of a given Type to a display name. |
1069
|
|
|
* |
1070
|
|
|
* @param string $type |
1071
|
|
|
* @param string $id |
1072
|
|
|
* @return string |
1073
|
|
|
* @throws \OutOfBoundsException |
1074
|
|
|
* @since 11.0.0 |
1075
|
|
|
* |
1076
|
|
|
* If a provided type was not registered, an \OutOfBoundsException shall |
1077
|
|
|
* be thrown. It is upon the resolver discretion what to return of the |
1078
|
|
|
* provided ID is unknown. It must be ensured that a string is returned. |
1079
|
|
|
*/ |
1080
|
|
|
public function resolveDisplayName($type, $id) { |
1081
|
|
|
if (!is_string($type)) { |
|
|
|
|
1082
|
|
|
throw new \InvalidArgumentException('String expected.'); |
1083
|
|
|
} |
1084
|
|
|
if (!isset($this->displayNameResolvers[$type])) { |
1085
|
|
|
throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
1086
|
|
|
} |
1087
|
|
|
return (string)$this->displayNameResolvers[$type]($id); |
1088
|
|
|
} |
1089
|
|
|
|
1090
|
|
|
/** |
1091
|
|
|
* returns valid, registered entities |
1092
|
|
|
* |
1093
|
|
|
* @return \OCP\Comments\ICommentsEventHandler[] |
1094
|
|
|
*/ |
1095
|
|
|
private function getEventHandlers() { |
1096
|
|
|
if (!empty($this->eventHandlers)) { |
1097
|
|
|
return $this->eventHandlers; |
1098
|
|
|
} |
1099
|
|
|
|
1100
|
|
|
$this->eventHandlers = []; |
1101
|
|
|
foreach ($this->eventHandlerClosures as $name => $closure) { |
1102
|
|
|
$entity = $closure(); |
1103
|
|
|
if (!($entity instanceof ICommentsEventHandler)) { |
1104
|
|
|
throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface'); |
1105
|
|
|
} |
1106
|
|
|
$this->eventHandlers[$name] = $entity; |
1107
|
|
|
} |
1108
|
|
|
|
1109
|
|
|
return $this->eventHandlers; |
1110
|
|
|
} |
1111
|
|
|
|
1112
|
|
|
/** |
1113
|
|
|
* sends notifications to the registered entities |
1114
|
|
|
* |
1115
|
|
|
* @param $eventType |
1116
|
|
|
* @param IComment $comment |
1117
|
|
|
*/ |
1118
|
|
|
private function sendEvent($eventType, IComment $comment) { |
1119
|
|
|
$entities = $this->getEventHandlers(); |
1120
|
|
|
$event = new CommentsEvent($eventType, $comment); |
1121
|
|
|
foreach ($entities as $entity) { |
1122
|
|
|
$entity->handle($event); |
1123
|
|
|
} |
1124
|
|
|
} |
1125
|
|
|
} |
1126
|
|
|
|