aboutsummaryrefslogtreecommitdiffstats
path: root/apps/updatenotification/lib
diff options
context:
space:
mode:
Diffstat (limited to 'apps/updatenotification/lib')
-rw-r--r--apps/updatenotification/lib/AppInfo/Application.php11
-rw-r--r--apps/updatenotification/lib/BackgroundJob/AppUpdatedNotifications.php2
-rw-r--r--apps/updatenotification/lib/BackgroundJob/UpdateAvailableNotifications.php61
-rw-r--r--apps/updatenotification/lib/Command/Check.php28
-rw-r--r--apps/updatenotification/lib/Controller/APIController.php39
-rw-r--r--apps/updatenotification/lib/Controller/AdminController.php5
-rw-r--r--apps/updatenotification/lib/Controller/ChangelogController.php9
-rw-r--r--apps/updatenotification/lib/Listener/BeforeTemplateRenderedEventListener.php3
-rw-r--r--apps/updatenotification/lib/Manager.php8
-rw-r--r--apps/updatenotification/lib/Migration/Version011901Date20240305120000.php56
-rw-r--r--apps/updatenotification/lib/Notification/Notifier.php76
-rw-r--r--apps/updatenotification/lib/Settings/Admin.php43
-rw-r--r--apps/updatenotification/lib/UpdateChecker.php33
13 files changed, 145 insertions, 229 deletions
diff --git a/apps/updatenotification/lib/AppInfo/Application.php b/apps/updatenotification/lib/AppInfo/Application.php
index 6b6baafae44..2a1678da5db 100644
--- a/apps/updatenotification/lib/AppInfo/Application.php
+++ b/apps/updatenotification/lib/AppInfo/Application.php
@@ -50,7 +50,8 @@ class Application extends App implements IBootstrap {
IAppManager $appManager,
IGroupManager $groupManager,
ContainerInterface $container,
- LoggerInterface $logger) {
+ LoggerInterface $logger,
+ ): void {
if ($config->getSystemValue('updatechecker', true) !== true) {
// Updater check is disabled
return;
@@ -62,8 +63,8 @@ class Application extends App implements IBootstrap {
return;
}
- if (!$appManager->isEnabledForUser('notifications') &&
- $groupManager->isAdmin($user->getUID())) {
+ if (!$appManager->isEnabledForUser('notifications')
+ && $groupManager->isAdmin($user->getUID())) {
try {
$updateChecker = $container->get(UpdateChecker::class);
} catch (ContainerExceptionInterface $e) {
@@ -72,8 +73,8 @@ class Application extends App implements IBootstrap {
}
if ($updateChecker->getUpdateState() !== []) {
- Util::addScript('updatenotification', 'legacy-notification');
- \OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'populateJavaScriptVariables');
+ Util::addScript(self::APP_NAME, 'update-notification-legacy');
+ $updateChecker->setInitialState();
}
}
});
diff --git a/apps/updatenotification/lib/BackgroundJob/AppUpdatedNotifications.php b/apps/updatenotification/lib/BackgroundJob/AppUpdatedNotifications.php
index 2a501b3d66c..049390546ed 100644
--- a/apps/updatenotification/lib/BackgroundJob/AppUpdatedNotifications.php
+++ b/apps/updatenotification/lib/BackgroundJob/AppUpdatedNotifications.php
@@ -88,7 +88,7 @@ class AppUpdatedNotifications extends QueuedJob {
$isDefer = $this->notificationManager->defer();
// Notify all seen users about the app update
- $this->userManager->callForSeenUsers(function (IUser $user) use ($guestsEnabled, $appId, $notification) {
+ $this->userManager->callForSeenUsers(function (IUser $user) use ($guestsEnabled, $appId, $notification): void {
if (!$guestsEnabled && ($user->getBackendClassName() === '\OCA\Guests\UserBackend')) {
return;
}
diff --git a/apps/updatenotification/lib/BackgroundJob/UpdateAvailableNotifications.php b/apps/updatenotification/lib/BackgroundJob/UpdateAvailableNotifications.php
index dd6497c7e48..8879bb0c223 100644
--- a/apps/updatenotification/lib/BackgroundJob/UpdateAvailableNotifications.php
+++ b/apps/updatenotification/lib/BackgroundJob/UpdateAvailableNotifications.php
@@ -10,6 +10,7 @@ namespace OCA\UpdateNotification\BackgroundJob;
use OC\Installer;
use OC\Updater\VersionCheck;
+use OCA\UpdateNotification\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\AppFramework\Services\IAppConfig;
use OCP\AppFramework\Utility\ITimeFactory;
@@ -18,15 +19,22 @@ use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\Notification\IManager;
+use OCP\ServerVersion;
class UpdateAvailableNotifications extends TimedJob {
- protected $connectionNotifications = [3, 7, 14, 30];
- /** @var string[] */
- protected $users;
+ /**
+ * Numbers of failed updater connection to report error as notification.
+ * @var list<int>
+ */
+ protected const CONNECTION_NOTIFICATIONS = [3, 7, 14, 30];
+
+ /** @var ?string[] */
+ protected $users = null;
public function __construct(
ITimeFactory $timeFactory,
+ protected ServerVersion $serverVersion,
protected IConfig $config,
protected IAppConfig $appConfig,
protected IManager $notificationManager,
@@ -38,6 +46,7 @@ class UpdateAvailableNotifications extends TimedJob {
parent::__construct($timeFactory);
// Run once a day
$this->setInterval(60 * 60 * 24);
+ $this->setTimeSensitivity(self::TIME_INSENSITIVE);
}
protected function run($argument) {
@@ -61,10 +70,15 @@ class UpdateAvailableNotifications extends TimedJob {
}
/**
- * Check for ownCloud update
+ * Check for Nextcloud server update
*/
- protected function checkCoreUpdate() {
- if (\in_array($this->getChannel(), ['daily', 'git'], true)) {
+ protected function checkCoreUpdate(): void {
+ if (!$this->config->getSystemValueBool('updatechecker', true)) {
+ // update checker is disabled so no core update check!
+ return;
+ }
+
+ if (\in_array($this->serverVersion->getChannel(), ['daily', 'git'], true)) {
// "These aren't the update channels you're looking for." - Ben Obi-Wan Kenobi
return;
}
@@ -74,7 +88,7 @@ class UpdateAvailableNotifications extends TimedJob {
$errors = 1 + $this->appConfig->getAppValueInt('update_check_errors', 0);
$this->appConfig->setAppValueInt('update_check_errors', $errors);
- if (\in_array($errors, $this->connectionNotifications, true)) {
+ if (\in_array($errors, self::CONNECTION_NOTIFICATIONS, true)) {
$this->sendErrorNotifications($errors);
}
} elseif (\is_array($status)) {
@@ -91,14 +105,14 @@ class UpdateAvailableNotifications extends TimedJob {
* Send a message to the admin when the update server could not be reached
* @param int $numDays
*/
- protected function sendErrorNotifications($numDays) {
+ protected function sendErrorNotifications($numDays): void {
$this->clearErrorNotifications();
$notification = $this->notificationManager->createNotification();
try {
- $notification->setApp('updatenotification')
+ $notification->setApp(Application::APP_NAME)
->setDateTime(new \DateTime())
- ->setObject('updatenotification', 'error')
+ ->setObject(Application::APP_NAME, 'error')
->setSubject('connection_error', ['days' => $numDays]);
foreach ($this->getUsersToNotify() as $uid) {
@@ -113,12 +127,12 @@ class UpdateAvailableNotifications extends TimedJob {
/**
* Remove error notifications again
*/
- protected function clearErrorNotifications() {
+ protected function clearErrorNotifications(): void {
$notification = $this->notificationManager->createNotification();
try {
- $notification->setApp('updatenotification')
+ $notification->setApp(Application::APP_NAME)
->setSubject('connection_error')
- ->setObject('updatenotification', 'error');
+ ->setObject(Application::APP_NAME, 'error');
} catch (\InvalidArgumentException $e) {
return;
}
@@ -128,8 +142,8 @@ class UpdateAvailableNotifications extends TimedJob {
/**
* Check all installed apps for updates
*/
- protected function checkAppUpdates() {
- $apps = $this->appManager->getInstalledApps();
+ protected function checkAppUpdates(): void {
+ $apps = $this->appManager->getEnabledApps();
foreach ($apps as $app) {
$update = $this->isUpdateAvailable($app);
if ($update !== false) {
@@ -145,7 +159,7 @@ class UpdateAvailableNotifications extends TimedJob {
* @param string $version
* @param string $visibleVersion
*/
- protected function createNotifications($app, $version, $visibleVersion = '') {
+ protected function createNotifications($app, $version, $visibleVersion = ''): void {
$lastNotification = $this->appConfig->getAppValueString($app, '');
if ($lastNotification === $version) {
// We already notified about this update
@@ -159,7 +173,7 @@ class UpdateAvailableNotifications extends TimedJob {
$notification = $this->notificationManager->createNotification();
try {
- $notification->setApp('updatenotification')
+ $notification->setApp(Application::APP_NAME)
->setDateTime(new \DateTime())
->setObject($app, $version);
@@ -209,25 +223,18 @@ class UpdateAvailableNotifications extends TimedJob {
* @param string $app
* @param string $version
*/
- protected function deleteOutdatedNotifications($app, $version) {
+ protected function deleteOutdatedNotifications($app, $version): void {
$notification = $this->notificationManager->createNotification();
try {
- $notification->setApp('updatenotification')
+ $notification->setApp(Application::APP_NAME)
->setObject($app, $version);
- } catch (\InvalidArgumentException $e) {
+ } catch (\InvalidArgumentException) {
return;
}
$this->notificationManager->markProcessed($notification);
}
/**
- * @return string
- */
- protected function getChannel(): string {
- return \OC_Util::getChannel();
- }
-
- /**
* @param string $app
* @return string|false
*/
diff --git a/apps/updatenotification/lib/Command/Check.php b/apps/updatenotification/lib/Command/Check.php
index 75e42904515..d93e4935012 100644
--- a/apps/updatenotification/lib/Command/Check.php
+++ b/apps/updatenotification/lib/Command/Check.php
@@ -17,26 +17,12 @@ use Symfony\Component\Console\Output\OutputInterface;
class Check extends Command {
- /**
- * @var Installer $installer
- */
- private $installer;
-
- /**
- * @var AppManager $appManager
- */
- private $appManager;
-
- /**
- * @var UpdateChecker $updateChecker
- */
- private $updateChecker;
-
- public function __construct(AppManager $appManager, UpdateChecker $updateChecker, Installer $installer) {
+ public function __construct(
+ private AppManager $appManager,
+ private UpdateChecker $updateChecker,
+ private Installer $installer,
+ ) {
parent::__construct();
- $this->installer = $installer;
- $this->appManager = $appManager;
- $this->updateChecker = $updateChecker;
}
protected function configure(): void {
@@ -52,13 +38,13 @@ class Check extends Command {
// Server
$r = $this->updateChecker->getUpdateState();
if (isset($r['updateAvailable']) && $r['updateAvailable']) {
- $output->writeln($r['updateVersionString'] . ' is available. Get more information on how to update at '. $r['updateLink'] . '.');
+ $output->writeln($r['updateVersionString'] . ' is available. Get more information on how to update at ' . $r['updateLink'] . '.');
$updatesAvailableCount += 1;
}
// Apps
- $apps = $this->appManager->getInstalledApps();
+ $apps = $this->appManager->getEnabledApps();
foreach ($apps as $app) {
$update = $this->installer->isUpdateAvailable($app);
if ($update !== false) {
diff --git a/apps/updatenotification/lib/Controller/APIController.php b/apps/updatenotification/lib/Controller/APIController.php
index 6a91b90139b..4360d814dd2 100644
--- a/apps/updatenotification/lib/Controller/APIController.php
+++ b/apps/updatenotification/lib/Controller/APIController.php
@@ -26,8 +26,7 @@ use OCP\L10N\IFactory;
*/
class APIController extends OCSController {
- /** @var string */
- protected $language;
+ protected ?string $language = null;
/**
* List of apps that were in the appstore but are now shipped and don't have
@@ -39,6 +38,9 @@ class APIController extends OCSController {
'bruteforcesettings' => 25,
'suspicious_login' => 25,
'twofactor_totp' => 25,
+ 'files_downloadlimit' => 29,
+ 'twofactor_nextcloud_notification' => 30,
+ 'app_api' => 30,
];
public function __construct(
@@ -59,7 +61,7 @@ class APIController extends OCSController {
*
* @param string $newVersion Server version to check updates for
*
- * @return DataResponse<Http::STATUS_OK, array{missing: UpdateNotificationApp[], available: UpdateNotificationApp[]}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{appstore_disabled: bool, already_on_latest?: bool}, array{}>
+ * @return DataResponse<Http::STATUS_OK, array{missing: list<UpdateNotificationApp>, available: list<UpdateNotificationApp>}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{appstore_disabled: bool, already_on_latest?: bool}, array{}>
*
* 200: Apps returned
* 404: New versions not found
@@ -72,7 +74,7 @@ class APIController extends OCSController {
}
// Get list of installed custom apps
- $installedApps = $this->appManager->getInstalledApps();
+ $installedApps = $this->appManager->getEnabledApps();
$installedApps = array_filter($installedApps, function ($app) {
try {
$this->appManager->getAppPath($app);
@@ -92,7 +94,7 @@ class APIController extends OCSController {
$this->appFetcher->setVersion($newVersion, 'future-apps.json', false);
// Apps available on the app store for that version
- $availableApps = array_map(static function (array $app) {
+ $availableApps = array_map(static function (array $app): string {
return $app['id'];
}, $this->appFetcher->get());
@@ -103,8 +105,6 @@ class APIController extends OCSController {
], Http::STATUS_NOT_FOUND);
}
- $this->language = $this->l10nFactory->getUserLanguage($this->userSession->getUser());
-
// Ignore apps that are deployed from git
$installedApps = array_filter($installedApps, function (string $appId) {
try {
@@ -136,28 +136,43 @@ class APIController extends OCSController {
*/
protected function getAppDetails(string $appId): array {
$app = $this->appManager->getAppInfo($appId, false, $this->language);
- /** @var ?string $name */
- $name = $app['name'];
+ $name = $app['name'] ?? $appId;
return [
'appId' => $appId,
- 'appName' => $name ?? $appId,
+ 'appName' => $name,
];
}
+ protected function getLanguage(): string {
+ if ($this->language === null) {
+ $this->language = $this->l10nFactory->getUserLanguage($this->userSession->getUser());
+ }
+ return $this->language;
+ }
+
/**
* Get changelog entry for an app
*
* @param string $appId App to search changelog entry for
* @param string|null $version The version to search the changelog entry for (defaults to the latest installed)
*
- * @return DataResponse<Http::STATUS_OK, array{appName: string, content: string, version: string}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>
+ * @return DataResponse<Http::STATUS_OK, array{appName: string, content: string, version: string}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{}, array{}>
*
* 200: Changelog entry returned
+ * 400: The `version` parameter is not a valid version format
* 404: No changelog found
*/
public function getAppChangelogEntry(string $appId, ?string $version = null): DataResponse {
$version = $version ?? $this->appManager->getAppVersion($appId);
- $changes = $this->manager->getChangelog($appId, $version);
+ // handle pre-release versions
+ $matches = [];
+ $result = preg_match('/^(\d+\.\d+(\.\d+)?)/', $version, $matches);
+ if ($result === false || $result === 0) {
+ return new DataResponse([], Http::STATUS_BAD_REQUEST);
+ }
+ $shortVersion = $matches[0];
+
+ $changes = $this->manager->getChangelog($appId, $shortVersion);
if ($changes === null) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
diff --git a/apps/updatenotification/lib/Controller/AdminController.php b/apps/updatenotification/lib/Controller/AdminController.php
index 6e7f9935d93..26745948890 100644
--- a/apps/updatenotification/lib/Controller/AdminController.php
+++ b/apps/updatenotification/lib/Controller/AdminController.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\UpdateNotification\Controller;
@@ -36,8 +37,8 @@ class AdminController extends Controller {
parent::__construct($appName, $request);
}
- private function isUpdaterEnabled() {
- return !$this->config->getSystemValue('upgrade.disable-web', false);
+ private function isUpdaterEnabled(): bool {
+ return !$this->config->getSystemValueBool('upgrade.disable-web');
}
/**
diff --git a/apps/updatenotification/lib/Controller/ChangelogController.php b/apps/updatenotification/lib/Controller/ChangelogController.php
index b3ce77b11d9..a274ed3d2b2 100644
--- a/apps/updatenotification/lib/Controller/ChangelogController.php
+++ b/apps/updatenotification/lib/Controller/ChangelogController.php
@@ -11,10 +11,13 @@ namespace OCA\UpdateNotification\Controller;
use OCA\UpdateNotification\Manager;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http\Attribute\NoAdminRequired;
+use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IRequest;
+use OCP\Util;
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class ChangelogController extends Controller {
@@ -33,9 +36,9 @@ class ChangelogController extends Controller {
* This page is only used for clients not support showing the app changelog feature in-app and thus need to show it on a dedicated page.
* @param string $app App to show the changelog for
* @param string|null $version Version entry to show (defaults to latest installed)
- * @NoCSRFRequired
- * @NoAdminRequired
*/
+ #[NoAdminRequired]
+ #[NoCSRFRequired]
public function showChangelog(string $app, ?string $version = null): TemplateResponse {
$version = $version ?? $this->appManager->getAppVersion($app);
$appInfo = $this->appManager->getAppInfo($app) ?? [];
@@ -53,7 +56,7 @@ class ChangelogController extends Controller {
'text' => $changes,
]);
- \OCP\Util::addScript($this->appName, 'view-changelog-page');
+ Util::addScript($this->appName, 'view-changelog-page');
return new TemplateResponse($this->appName, 'empty');
}
}
diff --git a/apps/updatenotification/lib/Listener/BeforeTemplateRenderedEventListener.php b/apps/updatenotification/lib/Listener/BeforeTemplateRenderedEventListener.php
index 0e221fb9bcf..974734a76f4 100644
--- a/apps/updatenotification/lib/Listener/BeforeTemplateRenderedEventListener.php
+++ b/apps/updatenotification/lib/Listener/BeforeTemplateRenderedEventListener.php
@@ -14,6 +14,7 @@ use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IAppConfig;
+use OCP\Util;
use Psr\Log\LoggerInterface;
/** @template-implements IEventListener<BeforeTemplateRenderedEvent> */
@@ -48,6 +49,6 @@ class BeforeTemplateRenderedEventListener implements IEventListener {
return;
}
- \OCP\Util::addInitScript(Application::APP_NAME, 'init');
+ Util::addInitScript(Application::APP_NAME, 'init');
}
}
diff --git a/apps/updatenotification/lib/Manager.php b/apps/updatenotification/lib/Manager.php
index f2e75eedfa4..ebc1c83a9b4 100644
--- a/apps/updatenotification/lib/Manager.php
+++ b/apps/updatenotification/lib/Manager.php
@@ -34,7 +34,7 @@ class Manager {
* @param ?string $languageCode The language in which to query the changelog (defaults to current user language and fallsback to English)
* @return string|null Either the changelog entry or null if no changelog is found
*/
- public function getChangelog(string $appId, string $version, ?string $languageCode = null): string|null {
+ public function getChangelog(string $appId, string $version, ?string $languageCode = null): ?string {
if ($languageCode === null) {
$languageCode = $this->l10NFactory->getUserLanguage($this->currentUser);
}
@@ -55,7 +55,7 @@ class Manager {
* @param string $languageCode The language code to search
* @return string|null Either the file path or null if not found
*/
- public function getChangelogFile(string $appId, string $languageCode): string|null {
+ public function getChangelogFile(string $appId, string $languageCode): ?string {
try {
$appPath = $this->appManager->getAppPath($appId);
$files = ["CHANGELOG.$languageCode.md", 'CHANGELOG.en.md'];
@@ -73,10 +73,10 @@ class Manager {
/**
* Retrieve a log entry from the changelog
- * @param string $path The path to the changlog file
+ * @param string $path The path to the changelog file
* @param string $version The version to query (make sure to only pass in "{major}.{minor}(.{patch}" format)
*/
- protected function retrieveChangelogEntry(string $path, string $version): string|null {
+ protected function retrieveChangelogEntry(string $path, string $version): ?string {
$matches = [];
$content = file_get_contents($path);
if ($content === false) {
diff --git a/apps/updatenotification/lib/Migration/Version011901Date20240305120000.php b/apps/updatenotification/lib/Migration/Version011901Date20240305120000.php
deleted file mode 100644
index f731df5399e..00000000000
--- a/apps/updatenotification/lib/Migration/Version011901Date20240305120000.php
+++ /dev/null
@@ -1,56 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/**
- * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
-
-namespace OCA\UpdateNotification\Migration;
-
-use OCA\UpdateNotification\BackgroundJob\ResetToken;
-use OCP\BackgroundJob\IJobList;
-use OCP\Migration\IOutput;
-use OCP\Migration\SimpleMigrationStep;
-
-/**
- * Drop this with Nextcloud 30
- */
-class Version011901Date20240305120000 extends SimpleMigrationStep {
-
- public function __construct(
- private IJobList $joblist,
- ) {
- }
-
- public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void {
- /**
- * Remove and replace the reset-updater-token background job
- * This class was renamed so it is now unknow but we still need to remove it
- * @psalm-suppress UndefinedClass, InvalidArgument
- */
- $hasOldResetToken = $this->joblist->has(\OCA\UpdateNotification\ResetTokenBackgroundJob::class, null);
- $hasNewResetToken = $this->joblist->has(ResetToken::class, null);
- if ($hasOldResetToken) {
- /**
- * @psalm-suppress UndefinedClass, InvalidArgument
- */
- $this->joblist->remove(\OCA\UpdateNotification\ResetTokenBackgroundJob::class);
- if (!$hasNewResetToken) {
- $this->joblist->add(ResetToken::class);
- }
- }
-
- /**
- * Remove the "has updates" background job, the new one is automatically started from the info.xml
- * @psalm-suppress UndefinedClass, InvalidArgument
- */
- if ($this->joblist->has(\OCA\UpdateNotification\Notification\BackgroundJob::class, null)) {
- /**
- * @psalm-suppress UndefinedClass, InvalidArgument
- */
- $this->joblist->remove(\OCA\UpdateNotification\Notification\BackgroundJob::class);
- }
- }
-}
diff --git a/apps/updatenotification/lib/Notification/Notifier.php b/apps/updatenotification/lib/Notification/Notifier.php
index 6f1e8514b11..787675bd98d 100644
--- a/apps/updatenotification/lib/Notification/Notifier.php
+++ b/apps/updatenotification/lib/Notification/Notifier.php
@@ -8,8 +8,9 @@ declare(strict_types=1);
*/
namespace OCA\UpdateNotification\Notification;
+use OCA\UpdateNotification\AppInfo\Application;
use OCP\App\IAppManager;
-use OCP\IConfig;
+use OCP\AppFramework\Services\IAppConfig;
use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUser;
@@ -20,48 +21,26 @@ use OCP\Notification\IManager;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCP\Notification\UnknownNotificationException;
-use OCP\Util;
+use OCP\ServerVersion;
class Notifier implements INotifier {
- /** @var IURLGenerator */
- protected $url;
-
- /** @var IConfig */
- protected $config;
-
- /** @var IManager */
- protected $notificationManager;
-
- /** @var IFactory */
- protected $l10NFactory;
-
- /** @var IUserSession */
- protected $userSession;
-
- /** @var IGroupManager */
- protected $groupManager;
-
/** @var string[] */
protected $appVersions;
/**
* Notifier constructor.
- *
- * @param IURLGenerator $url
- * @param IConfig $config
- * @param IManager $notificationManager
- * @param IFactory $l10NFactory
- * @param IUserSession $userSession
- * @param IGroupManager $groupManager
*/
- public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) {
- $this->url = $url;
- $this->notificationManager = $notificationManager;
- $this->config = $config;
- $this->l10NFactory = $l10NFactory;
- $this->userSession = $userSession;
- $this->groupManager = $groupManager;
- $this->appVersions = $this->getAppVersions();
+ public function __construct(
+ protected IURLGenerator $url,
+ protected IAppConfig $appConfig,
+ protected IManager $notificationManager,
+ protected IFactory $l10NFactory,
+ protected IUserSession $userSession,
+ protected IGroupManager $groupManager,
+ protected IAppManager $appManager,
+ protected ServerVersion $serverVersion,
+ ) {
+ $this->appVersions = $this->appManager->getAppInstalledVersions();
}
/**
@@ -71,7 +50,7 @@ class Notifier implements INotifier {
* @since 17.0.0
*/
public function getID(): string {
- return 'updatenotification';
+ return Application::APP_NAME;
}
/**
@@ -81,7 +60,7 @@ class Notifier implements INotifier {
* @since 17.0.0
*/
public function getName(): string {
- return $this->l10NFactory->get('updatenotification')->t('Update notifications');
+ return $this->l10NFactory->get(Application::APP_NAME)->t('Update notifications');
}
/**
@@ -93,7 +72,7 @@ class Notifier implements INotifier {
* @since 9.0.0
*/
public function prepare(INotification $notification, string $languageCode): INotification {
- if ($notification->getApp() !== 'updatenotification') {
+ if ($notification->getApp() !== Application::APP_NAME) {
throw new UnknownNotificationException('Unknown app id');
}
@@ -101,9 +80,9 @@ class Notifier implements INotifier {
throw new UnknownNotificationException('Unknown subject');
}
- $l = $this->l10NFactory->get('updatenotification', $languageCode);
+ $l = $this->l10NFactory->get(Application::APP_NAME, $languageCode);
if ($notification->getSubject() === 'connection_error') {
- $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', '0');
+ $errors = $this->appConfig->getAppValueInt('update_check_errors', 0);
if ($errors === 0) {
throw new AlreadyProcessedException();
}
@@ -148,7 +127,7 @@ class Notifier implements INotifier {
}
}
- $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg')));
+ $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_NAME, 'notification.svg')));
return $notification;
}
@@ -160,15 +139,12 @@ class Notifier implements INotifier {
* @param string $installedVersion
* @throws AlreadyProcessedException When the update is already installed
*/
- protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
+ protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion): void {
if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
throw new AlreadyProcessedException();
}
}
- /**
- * @return bool
- */
protected function isAdmin(): bool {
$user = $this->userSession->getUser();
@@ -180,14 +156,10 @@ class Notifier implements INotifier {
}
protected function getCoreVersions(): string {
- return implode('.', Util::getVersion());
- }
-
- protected function getAppVersions(): array {
- return \OC_App::getAppVersions();
+ return implode('.', $this->serverVersion->getVersion());
}
- protected function getAppInfo($appId, $languageCode) {
- return \OCP\Server::get(IAppManager::class)->getAppInfo($appId, false, $languageCode);
+ protected function getAppInfo(string $appId, ?string $languageCode): ?array {
+ return $this->appManager->getAppInfo($appId, false, $languageCode);
}
}
diff --git a/apps/updatenotification/lib/Settings/Admin.php b/apps/updatenotification/lib/Settings/Admin.php
index 6e39a40ad8d..22228f1bccc 100644
--- a/apps/updatenotification/lib/Settings/Admin.php
+++ b/apps/updatenotification/lib/Settings/Admin.php
@@ -8,7 +8,7 @@ declare(strict_types=1);
*/
namespace OCA\UpdateNotification\Settings;
-use OC\User\Backend;
+use OCA\UpdateNotification\AppInfo\Application;
use OCA\UpdateNotification\UpdateChecker;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
@@ -18,10 +18,9 @@ use OCP\IDateTimeFormatter;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\L10N\IFactory;
+use OCP\ServerVersion;
use OCP\Settings\ISettings;
use OCP\Support\Subscription\IRegistry;
-use OCP\User\Backend\ICountUsersBackend;
-use OCP\Util;
use Psr\Log\LoggerInterface;
class Admin implements ISettings {
@@ -35,7 +34,8 @@ class Admin implements ISettings {
private IRegistry $subscriptionRegistry,
private IUserManager $userManager,
private LoggerInterface $logger,
- private IInitialState $initialState
+ private IInitialState $initialState,
+ private ServerVersion $serverVersion,
) {
}
@@ -49,14 +49,14 @@ class Admin implements ISettings {
'stable',
'production',
];
- $currentChannel = Util::getChannel();
+ $currentChannel = $this->serverVersion->getChannel();
if ($currentChannel === 'git') {
$channels[] = 'git';
}
$updateState = $this->updateChecker->getUpdateState();
- $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
+ $notifyGroups = $this->appConfig->getValueArray(Application::APP_NAME, 'notify_groups', ['admin']);
$defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/';
$updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL);
@@ -114,7 +114,7 @@ class Admin implements ISettings {
}
/**
- * @param list<string> $groupIds
+ * @param string[] $groupIds
* @return list<array{id: string, displayname: string}>
*/
protected function getSelectedGroups(array $groupIds): array {
@@ -132,7 +132,12 @@ class Admin implements ISettings {
return $result;
}
- public function getSection(): string {
+ public function getSection(): ?string {
+ if (!$this->config->getSystemValueBool('updatechecker', true)) {
+ // update checker is disabled so we do not show the section at all
+ return null;
+ }
+
return 'overview';
}
@@ -141,26 +146,6 @@ class Admin implements ISettings {
}
private function isWebUpdaterRecommended(): bool {
- return $this->getUserCount() < 100;
- }
-
- /**
- * @see https://github.com/nextcloud/server/blob/39494fbf794d982f6f6551c984e6ca4c4e947d01/lib/private/Support/Subscription/Registry.php#L188-L216 implementation reference
- */
- private function getUserCount(): int {
- $userCount = 0;
- $backends = $this->userManager->getBackends();
- foreach ($backends as $backend) {
- // TODO: change below to 'if ($backend instanceof ICountUsersBackend) {'
- if ($backend->implementsActions(Backend::COUNT_USERS)) {
- /** @var ICountUsersBackend $backend */
- $backendUsers = $backend->countUsers();
- if ($backendUsers !== false) {
- $userCount += $backendUsers;
- }
- }
- }
-
- return $userCount;
+ return (int)$this->userManager->countUsersTotal(100) < 100;
}
}
diff --git a/apps/updatenotification/lib/UpdateChecker.php b/apps/updatenotification/lib/UpdateChecker.php
index f15974a0318..b206ba4a3e4 100644
--- a/apps/updatenotification/lib/UpdateChecker.php
+++ b/apps/updatenotification/lib/UpdateChecker.php
@@ -4,25 +4,22 @@ declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\UpdateNotification;
use OC\Updater\ChangesCheck;
use OC\Updater\VersionCheck;
+use OCP\AppFramework\Services\IInitialState;
class UpdateChecker {
- /** @var VersionCheck */
- private $updater;
- /** @var ChangesCheck */
- private $changesCheck;
- /**
- * @param VersionCheck $updater
- */
- public function __construct(VersionCheck $updater, ChangesCheck $changesCheck) {
- $this->updater = $updater;
- $this->changesCheck = $changesCheck;
+ public function __construct(
+ private VersionCheck $updater,
+ private ChangesCheck $changesCheck,
+ private IInitialState $initialState,
+ ) {
}
/**
@@ -59,13 +56,17 @@ class UpdateChecker {
}
/**
- * @param array $data
+ * Provide update information as initial state
*/
- public function populateJavaScriptVariables(array $data) {
- $data['array']['oc_updateState'] = json_encode([
- 'updateAvailable' => true,
- 'updateVersion' => $this->getUpdateState()['updateVersionString'],
- 'updateLink' => $this->getUpdateState()['updateLink'] ?? '',
+ public function setInitialState(): void {
+ $updateState = $this->getUpdateState();
+ if (empty($updateState)) {
+ return;
+ }
+
+ $this->initialState->provideInitialState('updateState', [
+ 'updateVersion' => $updateState['updateVersionString'],
+ 'updateLink' => $updateState['updateLink'] ?? '',
]);
}
}