Notification.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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\Notification\InvalidValueException;
  29. use OCP\RichObjectStrings\InvalidObjectExeption;
  30. use OCP\RichObjectStrings\IValidator;
  31. class Notification implements INotification {
  32. protected string $app = '';
  33. protected string $user = '';
  34. protected \DateTime $dateTime;
  35. protected string $objectType = '';
  36. protected string $objectId = '';
  37. protected string $subject = '';
  38. protected array $subjectParameters = [];
  39. protected string $subjectParsed = '';
  40. protected string $subjectRich = '';
  41. protected array $subjectRichParameters = [];
  42. protected string $message = '';
  43. protected array $messageParameters = [];
  44. protected string $messageParsed = '';
  45. protected string $messageRich = '';
  46. protected array $messageRichParameters = [];
  47. protected string $link = '';
  48. protected string $icon = '';
  49. protected array $actions = [];
  50. protected array $actionsParsed = [];
  51. protected bool $hasPrimaryAction = false;
  52. protected bool $hasPrimaryParsedAction = false;
  53. public function __construct(
  54. protected IValidator $richValidator,
  55. ) {
  56. $this->dateTime = new \DateTime();
  57. $this->dateTime->setTimestamp(0);
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function setApp(string $app): INotification {
  63. if ($app === '' || isset($app[32])) {
  64. throw new InvalidValueException('app');
  65. }
  66. $this->app = $app;
  67. return $this;
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function getApp(): string {
  73. return $this->app;
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function setUser(string $user): INotification {
  79. if ($user === '' || isset($user[64])) {
  80. throw new InvalidValueException('user');
  81. }
  82. $this->user = $user;
  83. return $this;
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public function getUser(): string {
  89. return $this->user;
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function setDateTime(\DateTime $dateTime): INotification {
  95. if ($dateTime->getTimestamp() === 0) {
  96. throw new InvalidValueException('dateTime');
  97. }
  98. $this->dateTime = $dateTime;
  99. return $this;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function getDateTime(): \DateTime {
  105. return $this->dateTime;
  106. }
  107. /**
  108. * {@inheritDoc}
  109. */
  110. public function setObject(string $type, string $id): INotification {
  111. if ($type === '' || isset($type[64])) {
  112. throw new InvalidValueException('objectType');
  113. }
  114. $this->objectType = $type;
  115. if ($id === '' || isset($id[64])) {
  116. throw new InvalidValueException('objectId');
  117. }
  118. $this->objectId = $id;
  119. return $this;
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public function getObjectType(): string {
  125. return $this->objectType;
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. public function getObjectId(): string {
  131. return $this->objectId;
  132. }
  133. /**
  134. * {@inheritDoc}
  135. */
  136. public function setSubject(string $subject, array $parameters = []): INotification {
  137. if ($subject === '' || isset($subject[64])) {
  138. throw new InvalidValueException('subject');
  139. }
  140. $this->subject = $subject;
  141. $this->subjectParameters = $parameters;
  142. return $this;
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public function getSubject(): string {
  148. return $this->subject;
  149. }
  150. /**
  151. * {@inheritDoc}
  152. */
  153. public function getSubjectParameters(): array {
  154. return $this->subjectParameters;
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public function setParsedSubject(string $subject): INotification {
  160. if ($subject === '') {
  161. throw new InvalidValueException('parsedSubject');
  162. }
  163. $this->subjectParsed = $subject;
  164. return $this;
  165. }
  166. /**
  167. * {@inheritDoc}
  168. */
  169. public function getParsedSubject(): string {
  170. return $this->subjectParsed;
  171. }
  172. /**
  173. * {@inheritDoc}
  174. */
  175. public function setRichSubject(string $subject, array $parameters = []): INotification {
  176. if ($subject === '') {
  177. throw new InvalidValueException('richSubject');
  178. }
  179. $this->subjectRich = $subject;
  180. $this->subjectRichParameters = $parameters;
  181. if ($this->subjectParsed === '') {
  182. try {
  183. $this->subjectParsed = $this->richToParsed($subject, $parameters);
  184. } catch (\InvalidArgumentException $e) {
  185. throw new InvalidValueException('richSubjectParameters', $e);
  186. }
  187. }
  188. return $this;
  189. }
  190. /**
  191. * @throws \InvalidArgumentException if a parameter has no name or no type
  192. */
  193. private function richToParsed(string $message, array $parameters): string {
  194. $placeholders = [];
  195. $replacements = [];
  196. foreach ($parameters as $placeholder => $parameter) {
  197. $placeholders[] = '{' . $placeholder . '}';
  198. foreach (['name','type'] as $requiredField) {
  199. if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) {
  200. throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing");
  201. }
  202. }
  203. if ($parameter['type'] === 'user') {
  204. $replacements[] = '@' . $parameter['name'];
  205. } elseif ($parameter['type'] === 'file') {
  206. $replacements[] = $parameter['path'] ?? $parameter['name'];
  207. } else {
  208. $replacements[] = $parameter['name'];
  209. }
  210. }
  211. return str_replace($placeholders, $replacements, $message);
  212. }
  213. /**
  214. * {@inheritDoc}
  215. */
  216. public function getRichSubject(): string {
  217. return $this->subjectRich;
  218. }
  219. /**
  220. * {@inheritDoc}
  221. */
  222. public function getRichSubjectParameters(): array {
  223. return $this->subjectRichParameters;
  224. }
  225. /**
  226. * {@inheritDoc}
  227. */
  228. public function setMessage(string $message, array $parameters = []): INotification {
  229. if ($message === '' || isset($message[64])) {
  230. throw new InvalidValueException('message');
  231. }
  232. $this->message = $message;
  233. $this->messageParameters = $parameters;
  234. return $this;
  235. }
  236. /**
  237. * {@inheritDoc}
  238. */
  239. public function getMessage(): string {
  240. return $this->message;
  241. }
  242. /**
  243. * {@inheritDoc}
  244. */
  245. public function getMessageParameters(): array {
  246. return $this->messageParameters;
  247. }
  248. /**
  249. * {@inheritDoc}
  250. */
  251. public function setParsedMessage(string $message): INotification {
  252. if ($message === '') {
  253. throw new InvalidValueException('parsedMessage');
  254. }
  255. $this->messageParsed = $message;
  256. return $this;
  257. }
  258. /**
  259. * {@inheritDoc}
  260. */
  261. public function getParsedMessage(): string {
  262. return $this->messageParsed;
  263. }
  264. /**
  265. * {@inheritDoc}
  266. */
  267. public function setRichMessage(string $message, array $parameters = []): INotification {
  268. if ($message === '') {
  269. throw new InvalidValueException('richMessage');
  270. }
  271. $this->messageRich = $message;
  272. $this->messageRichParameters = $parameters;
  273. if ($this->messageParsed === '') {
  274. try {
  275. $this->messageParsed = $this->richToParsed($message, $parameters);
  276. } catch (\InvalidArgumentException $e) {
  277. throw new InvalidValueException('richMessageParameters', $e);
  278. }
  279. }
  280. return $this;
  281. }
  282. /**
  283. * {@inheritDoc}
  284. */
  285. public function getRichMessage(): string {
  286. return $this->messageRich;
  287. }
  288. /**
  289. * {@inheritDoc}
  290. */
  291. public function getRichMessageParameters(): array {
  292. return $this->messageRichParameters;
  293. }
  294. /**
  295. * {@inheritDoc}
  296. */
  297. public function setLink(string $link): INotification {
  298. if ($link === '' || isset($link[4000])) {
  299. throw new InvalidValueException('link');
  300. }
  301. $this->link = $link;
  302. return $this;
  303. }
  304. /**
  305. * {@inheritDoc}
  306. */
  307. public function getLink(): string {
  308. return $this->link;
  309. }
  310. /**
  311. * {@inheritDoc}
  312. */
  313. public function setIcon(string $icon): INotification {
  314. if ($icon === '' || isset($icon[4000])) {
  315. throw new InvalidValueException('icon');
  316. }
  317. $this->icon = $icon;
  318. return $this;
  319. }
  320. /**
  321. * {@inheritDoc}
  322. */
  323. public function getIcon(): string {
  324. return $this->icon;
  325. }
  326. /**
  327. * {@inheritDoc}
  328. */
  329. public function createAction(): IAction {
  330. return new Action();
  331. }
  332. /**
  333. * {@inheritDoc}
  334. */
  335. public function addAction(IAction $action): INotification {
  336. if (!$action->isValid()) {
  337. throw new InvalidValueException('action');
  338. }
  339. if ($action->isPrimary()) {
  340. if ($this->hasPrimaryAction) {
  341. throw new InvalidValueException('primaryAction');
  342. }
  343. $this->hasPrimaryAction = true;
  344. }
  345. $this->actions[] = $action;
  346. return $this;
  347. }
  348. /**
  349. * {@inheritDoc}
  350. */
  351. public function getActions(): array {
  352. return $this->actions;
  353. }
  354. /**
  355. * {@inheritDoc}
  356. */
  357. public function addParsedAction(IAction $action): INotification {
  358. if (!$action->isValidParsed()) {
  359. throw new InvalidValueException('action');
  360. }
  361. if ($action->isPrimary()) {
  362. if ($this->hasPrimaryParsedAction) {
  363. throw new InvalidValueException('primaryAction');
  364. }
  365. $this->hasPrimaryParsedAction = true;
  366. // Make sure the primary action is always the first one
  367. array_unshift($this->actionsParsed, $action);
  368. } else {
  369. $this->actionsParsed[] = $action;
  370. }
  371. return $this;
  372. }
  373. /**
  374. * {@inheritDoc}
  375. */
  376. public function getParsedActions(): array {
  377. return $this->actionsParsed;
  378. }
  379. /**
  380. * {@inheritDoc}
  381. */
  382. public function isValid(): bool {
  383. return
  384. $this->isValidCommon()
  385. &&
  386. $this->getSubject() !== ''
  387. ;
  388. }
  389. /**
  390. * {@inheritDoc}
  391. */
  392. public function isValidParsed(): bool {
  393. if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) {
  394. try {
  395. $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters());
  396. } catch (InvalidObjectExeption $e) {
  397. return false;
  398. }
  399. }
  400. if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) {
  401. try {
  402. $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters());
  403. } catch (InvalidObjectExeption $e) {
  404. return false;
  405. }
  406. }
  407. return
  408. $this->isValidCommon()
  409. &&
  410. $this->getParsedSubject() !== ''
  411. ;
  412. }
  413. protected function isValidCommon(): bool {
  414. return
  415. $this->getApp() !== ''
  416. &&
  417. $this->getUser() !== ''
  418. &&
  419. $this->getDateTime()->getTimestamp() !== 0
  420. &&
  421. $this->getObjectType() !== ''
  422. &&
  423. $this->getObjectId() !== ''
  424. ;
  425. }
  426. }