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 9.6KB

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