Notification.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\Notification;
  26. use OCP\Notification\IAction;
  27. use OCP\Notification\INotification;
  28. use OCP\RichObjectStrings\InvalidObjectExeption;
  29. use OCP\RichObjectStrings\IValidator;
  30. class Notification implements INotification {
  31. /** @var IValidator */
  32. protected $richValidator;
  33. /** @var string */
  34. protected $app;
  35. /** @var string */
  36. protected $user;
  37. /** @var \DateTime */
  38. protected $dateTime;
  39. /** @var string */
  40. protected $objectType;
  41. /** @var string */
  42. protected $objectId;
  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 $link;
  65. /** @var string */
  66. protected $icon;
  67. /** @var array */
  68. protected $actions;
  69. /** @var array */
  70. protected $actionsParsed;
  71. /** @var bool */
  72. protected $hasPrimaryAction;
  73. /** @var bool */
  74. protected $hasPrimaryParsedAction;
  75. public function __construct(IValidator $richValidator) {
  76. $this->richValidator = $richValidator;
  77. $this->app = '';
  78. $this->user = '';
  79. $this->dateTime = new \DateTime();
  80. $this->dateTime->setTimestamp(0);
  81. $this->objectType = '';
  82. $this->objectId = '';
  83. $this->subject = '';
  84. $this->subjectParameters = [];
  85. $this->subjectParsed = '';
  86. $this->subjectRich = '';
  87. $this->subjectRichParameters = [];
  88. $this->message = '';
  89. $this->messageParameters = [];
  90. $this->messageParsed = '';
  91. $this->messageRich = '';
  92. $this->messageRichParameters = [];
  93. $this->link = '';
  94. $this->icon = '';
  95. $this->actions = [];
  96. $this->actionsParsed = [];
  97. }
  98. /**
  99. * @param string $app
  100. * @return $this
  101. * @throws \InvalidArgumentException if the app id is invalid
  102. * @since 8.2.0
  103. */
  104. public function setApp(string $app): INotification {
  105. if ($app === '' || isset($app[32])) {
  106. throw new \InvalidArgumentException('The given app name is invalid');
  107. }
  108. $this->app = $app;
  109. return $this;
  110. }
  111. /**
  112. * @return string
  113. * @since 8.2.0
  114. */
  115. public function getApp(): string {
  116. return $this->app;
  117. }
  118. /**
  119. * @param string $user
  120. * @return $this
  121. * @throws \InvalidArgumentException if the user id is invalid
  122. * @since 8.2.0
  123. */
  124. public function setUser(string $user): INotification {
  125. if ($user === '' || isset($user[64])) {
  126. throw new \InvalidArgumentException('The given user id is invalid');
  127. }
  128. $this->user = $user;
  129. return $this;
  130. }
  131. /**
  132. * @return string
  133. * @since 8.2.0
  134. */
  135. public function getUser(): string {
  136. return $this->user;
  137. }
  138. /**
  139. * @param \DateTime $dateTime
  140. * @return $this
  141. * @throws \InvalidArgumentException if the $dateTime is invalid
  142. * @since 9.0.0
  143. */
  144. public function setDateTime(\DateTime $dateTime): INotification {
  145. if ($dateTime->getTimestamp() === 0) {
  146. throw new \InvalidArgumentException('The given date time is invalid');
  147. }
  148. $this->dateTime = $dateTime;
  149. return $this;
  150. }
  151. /**
  152. * @return \DateTime
  153. * @since 9.0.0
  154. */
  155. public function getDateTime(): \DateTime {
  156. return $this->dateTime;
  157. }
  158. /**
  159. * @param string $type
  160. * @param string $id
  161. * @return $this
  162. * @throws \InvalidArgumentException if the object type or id is invalid
  163. * @since 8.2.0 - 9.0.0: Type of $id changed to string
  164. */
  165. public function setObject(string $type, string $id): INotification {
  166. if ($type === '' || isset($type[64])) {
  167. throw new \InvalidArgumentException('The given object type is invalid');
  168. }
  169. $this->objectType = $type;
  170. if ($id === '' || isset($id[64])) {
  171. throw new \InvalidArgumentException('The given object id is invalid');
  172. }
  173. $this->objectId = $id;
  174. return $this;
  175. }
  176. /**
  177. * @return string
  178. * @since 8.2.0
  179. */
  180. public function getObjectType(): string {
  181. return $this->objectType;
  182. }
  183. /**
  184. * @return string
  185. * @since 8.2.0 - 9.0.0: Return type changed to string
  186. */
  187. public function getObjectId(): string {
  188. return $this->objectId;
  189. }
  190. /**
  191. * @param string $subject
  192. * @param array $parameters
  193. * @return $this
  194. * @throws \InvalidArgumentException if the subject or parameters are invalid
  195. * @since 8.2.0
  196. */
  197. public function setSubject(string $subject, array $parameters = []): INotification {
  198. if ($subject === '' || isset($subject[64])) {
  199. throw new \InvalidArgumentException('The given subject is invalid');
  200. }
  201. $this->subject = $subject;
  202. $this->subjectParameters = $parameters;
  203. return $this;
  204. }
  205. /**
  206. * @return string
  207. * @since 8.2.0
  208. */
  209. public function getSubject(): string {
  210. return $this->subject;
  211. }
  212. /**
  213. * @return array
  214. * @since 8.2.0
  215. */
  216. public function getSubjectParameters(): array {
  217. return $this->subjectParameters;
  218. }
  219. /**
  220. * @param string $subject
  221. * @return $this
  222. * @throws \InvalidArgumentException if the subject is invalid
  223. * @since 8.2.0
  224. */
  225. public function setParsedSubject(string $subject): INotification {
  226. if ($subject === '') {
  227. throw new \InvalidArgumentException('The given parsed subject is invalid');
  228. }
  229. $this->subjectParsed = $subject;
  230. return $this;
  231. }
  232. /**
  233. * @return string
  234. * @since 8.2.0
  235. */
  236. public function getParsedSubject(): string {
  237. return $this->subjectParsed;
  238. }
  239. /**
  240. * @param string $subject
  241. * @param array $parameters
  242. * @return $this
  243. * @throws \InvalidArgumentException if the subject or parameters are invalid
  244. * @since 11.0.0
  245. */
  246. public function setRichSubject(string $subject, array $parameters = []): INotification {
  247. if ($subject === '') {
  248. throw new \InvalidArgumentException('The given parsed subject is invalid');
  249. }
  250. $this->subjectRich = $subject;
  251. $this->subjectRichParameters = $parameters;
  252. if ($this->subjectParsed === '') {
  253. $this->subjectParsed = $this->richToParsed($subject, $parameters);
  254. }
  255. return $this;
  256. }
  257. /**
  258. * @throws \InvalidArgumentException if a parameter has no name or no type
  259. */
  260. private function richToParsed(string $message, array $parameters): string {
  261. $placeholders = [];
  262. $replacements = [];
  263. foreach ($parameters as $placeholder => $parameter) {
  264. $placeholders[] = '{' . $placeholder . '}';
  265. foreach (['name','type'] as $requiredField) {
  266. if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) {
  267. throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing");
  268. }
  269. }
  270. if ($parameter['type'] === 'user') {
  271. $replacements[] = '@' . $parameter['name'];
  272. } elseif ($parameter['type'] === 'file') {
  273. $replacements[] = $parameter['path'] ?? $parameter['name'];
  274. } else {
  275. $replacements[] = $parameter['name'];
  276. }
  277. }
  278. return str_replace($placeholders, $replacements, $message);
  279. }
  280. /**
  281. * @return string
  282. * @since 11.0.0
  283. */
  284. public function getRichSubject(): string {
  285. return $this->subjectRich;
  286. }
  287. /**
  288. * @return array[]
  289. * @since 11.0.0
  290. */
  291. public function getRichSubjectParameters(): array {
  292. return $this->subjectRichParameters;
  293. }
  294. /**
  295. * @param string $message
  296. * @param array $parameters
  297. * @return $this
  298. * @throws \InvalidArgumentException if the message or parameters are invalid
  299. * @since 8.2.0
  300. */
  301. public function setMessage(string $message, array $parameters = []): INotification {
  302. if ($message === '' || isset($message[64])) {
  303. throw new \InvalidArgumentException('The given message is invalid');
  304. }
  305. $this->message = $message;
  306. $this->messageParameters = $parameters;
  307. return $this;
  308. }
  309. /**
  310. * @return string
  311. * @since 8.2.0
  312. */
  313. public function getMessage(): string {
  314. return $this->message;
  315. }
  316. /**
  317. * @return array
  318. * @since 8.2.0
  319. */
  320. public function getMessageParameters(): array {
  321. return $this->messageParameters;
  322. }
  323. /**
  324. * @param string $message
  325. * @return $this
  326. * @throws \InvalidArgumentException if the message is invalid
  327. * @since 8.2.0
  328. */
  329. public function setParsedMessage(string $message): INotification {
  330. if ($message === '') {
  331. throw new \InvalidArgumentException('The given parsed message is invalid');
  332. }
  333. $this->messageParsed = $message;
  334. return $this;
  335. }
  336. /**
  337. * @return string
  338. * @since 8.2.0
  339. */
  340. public function getParsedMessage(): string {
  341. return $this->messageParsed;
  342. }
  343. /**
  344. * @param string $message
  345. * @param array $parameters
  346. * @return $this
  347. * @throws \InvalidArgumentException if the message or parameters are invalid
  348. * @since 11.0.0
  349. */
  350. public function setRichMessage(string $message, array $parameters = []): INotification {
  351. if ($message === '') {
  352. throw new \InvalidArgumentException('The given parsed message is invalid');
  353. }
  354. $this->messageRich = $message;
  355. $this->messageRichParameters = $parameters;
  356. if ($this->messageParsed === '') {
  357. $this->messageParsed = $this->richToParsed($message, $parameters);
  358. }
  359. return $this;
  360. }
  361. /**
  362. * @return string
  363. * @since 11.0.0
  364. */
  365. public function getRichMessage(): string {
  366. return $this->messageRich;
  367. }
  368. /**
  369. * @return array[]
  370. * @since 11.0.0
  371. */
  372. public function getRichMessageParameters(): array {
  373. return $this->messageRichParameters;
  374. }
  375. /**
  376. * @param string $link
  377. * @return $this
  378. * @throws \InvalidArgumentException if the link is invalid
  379. * @since 8.2.0
  380. */
  381. public function setLink(string $link): INotification {
  382. if ($link === '' || isset($link[4000])) {
  383. throw new \InvalidArgumentException('The given link is invalid');
  384. }
  385. $this->link = $link;
  386. return $this;
  387. }
  388. /**
  389. * @return string
  390. * @since 8.2.0
  391. */
  392. public function getLink(): string {
  393. return $this->link;
  394. }
  395. /**
  396. * @param string $icon
  397. * @return $this
  398. * @throws \InvalidArgumentException if the icon is invalid
  399. * @since 11.0.0
  400. */
  401. public function setIcon(string $icon): INotification {
  402. if ($icon === '' || isset($icon[4000])) {
  403. throw new \InvalidArgumentException('The given icon is invalid');
  404. }
  405. $this->icon = $icon;
  406. return $this;
  407. }
  408. /**
  409. * @return string
  410. * @since 11.0.0
  411. */
  412. public function getIcon(): string {
  413. return $this->icon;
  414. }
  415. /**
  416. * @return IAction
  417. * @since 8.2.0
  418. */
  419. public function createAction(): IAction {
  420. return new Action();
  421. }
  422. /**
  423. * @param IAction $action
  424. * @return $this
  425. * @throws \InvalidArgumentException if the action is invalid
  426. * @since 8.2.0
  427. */
  428. public function addAction(IAction $action): INotification {
  429. if (!$action->isValid()) {
  430. throw new \InvalidArgumentException('The given action is invalid');
  431. }
  432. if ($action->isPrimary()) {
  433. if ($this->hasPrimaryAction) {
  434. throw new \InvalidArgumentException('The notification already has a primary action');
  435. }
  436. $this->hasPrimaryAction = true;
  437. }
  438. $this->actions[] = $action;
  439. return $this;
  440. }
  441. /**
  442. * @return IAction[]
  443. * @since 8.2.0
  444. */
  445. public function getActions(): array {
  446. return $this->actions;
  447. }
  448. /**
  449. * @param IAction $action
  450. * @return $this
  451. * @throws \InvalidArgumentException if the action is invalid
  452. * @since 8.2.0
  453. */
  454. public function addParsedAction(IAction $action): INotification {
  455. if (!$action->isValidParsed()) {
  456. throw new \InvalidArgumentException('The given parsed action is invalid');
  457. }
  458. if ($action->isPrimary()) {
  459. if ($this->hasPrimaryParsedAction) {
  460. throw new \InvalidArgumentException('The notification already has a primary action');
  461. }
  462. $this->hasPrimaryParsedAction = true;
  463. // Make sure the primary action is always the first one
  464. array_unshift($this->actionsParsed, $action);
  465. } else {
  466. $this->actionsParsed[] = $action;
  467. }
  468. return $this;
  469. }
  470. /**
  471. * @return IAction[]
  472. * @since 8.2.0
  473. */
  474. public function getParsedActions(): array {
  475. return $this->actionsParsed;
  476. }
  477. /**
  478. * @return bool
  479. * @since 8.2.0
  480. */
  481. public function isValid(): bool {
  482. return
  483. $this->isValidCommon()
  484. &&
  485. $this->getSubject() !== ''
  486. ;
  487. }
  488. /**
  489. * @return bool
  490. * @since 8.2.0
  491. */
  492. public function isValidParsed(): bool {
  493. if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) {
  494. try {
  495. $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters());
  496. } catch (InvalidObjectExeption $e) {
  497. return false;
  498. }
  499. }
  500. if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) {
  501. try {
  502. $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters());
  503. } catch (InvalidObjectExeption $e) {
  504. return false;
  505. }
  506. }
  507. return
  508. $this->isValidCommon()
  509. &&
  510. $this->getParsedSubject() !== ''
  511. ;
  512. }
  513. protected function isValidCommon(): bool {
  514. return
  515. $this->getApp() !== ''
  516. &&
  517. $this->getUser() !== ''
  518. &&
  519. $this->getDateTime()->getTimestamp() !== 0
  520. &&
  521. $this->getObjectType() !== ''
  522. &&
  523. $this->getObjectId() !== ''
  524. ;
  525. }
  526. }