From b35d2fd8f2e96f56951e33d27d07cb213375e46b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 14 Oct 2016 16:55:20 +0200 Subject: Allow rich object subjects for Notifications Signed-off-by: Joas Schilling --- lib/private/Notification/Manager.php | 5 ++- lib/private/Notification/Notification.php | 64 ++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) (limited to 'lib/private/Notification') diff --git a/lib/private/Notification/Manager.php b/lib/private/Notification/Manager.php index ec831cf9793..4b57db4bac9 100644 --- a/lib/private/Notification/Manager.php +++ b/lib/private/Notification/Manager.php @@ -24,6 +24,7 @@ namespace OC\Notification; +use OC\RichObjectStrings\Validator; use OCP\Notification\IApp; use OCP\Notification\IManager; use OCP\Notification\INotification; @@ -149,7 +150,9 @@ class Manager implements IManager { * @since 8.2.0 */ public function createNotification() { - return new Notification(); + return new Notification( + new Validator() + ); } /** diff --git a/lib/private/Notification/Notification.php b/lib/private/Notification/Notification.php index 7bf4b9a74cf..7346f35f966 100644 --- a/lib/private/Notification/Notification.php +++ b/lib/private/Notification/Notification.php @@ -26,8 +26,14 @@ namespace OC\Notification; use OCP\Notification\IAction; use OCP\Notification\INotification; +use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IValidator; class Notification implements INotification { + + /** @var IValidator */ + protected $richValidator; + /** @var string */ protected $app; @@ -52,6 +58,12 @@ class Notification implements INotification { /** @var string */ protected $subjectParsed; + /** @var string */ + protected $subjectRich; + + /** @var array */ + protected $subjectRichParameters; + /** @var string */ protected $message; @@ -81,8 +93,11 @@ class Notification implements INotification { /** * Constructor + * + * @param IValidator $richValidator */ - public function __construct() { + public function __construct(IValidator $richValidator) { + $this->richValidator = $richValidator; $this->app = ''; $this->user = ''; $this->dateTime = new \DateTime(); @@ -92,6 +107,8 @@ class Notification implements INotification { $this->subject = ''; $this->subjectParameters = []; $this->subjectParsed = ''; + $this->subjectRich = ''; + $this->subjectRichParameters = []; $this->message = ''; $this->messageParameters = []; $this->messageParsed = ''; @@ -261,6 +278,43 @@ class Notification implements INotification { return $this->subjectParsed; } + /** + * @param string $subject + * @param array $parameters + * @return $this + * @throws \InvalidArgumentException if the subject or parameters are invalid + * @since 9.2.0 + */ + public function setRichSubject($subject, array $parameters = []) { + if (!is_string($subject) || $subject === '') { + throw new \InvalidArgumentException('The given parsed subject is invalid'); + } + $this->subjectRich = $subject; + + if (!is_array($parameters)) { + throw new \InvalidArgumentException('The given subject parameters are invalid'); + } + $this->subjectRichParameters = $parameters; + + return $this; + } + + /** + * @return string + * @since 9.2.0 + */ + public function getRichSubject() { + return $this->subjectRich; + } + + /** + * @return array[] + * @since 9.2.0 + */ + public function getRichSubjectParameters() { + return $this->subjectRichParameters; + } + /** * @param string $message * @param array $parameters @@ -454,6 +508,14 @@ class Notification implements INotification { * @since 8.2.0 */ public function isValidParsed() { + if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) { + try { + $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters()); + } catch (InvalidObjectExeption $e) { + return false; + } + } + return $this->isValidCommon() && -- cgit v1.2.3