diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php index 4391836d..ed162ea3 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -24,6 +24,8 @@ namespace OCA\NextcloudAnnouncements\Notification; +use OCP\IConfig; +use OCP\IGroupManager; use OCP\IURLGenerator; use OCP\L10N\IFactory; use OCP\Notification\INotification; @@ -35,22 +37,25 @@ class Notifier implements INotifier { /** @var string */ protected $appName; - /** @var IFactory */ protected $l10nFactory; - /** @var IURLGenerator */ protected $url; + /** @var IConfig */ + protected $config; + /** @var IGroupManager */ + protected $groupManager; - /** - * @param string $appName - * @param IFactory $l10nFactory - * @param IURLGenerator $url - */ - public function __construct($appName, IFactory $l10nFactory, IURLGenerator $url) { + public function __construct(string $appName, + IFactory $l10nFactory, + IURLGenerator $url, + IConfig $config, + IGroupManager $groupManager) { $this->appName = $appName; $this->l10nFactory = $l10nFactory; $this->url = $url; + $this->config = $config; + $this->groupManager = $groupManager; } /** @@ -71,10 +76,26 @@ public function prepare(INotification $notification, $languageCode) { switch ($notification->getSubject()) { case self::SUBJECT: $parameters = $notification->getSubjectParameters(); + $message = $parameters[0]; $notification->setParsedSubject($l->t('Nextcloud announcement')) - ->setParsedMessage($parameters[0]) ->setIcon($this->url->getAbsoluteURL($this->url->imagePath($this->appName, 'app-dark.svg'))); + $isAdmin = $this->groupManager->isAdmin($notification->getUser()); + if ($isAdmin) { + $groups = $this->config->getAppValue($this->appName, 'notification_groups', ''); + if ($groups === '') { + $action = $notification->createAction(); + $action->setParsedLabel($l->t('Disable announcements')) + ->setLink($this->url->linkToOCSRouteAbsolute('provisioning_api.AppsController.disable', ['app' => 'nextcloud_announcements']), 'DELETE') + ->setPrimary(false); + $notification->addParsedAction($action); + + $message .= "\n\n" . $l->t('(These announcements are only shown to administrators)'); + } + } + + $notification->setParsedMessage($message); + return $notification; default: