Skip to content

Commit

Permalink
feat(prioritynotifications): Allow some apps to mark notifications as…
Browse files Browse the repository at this point in the history
… 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 <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Sep 23, 2024
1 parent 4ba3d4a commit 5193579
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/private/Notification/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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() !== ''
&&
Expand Down
12 changes: 12 additions & 0 deletions lib/public/Notification/INotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5193579

Please sign in to comment.