mirror of
https://github.com/nextcloud/server.git
synced 2026-06-08 16:26:59 -04:00
fix(updatenotification): Replace deprecated code and move background jobs into BackgroundJobs subfolder
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
parent
8a6ac51583
commit
d9d3448e23
5 changed files with 49 additions and 64 deletions
|
|
@ -16,7 +16,7 @@
|
|||
</dependencies>
|
||||
|
||||
<background-jobs>
|
||||
<job>OCA\UpdateNotification\Notification\BackgroundJob</job>
|
||||
<job>OCA\UpdateNotification\BackgroundJob\UpdateAvailableNotifications</job>
|
||||
</background-jobs>
|
||||
|
||||
<settings>
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue