1
0

Comment.php 12 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 array $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. 'metaData' => null,
  44. 'creationDT' => null,
  45. 'latestChildDT' => null,
  46. 'reactions' => null,
  47. 'expire_date' => null,
  48. ];
  49. /**
  50. * Comment constructor.
  51. *
  52. * @param array $data optional, array with keys according to column names from
  53. * the comments database scheme
  54. */
  55. public function __construct(array $data = null) {
  56. if (is_array($data)) {
  57. $this->fromArray($data);
  58. }
  59. }
  60. /**
  61. * Returns the ID of the comment
  62. *
  63. * It may return an empty string, if the comment was not stored.
  64. * It is expected that the concrete Comment implementation gives an ID
  65. * by itself (e.g. after saving).
  66. *
  67. * @since 9.0.0
  68. */
  69. public function getId(): string {
  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): IComment {
  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. * @since 9.0.0
  100. */
  101. public function getParentId(): string {
  102. return $this->data['parentId'];
  103. }
  104. /**
  105. * Sets the parent ID and returns itself
  106. *
  107. * @param string $parentId
  108. * @since 9.0.0
  109. */
  110. public function setParentId($parentId): IComment {
  111. if (!is_string($parentId)) {
  112. throw new \InvalidArgumentException('String expected.');
  113. }
  114. $this->data['parentId'] = trim($parentId);
  115. return $this;
  116. }
  117. /**
  118. * Returns the topmost parent ID of the comment
  119. *
  120. * @since 9.0.0
  121. */
  122. public function getTopmostParentId(): string {
  123. return $this->data['topmostParentId'];
  124. }
  125. /**
  126. * Sets the topmost parent ID and returns itself
  127. *
  128. * @param string $id
  129. * @since 9.0.0
  130. */
  131. public function setTopmostParentId($id): IComment {
  132. if (!is_string($id)) {
  133. throw new \InvalidArgumentException('String expected.');
  134. }
  135. $this->data['topmostParentId'] = trim($id);
  136. return $this;
  137. }
  138. /**
  139. * Returns the number of children
  140. *
  141. * @since 9.0.0
  142. */
  143. public function getChildrenCount(): int {
  144. return $this->data['childrenCount'];
  145. }
  146. /**
  147. * Sets the number of children
  148. *
  149. * @param int $count
  150. * @since 9.0.0
  151. */
  152. public function setChildrenCount($count): IComment {
  153. if (!is_int($count)) {
  154. throw new \InvalidArgumentException('Integer expected.');
  155. }
  156. $this->data['childrenCount'] = $count;
  157. return $this;
  158. }
  159. /**
  160. * Returns the message of the comment
  161. * @since 9.0.0
  162. */
  163. public function getMessage(): string {
  164. return $this->data['message'];
  165. }
  166. /**
  167. * sets the message of the comment and returns itself
  168. *
  169. * @param string $message
  170. * @param int $maxLength
  171. * @throws MessageTooLongException
  172. * @since 9.0.0
  173. */
  174. public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH): IComment {
  175. if (!is_string($message)) {
  176. throw new \InvalidArgumentException('String expected.');
  177. }
  178. $message = trim($message);
  179. if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) {
  180. throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters');
  181. }
  182. $this->data['message'] = $message;
  183. return $this;
  184. }
  185. /**
  186. * returns an array containing mentions that are included in the comment
  187. *
  188. * @return array each mention provides a 'type' and an 'id', see example below
  189. * @since 11.0.0
  190. *
  191. * The return array looks like:
  192. * [
  193. * [
  194. * 'type' => 'user',
  195. * 'id' => 'citizen4'
  196. * ],
  197. * [
  198. * 'type' => 'group',
  199. * 'id' => 'media'
  200. * ],
  201. * …
  202. * ]
  203. *
  204. */
  205. public function getMentions(): array {
  206. $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);
  207. if (!$ok || !isset($mentions[0])) {
  208. return [];
  209. }
  210. $mentionIds = array_unique($mentions[0]);
  211. usort($mentionIds, static function ($mentionId1, $mentionId2) {
  212. return mb_strlen($mentionId2) <=> mb_strlen($mentionId1);
  213. });
  214. $result = [];
  215. foreach ($mentionIds as $mentionId) {
  216. $cleanId = trim(substr($mentionId, 1), '"');
  217. if (str_starts_with($cleanId, 'guest/')) {
  218. $result[] = ['type' => 'guest', 'id' => $cleanId];
  219. } elseif (str_starts_with($cleanId, 'group/')) {
  220. $result[] = ['type' => 'group', 'id' => substr($cleanId, 6)];
  221. } else {
  222. $result[] = ['type' => 'user', 'id' => $cleanId];
  223. }
  224. }
  225. return $result;
  226. }
  227. /**
  228. * Returns the verb of the comment
  229. *
  230. * @since 9.0.0
  231. */
  232. public function getVerb(): string {
  233. return $this->data['verb'];
  234. }
  235. /**
  236. * Sets the verb of the comment, e.g. 'comment' or 'like'
  237. *
  238. * @param string $verb
  239. * @since 9.0.0
  240. */
  241. public function setVerb($verb): IComment {
  242. if (!is_string($verb) || !trim($verb)) {
  243. throw new \InvalidArgumentException('Non-empty String expected.');
  244. }
  245. $this->data['verb'] = trim($verb);
  246. return $this;
  247. }
  248. /**
  249. * Returns the actor type
  250. * @since 9.0.0
  251. */
  252. public function getActorType(): string {
  253. return $this->data['actorType'];
  254. }
  255. /**
  256. * Returns the actor ID
  257. * @since 9.0.0
  258. */
  259. public function getActorId(): string {
  260. return $this->data['actorId'];
  261. }
  262. /**
  263. * Sets (overwrites) the actor type and id
  264. *
  265. * @param string $actorType e.g. 'users'
  266. * @param string $actorId e.g. 'zombie234'
  267. * @since 9.0.0
  268. */
  269. public function setActor($actorType, $actorId): IComment {
  270. if (
  271. !is_string($actorType) || !trim($actorType)
  272. || !is_string($actorId) || $actorId === ''
  273. ) {
  274. throw new \InvalidArgumentException('String expected.');
  275. }
  276. $this->data['actorType'] = trim($actorType);
  277. $this->data['actorId'] = $actorId;
  278. return $this;
  279. }
  280. /**
  281. * Returns the creation date of the comment.
  282. *
  283. * If not explicitly set, it shall default to the time of initialization.
  284. * @since 9.0.0
  285. * @throw \LogicException if creation date time is not set yet
  286. */
  287. public function getCreationDateTime(): \DateTime {
  288. if (!isset($this->data['creationDT'])) {
  289. throw new \LogicException('Cannot get creation date before setting one or writting to database');
  290. }
  291. return $this->data['creationDT'];
  292. }
  293. /**
  294. * Sets the creation date of the comment and returns itself
  295. * @since 9.0.0
  296. */
  297. public function setCreationDateTime(\DateTime $dateTime): IComment {
  298. $this->data['creationDT'] = $dateTime;
  299. return $this;
  300. }
  301. /**
  302. * Returns the DateTime of the most recent child, if set, otherwise null
  303. * @since 9.0.0
  304. */
  305. public function getLatestChildDateTime(): ?\DateTime {
  306. return $this->data['latestChildDT'];
  307. }
  308. /**
  309. * @inheritDoc
  310. */
  311. public function setLatestChildDateTime(?\DateTime $dateTime = null): IComment {
  312. $this->data['latestChildDT'] = $dateTime;
  313. return $this;
  314. }
  315. /**
  316. * Returns the object type the comment is attached to
  317. * @since 9.0.0
  318. */
  319. public function getObjectType(): string {
  320. return $this->data['objectType'];
  321. }
  322. /**
  323. * Returns the object id the comment is attached to
  324. * @since 9.0.0
  325. */
  326. public function getObjectId(): string {
  327. return $this->data['objectId'];
  328. }
  329. /**
  330. * Sets (overwrites) the object of the comment
  331. *
  332. * @param string $objectType e.g. 'files'
  333. * @param string $objectId e.g. '16435'
  334. * @since 9.0.0
  335. */
  336. public function setObject($objectType, $objectId): IComment {
  337. if (
  338. !is_string($objectType) || !trim($objectType)
  339. || !is_string($objectId) || trim($objectId) === ''
  340. ) {
  341. throw new \InvalidArgumentException('String expected.');
  342. }
  343. $this->data['objectType'] = trim($objectType);
  344. $this->data['objectId'] = trim($objectId);
  345. return $this;
  346. }
  347. /**
  348. * Returns the reference id of the comment
  349. * @since 19.0.0
  350. */
  351. public function getReferenceId(): ?string {
  352. return $this->data['referenceId'];
  353. }
  354. /**
  355. * Sets (overwrites) the reference id of the comment
  356. *
  357. * @param string $referenceId e.g. sha256 hash sum
  358. * @since 19.0.0
  359. */
  360. public function setReferenceId(?string $referenceId): IComment {
  361. if ($referenceId === null) {
  362. $this->data['referenceId'] = $referenceId;
  363. } else {
  364. $referenceId = trim($referenceId);
  365. if ($referenceId === '') {
  366. throw new \InvalidArgumentException('Non empty string expected.');
  367. }
  368. $this->data['referenceId'] = $referenceId;
  369. }
  370. return $this;
  371. }
  372. /**
  373. * @inheritDoc
  374. */
  375. public function getMetaData(): ?array {
  376. if ($this->data['metaData'] === null) {
  377. return null;
  378. }
  379. try {
  380. $metaData = json_decode($this->data['metaData'], true, flags: JSON_THROW_ON_ERROR);
  381. } catch (\JsonException $e) {
  382. return null;
  383. }
  384. return is_array($metaData) ? $metaData : null;
  385. }
  386. /**
  387. * @inheritDoc
  388. */
  389. public function setMetaData(?array $metaData): IComment {
  390. if ($metaData === null) {
  391. $this->data['metaData'] = null;
  392. } else {
  393. $this->data['metaData'] = json_encode($metaData, JSON_THROW_ON_ERROR);
  394. }
  395. return $this;
  396. }
  397. /**
  398. * @inheritDoc
  399. */
  400. public function getReactions(): array {
  401. return $this->data['reactions'] ?? [];
  402. }
  403. /**
  404. * @inheritDoc
  405. */
  406. public function setReactions(?array $reactions): IComment {
  407. $this->data['reactions'] = $reactions;
  408. return $this;
  409. }
  410. /**
  411. * @inheritDoc
  412. */
  413. public function setExpireDate(?\DateTime $dateTime): IComment {
  414. $this->data['expire_date'] = $dateTime;
  415. return $this;
  416. }
  417. /**
  418. * @inheritDoc
  419. */
  420. public function getExpireDate(): ?\DateTime {
  421. return $this->data['expire_date'];
  422. }
  423. /**
  424. * sets the comment data based on an array with keys as taken from the
  425. * database.
  426. *
  427. * @param array $data
  428. */
  429. protected function fromArray($data): IComment {
  430. foreach (array_keys($data) as $key) {
  431. // translate DB keys to internal setter names
  432. $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key)));
  433. $setter = str_replace('Timestamp', 'DateTime', $setter);
  434. if (method_exists($this, $setter)) {
  435. $this->$setter($data[$key]);
  436. }
  437. }
  438. foreach (['actor', 'object'] as $role) {
  439. if (isset($data[$role . '_type']) && isset($data[$role . '_id'])) {
  440. $setter = 'set' . ucfirst($role);
  441. $this->$setter($data[$role . '_type'], $data[$role . '_id']);
  442. }
  443. }
  444. return $this;
  445. }
  446. }