Event.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Phil Davis <phil.davis@inf.org>
  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\Activity;
  25. use OCP\Activity\IEvent;
  26. use OCP\RichObjectStrings\InvalidObjectExeption;
  27. use OCP\RichObjectStrings\IValidator;
  28. class Event implements IEvent {
  29. /** @var string */
  30. protected $app = '';
  31. /** @var string */
  32. protected $type = '';
  33. /** @var string */
  34. protected $affectedUser = '';
  35. /** @var string */
  36. protected $author = '';
  37. /** @var int */
  38. protected $timestamp = 0;
  39. /** @var string */
  40. protected $subject = '';
  41. /** @var array */
  42. protected $subjectParameters = [];
  43. /** @var string */
  44. protected $subjectParsed;
  45. /** @var string */
  46. protected $subjectRich;
  47. /** @var array */
  48. protected $subjectRichParameters;
  49. /** @var string */
  50. protected $message = '';
  51. /** @var array */
  52. protected $messageParameters = [];
  53. /** @var string */
  54. protected $messageParsed;
  55. /** @var string */
  56. protected $messageRich;
  57. /** @var array */
  58. protected $messageRichParameters;
  59. /** @var string */
  60. protected $objectType = '';
  61. /** @var int */
  62. protected $objectId = 0;
  63. /** @var string */
  64. protected $objectName = '';
  65. /** @var string */
  66. protected $link = '';
  67. /** @var string */
  68. protected $icon = '';
  69. /** @var IEvent */
  70. protected $child = null;
  71. /** @var IValidator */
  72. protected $richValidator;
  73. /**
  74. * @param IValidator $richValidator
  75. */
  76. public function __construct(IValidator $richValidator) {
  77. $this->richValidator = $richValidator;
  78. }
  79. /**
  80. * Set the app of the activity
  81. *
  82. * @param string $app
  83. * @return IEvent
  84. * @throws \InvalidArgumentException if the app id is invalid
  85. * @since 8.2.0
  86. */
  87. public function setApp($app) {
  88. if (!is_string($app) || $app === '' || isset($app[32])) {
  89. throw new \InvalidArgumentException('The given app is invalid');
  90. }
  91. $this->app = (string) $app;
  92. return $this;
  93. }
  94. /**
  95. * @return string
  96. */
  97. public function getApp() {
  98. return $this->app;
  99. }
  100. /**
  101. * Set the type of the activity
  102. *
  103. * @param string $type
  104. * @return IEvent
  105. * @throws \InvalidArgumentException if the type is invalid
  106. * @since 8.2.0
  107. */
  108. public function setType($type) {
  109. if (!is_string($type) || $type === '' || isset($type[255])) {
  110. throw new \InvalidArgumentException('The given type is invalid');
  111. }
  112. $this->type = (string) $type;
  113. return $this;
  114. }
  115. /**
  116. * @return string
  117. */
  118. public function getType() {
  119. return $this->type;
  120. }
  121. /**
  122. * Set the affected user of the activity
  123. *
  124. * @param string $affectedUser
  125. * @return IEvent
  126. * @throws \InvalidArgumentException if the affected user is invalid
  127. * @since 8.2.0
  128. */
  129. public function setAffectedUser($affectedUser) {
  130. if (!is_string($affectedUser) || $affectedUser === '' || isset($affectedUser[64])) {
  131. throw new \InvalidArgumentException('The given affected user is invalid');
  132. }
  133. $this->affectedUser = (string) $affectedUser;
  134. return $this;
  135. }
  136. /**
  137. * @return string
  138. */
  139. public function getAffectedUser() {
  140. return $this->affectedUser;
  141. }
  142. /**
  143. * Set the author of the activity
  144. *
  145. * @param string $author
  146. * @return IEvent
  147. * @throws \InvalidArgumentException if the author is invalid
  148. * @since 8.2.0
  149. */
  150. public function setAuthor($author) {
  151. if (!is_string($author) || isset($author[64])) {
  152. throw new \InvalidArgumentException('The given author user is invalid'. serialize($author));
  153. }
  154. $this->author = (string) $author;
  155. return $this;
  156. }
  157. /**
  158. * @return string
  159. */
  160. public function getAuthor() {
  161. return $this->author;
  162. }
  163. /**
  164. * Set the timestamp of the activity
  165. *
  166. * @param int $timestamp
  167. * @return IEvent
  168. * @throws \InvalidArgumentException if the timestamp is invalid
  169. * @since 8.2.0
  170. */
  171. public function setTimestamp($timestamp) {
  172. if (!is_int($timestamp)) {
  173. throw new \InvalidArgumentException('The given timestamp is invalid');
  174. }
  175. $this->timestamp = (int) $timestamp;
  176. return $this;
  177. }
  178. /**
  179. * @return int
  180. */
  181. public function getTimestamp() {
  182. return $this->timestamp;
  183. }
  184. /**
  185. * Set the subject of the activity
  186. *
  187. * @param string $subject
  188. * @param array $parameters
  189. * @return IEvent
  190. * @throws \InvalidArgumentException if the subject or parameters are invalid
  191. * @since 8.2.0
  192. */
  193. public function setSubject($subject, array $parameters = []) {
  194. if (!is_string($subject) || isset($subject[255])) {
  195. throw new \InvalidArgumentException('The given subject is invalid');
  196. }
  197. $this->subject = (string) $subject;
  198. $this->subjectParameters = $parameters;
  199. return $this;
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function getSubject() {
  205. return $this->subject;
  206. }
  207. /**
  208. * @return array
  209. */
  210. public function getSubjectParameters() {
  211. return $this->subjectParameters;
  212. }
  213. /**
  214. * @param string $subject
  215. * @return $this
  216. * @throws \InvalidArgumentException if the subject is invalid
  217. * @since 11.0.0
  218. */
  219. public function setParsedSubject($subject) {
  220. if (!is_string($subject) || $subject === '') {
  221. throw new \InvalidArgumentException('The given parsed subject is invalid');
  222. }
  223. $this->subjectParsed = $subject;
  224. return $this;
  225. }
  226. /**
  227. * @return string
  228. * @since 11.0.0
  229. */
  230. public function getParsedSubject() {
  231. return $this->subjectParsed;
  232. }
  233. /**
  234. * @param string $subject
  235. * @param array $parameters
  236. * @return $this
  237. * @throws \InvalidArgumentException if the subject or parameters are invalid
  238. * @since 11.0.0
  239. */
  240. public function setRichSubject($subject, array $parameters = []) {
  241. if (!is_string($subject) || $subject === '') {
  242. throw new \InvalidArgumentException('The given parsed subject is invalid');
  243. }
  244. $this->subjectRich = $subject;
  245. if (!is_array($parameters)) {
  246. throw new \InvalidArgumentException('The given subject parameters are invalid');
  247. }
  248. $this->subjectRichParameters = $parameters;
  249. return $this;
  250. }
  251. /**
  252. * @return string
  253. * @since 11.0.0
  254. */
  255. public function getRichSubject() {
  256. return $this->subjectRich;
  257. }
  258. /**
  259. * @return array[]
  260. * @since 11.0.0
  261. */
  262. public function getRichSubjectParameters() {
  263. return $this->subjectRichParameters;
  264. }
  265. /**
  266. * Set the message of the activity
  267. *
  268. * @param string $message
  269. * @param array $parameters
  270. * @return IEvent
  271. * @throws \InvalidArgumentException if the message or parameters are invalid
  272. * @since 8.2.0
  273. */
  274. public function setMessage($message, array $parameters = []) {
  275. if (!is_string($message) || isset($message[255])) {
  276. throw new \InvalidArgumentException('The given message is invalid');
  277. }
  278. $this->message = (string) $message;
  279. $this->messageParameters = $parameters;
  280. return $this;
  281. }
  282. /**
  283. * @return string
  284. */
  285. public function getMessage() {
  286. return $this->message;
  287. }
  288. /**
  289. * @return array
  290. */
  291. public function getMessageParameters() {
  292. return $this->messageParameters;
  293. }
  294. /**
  295. * @param string $message
  296. * @return $this
  297. * @throws \InvalidArgumentException if the message is invalid
  298. * @since 11.0.0
  299. */
  300. public function setParsedMessage($message) {
  301. if (!is_string($message)) {
  302. throw new \InvalidArgumentException('The given parsed message is invalid');
  303. }
  304. $this->messageParsed = $message;
  305. return $this;
  306. }
  307. /**
  308. * @return string
  309. * @since 11.0.0
  310. */
  311. public function getParsedMessage() {
  312. return $this->messageParsed;
  313. }
  314. /**
  315. * @param string $message
  316. * @param array $parameters
  317. * @return $this
  318. * @throws \InvalidArgumentException if the subject or parameters are invalid
  319. * @since 11.0.0
  320. */
  321. public function setRichMessage($message, array $parameters = []) {
  322. if (!is_string($message)) {
  323. throw new \InvalidArgumentException('The given parsed message is invalid');
  324. }
  325. $this->messageRich = $message;
  326. if (!is_array($parameters)) {
  327. throw new \InvalidArgumentException('The given message parameters are invalid');
  328. }
  329. $this->messageRichParameters = $parameters;
  330. return $this;
  331. }
  332. /**
  333. * @return string
  334. * @since 11.0.0
  335. */
  336. public function getRichMessage() {
  337. return $this->messageRich;
  338. }
  339. /**
  340. * @return array[]
  341. * @since 11.0.0
  342. */
  343. public function getRichMessageParameters() {
  344. return $this->messageRichParameters;
  345. }
  346. /**
  347. * Set the object of the activity
  348. *
  349. * @param string $objectType
  350. * @param int $objectId
  351. * @param string $objectName
  352. * @return IEvent
  353. * @throws \InvalidArgumentException if the object is invalid
  354. * @since 8.2.0
  355. */
  356. public function setObject($objectType, $objectId, $objectName = '') {
  357. if (!is_string($objectType) || isset($objectType[255])) {
  358. throw new \InvalidArgumentException('The given object type is invalid');
  359. }
  360. if (!is_int($objectId)) {
  361. throw new \InvalidArgumentException('The given object id is invalid');
  362. }
  363. if (!is_string($objectName) || isset($objectName[4000])) {
  364. throw new \InvalidArgumentException('The given object name is invalid');
  365. }
  366. $this->objectType = (string) $objectType;
  367. $this->objectId = (int) $objectId;
  368. $this->objectName = (string) $objectName;
  369. return $this;
  370. }
  371. /**
  372. * @return string
  373. */
  374. public function getObjectType() {
  375. return $this->objectType;
  376. }
  377. /**
  378. * @return string
  379. */
  380. public function getObjectId() {
  381. return $this->objectId;
  382. }
  383. /**
  384. * @return string
  385. */
  386. public function getObjectName() {
  387. return $this->objectName;
  388. }
  389. /**
  390. * Set the link of the activity
  391. *
  392. * @param string $link
  393. * @return IEvent
  394. * @throws \InvalidArgumentException if the link is invalid
  395. * @since 8.2.0
  396. */
  397. public function setLink($link) {
  398. if (!is_string($link) || isset($link[4000])) {
  399. throw new \InvalidArgumentException('The given link is invalid');
  400. }
  401. $this->link = (string) $link;
  402. return $this;
  403. }
  404. /**
  405. * @return string
  406. */
  407. public function getLink() {
  408. return $this->link;
  409. }
  410. /**
  411. * @param string $icon
  412. * @return $this
  413. * @throws \InvalidArgumentException if the icon is invalid
  414. * @since 11.0.0
  415. */
  416. public function setIcon($icon) {
  417. if (!is_string($icon) || isset($icon[4000])) {
  418. throw new \InvalidArgumentException('The given icon is invalid');
  419. }
  420. $this->icon = $icon;
  421. return $this;
  422. }
  423. /**
  424. * @return string
  425. * @since 11.0.0
  426. */
  427. public function getIcon() {
  428. return $this->icon;
  429. }
  430. /**
  431. * @param IEvent $child
  432. * @since 11.0.0
  433. */
  434. public function setChildEvent(IEvent $child) {
  435. $this->child = $child;
  436. }
  437. /**
  438. * @return IEvent|null
  439. * @since 11.0.0
  440. */
  441. public function getChildEvent() {
  442. return $this->child;
  443. }
  444. /**
  445. * @return bool
  446. * @since 8.2.0
  447. */
  448. public function isValid() {
  449. return
  450. $this->isValidCommon()
  451. &&
  452. $this->getSubject() !== ''
  453. ;
  454. }
  455. /**
  456. * @return bool
  457. * @since 8.2.0
  458. */
  459. public function isValidParsed() {
  460. if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) {
  461. try {
  462. $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters());
  463. } catch (InvalidObjectExeption $e) {
  464. return false;
  465. }
  466. }
  467. if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) {
  468. try {
  469. $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters());
  470. } catch (InvalidObjectExeption $e) {
  471. return false;
  472. }
  473. }
  474. return
  475. $this->isValidCommon()
  476. &&
  477. $this->getParsedSubject() !== ''
  478. ;
  479. }
  480. /**
  481. * @return bool
  482. */
  483. protected function isValidCommon() {
  484. return
  485. $this->getApp() !== ''
  486. &&
  487. $this->getType() !== ''
  488. &&
  489. $this->getAffectedUser() !== ''
  490. &&
  491. $this->getTimestamp() !== 0
  492. /**
  493. * Disabled for BC with old activities
  494. &&
  495. $this->getObjectType() !== ''
  496. &&
  497. $this->getObjectId() !== 0
  498. */
  499. ;
  500. }
  501. }