diff --git a/lib/BackgroundJobs/AdminNotification.php b/lib/BackgroundJobs/AdminNotification.php index 743af07..de2f74f 100644 --- a/lib/BackgroundJobs/AdminNotification.php +++ b/lib/BackgroundJobs/AdminNotification.php @@ -1,4 +1,6 @@ manager = $manager; - $this->groupManager = $groupManager; - $this->url = $url; } protected function run($argument): void { $notification = $this->manager->createNotification(); $notification->setApp('survey_client') - ->setDateTime(new \DateTime()) + ->setDateTime($this->time->getDateTime()) ->setSubject('updated') ->setObject('dummy', '23'); - $enableLink = $this->url->linkToOCSRouteAbsolute('survey_client.Endpoint.enableMonthly'); - $enableAction = $notification->createAction(); - $enableAction->setLabel('enable') - ->setLink($enableLink, 'POST') - ->setPrimary(true); - $notification->addAction($enableAction); - - $disableLink = $this->url->linkToOCSRouteAbsolute('survey_client.Endpoint.disableMonthly'); - $disableAction = $notification->createAction(); - $disableAction->setLabel('disable') - ->setLink($disableLink, 'DELETE') - ->setPrimary(false); - $notification->addAction($disableAction); - $adminGroup = $this->groupManager->get('admin'); foreach ($adminGroup->getUsers() as $admin) { $notification->setUser($admin->getUID()); diff --git a/lib/Notifier.php b/lib/Notifier.php index 1d24ab9..ab6907f 100644 --- a/lib/Notifier.php +++ b/lib/Notifier.php @@ -1,4 +1,7 @@ l10nFactory = $l10nFactory; - $this->url = $url; + public function __construct( + protected IFactory $l10nFactory, + protected IURLGenerator $url, + ) { } /** @@ -42,7 +34,7 @@ public function getID(): string { } /** - * Human readable name describing the notifier + * Human-readable name describing the notifier * * @return string * @since 17.0.0 @@ -55,12 +47,12 @@ public function getName(): string { * @param INotification $notification * @param string $languageCode The code of the language that should be used to prepare the notification * @return INotification - * @throws \InvalidArgumentException When the notification was not prepared by a notifier + * @throws UnknownNotificationException When the notification was not prepared by a notifier */ public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'survey_client') { // Not my app => throw - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } // Read the language from the notification @@ -68,19 +60,22 @@ public function prepare(INotification $notification, string $languageCode): INot $notification->setParsedSubject($l->t('Help improve Nextcloud')) ->setParsedMessage($l->t('Do you want to help us to improve Nextcloud by providing some anonymized data about your setup and usage? You can disable it at any time in the admin settings again.')) - ->setLink($this->url->linkToRoute('settings.AdminSettings.index', ['section' => 'survey_client'])) - ->setIcon($this->url->imagePath('survey_client', 'app-dark.svg')); + ->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'survey_client'])) + ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('survey_client', 'app-dark.svg'))); - foreach ($notification->getActions() as $action) { - if ($action->getLabel() === 'disable') { - $action->setParsedLabel($l->t('Not now')) - ->setLink($this->url->getAbsoluteURL('ocs/v2.php/apps/survey_client/api/v1/monthly'), 'DELETE'); - } elseif ($action->getLabel() === 'enable') { - $action->setParsedLabel($l->t('Send usage')) - ->setLink($this->url->getAbsoluteURL('ocs/v2.php/apps/survey_client/api/v1/monthly'), 'POST'); - } - $notification->addParsedAction($action); - } + $enableAction = $notification->createAction(); + $enableAction->setLabel('enable') + ->setParsedLabel($l->t('Send usage')) + ->setLink($this->url->linkToOCSRouteAbsolute('survey_client.Endpoint.enableMonthly'), 'POST') + ->setPrimary(true); + $notification->addParsedAction($enableAction); + + $disableAction = $notification->createAction(); + $disableAction->setLabel('disable') + ->setParsedLabel($l->t('Not now')) + ->setLink($this->url->linkToOCSRouteAbsolute('survey_client.Endpoint.disableMonthly'), 'DELETE') + ->setPrimary(false); + $notification->addParsedAction($disableAction); return $notification; } diff --git a/psalm.xml b/psalm.xml index 21f3ae2..2d822aa 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,10 @@