aboutsummaryrefslogtreecommitdiffstats
path: root/apps/updatenotification/lib
diff options
context:
space:
mode:
authorFerdinand Thiessen <opensource@fthiessen.de>2024-03-02 21:04:56 +0100
committerJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2024-03-07 22:40:29 +0100
commitd9d3448e237abbf6ba676cacb985ef43cf6d4b41 (patch)
tree8f912c03bc5aea0ba9377a0aa1d35dc1b9179014 /apps/updatenotification/lib
parent8a6ac51583094ebd13f8858726cb94d3ecc37668 (diff)
downloadnextcloud-server-d9d3448e237abbf6ba676cacb985ef43cf6d4b41.tar.gz
nextcloud-server-d9d3448e237abbf6ba676cacb985ef43cf6d4b41.zip
fix(updatenotification): Replace deprecated code and move background jobs into `BackgroundJobs` subfolder
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'apps/updatenotification/lib')
-rw-r--r--apps/updatenotification/lib/AppInfo/Application.php10
-rw-r--r--apps/updatenotification/lib/BackgroundJob/ResetToken.php (renamed from apps/updatenotification/lib/ResetTokenBackgroundJob.php)33
-rw-r--r--apps/updatenotification/lib/BackgroundJob/UpdateAvailableNotifications.php (renamed from apps/updatenotification/lib/Notification/BackgroundJob.php)19
-rw-r--r--apps/updatenotification/lib/Controller/AdminController.php49
4 files changed, 48 insertions, 63 deletions
diff --git a/apps/updatenotification/lib/AppInfo/Application.php b/apps/updatenotification/lib/AppInfo/Application.php
index adc16bcb54b..1aaf3be6e7c 100644
--- a/apps/updatenotification/lib/AppInfo/Application.php
+++ b/apps/updatenotification/lib/AppInfo/Application.php
@@ -36,13 +36,13 @@ use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
-use OCP\AppFramework\IAppContainer;
-use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Util;
+use Psr\Container\ContainerExceptionInterface;
+use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
class Application extends App implements IBootstrap {
@@ -59,7 +59,7 @@ class Application extends App implements IBootstrap {
IUserSession $userSession,
IAppManager $appManager,
IGroupManager $groupManager,
- IAppContainer $appContainer,
+ ContainerInterface $container,
LoggerInterface $logger) {
if ($config->getSystemValue('updatechecker', true) !== true) {
// Updater check is disabled
@@ -75,8 +75,8 @@ class Application extends App implements IBootstrap {
if (!$appManager->isEnabledForUser('notifications') &&
$groupManager->isAdmin($user->getUID())) {
try {
- $updateChecker = $appContainer->get(UpdateChecker::class);
- } catch (QueryException $e) {
+ $updateChecker = $container->get(UpdateChecker::class);
+ } catch (ContainerExceptionInterface $e) {
$logger->error($e->getMessage(), ['exception' => $e]);
return;
}
diff --git a/apps/updatenotification/lib/ResetTokenBackgroundJob.php b/apps/updatenotification/lib/BackgroundJob/ResetToken.php
index 723922157b3..856e179814b 100644
--- a/apps/updatenotification/lib/ResetTokenBackgroundJob.php
+++ b/apps/updatenotification/lib/BackgroundJob/ResetToken.php
@@ -8,6 +8,7 @@ declare(strict_types=1);
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Lukas Reschke <lukas@statuscode.ch>
+ * @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @license AGPL-3.0
*
@@ -24,43 +25,43 @@ declare(strict_types=1);
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
-namespace OCA\UpdateNotification;
+namespace OCA\UpdateNotification\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
+use OCP\IAppConfig;
use OCP\IConfig;
/**
- * Class ResetTokenBackgroundJob deletes any configured token all 24 hours for
- *
- *
- * @package OCA\UpdateNotification
+ * Deletes the updater secret after if it is older than 48h
*/
-class ResetTokenBackgroundJob extends TimedJob {
- /** @var IConfig */
- private $config;
- /** @var ITimeFactory */
- private $timeFactory;
+class ResetToken extends TimedJob {
/**
* @param IConfig $config
* @param ITimeFactory $timeFactory
*/
- public function __construct(IConfig $config,
- ITimeFactory $timeFactory) {
- parent::__construct($timeFactory);
+ public function __construct(
+ ITimeFactory $time,
+ private IConfig $config,
+ private IAppConfig $appConfig,
+ ) {
+ parent::__construct($time);
// Run all 10 minutes
parent::setInterval(60 * 10);
- $this->config = $config;
- $this->timeFactory = $timeFactory;
}
/**
* @param $argument
*/
protected function run($argument) {
+ if ($this->config->getSystemValueBool('config_is_read_only') !== false) {
+ return;
+ }
+
+ $secretCreated = $this->appConfig->getValueInt('core', 'updater.secret.created', $this->time->getTime());
// Delete old tokens after 2 days
- if ($this->config->getSystemValueBool('config_is_read_only') === false && $this->timeFactory->getTime() - (int) $this->config->getAppValue('core', 'updater.secret.created', (string) $this->timeFactory->getTime()) >= 172800) {
+ if ($secretCreated >= 172800) {
$this->config->deleteSystemValue('updater.secret');
}
}
diff --git a/apps/updatenotification/lib/Notification/BackgroundJob.php b/apps/updatenotification/lib/BackgroundJob/UpdateAvailableNotifications.php
index 08142d5b00b..6afe1ec5e37 100644
--- a/apps/updatenotification/lib/Notification/BackgroundJob.php
+++ b/apps/updatenotification/lib/BackgroundJob/UpdateAvailableNotifications.php
@@ -8,6 +8,7 @@ declare(strict_types=1);
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Morris Jobke <hey@morrisjobke.de>
+ * @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @license AGPL-3.0
*
@@ -24,11 +25,12 @@ declare(strict_types=1);
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
-namespace OCA\UpdateNotification\Notification;
+namespace OCA\UpdateNotification\BackgroundJob;
use OC\Installer;
use OC\Updater\VersionCheck;
use OCP\App\IAppManager;
+use OCP\AppFramework\Services\IAppConfig;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\IConfig;
@@ -36,7 +38,7 @@ use OCP\IGroup;
use OCP\IGroupManager;
use OCP\Notification\IManager;
-class BackgroundJob extends TimedJob {
+class UpdateAvailableNotifications extends TimedJob {
protected $connectionNotifications = [3, 7, 14, 30];
/** @var string[] */
@@ -45,6 +47,7 @@ class BackgroundJob extends TimedJob {
public function __construct(
ITimeFactory $timeFactory,
protected IConfig $config,
+ protected IAppConfig $appConfig,
protected IManager $notificationManager,
protected IGroupManager $groupManager,
protected IAppManager $appManager,
@@ -87,14 +90,14 @@ class BackgroundJob extends TimedJob {
$status = $this->versionCheck->check();
if ($status === false) {
- $errors = 1 + (int) $this->config->getAppValue('updatenotification', 'update_check_errors', '0');
- $this->config->setAppValue('updatenotification', 'update_check_errors', (string) $errors);
+ $errors = 1 + $this->appConfig->getAppValueInt('update_check_errors', 0);
+ $this->appConfig->setAppValueInt('update_check_errors', $errors);
if (\in_array($errors, $this->connectionNotifications, true)) {
$this->sendErrorNotifications($errors);
}
} elseif (\is_array($status)) {
- $this->config->setAppValue('updatenotification', 'update_check_errors', '0');
+ $this->appConfig->setAppValueInt('update_check_errors', 0);
$this->clearErrorNotifications();
if (isset($status['version'])) {
@@ -162,7 +165,7 @@ class BackgroundJob extends TimedJob {
* @param string $visibleVersion
*/
protected function createNotifications($app, $version, $visibleVersion = '') {
- $lastNotification = $this->config->getAppValue('updatenotification', $app, false);
+ $lastNotification = $this->appConfig->getAppValueString($app, '');
if ($lastNotification === $version) {
// We already notified about this update
return;
@@ -193,7 +196,7 @@ class BackgroundJob extends TimedJob {
return;
}
- $this->config->setAppValue('updatenotification', $app, $version);
+ $this->appConfig->setAppValueString($app, $version);
}
/**
@@ -204,7 +207,7 @@ class BackgroundJob extends TimedJob {
return $this->users;
}
- $notifyGroups = (array) json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
+ $notifyGroups = $this->appConfig->getAppValueArray('notify_groups', ['admin']);
$this->users = [];
foreach ($notifyGroups as $group) {
$groupToNotify = $this->groupManager->get($group);
diff --git a/apps/updatenotification/lib/Controller/AdminController.php b/apps/updatenotification/lib/Controller/AdminController.php
index e5a8aa45838..ae5893a00ab 100644
--- a/apps/updatenotification/lib/Controller/AdminController.php
+++ b/apps/updatenotification/lib/Controller/AdminController.php
@@ -9,6 +9,7 @@ declare(strict_types=1);
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Vincent Petry <vincent@nextcloud.com>
+ * @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @license AGPL-3.0
*
@@ -27,12 +28,13 @@ declare(strict_types=1);
*/
namespace OCA\UpdateNotification\Controller;
-use OCA\UpdateNotification\ResetTokenBackgroundJob;
+use OCA\UpdateNotification\BackgroundJob\ResetToken;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
+use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
@@ -40,39 +42,18 @@ use OCP\Security\ISecureRandom;
use OCP\Util;
class AdminController extends Controller {
- /** @var IJobList */
- private $jobList;
- /** @var ISecureRandom */
- private $secureRandom;
- /** @var IConfig */
- private $config;
- /** @var ITimeFactory */
- private $timeFactory;
- /** @var IL10N */
- private $l10n;
- /**
- * @param string $appName
- * @param IRequest $request
- * @param IJobList $jobList
- * @param ISecureRandom $secureRandom
- * @param IConfig $config
- * @param ITimeFactory $timeFactory
- * @param IL10N $l10n
- */
- public function __construct($appName,
+ public function __construct(
+ string $appName,
IRequest $request,
- IJobList $jobList,
- ISecureRandom $secureRandom,
- IConfig $config,
- ITimeFactory $timeFactory,
- IL10N $l10n) {
+ private IJobList $jobList,
+ private ISecureRandom $secureRandom,
+ private IConfig $config,
+ private IAppConfig $appConfig,
+ private ITimeFactory $timeFactory,
+ private IL10N $l10n,
+ ) {
parent::__construct($appName, $request);
- $this->jobList = $jobList;
- $this->secureRandom = $secureRandom;
- $this->config = $config;
- $this->timeFactory = $timeFactory;
- $this->l10n = $l10n;
}
private function isUpdaterEnabled() {
@@ -85,7 +66,7 @@ class AdminController extends Controller {
*/
public function setChannel(string $channel): DataResponse {
Util::setChannel($channel);
- $this->config->setAppValue('core', 'lastupdatedat', '0');
+ $this->appConfig->setValueInt('core', 'lastupdatedat', 0);
return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Channel updated')]]);
}
@@ -98,8 +79,8 @@ class AdminController extends Controller {
}
// Create a new job and store the creation date
- $this->jobList->add(ResetTokenBackgroundJob::class);
- $this->config->setAppValue('core', 'updater.secret.created', (string)$this->timeFactory->getTime());
+ $this->jobList->add(ResetToken::class);
+ $this->appConfig->setValueInt('core', 'updater.secret.created', $this->timeFactory->getTime());
// Create a new token
$newToken = $this->secureRandom->generate(64);