Skip to content

Commit

Permalink
fix(updatenotification): spread the use of new iappconfig
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Mar 12, 2024
1 parent 007731d commit c0f0d85
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 79 deletions.
35 changes: 12 additions & 23 deletions apps/settings/lib/Settings/Admin/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
Expand All @@ -39,28 +40,16 @@
class Server implements IDelegatedSettings {
use TProfileHelper;

private IDBConnection $connection;
private IInitialState $initialStateService;
private ProfileManager $profileManager;
private ITimeFactory $timeFactory;
private IConfig $config;
private IL10N $l;
private IURLGenerator $urlGenerator;

public function __construct(IDBConnection $connection,
IInitialState $initialStateService,
ProfileManager $profileManager,
ITimeFactory $timeFactory,
IURLGenerator $urlGenerator,
IConfig $config,
IL10N $l) {
$this->connection = $connection;
$this->initialStateService = $initialStateService;
$this->profileManager = $profileManager;
$this->timeFactory = $timeFactory;
$this->config = $config;
$this->l = $l;
$this->urlGenerator = $urlGenerator;
public function __construct(
private IDBConnection $connection,
private IInitialState $initialStateService,
private ProfileManager $profileManager,
private ITimeFactory $timeFactory,
private IURLGenerator $urlGenerator,
private IConfig $config,
private IAppConfig $appConfig,
private IL10N $l,
) {
}

/**
Expand All @@ -69,7 +58,7 @@ public function __construct(IDBConnection $connection,
public function getForm() {
// Background jobs
$this->initialStateService->provideInitialState('backgroundJobsMode', $this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax'));
$this->initialStateService->provideInitialState('lastCron', (int)$this->config->getAppValue('core', 'lastcron', '0'));
$this->initialStateService->provideInitialState('lastCron', $this->appConfig->getValueInt('core', 'lastcron', 0));
$this->initialStateService->provideInitialState('cronMaxAge', $this->cronMaxAge());
$this->initialStateService->provideInitialState('cronErrors', $this->config->getAppValue('core', 'cronErrors'));
$this->initialStateService->provideInitialState('cliBasedCronPossible', function_exists('posix_getpwuid'));
Expand Down
4 changes: 3 additions & 1 deletion apps/settings/lib/SetupChecks/CronInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OCA\Settings\SetupChecks;

use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IL10N;
Expand All @@ -37,6 +38,7 @@ class CronInfo implements ISetupCheck {
public function __construct(
private IL10N $l10n,
private IConfig $config,
private IAppConfig $appConfig,
private IURLGenerator $urlGenerator,
private IDateTimeFormatter $dateTimeFormatter,
) {
Expand All @@ -51,7 +53,7 @@ public function getName(): string {
}

public function run(): SetupResult {
$lastCronRun = (int)$this->config->getAppValue('core', 'lastcron', '0');
$lastCronRun = (int)$this->appConfig->getValueInt('core', 'lastcron', 0);

Check failure

Code scanning / Psalm

RedundantCast Error

Redundant cast to int

Check failure on line 56 in apps/settings/lib/SetupChecks/CronInfo.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCast

apps/settings/lib/SetupChecks/CronInfo.php:56:18: RedundantCast: Redundant cast to int (see https://psalm.dev/262)
$relativeTime = $this->dateTimeFormatter->formatTimeSpan($lastCronRun);

if ((time() - $lastCronRun) > 3600) {
Expand Down
41 changes: 12 additions & 29 deletions apps/updatenotification/lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\UpdateNotification\UpdateChecker;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IGroupManager;
Expand All @@ -45,40 +46,22 @@
use Psr\Log\LoggerInterface;

class Admin implements ISettings {
private IConfig $config;
private UpdateChecker $updateChecker;
private IGroupManager $groupManager;
private IDateTimeFormatter $dateTimeFormatter;
private IFactory $l10nFactory;
private IRegistry $subscriptionRegistry;
private IUserManager $userManager;
private LoggerInterface $logger;
private IInitialState $initialState;

public function __construct(
IConfig $config,
UpdateChecker $updateChecker,
IGroupManager $groupManager,
IDateTimeFormatter $dateTimeFormatter,
IFactory $l10nFactory,
IRegistry $subscriptionRegistry,
IUserManager $userManager,
LoggerInterface $logger,
IInitialState $initialState
private IConfig $config,
private IAppConfig $appConfig,
private UpdateChecker $updateChecker,
private IGroupManager $groupManager,
private IDateTimeFormatter $dateTimeFormatter,
private IFactory $l10nFactory,
private IRegistry $subscriptionRegistry,
private IUserManager $userManager,
private LoggerInterface $logger,
private IInitialState $initialState
) {
$this->config = $config;
$this->updateChecker = $updateChecker;
$this->groupManager = $groupManager;
$this->dateTimeFormatter = $dateTimeFormatter;
$this->l10nFactory = $l10nFactory;
$this->subscriptionRegistry = $subscriptionRegistry;
$this->userManager = $userManager;
$this->logger = $logger;
$this->initialState = $initialState;
}

public function getForm(): TemplateResponse {
$lastUpdateCheckTimestamp = (int)$this->config->getAppValue('core', 'lastupdatedat');
$lastUpdateCheckTimestamp = $this->appConfig->getValueInt('core', 'lastupdatedat');
$lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);

$channels = [
Expand Down
5 changes: 5 additions & 0 deletions apps/updatenotification/tests/Settings/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\UpdateNotification\UpdateChecker;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IGroup;
Expand All @@ -55,6 +56,8 @@ class AdminTest extends TestCase {
private $admin;
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $config;
/** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
private $appConfig;
/** @var UpdateChecker|\PHPUnit\Framework\MockObject\MockObject */
private $updateChecker;
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
Expand All @@ -74,6 +77,7 @@ protected function setUp(): void {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->updateChecker = $this->createMock(UpdateChecker::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
Expand All @@ -85,6 +89,7 @@ protected function setUp(): void {

$this->admin = new Admin(
$this->config,
$this->appConfig,
$this->updateChecker,
$this->groupManager,
$this->dateTimeFormatter,
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\Defaults;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
Expand Down Expand Up @@ -382,7 +383,8 @@ public function install(array $options, ?IOutput $output = null): array {

$config = Server::get(IConfig::class);
$config->setAppValue('core', 'installedat', (string)microtime(true));
$config->setAppValue('core', 'lastupdatedat', (string)microtime(true));
$appConfig = Server::get(IAppConfig::class);
$appConfig->setValueInt('core', 'lastupdatedat', time());

$vendorData = $this->getVendorData();
$config->setAppValue('core', 'vendor', $vendorData['vendor']);
Expand Down
32 changes: 10 additions & 22 deletions lib/private/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\HintException;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\ILogger;
use OCP\Util;
Expand All @@ -74,34 +75,21 @@
* - failure(string $message)
*/
class Updater extends BasicEmitter {
/** @var LoggerInterface */
private $log;

/** @var IConfig */
private $config;

/** @var Checker */
private $checker;

/** @var Installer */
private $installer;

private $logLevelNames = [
private array $logLevelNames = [
0 => 'Debug',
1 => 'Info',
2 => 'Warning',
3 => 'Error',
4 => 'Fatal',
];

public function __construct(IConfig $config,
Checker $checker,
?LoggerInterface $log,
Installer $installer) {
$this->log = $log;
$this->config = $config;
$this->checker = $checker;
$this->installer = $installer;
public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private Checker $checker,
private ?LoggerInterface $log,
private Installer $installer
) {
}

/**
Expand Down Expand Up @@ -303,7 +291,7 @@ private function doUpgrade(string $currentVersion, string $installedVersion): vo
$repair->run();

//Invalidate update feed
$this->config->setAppValue('core', 'lastupdatedat', '0');
$this->appConfig->setValueInt('core', 'lastupdatedat', 0);

// Check for code integrity if not disabled
if (\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) {
Expand Down
8 changes: 5 additions & 3 deletions lib/private/Updater/VersionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OC\Updater;

use OCP\Http\Client\IClientService;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\Support\Subscription\IRegistry;
Expand All @@ -37,6 +38,7 @@ class VersionCheck {
public function __construct(
private IClientService $clientService,
private IConfig $config,
private IAppConfig $appConfig,
private IUserManager $userManager,
private IRegistry $registry,
private LoggerInterface $logger,
Expand All @@ -56,21 +58,21 @@ public function check() {
}

// Look up the cache - it is invalidated all 30 minutes
if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) {
if (($this->appConfig->getValueInt('core', 'lastupdatedat') + 1800) > time()) {
return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true);
}

$updaterUrl = $this->config->getSystemValueString('updater.server.url', 'https://updates.nextcloud.com/updater_server/');

$this->config->setAppValue('core', 'lastupdatedat', (string)time());
$this->appConfig->setValueInt('core', 'lastupdatedat', time());

if ($this->config->getAppValue('core', 'installedat', '') === '') {
$this->config->setAppValue('core', 'installedat', (string)microtime(true));
}

$version = Util::getVersion();
$version['installed'] = $this->config->getAppValue('core', 'installedat');
$version['updated'] = $this->config->getAppValue('core', 'lastupdatedat');
$version['updated'] = $this->appConfig->getValueInt('core', 'lastupdatedat');
$version['updatechannel'] = \OC_Util::getChannel();
$version['edition'] = '';
$version['build'] = \OC_Util::getBuild();
Expand Down

0 comments on commit c0f0d85

Please sign in to comment.