Notification.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Notification;
  24. use OCP\Notification\IAction;
  25. use OCP\Notification\INotification;
  26. use OCP\RichObjectStrings\InvalidObjectExeption;
  27. use OCP\RichObjectStrings\IValidator;
  28. class Notification implements INotification {
  29. /** @var IValidator */
  30. protected $richValidator;
  31. /** @var string */
  32. protected $app;
  33. /** @var string */
  34. protected $user;
  35. /** @var \DateTime */
  36. protected $dateTime;
  37. /** @var string */
  38. protected $objectType;
  39. /** @var string */
  40. protected $objectId;
  41. /** @var string */
  42. protected $subject;
  43. /** @var array */
  44. protected $subjectParameters;
  45. /** @var string */
  46. protected $subjectParsed;
  47. /** @var string */
  48. protected $subjectRich;
  49. /** @var array */
  50. protected $subjectRichParameters;
  51. /** @var string */
  52. protected $message;
  53. /** @var array */
  54. protected $messageParameters;
  55. /** @var string */
  56. protected $messageParsed;
  57. /** @var string */
  58. protected $messageRich;
  59. /** @var array */
  60. protected $messageRichParameters;
  61. /** @var string */
  62. protected $link;
  63. /** @var string */
  64. protected $icon;
  65. /** @var array */
  66. protected $actions;
  67. /** @var array */
  68. protected $actionsParsed;
  69. /** @var bool */
  70. protected $hasPrimaryAction;
  71. /** @var bool */
  72. protected $hasPrimaryParsedAction;
  73. /**
  74. * Constructor
  75. *
  76. * @param IValidator $richValidator
  77. */
  78. public function __construct(IValidator $richValidator) {
  79. $this->richValidator = $richValidator;
  80. $this->app = '';
  81. $this->user = '';
  82. $this->dateTime = new \DateTime();
  83. $this->dateTime->setTimestamp(0);
  84. $this->objectType = '';
  85. $this->objectId = '';
  86. $this->subject = '';
  87. $this->subjectParameters = [];
  88. $this->subjectParsed = '';
  89. $this->subjectRich = '';
  90. $this->subjectRichParameters = [];
  91. $this->message = '';
  92. $this->messageParameters = [];
  93. $this->messageParsed = '';
  94. $this->messageRich = '';
  95. $this->messageRichParameters = [];
  96. $this->link = '';
  97. $this->icon = '';
  98. $this->actions = [];
  99. $this->actionsParsed = [];
  100. }
  101. /**
  102. * @param string $app
  103. * @return $this
  104. * @throws \InvalidArgumentException if the app id is invalid
  105. * @since 8.2.0
  106. */
  107. public function setApp($app) {
  108. if (!is_string($app) || $app === '' || isset($app[32])) {
  109. throw new \InvalidArgumentException('The given app name is invalid');
  110. }
  111. $this->app = $app;
  112. return $this;
  113. }
  114. /**
  115. * @return string
  116. * @since 8.2.0
  117. */
  118. public function getApp() {
  119. return $this->app;
  120. }
  121. /**
  122. * @param string $user
  123. * @return $this
  124. * @throws \InvalidArgumentException if the user id is invalid
  125. * @since 8.2.0
  126. */
  127. public function setUser($user) {
  128. if (!is_string($user) || $user === '' || isset($user[64])) {
  129. throw new \InvalidArgumentException('The given user id is invalid');
  130. }
  131. $this->user = $user;
  132. return $this;
  133. }
  134. /**
  135. * @return string
  136. * @since 8.2.0
  137. */
  138. public function getUser() {
  139. return $this->user;
  140. }
  141. /**
  142. * @param \DateTime $dateTime
  143. * @return $this
  144. * @throws \InvalidArgumentException if the $dateTime is invalid
  145. * @since 9.0.0
  146. */
  147. public function setDateTime(\DateTime $dateTime) {
  148. if ($dateTime->getTimestamp() === 0) {
  149. throw new \InvalidArgumentException('The given date time is invalid');
  150. }
  151. $this->dateTime = $dateTime;
  152. return $this;
  153. }
  154. /**
  155. * @return \DateTime
  156. * @since 9.0.0
  157. */
  158. public function getDateTime() {
  159. return $this->dateTime;
  160. }
  161. /**
  162. * @param string $type
  163. * @param string $id
  164. * @return $this
  165. * @throws \InvalidArgumentException if the object type or id is invalid
  166. * @since 8.2.0 - 9.0.0: Type of $id changed to string
  167. */
  168. public function setObject($type, $id) {
  169. if (!is_string($type) || $type === '' || isset($type[64])) {
  170. throw new \InvalidArgumentException('The given object type is invalid');
  171. }
  172. $this->objectType = $type;
  173. if (!is_int($id) && (!is_string($id) || $id === '' || isset($id[64]))) {
  174. throw new \InvalidArgumentException('The given object id is invalid');
  175. }
  176. $this->objectId = (string) $id;
  177. return $this;
  178. }
  179. /**
  180. * @return string
  181. * @since 8.2.0
  182. */
  183. public function getObjectType() {
  184. return $this->objectType;
  185. }
  186. /**
  187. * @return string
  188. * @since 8.2.0 - 9.0.0: Return type changed to string
  189. */
  190. public function getObjectId() {
  191. return $this->objectId;
  192. }
  193. /**
  194. * @param string $subject
  195. * @param array $parameters
  196. * @return $this
  197. * @throws \InvalidArgumentException if the subject or parameters are invalid
  198. * @since 8.2.0
  199. */
  200. public function setSubject($subject, array $parameters = []) {
  201. if (!is_string($subject) || $subject === '' || isset($subject[64])) {
  202. throw new \InvalidArgumentException('The given subject is invalid');
  203. }
  204. $this->subject = $subject;
  205. $this->subjectParameters = $parameters;
  206. return $this;
  207. }
  208. /**
  209. * @return string
  210. * @since 8.2.0
  211. */
  212. public function getSubject() {
  213. return $this->subject;
  214. }
  215. /**
  216. * @return string[]
  217. * @since 8.2.0
  218. */
  219. public function getSubjectParameters() {
  220. return $this->subjectParameters;
  221. }
  222. /**
  223. * @param string $subject
  224. * @return $this
  225. * @throws \InvalidArgumentException if the subject is invalid
  226. * @since 8.2.0
  227. */
  228. public function setParsedSubject($subject) {
  229. if (!is_string($subject) || $subject === '') {
  230. throw new \InvalidArgumentException('The given parsed subject is invalid');
  231. }
  232. $this->subjectParsed = $subject;
  233. return $this;
  234. }
  235. /**
  236. * @return string
  237. * @since 8.2.0
  238. */
  239. public function getParsedSubject() {
  240. return $this->subjectParsed;
  241. }
  242. /**
  243. * @param string $subject
  244. * @param array $parameters
  245. * @return $this
  246. * @throws \InvalidArgumentException if the subject or parameters are invalid
  247. * @since 11.0.0
  248. */
  249. public function setRichSubject($subject, array $parameters = []) {
  250. if (!is_string($subject) || $subject === '') {
  251. throw new \InvalidArgumentException('The given parsed subject is invalid');
  252. }
  253. $this->subjectRich = $subject;
  254. $this->subjectRichParameters = $parameters;
  255. return $this;
  256. }
  257. /**
  258. * @return string
  259. * @since 11.0.0
  260. */
  261. public function getRichSubject() {
  262. return $this->subjectRich;
  263. }
  264. /**
  265. * @return array[]
  266. * @since 11.0.0
  267. */
  268. public function getRichSubjectParameters() {
  269. return $this->subjectRichParameters;
  270. }
  271. /**
  272. * @param string $message
  273. * @param array $parameters
  274. * @return $this
  275. * @throws \InvalidArgumentException if the message or parameters are invalid
  276. * @since 8.2.0
  277. */
  278. public function setMessage($message, array $parameters = []) {
  279. if (!is_string($message) || $message === '' || isset($message[64])) {
  280. throw new \InvalidArgumentException('The given message is invalid');
  281. }
  282. $this->message = $message;
  283. $this->messageParameters = $parameters;
  284. return $this;
  285. }
  286. /**
  287. * @return string
  288. * @since 8.2.0
  289. */
  290. public function getMessage() {
  291. return $this->message;
  292. }
  293. /**
  294. * @return string[]
  295. * @since 8.2.0
  296. */
  297. public function getMessageParameters() {
  298. return $this->messageParameters;
  299. }
  300. /**
  301. * @param string $message
  302. * @return $this
  303. * @throws \InvalidArgumentException if the message is invalid
  304. * @since 8.2.0
  305. */
  306. public function setParsedMessage($message) {
  307. if (!is_string($message) || $message === '') {
  308. throw new \InvalidArgumentException('The given parsed message is invalid');
  309. }
  310. $this->messageParsed = $message;
  311. return $this;
  312. }
  313. /**
  314. * @return string
  315. * @since 8.2.0
  316. */
  317. public function getParsedMessage() {
  318. return $this->messageParsed;
  319. }
  320. /**
  321. * @param string $message
  322. * @param array $parameters
  323. * @return $this
  324. * @throws \InvalidArgumentException if the message or parameters are invalid
  325. * @since 11.0.0
  326. */
  327. public function setRichMessage($message, array $parameters = []) {
  328. if (!is_string($message) || $message === '') {
  329. throw new \InvalidArgumentException('The given parsed message is invalid');
  330. }
  331. $this->messageRich = $message;
  332. $this->messageRichParameters = $parameters;
  333. return $this;
  334. }
  335. /**
  336. * @return string
  337. * @since 11.0.0
  338. */
  339. public function getRichMessage() {
  340. return $this->messageRich;
  341. }
  342. /**
  343. * @return array[]
  344. * @since 11.0.0
  345. */
  346. public function getRichMessageParameters() {
  347. return $this->messageRichParameters;
  348. }
  349. /**
  350. * @param string $link
  351. * @return $this
  352. * @throws \InvalidArgumentException if the link is invalid
  353. * @since 8.2.0
  354. */
  355. public function setLink($link) {
  356. if (!is_string($link) || $link === '' || isset($link[4000])) {
  357. throw new \InvalidArgumentException('The given link is invalid');
  358. }
  359. $this->link = $link;
  360. return $this;
  361. }
  362. /**
  363. * @return string
  364. * @since 8.2.0
  365. */
  366. public function getLink() {
  367. return $this->link;
  368. }
  369. /**
  370. * @param string $icon
  371. * @return $this
  372. * @throws \InvalidArgumentException if the icon is invalid
  373. * @since 11.0.0
  374. */
  375. public function setIcon($icon) {
  376. if (!is_string($icon) || $icon === '' || isset($icon[4000])) {
  377. throw new \InvalidArgumentException('The given icon is invalid');
  378. }
  379. $this->icon = $icon;
  380. return $this;
  381. }
  382. /**
  383. * @return string
  384. * @since 11.0.0
  385. */
  386. public function getIcon() {
  387. return $this->icon;
  388. }
  389. /**
  390. * @return IAction
  391. * @since 8.2.0
  392. */
  393. public function createAction() {
  394. return new Action();
  395. }
  396. /**
  397. * @param IAction $action
  398. * @return $this
  399. * @throws \InvalidArgumentException if the action is invalid
  400. * @since 8.2.0
  401. */
  402. public function addAction(IAction $action) {
  403. if (!$action->isValid()) {
  404. throw new \InvalidArgumentException('The given action is invalid');
  405. }
  406. if ($action->isPrimary()) {
  407. if ($this->hasPrimaryAction) {
  408. throw new \InvalidArgumentException('The notification already has a primary action');
  409. }
  410. $this->hasPrimaryAction = true;
  411. }
  412. $this->actions[] = $action;
  413. return $this;
  414. }
  415. /**
  416. * @return IAction[]
  417. * @since 8.2.0
  418. */
  419. public function getActions() {
  420. return $this->actions;
  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 addParsedAction(IAction $action) {
  429. if (!$action->isValidParsed()) {
  430. throw new \InvalidArgumentException('The given parsed action is invalid');
  431. }
  432. if ($action->isPrimary()) {
  433. if ($this->hasPrimaryParsedAction) {
  434. throw new \InvalidArgumentException('The notification already has a primary action');
  435. }
  436. $this->hasPrimaryParsedAction = true;
  437. // Make sure the primary action is always the first one
  438. array_unshift($this->actionsParsed, $action);
  439. } else {
  440. $this->actionsParsed[] = $action;
  441. }
  442. return $this;
  443. }
  444. /**
  445. * @return IAction[]
  446. * @since 8.2.0
  447. */
  448. public function getParsedActions() {
  449. return $this->actionsParsed;
  450. }
  451. /**
  452. * @return bool
  453. * @since 8.2.0
  454. */
  455. public function isValid() {
  456. return
  457. $this->isValidCommon()
  458. &&
  459. $this->getSubject() !== ''
  460. ;
  461. }
  462. /**
  463. * @return bool
  464. * @since 8.2.0
  465. */
  466. public function isValidParsed() {
  467. if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) {
  468. try {
  469. $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters());
  470. } catch (InvalidObjectExeption $e) {
  471. return false;
  472. }
  473. }
  474. if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) {
  475. try {
  476. $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters());
  477. } catch (InvalidObjectExeption $e) {
  478. return false;
  479. }
  480. }
  481. return
  482. $this->isValidCommon()
  483. &&
  484. $this->getParsedSubject() !== ''
  485. ;
  486. }
  487. /**
  488. * @return bool
  489. */
  490. protected function isValidCommon() {
  491. return
  492. $this->getApp() !== ''
  493. &&
  494. $this->getUser() !== ''
  495. &&
  496. $this->getDateTime()->getTimestamp() !== 0
  497. &&
  498. $this->getObjectType() !== ''
  499. &&
  500. $this->getObjectId() !== ''
  501. ;
  502. }
  503. }