Notification.php 12 KB

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