diff --git a/lib/private/Activity/Event.php b/lib/private/Activity/Event.php index 299199766c..a830cc4857 100644 --- a/lib/private/Activity/Event.php +++ b/lib/private/Activity/Event.php @@ -24,6 +24,8 @@ namespace OC\Activity; use OCP\Activity\IEvent; +use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IValidator; class Event implements IEvent { @@ -42,10 +44,22 @@ class Event implements IEvent { /** @var array */ protected $subjectParameters = []; /** @var string */ + protected $subjectParsed; + /** @var string */ + protected $subjectRich; + /** @var array */ + protected $subjectRichParameters; + /** @var string */ protected $message = ''; /** @var array */ protected $messageParameters = []; /** @var string */ + protected $messageParsed; + /** @var string */ + protected $messageRich; + /** @var array */ + protected $messageRichParameters; + /** @var string */ protected $objectType = ''; /** @var int */ protected $objectId = 0; @@ -53,6 +67,18 @@ class Event implements IEvent { protected $objectName = ''; /** @var string */ protected $link = ''; + /** @var string */ + protected $icon = ''; + + /** @var IValidator */ + protected $richValidator; + + /** + * @param IValidator $richValidator + */ + public function __construct(IValidator $richValidator) { + $this->richValidator = $richValidator; + } /** * Set the app of the activity @@ -201,6 +227,65 @@ class Event implements IEvent { return $this->subjectParameters; } + /** + * @param string $subject + * @return $this + * @throws \InvalidArgumentException if the subject is invalid + * @since 9.2.0 + */ + public function setParsedSubject($subject) { + if (!is_string($subject) || $subject === '') { + throw new \InvalidArgumentException('The given parsed subject is invalid'); + } + $this->subjectParsed = $subject; + return $this; + } + + /** + * @return string + * @since 9.2.0 + */ + public function getParsedSubject() { + 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; + } + /** * Set the message of the activity * @@ -211,7 +296,7 @@ class Event implements IEvent { * @since 8.2.0 */ public function setMessage($message, array $parameters = []) { - if (!is_string($message) || $message === '' || isset($message[255])) { + if (!is_string($message) || isset($message[255])) { throw new \InvalidArgumentException('The given message is invalid'); } $this->message = (string) $message; @@ -233,6 +318,65 @@ class Event implements IEvent { return $this->messageParameters; } + /** + * @param string $message + * @return $this + * @throws \InvalidArgumentException if the message is invalid + * @since 9.2.0 + */ + public function setParsedMessage($message) { + if (!is_string($message) || $message === '') { + throw new \InvalidArgumentException('The given parsed message is invalid'); + } + $this->messageParsed = $message; + return $this; + } + + /** + * @return string + * @since 9.2.0 + */ + public function getParsedMessage() { + return $this->messageParsed; + } + + /** + * @param string $message + * @param array $parameters + * @return $this + * @throws \InvalidArgumentException if the subject or parameters are invalid + * @since 9.2.0 + */ + public function setRichMessage($message, array $parameters = []) { + if (!is_string($message) || $message === '') { + throw new \InvalidArgumentException('The given parsed message is invalid'); + } + $this->messageRich = $message; + + if (!is_array($parameters)) { + throw new \InvalidArgumentException('The given message parameters are invalid'); + } + $this->messageRichParameters = $parameters; + + return $this; + } + + /** + * @return string + * @since 9.2.0 + */ + public function getRichMessage() { + return $this->messageRich; + } + + /** + * @return array[] + * @since 9.2.0 + */ + public function getRichMessageParameters() { + return $this->messageRichParameters; + } + /** * Set the object of the activity * @@ -289,7 +433,7 @@ class Event implements IEvent { * @since 8.2.0 */ public function setLink($link) { - if (!is_string($link) || $link === '' || isset($link[4000])) { + if (!is_string($link) || isset($link[4000])) { throw new \InvalidArgumentException('The given link is invalid'); } $this->link = (string) $link; @@ -302,6 +446,29 @@ class Event implements IEvent { public function getLink() { return $this->link; } + + /** + * @param string $icon + * @return $this + * @throws \InvalidArgumentException if the icon is invalid + * @since 9.2.0 + */ + public function setIcon($icon) { + if (!is_string($icon) || isset($icon[4000])) { + throw new \InvalidArgumentException('The given icon is invalid'); + } + $this->icon = $icon; + return $this; + } + + /** + * @return string + * @since 9.2.0 + */ + public function getIcon() { + return $this->icon; + } + /** * @return bool * @since 8.2.0 @@ -319,18 +486,26 @@ class Event implements IEvent { * @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; -// } -// } + if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) { + try { + $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters()); + } catch (InvalidObjectExeption $e) { + return false; + } + } + + if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) { + try { + $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters()); + } catch (InvalidObjectExeption $e) { + return false; + } + } return $this->isValidCommon() -// && -// $this->getParsedSubject() !== '' + && + $this->getParsedSubject() !== '' ; } diff --git a/lib/private/Activity/Manager.php b/lib/private/Activity/Manager.php index 60a4a93589..c0e75b1209 100644 --- a/lib/private/Activity/Manager.php +++ b/lib/private/Activity/Manager.php @@ -24,6 +24,7 @@ namespace OC\Activity; +use OC\RichObjectStrings\Validator; use OCP\Activity\IConsumer; use OCP\Activity\IEvent; use OCP\Activity\IExtension; @@ -149,7 +150,9 @@ class Manager implements IManager { * @return IEvent */ public function generateEvent() { - return new Event(); + return new Event( + new Validator() + ); } /** @@ -166,18 +169,6 @@ class Manager implements IManager { * @throws \BadMethodCallException if required values have not been set */ public function publish(IEvent $event) { - if (!$event->getApp()) { - throw new \BadMethodCallException('App not set', 10); - } - if (!$event->getType()) { - throw new \BadMethodCallException('Type not set', 11); - } - if ($event->getAffectedUser() === null) { - throw new \BadMethodCallException('Affected user not set', 12); - } - if ($event->getSubject() === null || $event->getSubjectParameters() === null) { - throw new \BadMethodCallException('Subject not set', 13); - } if ($event->getAuthor() === null) { if ($this->session->getUser() instanceof IUser) { @@ -189,6 +180,10 @@ class Manager implements IManager { $event->setTimestamp(time()); } + if (!$event->isValid()) { + throw new \BadMethodCallException('The given event is invalid'); + } + foreach ($this->getConsumers() as $c) { $c->receive($event); } diff --git a/lib/public/Activity/IEvent.php b/lib/public/Activity/IEvent.php index 3af8a8b827..6d587c381c 100644 --- a/lib/public/Activity/IEvent.php +++ b/lib/public/Activity/IEvent.php @@ -97,6 +97,41 @@ interface IEvent { */ public function setSubject($subject, array $parameters = []); + /** + * @param string $subject + * @return $this + * @throws \InvalidArgumentException if the subject is invalid + * @since 9.2.0 + */ + public function setParsedSubject($subject); + + /** + * @return string + * @since 9.2.0 + */ + public function getParsedSubject(); + + /** + * @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 = []); + + /** + * @return string + * @since 9.2.0 + */ + public function getRichSubject(); + + /** + * @return array[] + * @since 9.2.0 + */ + public function getRichSubjectParameters(); + /** * Set the message of the activity * @@ -108,6 +143,41 @@ interface IEvent { */ public function setMessage($message, array $parameters = []); + /** + * @param string $message + * @return $this + * @throws \InvalidArgumentException if the message is invalid + * @since 9.2.0 + */ + public function setParsedMessage($message); + + /** + * @return string + * @since 9.2.0 + */ + public function getParsedMessage(); + + /** + * @param string $message + * @param array $parameters + * @return $this + * @throws \InvalidArgumentException if the message or parameters are invalid + * @since 9.2.0 + */ + public function setRichMessage($message, array $parameters = []); + + /** + * @return string + * @since 9.2.0 + */ + public function getRichMessage(); + + /** + * @return array[] + * @since 9.2.0 + */ + public function getRichMessageParameters(); + /** * Set the object of the activity * @@ -208,6 +278,20 @@ interface IEvent { */ public function getLink(); + /** + * @param string $icon + * @return $this + * @throws \InvalidArgumentException if the icon is invalid + * @since 9.2.0 + */ + public function setIcon($icon); + + /** + * @return string + * @since 9.2.0 + */ + public function getIcon(); + /** * @return bool * @since 9.2.0