Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/BackgroundJobs/MonthlyReport.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand All @@ -9,28 +11,35 @@

use OCA\Survey_Client\Collector;
use OCP\AppFramework\Http;
use OCP\AppFramework\Services\IAppConfig;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\TimedJob;
use Psr\Log\LoggerInterface;

class MonthlyReport extends TimedJob {
protected Collector $collector;
protected LoggerInterface $logger;

public function __construct(ITimeFactory $time,
Collector $collector,
LoggerInterface $logger) {
public function __construct(
ITimeFactory $time,
protected Collector $collector,
protected LoggerInterface $logger,
protected IJobList $jobList,
protected IAppConfig $appConfig,
) {
parent::__construct($time);
$this->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
$this->setTimeSensitivity(IJob::TIME_SENSITIVE);
}

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) {
Expand Down
41 changes: 15 additions & 26 deletions lib/Controller/EndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,34 @@

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;
}

/**
* @return DataResponse
*/
public function enableMonthly(): DataResponse {
$this->jobList->add(MonthlyReport::class);
$this->appConfig->deleteAppValue('never_again');

$notification = $this->manager->createNotification();
$notification->setApp('survey_client');
Expand All @@ -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');
Expand Down
14 changes: 9 additions & 5 deletions lib/Migration/SendAdminNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@

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 {
return 'Send an admin notification if monthly report is disabled';
}

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);
}
Expand Down
15 changes: 15 additions & 0 deletions lib/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +22,7 @@ class Notifier implements INotifier {
public function __construct(
protected IFactory $l10nFactory,
protected IURLGenerator $url,
protected IAppConfig $appConfig,
) {
}

Expand Down Expand Up @@ -48,13 +51,18 @@ 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') {
// Not my app => throw
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);

Expand All @@ -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;
}
}
Loading