You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Comment.php 11KB

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