1
0

Event.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Activity;
  9. use OCP\Activity\Exceptions\InvalidValueException;
  10. use OCP\Activity\IEvent;
  11. use OCP\RichObjectStrings\InvalidObjectExeption;
  12. use OCP\RichObjectStrings\IRichTextFormatter;
  13. use OCP\RichObjectStrings\IValidator;
  14. class Event implements IEvent {
  15. /** @var string */
  16. protected $app = '';
  17. /** @var string */
  18. protected $type = '';
  19. /** @var string */
  20. protected $affectedUser = '';
  21. /** @var string */
  22. protected $author = '';
  23. /** @var int */
  24. protected $timestamp = 0;
  25. /** @var string */
  26. protected $subject = '';
  27. /** @var array */
  28. protected $subjectParameters = [];
  29. /** @var string */
  30. protected $subjectParsed = '';
  31. /** @var string */
  32. protected $subjectRich = '';
  33. /** @var array */
  34. protected $subjectRichParameters = [];
  35. /** @var string */
  36. protected $message = '';
  37. /** @var array */
  38. protected $messageParameters = [];
  39. /** @var string */
  40. protected $messageParsed = '';
  41. /** @var string */
  42. protected $messageRich = '';
  43. /** @var array */
  44. protected $messageRichParameters = [];
  45. /** @var string */
  46. protected $objectType = '';
  47. /** @var int */
  48. protected $objectId = 0;
  49. /** @var string */
  50. protected $objectName = '';
  51. /** @var string */
  52. protected $link = '';
  53. /** @var string */
  54. protected $icon = '';
  55. /** @var bool */
  56. protected $generateNotification = true;
  57. /** @var IEvent|null */
  58. protected $child;
  59. public function __construct(
  60. protected IValidator $richValidator,
  61. protected IRichTextFormatter $richTextFormatter,
  62. ) {
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function setApp(string $app): IEvent {
  68. if ($app === '' || isset($app[32])) {
  69. throw new InvalidValueException('app');
  70. }
  71. $this->app = $app;
  72. return $this;
  73. }
  74. /**
  75. * @return string
  76. */
  77. public function getApp(): string {
  78. return $this->app;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function setType(string $type): IEvent {
  84. if ($type === '' || isset($type[255])) {
  85. throw new InvalidValueException('type');
  86. }
  87. $this->type = $type;
  88. return $this;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getType(): string {
  94. return $this->type;
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function setAffectedUser(string $affectedUser): IEvent {
  100. if ($affectedUser === '' || isset($affectedUser[64])) {
  101. throw new InvalidValueException('affectedUser');
  102. }
  103. $this->affectedUser = $affectedUser;
  104. return $this;
  105. }
  106. /**
  107. * @return string
  108. */
  109. public function getAffectedUser(): string {
  110. return $this->affectedUser;
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public function setAuthor(string $author): IEvent {
  116. if (isset($author[64])) {
  117. throw new InvalidValueException('author');
  118. }
  119. $this->author = $author;
  120. return $this;
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function getAuthor(): string {
  126. return $this->author;
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. public function setTimestamp(int $timestamp): IEvent {
  132. if ($timestamp < 0) {
  133. throw new InvalidValueException('timestamp');
  134. }
  135. $this->timestamp = $timestamp;
  136. return $this;
  137. }
  138. /**
  139. * @return int
  140. */
  141. public function getTimestamp(): int {
  142. return $this->timestamp;
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public function setSubject(string $subject, array $parameters = []): IEvent {
  148. if (isset($subject[255])) {
  149. throw new InvalidValueException('subject');
  150. }
  151. $this->subject = $subject;
  152. $this->subjectParameters = $parameters;
  153. return $this;
  154. }
  155. /**
  156. * @return string
  157. */
  158. public function getSubject(): string {
  159. return $this->subject;
  160. }
  161. /**
  162. * @return array
  163. */
  164. public function getSubjectParameters(): array {
  165. return $this->subjectParameters;
  166. }
  167. /**
  168. * {@inheritDoc}
  169. */
  170. public function setParsedSubject(string $subject): IEvent {
  171. if ($subject === '') {
  172. throw new InvalidValueException('parsedSubject');
  173. }
  174. $this->subjectParsed = $subject;
  175. return $this;
  176. }
  177. /**
  178. * @return string
  179. * @since 11.0.0
  180. */
  181. public function getParsedSubject(): string {
  182. return $this->subjectParsed;
  183. }
  184. /**
  185. * {@inheritDoc}
  186. */
  187. public function setRichSubject(string $subject, array $parameters = []): IEvent {
  188. if ($subject === '') {
  189. throw new InvalidValueException('richSubject');
  190. }
  191. $this->subjectRich = $subject;
  192. $this->subjectRichParameters = $parameters;
  193. if ($this->subjectParsed === '') {
  194. try {
  195. $this->subjectParsed = $this->richTextFormatter->richToParsed($subject, $parameters);
  196. } catch (\InvalidArgumentException $e) {
  197. throw new InvalidValueException('richSubjectParameters', $e);
  198. }
  199. }
  200. return $this;
  201. }
  202. /**
  203. * @return string
  204. * @since 11.0.0
  205. */
  206. public function getRichSubject(): string {
  207. return $this->subjectRich;
  208. }
  209. /**
  210. * @return array<string, array<string, string>>
  211. * @since 11.0.0
  212. */
  213. public function getRichSubjectParameters(): array {
  214. return $this->subjectRichParameters;
  215. }
  216. /**
  217. * {@inheritDoc}
  218. */
  219. public function setMessage(string $message, array $parameters = []): IEvent {
  220. if (isset($message[255])) {
  221. throw new InvalidValueException('message');
  222. }
  223. $this->message = $message;
  224. $this->messageParameters = $parameters;
  225. return $this;
  226. }
  227. /**
  228. * @return string
  229. */
  230. public function getMessage(): string {
  231. return $this->message;
  232. }
  233. /**
  234. * @return array
  235. */
  236. public function getMessageParameters(): array {
  237. return $this->messageParameters;
  238. }
  239. /**
  240. * {@inheritDoc}
  241. */
  242. public function setParsedMessage(string $message): IEvent {
  243. $this->messageParsed = $message;
  244. return $this;
  245. }
  246. /**
  247. * @return string
  248. * @since 11.0.0
  249. */
  250. public function getParsedMessage(): string {
  251. return $this->messageParsed;
  252. }
  253. /**
  254. * {@inheritDoc}
  255. */
  256. public function setRichMessage(string $message, array $parameters = []): IEvent {
  257. $this->messageRich = $message;
  258. $this->messageRichParameters = $parameters;
  259. if ($this->messageParsed === '') {
  260. try {
  261. $this->messageParsed = $this->richTextFormatter->richToParsed($message, $parameters);
  262. } catch (\InvalidArgumentException $e) {
  263. throw new InvalidValueException('richMessageParameters', $e);
  264. }
  265. }
  266. return $this;
  267. }
  268. /**
  269. * @return string
  270. * @since 11.0.0
  271. */
  272. public function getRichMessage(): string {
  273. return $this->messageRich;
  274. }
  275. /**
  276. * @return array<string, array<string, string>>
  277. * @since 11.0.0
  278. */
  279. public function getRichMessageParameters(): array {
  280. return $this->messageRichParameters;
  281. }
  282. /**
  283. * {@inheritDoc}
  284. */
  285. public function setObject(string $objectType, int $objectId, string $objectName = ''): IEvent {
  286. if (isset($objectType[255])) {
  287. throw new InvalidValueException('objectType');
  288. }
  289. if (isset($objectName[4000])) {
  290. throw new InvalidValueException('objectName');
  291. }
  292. $this->objectType = $objectType;
  293. $this->objectId = $objectId;
  294. $this->objectName = $objectName;
  295. return $this;
  296. }
  297. /**
  298. * @return string
  299. */
  300. public function getObjectType(): string {
  301. return $this->objectType;
  302. }
  303. /**
  304. * @return int
  305. */
  306. public function getObjectId(): int {
  307. return $this->objectId;
  308. }
  309. /**
  310. * @return string
  311. */
  312. public function getObjectName(): string {
  313. return $this->objectName;
  314. }
  315. /**
  316. * {@inheritDoc}
  317. */
  318. public function setLink(string $link): IEvent {
  319. if (isset($link[4000])) {
  320. throw new InvalidValueException('link');
  321. }
  322. $this->link = $link;
  323. return $this;
  324. }
  325. /**
  326. * @return string
  327. */
  328. public function getLink(): string {
  329. return $this->link;
  330. }
  331. /**
  332. * {@inheritDoc}
  333. */
  334. public function setIcon(string $icon): IEvent {
  335. if (isset($icon[4000])) {
  336. throw new InvalidValueException('icon');
  337. }
  338. $this->icon = $icon;
  339. return $this;
  340. }
  341. /**
  342. * @return string
  343. * @since 11.0.0
  344. */
  345. public function getIcon(): string {
  346. return $this->icon;
  347. }
  348. /**
  349. * @param IEvent $child
  350. * @return $this
  351. * @since 11.0.0 - Since 15.0.0 returns $this
  352. */
  353. public function setChildEvent(IEvent $child): IEvent {
  354. $this->child = $child;
  355. return $this;
  356. }
  357. /**
  358. * @return IEvent|null
  359. * @since 11.0.0
  360. */
  361. public function getChildEvent() {
  362. return $this->child;
  363. }
  364. /**
  365. * @return bool
  366. * @since 8.2.0
  367. */
  368. public function isValid(): bool {
  369. return
  370. $this->isValidCommon()
  371. &&
  372. $this->getSubject() !== ''
  373. ;
  374. }
  375. /**
  376. * @return bool
  377. * @since 8.2.0
  378. */
  379. public function isValidParsed(): bool {
  380. if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) {
  381. try {
  382. $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters());
  383. } catch (InvalidObjectExeption $e) {
  384. return false;
  385. }
  386. }
  387. if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) {
  388. try {
  389. $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters());
  390. } catch (InvalidObjectExeption $e) {
  391. return false;
  392. }
  393. }
  394. return
  395. $this->isValidCommon()
  396. &&
  397. $this->getParsedSubject() !== ''
  398. ;
  399. }
  400. protected function isValidCommon(): bool {
  401. return
  402. $this->getApp() !== ''
  403. &&
  404. $this->getType() !== ''
  405. &&
  406. $this->getAffectedUser() !== ''
  407. &&
  408. $this->getTimestamp() !== 0
  409. /**
  410. * Disabled for BC with old activities
  411. * &&
  412. * $this->getObjectType() !== ''
  413. * &&
  414. * $this->getObjectId() !== 0
  415. */
  416. ;
  417. }
  418. public function setGenerateNotification(bool $generate): IEvent {
  419. $this->generateNotification = $generate;
  420. return $this;
  421. }
  422. public function getGenerateNotification(): bool {
  423. return $this->generateNotification;
  424. }
  425. }