From 5193579e4f085fc8837a641522ed02570b9b02da Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 20 Sep 2024 13:15:20 +0200 Subject: [PATCH] feat(prioritynotifications): Allow some apps to mark notifications as priority They will be still send as push during DND. Apps are currently limited to: - twofactor_nextcloud_notification to help with login - spreed which will only set it for pushes in manually picked conversations Signed-off-by: Joas Schilling --- lib/private/Notification/Notification.php | 32 +++++++++++++++++++++++ lib/public/Notification/INotification.php | 12 +++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/private/Notification/Notification.php b/lib/private/Notification/Notification.php index 1c624b50d446e..f8f1e2478547c 100644 --- a/lib/private/Notification/Notification.php +++ b/lib/private/Notification/Notification.php @@ -16,6 +16,14 @@ 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; @@ -33,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; @@ -330,6 +339,25 @@ public function getIcon(): string { return $this->icon; } + /** + * {@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} */ @@ -434,6 +462,10 @@ public function isValidParsed(): bool { } protected function isValidCommon(): bool { + if ($this->isPriorityNotification() && !in_array($this->getApp(), self::PRIORITY_NOTIFICATION_APPS, true)) { + return false; + } + return $this->getApp() !== '' && diff --git a/lib/public/Notification/INotification.php b/lib/public/Notification/INotification.php index 4a35ee5908779..6083cca4f9a85 100644 --- a/lib/public/Notification/INotification.php +++ b/lib/public/Notification/INotification.php @@ -263,6 +263,18 @@ public function setIcon(string $icon): INotification; */ public function getIcon(): string; + /** + * @return $this + * @throws InvalidValueException if the app is not allowed to send priority notifications + * @since 31.0.0 + */ + public function setPriorityNotification(bool $priorityNotification): INotification; + + /** + * @since 31.0.0 + */ + public function isPriorityNotification(): bool; + /** * @return IAction * @since 9.0.0