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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
<?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 Thomas Müller <thomas.mueller@tmit.eu>
*
* @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 OC\Comments;
use OCP\Comments\IComment;
use OCP\Comments\IllegalIDChangeException;
use OCP\Comments\MessageTooLongException;
class Comment implements IComment {
protected $data = [
'id' => '',
'parentId' => '0',
'topmostParentId' => '0',
'childrenCount' => '0',
'message' => '',
'verb' => '',
'actorType' => '',
'actorId' => '',
'objectType' => '',
'objectId' => '',
'creationDT' => null,
'latestChildDT' => null,
];
/**
* Comment constructor.
*
* @param array $data optional, array with keys according to column names from
* the comments database scheme
*/
public function __construct(array $data = null) {
if(is_array($data)) {
$this->fromArray($data);
}
}
/**
* returns the ID of the comment
*
* It may return an empty string, if the comment was not stored.
* It is expected that the concrete Comment implementation gives an ID
* by itself (e.g. after saving).
*
* @return string
* @since 9.0.0
*/
public function getId() {
return $this->data['id'];
}
/**
* sets the ID of the comment and returns itself
*
* It is only allowed to set the ID only, if the current id is an empty
* string (which means it is not stored in a database, storage or whatever
* the concrete implementation does), or vice versa. Changing a given ID is
* not permitted and must result in an IllegalIDChangeException.
*
* @param string $id
* @return IComment
* @throws IllegalIDChangeException
* @since 9.0.0
*/
public function setId($id) {
if(!is_string($id)) {
throw new \InvalidArgumentException('String expected.');
}
$id = trim($id);
if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) {
$this->data['id'] = $id;
return $this;
}
throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.');
}
/**
* returns the parent ID of the comment
*
* @return string
* @since 9.0.0
*/
public function getParentId() {
return $this->data['parentId'];
}
/**
* sets the parent ID and returns itself
*
* @param string $parentId
* @return IComment
* @since 9.0.0
*/
public function setParentId($parentId) {
if(!is_string($parentId)) {
throw new \InvalidArgumentException('String expected.');
}
$this->data['parentId'] = trim($parentId);
return $this;
}
/**
* returns the topmost parent ID of the comment
*
* @return string
* @since 9.0.0
*/
public function getTopmostParentId() {
return $this->data['topmostParentId'];
}
/**
* sets the topmost parent ID and returns itself
*
* @param string $id
* @return IComment
* @since 9.0.0
*/
public function setTopmostParentId($id) {
if(!is_string($id)) {
throw new \InvalidArgumentException('String expected.');
}
$this->data['topmostParentId'] = trim($id);
return $this;
}
/**
* returns the number of children
*
* @return int
* @since 9.0.0
*/
public function getChildrenCount() {
return $this->data['childrenCount'];
}
/**
* sets the number of children
*
* @param int $count
* @return IComment
* @since 9.0.0
*/
public function setChildrenCount($count) {
if(!is_int($count)) {
throw new \InvalidArgumentException('Integer expected.');
}
$this->data['childrenCount'] = $count;
return $this;
}
/**
* returns the message of the comment
*
* @return string
* @since 9.0.0
*/
public function getMessage() {
return $this->data['message'];
}
/**
* sets the message of the comment and returns itself
*
* @param string $message
* @return IComment
* @throws MessageTooLongException
* @since 9.0.0
*/
public function setMessage($message) {
if(!is_string($message)) {
throw new \InvalidArgumentException('String expected.');
}
$message = trim($message);
if(mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) {
throw new MessageTooLongException('Comment message must not exceed ' . IComment::MAX_MESSAGE_LENGTH . ' characters');
}
$this->data['message'] = $message;
return $this;
}
/**
* returns an array containing mentions that are included in the comment
*
* @return array each mention provides a 'type' and an 'id', see example below
* @since 11.0.0
*
* The return array looks like:
* [
* [
* 'type' => 'user',
* 'id' => 'citizen4'
* ],
* [
* 'type' => 'group',
* 'id' => 'media'
* ],
* …
* ]
*
*/
public function getMentions() {
$ok = preg_match_all('/\B@[a-z0-9_\-@\.\']+/i', $this->getMessage(), $mentions);
if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) {
return [];
}
$uids = array_unique($mentions[0]);
$result = [];
foreach ($uids as $uid) {
// exclude author, no self-mentioning
if($uid === '@' . $this->getActorId()) {
continue;
}
$result[] = ['type' => 'user', 'id' => substr($uid, 1)];
}
return $result;
}
/**
* returns the verb of the comment
*
* @return string
* @since 9.0.0
*/
public function getVerb() {
return $this->data['verb'];
}
/**
* sets the verb of the comment, e.g. 'comment' or 'like'
*
* @param string $verb
* @return IComment
* @since 9.0.0
*/
public function setVerb($verb) {
if(!is_string($verb) || !trim($verb)) {
throw new \InvalidArgumentException('Non-empty String expected.');
}
$this->data['verb'] = trim($verb);
return $this;
}
/**
* returns the actor type
*
* @return string
* @since 9.0.0
*/
public function getActorType() {
return $this->data['actorType'];
}
/**
* returns the actor ID
*
* @return string
* @since 9.0.0
*/
public function getActorId() {
return $this->data['actorId'];
}
/**
* sets (overwrites) the actor type and id
*
* @param string $actorType e.g. 'users'
* @param string $actorId e.g. 'zombie234'
* @return IComment
* @since 9.0.0
*/
public function setActor($actorType, $actorId) {
if(
!is_string($actorType) || !trim($actorType)
|| !is_string($actorId) || !trim($actorId)
) {
throw new \InvalidArgumentException('String expected.');
}
$this->data['actorType'] = trim($actorType);
$this->data['actorId'] = trim($actorId);
return $this;
}
/**
* returns the creation date of the comment.
*
* If not explicitly set, it shall default to the time of initialization.
*
* @return \DateTime
* @since 9.0.0
*/
public function getCreationDateTime() {
return $this->data['creationDT'];
}
/**
* sets the creation date of the comment and returns itself
*
* @param \DateTime $timestamp
* @return IComment
* @since 9.0.0
*/
public function setCreationDateTime(\DateTime $timestamp) {
$this->data['creationDT'] = $timestamp;
return $this;
}
/**
* returns the DateTime of the most recent child, if set, otherwise null
*
* @return \DateTime|null
* @since 9.0.0
*/
public function getLatestChildDateTime() {
return $this->data['latestChildDT'];
}
/**
* sets the date of the most recent child
*
* @param \DateTime $dateTime
* @return IComment
* @since 9.0.0
*/
public function setLatestChildDateTime(\DateTime $dateTime = null) {
$this->data['latestChildDT'] = $dateTime;
return $this;
}
/**
* returns the object type the comment is attached to
*
* @return string
* @since 9.0.0
*/
public function getObjectType() {
return $this->data['objectType'];
}
/**
* returns the object id the comment is attached to
*
* @return string
* @since 9.0.0
*/
public function getObjectId() {
return $this->data['objectId'];
}
/**
* sets (overwrites) the object of the comment
*
* @param string $objectType e.g. 'files'
* @param string $objectId e.g. '16435'
* @return IComment
* @since 9.0.0
*/
public function setObject($objectType, $objectId) {
if(
!is_string($objectType) || !trim($objectType)
|| !is_string($objectId) || !trim($objectId)
) {
throw new \InvalidArgumentException('String expected.');
}
$this->data['objectType'] = trim($objectType);
$this->data['objectId'] = trim($objectId);
return $this;
}
/**
* sets the comment data based on an array with keys as taken from the
* database.
*
* @param array $data
* @return IComment
*/
protected function fromArray($data) {
foreach(array_keys($data) as $key) {
// translate DB keys to internal setter names
$setter = 'set' . implode('', array_map('ucfirst', explode('_', $key)));
$setter = str_replace('Timestamp', 'DateTime', $setter);
if(method_exists($this, $setter)) {
$this->$setter($data[$key]);
}
}
foreach(['actor', 'object'] as $role) {
if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) {
$setter = 'set' . ucfirst($role);
$this->$setter($data[$role . '_type'], $data[$role . '_id']);
}
}
return $this;
}
}
|