1
0

Event.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Phil Davis <phil.davis@inf.org>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Activity;
  28. use OCP\Activity\Exceptions\InvalidValueException;
  29. use OCP\Activity\IEvent;
  30. use OCP\RichObjectStrings\InvalidObjectExeption;
  31. use OCP\RichObjectStrings\IValidator;
  32. class Event implements IEvent {
  33. /** @var string */
  34. protected $app = '';
  35. /** @var string */
  36. protected $type = '';
  37. /** @var string */
  38. protected $affectedUser = '';
  39. /** @var string */
  40. protected $author = '';
  41. /** @var int */
  42. protected $timestamp = 0;
  43. /** @var string */
  44. protected $subject = '';
  45. /** @var array */
  46. protected $subjectParameters = [];
  47. /** @var string */
  48. protected $subjectParsed = '';
  49. /** @var string */
  50. protected $subjectRich = '';
  51. /** @var array */
  52. protected $subjectRichParameters = [];
  53. /** @var string */
  54. protected $message = '';
  55. /** @var array */
  56. protected $messageParameters = [];
  57. /** @var string */
  58. protected $messageParsed = '';
  59. /** @var string */
  60. protected $messageRich = '';
  61. /** @var array */
  62. protected $messageRichParameters = [];
  63. /** @var string */
  64. protected $objectType = '';
  65. /** @var int */
  66. protected $objectId = 0;
  67. /** @var string */
  68. protected $objectName = '';
  69. /** @var string */
  70. protected $link = '';
  71. /** @var string */
  72. protected $icon = '';
  73. /** @var bool */
  74. protected $generateNotification = true;
  75. /** @var IEvent|null */
  76. protected $child;
  77. /** @var IValidator */
  78. protected $richValidator;
  79. /**
  80. * @param IValidator $richValidator
  81. */
  82. public function __construct(IValidator $richValidator) {
  83. $this->richValidator = $richValidator;
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public function setApp(string $app): IEvent {
  89. if ($app === '' || isset($app[32])) {
  90. throw new InvalidValueException('app');
  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. * {@inheritDoc}
  103. */
  104. public function setType(string $type): IEvent {
  105. if ($type === '' || isset($type[255])) {
  106. throw new InvalidValueException('type');
  107. }
  108. $this->type = $type;
  109. return $this;
  110. }
  111. /**
  112. * @return string
  113. */
  114. public function getType(): string {
  115. return $this->type;
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public function setAffectedUser(string $affectedUser): IEvent {
  121. if ($affectedUser === '' || isset($affectedUser[64])) {
  122. throw new InvalidValueException('affectedUser');
  123. }
  124. $this->affectedUser = $affectedUser;
  125. return $this;
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function getAffectedUser(): string {
  131. return $this->affectedUser;
  132. }
  133. /**
  134. * {@inheritDoc}
  135. */
  136. public function setAuthor(string $author): IEvent {
  137. if (isset($author[64])) {
  138. throw new InvalidValueException('author');
  139. }
  140. $this->author = $author;
  141. return $this;
  142. }
  143. /**
  144. * @return string
  145. */
  146. public function getAuthor(): string {
  147. return $this->author;
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function setTimestamp(int $timestamp): IEvent {
  153. if ($timestamp < 0) {
  154. throw new InvalidValueException('timestamp');
  155. }
  156. $this->timestamp = $timestamp;
  157. return $this;
  158. }
  159. /**
  160. * @return int
  161. */
  162. public function getTimestamp(): int {
  163. return $this->timestamp;
  164. }
  165. /**
  166. * {@inheritDoc}
  167. */
  168. public function setSubject(string $subject, array $parameters = []): IEvent {
  169. if (isset($subject[255])) {
  170. throw new InvalidValueException('subject');
  171. }
  172. $this->subject = $subject;
  173. $this->subjectParameters = $parameters;
  174. return $this;
  175. }
  176. /**
  177. * @return string
  178. */
  179. public function getSubject(): string {
  180. return $this->subject;
  181. }
  182. /**
  183. * @return array
  184. */
  185. public function getSubjectParameters(): array {
  186. return $this->subjectParameters;
  187. }
  188. /**
  189. * {@inheritDoc}
  190. */
  191. public function setParsedSubject(string $subject): IEvent {
  192. if ($subject === '') {
  193. throw new InvalidValueException('parsedSubject');
  194. }
  195. $this->subjectParsed = $subject;
  196. return $this;
  197. }
  198. /**
  199. * @return string
  200. * @since 11.0.0
  201. */
  202. public function getParsedSubject(): string {
  203. return $this->subjectParsed;
  204. }
  205. /**
  206. * {@inheritDoc}
  207. */
  208. public function setRichSubject(string $subject, array $parameters = []): IEvent {
  209. if ($subject === '') {
  210. throw new InvalidValueException('richSubject');
  211. }
  212. $this->subjectRich = $subject;
  213. $this->subjectRichParameters = $parameters;
  214. if ($this->subjectParsed === '') {
  215. try {
  216. $this->subjectParsed = $this->richToParsed($subject, $parameters);
  217. } catch (\InvalidArgumentException $e) {
  218. throw new InvalidValueException('richSubjectParameters', $e);
  219. }
  220. }
  221. return $this;
  222. }
  223. /**
  224. * @throws \InvalidArgumentException if a parameter has no name or no type
  225. */
  226. private function richToParsed(string $message, array $parameters): string {
  227. $placeholders = [];
  228. $replacements = [];
  229. foreach ($parameters as $placeholder => $parameter) {
  230. $placeholders[] = '{' . $placeholder . '}';
  231. foreach (['name','type'] as $requiredField) {
  232. if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) {
  233. throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing");
  234. }
  235. }
  236. if ($parameter['type'] === 'user') {
  237. $replacements[] = '@' . $parameter['name'];
  238. } elseif ($parameter['type'] === 'file') {
  239. $replacements[] = $parameter['path'] ?? $parameter['name'];
  240. } else {
  241. $replacements[] = $parameter['name'];
  242. }
  243. }
  244. return str_replace($placeholders, $replacements, $message);
  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. * {@inheritDoc}
  262. */
  263. public function setMessage(string $message, array $parameters = []): IEvent {
  264. if (isset($message[255])) {
  265. throw new InvalidValueException('message');
  266. }
  267. $this->message = $message;
  268. $this->messageParameters = $parameters;
  269. return $this;
  270. }
  271. /**
  272. * @return string
  273. */
  274. public function getMessage(): string {
  275. return $this->message;
  276. }
  277. /**
  278. * @return array
  279. */
  280. public function getMessageParameters(): array {
  281. return $this->messageParameters;
  282. }
  283. /**
  284. * {@inheritDoc}
  285. */
  286. public function setParsedMessage(string $message): IEvent {
  287. $this->messageParsed = $message;
  288. return $this;
  289. }
  290. /**
  291. * @return string
  292. * @since 11.0.0
  293. */
  294. public function getParsedMessage(): string {
  295. return $this->messageParsed;
  296. }
  297. /**
  298. * {@inheritDoc}
  299. */
  300. public function setRichMessage(string $message, array $parameters = []): IEvent {
  301. $this->messageRich = $message;
  302. $this->messageRichParameters = $parameters;
  303. if ($this->messageParsed === '') {
  304. try {
  305. $this->messageParsed = $this->richToParsed($message, $parameters);
  306. } catch (\InvalidArgumentException $e) {
  307. throw new InvalidValueException('richMessageParameters', $e);
  308. }
  309. }
  310. return $this;
  311. }
  312. /**
  313. * @return string
  314. * @since 11.0.0
  315. */
  316. public function getRichMessage(): string {
  317. return $this->messageRich;
  318. }
  319. /**
  320. * @return array[]
  321. * @since 11.0.0
  322. */
  323. public function getRichMessageParameters(): array {
  324. return $this->messageRichParameters;
  325. }
  326. /**
  327. * {@inheritDoc}
  328. */
  329. public function setObject(string $objectType, int $objectId, string $objectName = ''): IEvent {
  330. if (isset($objectType[255])) {
  331. throw new InvalidValueException('objectType');
  332. }
  333. if (isset($objectName[4000])) {
  334. throw new InvalidValueException('objectName');
  335. }
  336. $this->objectType = $objectType;
  337. $this->objectId = $objectId;
  338. $this->objectName = $objectName;
  339. return $this;
  340. }
  341. /**
  342. * @return string
  343. */
  344. public function getObjectType(): string {
  345. return $this->objectType;
  346. }
  347. /**
  348. * @return int
  349. */
  350. public function getObjectId(): int {
  351. return $this->objectId;
  352. }
  353. /**
  354. * @return string
  355. */
  356. public function getObjectName(): string {
  357. return $this->objectName;
  358. }
  359. /**
  360. * {@inheritDoc}
  361. */
  362. public function setLink(string $link): IEvent {
  363. if (isset($link[4000])) {
  364. throw new InvalidValueException('link');
  365. }
  366. $this->link = $link;
  367. return $this;
  368. }
  369. /**
  370. * @return string
  371. */
  372. public function getLink(): string {
  373. return $this->link;
  374. }
  375. /**
  376. * {@inheritDoc}
  377. */
  378. public function setIcon(string $icon): IEvent {
  379. if (isset($icon[4000])) {
  380. throw new InvalidValueException('icon');
  381. }
  382. $this->icon = $icon;
  383. return $this;
  384. }
  385. /**
  386. * @return string
  387. * @since 11.0.0
  388. */
  389. public function getIcon(): string {
  390. return $this->icon;
  391. }
  392. /**
  393. * @param IEvent $child
  394. * @return $this
  395. * @since 11.0.0 - Since 15.0.0 returns $this
  396. */
  397. public function setChildEvent(IEvent $child): IEvent {
  398. $this->child = $child;
  399. return $this;
  400. }
  401. /**
  402. * @return IEvent|null
  403. * @since 11.0.0
  404. */
  405. public function getChildEvent() {
  406. return $this->child;
  407. }
  408. /**
  409. * @return bool
  410. * @since 8.2.0
  411. */
  412. public function isValid(): bool {
  413. return
  414. $this->isValidCommon()
  415. &&
  416. $this->getSubject() !== ''
  417. ;
  418. }
  419. /**
  420. * @return bool
  421. * @since 8.2.0
  422. */
  423. public function isValidParsed(): bool {
  424. if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) {
  425. try {
  426. $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters());
  427. } catch (InvalidObjectExeption $e) {
  428. return false;
  429. }
  430. }
  431. if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) {
  432. try {
  433. $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters());
  434. } catch (InvalidObjectExeption $e) {
  435. return false;
  436. }
  437. }
  438. return
  439. $this->isValidCommon()
  440. &&
  441. $this->getParsedSubject() !== ''
  442. ;
  443. }
  444. protected function isValidCommon(): bool {
  445. return
  446. $this->getApp() !== ''
  447. &&
  448. $this->getType() !== ''
  449. &&
  450. $this->getAffectedUser() !== ''
  451. &&
  452. $this->getTimestamp() !== 0
  453. /**
  454. * Disabled for BC with old activities
  455. * &&
  456. * $this->getObjectType() !== ''
  457. * &&
  458. * $this->getObjectId() !== 0
  459. */
  460. ;
  461. }
  462. public function setGenerateNotification(bool $generate): IEvent {
  463. $this->generateNotification = $generate;
  464. return $this;
  465. }
  466. public function getGenerateNotification(): bool {
  467. return $this->generateNotification;
  468. }
  469. }