Event.php 11 KB

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