Comment.php 11 KB

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