1
0

Comment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Comments;
  27. use OCP\Comments\IComment;
  28. use OCP\Comments\IllegalIDChangeException;
  29. use OCP\Comments\MessageTooLongException;
  30. class Comment implements IComment {
  31. protected $data = [
  32. 'id' => '',
  33. 'parentId' => '0',
  34. 'topmostParentId' => '0',
  35. 'childrenCount' => '0',
  36. 'message' => '',
  37. 'verb' => '',
  38. 'actorType' => '',
  39. 'actorId' => '',
  40. 'objectType' => '',
  41. 'objectId' => '',
  42. 'referenceId' => null,
  43. 'creationDT' => null,
  44. 'latestChildDT' => null,
  45. 'reactions' => null,
  46. 'expire_date' => null,
  47. ];
  48. /**
  49. * Comment constructor.
  50. *
  51. * @param array $data optional, array with keys according to column names from
  52. * the comments database scheme
  53. */
  54. public function __construct(array $data = null) {
  55. if (is_array($data)) {
  56. $this->fromArray($data);
  57. }
  58. }
  59. /**
  60. * returns the ID of the comment
  61. *
  62. * It may return an empty string, if the comment was not stored.
  63. * It is expected that the concrete Comment implementation gives an ID
  64. * by itself (e.g. after saving).
  65. *
  66. * @return string
  67. * @since 9.0.0
  68. */
  69. public function getId() {
  70. return $this->data['id'];
  71. }
  72. /**
  73. * sets the ID of the comment and returns itself
  74. *
  75. * It is only allowed to set the ID only, if the current id is an empty
  76. * string (which means it is not stored in a database, storage or whatever
  77. * the concrete implementation does), or vice versa. Changing a given ID is
  78. * not permitted and must result in an IllegalIDChangeException.
  79. *
  80. * @param string $id
  81. * @return IComment
  82. * @throws IllegalIDChangeException
  83. * @since 9.0.0
  84. */
  85. public function setId($id) {
  86. if (!is_string($id)) {
  87. throw new \InvalidArgumentException('String expected.');
  88. }
  89. $id = trim($id);
  90. if ($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) {
  91. $this->data['id'] = $id;
  92. return $this;
  93. }
  94. throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.');
  95. }
  96. /**
  97. * returns the parent ID of the comment
  98. *
  99. * @return string
  100. * @since 9.0.0
  101. */
  102. public function getParentId() {
  103. return $this->data['parentId'];
  104. }
  105. /**
  106. * sets the parent ID and returns itself
  107. *
  108. * @param string $parentId
  109. * @return IComment
  110. * @since 9.0.0
  111. */
  112. public function setParentId($parentId) {
  113. if (!is_string($parentId)) {
  114. throw new \InvalidArgumentException('String expected.');
  115. }
  116. $this->data['parentId'] = trim($parentId);
  117. return $this;
  118. }
  119. /**
  120. * returns the topmost parent ID of the comment
  121. *
  122. * @return string
  123. * @since 9.0.0
  124. */
  125. public function getTopmostParentId() {
  126. return $this->data['topmostParentId'];
  127. }
  128. /**
  129. * sets the topmost parent ID and returns itself
  130. *
  131. * @param string $id
  132. * @return IComment
  133. * @since 9.0.0
  134. */
  135. public function setTopmostParentId($id) {
  136. if (!is_string($id)) {
  137. throw new \InvalidArgumentException('String expected.');
  138. }
  139. $this->data['topmostParentId'] = trim($id);
  140. return $this;
  141. }
  142. /**
  143. * returns the number of children
  144. *
  145. * @return int
  146. * @since 9.0.0
  147. */
  148. public function getChildrenCount() {
  149. return $this->data['childrenCount'];
  150. }
  151. /**
  152. * sets the number of children
  153. *
  154. * @param int $count
  155. * @return IComment
  156. * @since 9.0.0
  157. */
  158. public function setChildrenCount($count) {
  159. if (!is_int($count)) {
  160. throw new \InvalidArgumentException('Integer expected.');
  161. }
  162. $this->data['childrenCount'] = $count;
  163. return $this;
  164. }
  165. /**
  166. * returns the message of the comment
  167. *
  168. * @return string
  169. * @since 9.0.0
  170. */
  171. public function getMessage() {
  172. return $this->data['message'];
  173. }
  174. /**
  175. * sets the message of the comment and returns itself
  176. *
  177. * @param string $message
  178. * @param int $maxLength
  179. * @return IComment
  180. * @throws MessageTooLongException
  181. * @since 9.0.0
  182. */
  183. public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) {
  184. if (!is_string($message)) {
  185. throw new \InvalidArgumentException('String expected.');
  186. }
  187. $message = trim($message);
  188. if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) {
  189. throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters');
  190. }
  191. $this->data['message'] = $message;
  192. return $this;
  193. }
  194. /**
  195. * returns an array containing mentions that are included in the comment
  196. *
  197. * @return array each mention provides a 'type' and an 'id', see example below
  198. * @since 11.0.0
  199. *
  200. * The return array looks like:
  201. * [
  202. * [
  203. * 'type' => 'user',
  204. * 'id' => 'citizen4'
  205. * ],
  206. * [
  207. * 'type' => 'group',
  208. * 'id' => 'media'
  209. * ],
  210. * …
  211. * ]
  212. *
  213. */
  214. public function getMentions() {
  215. $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"group\/[a-z0-9_\-@\.\' ]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
  216. if (!$ok || !isset($mentions[0]) || !is_array($mentions[0])) {
  217. return [];
  218. }
  219. $mentionIds = array_unique($mentions[0]);
  220. usort($mentionIds, static function ($mentionId1, $mentionId2) {
  221. return mb_strlen($mentionId2) <=> mb_strlen($mentionId1);
  222. });
  223. $result = [];
  224. foreach ($mentionIds as $mentionId) {
  225. $cleanId = trim(substr($mentionId, 1), '"');
  226. if (strpos($cleanId, 'guest/') === 0) {
  227. $result[] = ['type' => 'guest', 'id' => $cleanId];
  228. } elseif (strpos($cleanId, 'group/') === 0) {
  229. $result[] = ['type' => 'group', 'id' => substr($cleanId, 6)];
  230. } else {
  231. $result[] = ['type' => 'user', 'id' => $cleanId];
  232. }
  233. }
  234. return $result;
  235. }
  236. /**
  237. * returns the verb of the comment
  238. *
  239. * @return string
  240. * @since 9.0.0
  241. */
  242. public function getVerb() {
  243. return $this->data['verb'];
  244. }
  245. /**
  246. * sets the verb of the comment, e.g. 'comment' or 'like'
  247. *
  248. * @param string $verb
  249. * @return IComment
  250. * @since 9.0.0
  251. */
  252. public function setVerb($verb) {
  253. if (!is_string($verb) || !trim($verb)) {
  254. throw new \InvalidArgumentException('Non-empty String expected.');
  255. }
  256. $this->data['verb'] = trim($verb);
  257. return $this;
  258. }
  259. /**
  260. * returns the actor type
  261. *
  262. * @return string
  263. * @since 9.0.0
  264. */
  265. public function getActorType() {
  266. return $this->data['actorType'];
  267. }
  268. /**
  269. * returns the actor ID
  270. *
  271. * @return string
  272. * @since 9.0.0
  273. */
  274. public function getActorId() {
  275. return $this->data['actorId'];
  276. }
  277. /**
  278. * sets (overwrites) the actor type and id
  279. *
  280. * @param string $actorType e.g. 'users'
  281. * @param string $actorId e.g. 'zombie234'
  282. * @return IComment
  283. * @since 9.0.0
  284. */
  285. public function setActor($actorType, $actorId) {
  286. if (
  287. !is_string($actorType) || !trim($actorType)
  288. || !is_string($actorId) || $actorId === ''
  289. ) {
  290. throw new \InvalidArgumentException('String expected.');
  291. }
  292. $this->data['actorType'] = trim($actorType);
  293. $this->data['actorId'] = $actorId;
  294. return $this;
  295. }
  296. /**
  297. * returns the creation date of the comment.
  298. *
  299. * If not explicitly set, it shall default to the time of initialization.
  300. *
  301. * @return \DateTime
  302. * @since 9.0.0
  303. */
  304. public function getCreationDateTime() {
  305. return $this->data['creationDT'];
  306. }
  307. /**
  308. * sets the creation date of the comment and returns itself
  309. *
  310. * @param \DateTime $timestamp
  311. * @return IComment
  312. * @since 9.0.0
  313. */
  314. public function setCreationDateTime(\DateTime $timestamp) {
  315. $this->data['creationDT'] = $timestamp;
  316. return $this;
  317. }
  318. /**
  319. * returns the DateTime of the most recent child, if set, otherwise null
  320. *
  321. * @return \DateTime|null
  322. * @since 9.0.0
  323. */
  324. public function getLatestChildDateTime() {
  325. return $this->data['latestChildDT'];
  326. }
  327. /**
  328. * @inheritDoc
  329. */
  330. public function setLatestChildDateTime(?\DateTime $dateTime = null) {
  331. $this->data['latestChildDT'] = $dateTime;
  332. return $this;
  333. }
  334. /**
  335. * returns the object type the comment is attached to
  336. *
  337. * @return string
  338. * @since 9.0.0
  339. */
  340. public function getObjectType() {
  341. return $this->data['objectType'];
  342. }
  343. /**
  344. * returns the object id the comment is attached to
  345. *
  346. * @return string
  347. * @since 9.0.0
  348. */
  349. public function getObjectId() {
  350. return $this->data['objectId'];
  351. }
  352. /**
  353. * sets (overwrites) the object of the comment
  354. *
  355. * @param string $objectType e.g. 'files'
  356. * @param string $objectId e.g. '16435'
  357. * @return IComment
  358. * @since 9.0.0
  359. */
  360. public function setObject($objectType, $objectId) {
  361. if (
  362. !is_string($objectType) || !trim($objectType)
  363. || !is_string($objectId) || trim($objectId) === ''
  364. ) {
  365. throw new \InvalidArgumentException('String expected.');
  366. }
  367. $this->data['objectType'] = trim($objectType);
  368. $this->data['objectId'] = trim($objectId);
  369. return $this;
  370. }
  371. /**
  372. * returns the reference id of the comment
  373. *
  374. * @return string|null
  375. * @since 19.0.0
  376. */
  377. public function getReferenceId(): ?string {
  378. return $this->data['referenceId'];
  379. }
  380. /**
  381. * sets (overwrites) the reference id of the comment
  382. *
  383. * @param string $referenceId e.g. sha256 hash sum
  384. * @return IComment
  385. * @since 19.0.0
  386. */
  387. public function setReferenceId(?string $referenceId): IComment {
  388. if ($referenceId === null) {
  389. $this->data['referenceId'] = $referenceId;
  390. } else {
  391. $referenceId = trim($referenceId);
  392. if ($referenceId === '') {
  393. throw new \InvalidArgumentException('Non empty string expected.');
  394. }
  395. $this->data['referenceId'] = $referenceId;
  396. }
  397. return $this;
  398. }
  399. /**
  400. * @inheritDoc
  401. */
  402. public function getReactions(): array {
  403. return $this->data['reactions'] ?? [];
  404. }
  405. /**
  406. * @inheritDoc
  407. */
  408. public function setReactions(?array $reactions): IComment {
  409. $this->data['reactions'] = $reactions;
  410. return $this;
  411. }
  412. /**
  413. * @inheritDoc
  414. */
  415. public function setExpireDate(?\DateTime $dateTime): IComment {
  416. $this->data['expire_date'] = $dateTime;
  417. return $this;
  418. }
  419. /**
  420. * @inheritDoc
  421. */
  422. public function getExpireDate(): ?\DateTime {
  423. return $this->data['expire_date'];
  424. }
  425. /**
  426. * sets the comment data based on an array with keys as taken from the
  427. * database.
  428. *
  429. * @param array $data
  430. * @return IComment
  431. */
  432. protected function fromArray($data) {
  433. foreach (array_keys($data) as $key) {
  434. // translate DB keys to internal setter names
  435. $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key)));
  436. $setter = str_replace('Timestamp', 'DateTime', $setter);
  437. if (method_exists($this, $setter)) {
  438. $this->$setter($data[$key]);
  439. }
  440. }
  441. foreach (['actor', 'object'] as $role) {
  442. if (isset($data[$role . '_type']) && isset($data[$role . '_id'])) {
  443. $setter = 'set' . ucfirst($role);
  444. $this->$setter($data[$role . '_type'], $data[$role . '_id']);
  445. }
  446. }
  447. return $this;
  448. }
  449. }