diff --git a/lib/BackgroundJobs/MonthlyReport.php b/lib/BackgroundJobs/MonthlyReport.php index c3b760a8..26c42190 100644 --- a/lib/BackgroundJobs/MonthlyReport.php +++ b/lib/BackgroundJobs/MonthlyReport.php @@ -1,4 +1,6 @@ collector = $collector; - $this->logger = $logger; + // Run all 28 days $this->setInterval(28 * 24 * 60 * 60); // keeping time sensitive to not overload the target server at a single specific time of the day @@ -31,6 +35,11 @@ public function __construct(ITimeFactory $time, } protected function run($argument) { + if ($this->appConfig->getAppValueBool('never_again')) { + $this->jobList->remove(self::class); + return; + } + $result = $this->collector->sendReport(); if ($result->getStatus() !== Http::STATUS_OK) { diff --git a/lib/Controller/EndpointController.php b/lib/Controller/EndpointController.php index bcbff727..4ab154d5 100644 --- a/lib/Controller/EndpointController.php +++ b/lib/Controller/EndpointController.php @@ -9,42 +9,26 @@ namespace OCA\Survey_Client\Controller; +use OCA\Survey_Client\BackgroundJobs\AdminNotification; use OCA\Survey_Client\BackgroundJobs\MonthlyReport; use OCA\Survey_Client\Collector; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; +use OCP\AppFramework\Services\IAppConfig; use OCP\BackgroundJob\IJobList; use OCP\IRequest; use OCP\Notification\IManager; class EndpointController extends OCSController { - - /** @var Collector */ - protected $collector; - - /** @var IJobList */ - protected $jobList; - - /** @var IManager */ - protected $manager; - - /** - * @param string $appName - * @param IRequest $request - * @param Collector $collector - * @param IJobList $jobList - * @param IManager $manager - */ - public function __construct(string $appName, + public function __construct( + string $appName, IRequest $request, - Collector $collector, - IJobList $jobList, - IManager $manager) { + protected Collector $collector, + protected IJobList $jobList, + protected IManager $manager, + protected IAppConfig $appConfig, + ) { parent::__construct($appName, $request); - - $this->collector = $collector; - $this->jobList = $jobList; - $this->manager = $manager; } /** @@ -52,6 +36,7 @@ public function __construct(string $appName, */ public function enableMonthly(): DataResponse { $this->jobList->add(MonthlyReport::class); + $this->appConfig->deleteAppValue('never_again'); $notification = $this->manager->createNotification(); $notification->setApp('survey_client'); @@ -63,8 +48,12 @@ public function enableMonthly(): DataResponse { /** * @return DataResponse */ - public function disableMonthly(): DataResponse { + public function disableMonthly(bool $never = false): DataResponse { $this->jobList->remove(MonthlyReport::class); + if ($never) { + $this->jobList->remove(AdminNotification::class); + $this->appConfig->setAppValueBool('never_again', true); + } $notification = $this->manager->createNotification(); $notification->setApp('survey_client'); diff --git a/lib/Migration/SendAdminNotification.php b/lib/Migration/SendAdminNotification.php index 65ae43ff..e104ed68 100644 --- a/lib/Migration/SendAdminNotification.php +++ b/lib/Migration/SendAdminNotification.php @@ -11,16 +11,16 @@ use OCA\Survey_Client\BackgroundJobs\AdminNotification; use OCA\Survey_Client\BackgroundJobs\MonthlyReport; +use OCP\AppFramework\Services\IAppConfig; use OCP\BackgroundJob\IJobList; use OCP\Migration\IOutput; use OCP\Migration\IRepairStep; class SendAdminNotification implements IRepairStep { - /** @var IJobList */ - private $jobList; - - public function __construct(IJobList $jobList) { - $this->jobList = $jobList; + public function __construct( + protected IJobList $jobList, + protected IAppConfig $appConfig, + ) { } public function getName(): string { @@ -28,6 +28,10 @@ public function getName(): string { } public function run(IOutput $output): void { + if ($this->appConfig->getAppValueBool('never_again')) { + return; + } + if (!$this->jobList->has(MonthlyReport::class, null)) { $this->jobList->add(AdminNotification::class); } diff --git a/lib/Notifier.php b/lib/Notifier.php index ab6907f9..c5bf7eef 100644 --- a/lib/Notifier.php +++ b/lib/Notifier.php @@ -10,8 +10,10 @@ namespace OCA\Survey_Client; +use OCP\AppFramework\Services\IAppConfig; use OCP\IURLGenerator; use OCP\L10N\IFactory; +use OCP\Notification\AlreadyProcessedException; use OCP\Notification\INotification; use OCP\Notification\INotifier; use OCP\Notification\UnknownNotificationException; @@ -20,6 +22,7 @@ class Notifier implements INotifier { public function __construct( protected IFactory $l10nFactory, protected IURLGenerator $url, + protected IAppConfig $appConfig, ) { } @@ -48,6 +51,7 @@ public function getName(): string { * @param string $languageCode The code of the language that should be used to prepare the notification * @return INotification * @throws UnknownNotificationException When the notification was not prepared by a notifier + * @throws AlreadyProcessedException When the notification is no longer applicable */ public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'survey_client') { @@ -55,6 +59,10 @@ public function prepare(INotification $notification, string $languageCode): INot throw new UnknownNotificationException(); } + if ($this->appConfig->getAppValueBool('never_again')) { + throw new AlreadyProcessedException(); + } + // Read the language from the notification $l = $this->l10nFactory->get('survey_client', $languageCode); @@ -77,6 +85,13 @@ public function prepare(INotification $notification, string $languageCode): INot ->setPrimary(false); $notification->addParsedAction($disableAction); + $disableAction = $notification->createAction(); + $disableAction->setLabel('never') + ->setParsedLabel($l->t('Never ask again')) + ->setLink($this->url->linkToOCSRouteAbsolute('survey_client.Endpoint.disableMonthly', ['never' => true]), 'DELETE') + ->setPrimary(false); + $notification->addParsedAction($disableAction); + return $notification; } }