Comment.php 11 KB

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