diff options
Diffstat (limited to 'lib/private/Notification/Notification.php')
-rw-r--r-- | lib/private/Notification/Notification.php | 80 |
1 files changed, 42 insertions, 38 deletions
diff --git a/lib/private/Notification/Notification.php b/lib/private/Notification/Notification.php index 19c836c2044..fcce7fd0020 100644 --- a/lib/private/Notification/Notification.php +++ b/lib/private/Notification/Notification.php @@ -12,9 +12,18 @@ use OCP\Notification\IAction; use OCP\Notification\INotification; use OCP\Notification\InvalidValueException; use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IRichTextFormatter; use OCP\RichObjectStrings\IValidator; class Notification implements INotification { + /** + * A very small and privileged list of apps that are allowed to push during DND. + */ + public const PRIORITY_NOTIFICATION_APPS = [ + 'spreed', + 'twofactor_nextcloud_notification', + ]; + protected string $app = ''; protected string $user = ''; protected \DateTime $dateTime; @@ -32,6 +41,7 @@ class Notification implements INotification { protected array $messageRichParameters = []; protected string $link = ''; protected string $icon = ''; + protected bool $priorityNotification = false; protected array $actions = []; protected array $actionsParsed = []; protected bool $hasPrimaryAction = false; @@ -39,6 +49,7 @@ class Notification implements INotification { public function __construct( protected IValidator $richValidator, + protected IRichTextFormatter $richTextFormatter, ) { $this->dateTime = new \DateTime(); $this->dateTime->setTimestamp(0); @@ -187,7 +198,7 @@ class Notification implements INotification { if ($this->subjectParsed === '') { try { - $this->subjectParsed = $this->richToParsed($subject, $parameters); + $this->subjectParsed = $this->richTextFormatter->richToParsed($subject, $parameters); } catch (\InvalidArgumentException $e) { throw new InvalidValueException('richSubjectParameters', $e); } @@ -197,30 +208,6 @@ class Notification implements INotification { } /** - * @throws \InvalidArgumentException if a parameter has no name or no type - */ - private function richToParsed(string $message, array $parameters): string { - $placeholders = []; - $replacements = []; - foreach ($parameters as $placeholder => $parameter) { - $placeholders[] = '{' . $placeholder . '}'; - foreach (['name','type'] as $requiredField) { - if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) { - throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing"); - } - } - if ($parameter['type'] === 'user') { - $replacements[] = '@' . $parameter['name']; - } elseif ($parameter['type'] === 'file') { - $replacements[] = $parameter['path'] ?? $parameter['name']; - } else { - $replacements[] = $parameter['name']; - } - } - return str_replace($placeholders, $replacements, $message); - } - - /** * {@inheritDoc} */ public function getRichSubject(): string { @@ -293,7 +280,7 @@ class Notification implements INotification { if ($this->messageParsed === '') { try { - $this->messageParsed = $this->richToParsed($message, $parameters); + $this->messageParsed = $this->richTextFormatter->richToParsed($message, $parameters); } catch (\InvalidArgumentException $e) { throw new InvalidValueException('richMessageParameters', $e); } @@ -355,6 +342,25 @@ class Notification implements INotification { /** * {@inheritDoc} */ + public function setPriorityNotification(bool $priorityNotification): INotification { + if ($priorityNotification && !in_array($this->getApp(), self::PRIORITY_NOTIFICATION_APPS, true)) { + throw new InvalidValueException('priorityNotification'); + } + + $this->priorityNotification = $priorityNotification; + return $this; + } + + /** + * {@inheritDoc} + */ + public function isPriorityNotification(): bool { + return $this->priorityNotification; + } + + /** + * {@inheritDoc} + */ public function createAction(): IAction { return new Action(); } @@ -423,8 +429,7 @@ class Notification implements INotification { public function isValid(): bool { return $this->isValidCommon() - && - $this->getSubject() !== '' + && $this->getSubject() !== '' ; } @@ -450,22 +455,21 @@ class Notification implements INotification { return $this->isValidCommon() - && - $this->getParsedSubject() !== '' + && $this->getParsedSubject() !== '' ; } protected function isValidCommon(): bool { + if ($this->isPriorityNotification() && !in_array($this->getApp(), self::PRIORITY_NOTIFICATION_APPS, true)) { + return false; + } + return $this->getApp() !== '' - && - $this->getUser() !== '' - && - $this->getDateTime()->getTimestamp() !== 0 - && - $this->getObjectType() !== '' - && - $this->getObjectId() !== '' + && $this->getUser() !== '' + && $this->getDateTime()->getTimestamp() !== 0 + && $this->getObjectType() !== '' + && $this->getObjectId() !== '' ; } } |