Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Provide app-updated notifications for users #43967

Merged
merged 8 commits into from
Mar 7, 2024
8 changes: 4 additions & 4 deletions apps/updatenotification/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
xsi:noNamespaceSchemaLocation="https://apps.nextcloud.com/schema/apps/info.xsd">
<id>updatenotification</id>
<name>Update notification</name>
<summary>Displays update notifications for Nextcloud and provides the SSO for the updater.</summary>
<description>Displays update notifications for Nextcloud and provides the SSO for the updater.</description>
<version>1.19.0</version>
<summary>Displays update notifications for Nextcloud, app updates, and provides the SSO for the updater.</summary>
<description>Displays update notifications for Nextcloud, app updates, and provides the SSO for the updater.</description>
<version>1.19.1</version>
<licence>agpl</licence>
<author>Lukas Reschke</author>
<namespace>UpdateNotification</namespace>
Expand All @@ -16,7 +16,7 @@
</dependencies>

<background-jobs>
<job>OCA\UpdateNotification\Notification\BackgroundJob</job>
<job>OCA\UpdateNotification\BackgroundJob\UpdateAvailableNotifications</job>
</background-jobs>

<settings>
Expand Down
3 changes: 3 additions & 0 deletions apps/updatenotification/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
'routes' => [
['name' => 'Admin#createCredentials', 'url' => '/credentials', 'verb' => 'GET'],
['name' => 'Admin#setChannel', 'url' => '/channel', 'verb' => 'POST'],
// Fallback app changelog information for mobile clients
['name' => 'Changelog#showChangelog', 'url' => '/changelog/{app}', 'verb' => 'GET'],
],
'ocs' => [
['name' => 'API#getAppList', 'url' => '/api/{apiVersion}/applist/{newVersion}', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v1)']],
['name' => 'API#getAppChangelogEntry', 'url' => '/api/{apiVersion}/changelog/{appId}', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v1)']],
],
];
11 changes: 9 additions & 2 deletions apps/updatenotification/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
return array(
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
'OCA\\UpdateNotification\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
'OCA\\UpdateNotification\\BackgroundJob\\AppUpdatedNotifications' => $baseDir . '/../lib/BackgroundJob/AppUpdatedNotifications.php',
'OCA\\UpdateNotification\\BackgroundJob\\ResetToken' => $baseDir . '/../lib/BackgroundJob/ResetToken.php',
'OCA\\UpdateNotification\\BackgroundJob\\UpdateAvailableNotifications' => $baseDir . '/../lib/BackgroundJob/UpdateAvailableNotifications.php',
'OCA\\UpdateNotification\\Command\\Check' => $baseDir . '/../lib/Command/Check.php',
'OCA\\UpdateNotification\\Controller\\APIController' => $baseDir . '/../lib/Controller/APIController.php',
'OCA\\UpdateNotification\\Controller\\AdminController' => $baseDir . '/../lib/Controller/AdminController.php',
'OCA\\UpdateNotification\\Notification\\BackgroundJob' => $baseDir . '/../lib/Notification/BackgroundJob.php',
'OCA\\UpdateNotification\\Controller\\ChangelogController' => $baseDir . '/../lib/Controller/ChangelogController.php',
'OCA\\UpdateNotification\\Listener\\AppUpdateEventListener' => $baseDir . '/../lib/Listener/AppUpdateEventListener.php',
'OCA\\UpdateNotification\\Listener\\BeforeTemplateRenderedEventListener' => $baseDir . '/../lib/Listener/BeforeTemplateRenderedEventListener.php',
'OCA\\UpdateNotification\\Manager' => $baseDir . '/../lib/Manager.php',
'OCA\\UpdateNotification\\Migration\\Version011901Date20240305120000' => $baseDir . '/../lib/Migration/Version011901Date20240305120000.php',
'OCA\\UpdateNotification\\Notification\\AppUpdateNotifier' => $baseDir . '/../lib/Notification/AppUpdateNotifier.php',
'OCA\\UpdateNotification\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
'OCA\\UpdateNotification\\ResetTokenBackgroundJob' => $baseDir . '/../lib/ResetTokenBackgroundJob.php',
'OCA\\UpdateNotification\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php',
'OCA\\UpdateNotification\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',
'OCA\\UpdateNotification\\UpdateChecker' => $baseDir . '/../lib/UpdateChecker.php',
Expand Down
11 changes: 9 additions & 2 deletions apps/updatenotification/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ class ComposerStaticInitUpdateNotification
public static $classMap = array (
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
'OCA\\UpdateNotification\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
'OCA\\UpdateNotification\\BackgroundJob\\AppUpdatedNotifications' => __DIR__ . '/..' . '/../lib/BackgroundJob/AppUpdatedNotifications.php',
'OCA\\UpdateNotification\\BackgroundJob\\ResetToken' => __DIR__ . '/..' . '/../lib/BackgroundJob/ResetToken.php',
'OCA\\UpdateNotification\\BackgroundJob\\UpdateAvailableNotifications' => __DIR__ . '/..' . '/../lib/BackgroundJob/UpdateAvailableNotifications.php',
'OCA\\UpdateNotification\\Command\\Check' => __DIR__ . '/..' . '/../lib/Command/Check.php',
'OCA\\UpdateNotification\\Controller\\APIController' => __DIR__ . '/..' . '/../lib/Controller/APIController.php',
'OCA\\UpdateNotification\\Controller\\AdminController' => __DIR__ . '/..' . '/../lib/Controller/AdminController.php',
'OCA\\UpdateNotification\\Notification\\BackgroundJob' => __DIR__ . '/..' . '/../lib/Notification/BackgroundJob.php',
'OCA\\UpdateNotification\\Controller\\ChangelogController' => __DIR__ . '/..' . '/../lib/Controller/ChangelogController.php',
'OCA\\UpdateNotification\\Listener\\AppUpdateEventListener' => __DIR__ . '/..' . '/../lib/Listener/AppUpdateEventListener.php',
'OCA\\UpdateNotification\\Listener\\BeforeTemplateRenderedEventListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeTemplateRenderedEventListener.php',
'OCA\\UpdateNotification\\Manager' => __DIR__ . '/..' . '/../lib/Manager.php',
'OCA\\UpdateNotification\\Migration\\Version011901Date20240305120000' => __DIR__ . '/..' . '/../lib/Migration/Version011901Date20240305120000.php',
'OCA\\UpdateNotification\\Notification\\AppUpdateNotifier' => __DIR__ . '/..' . '/../lib/Notification/AppUpdateNotifier.php',
'OCA\\UpdateNotification\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
'OCA\\UpdateNotification\\ResetTokenBackgroundJob' => __DIR__ . '/..' . '/../lib/ResetTokenBackgroundJob.php',
'OCA\\UpdateNotification\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php',
'OCA\\UpdateNotification\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',
'OCA\\UpdateNotification\\UpdateChecker' => __DIR__ . '/..' . '/../lib/UpdateChecker.php',
Expand Down
24 changes: 18 additions & 6 deletions apps/updatenotification/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -29,37 +30,48 @@
*/
namespace OCA\UpdateNotification\AppInfo;

use OCA\UpdateNotification\Listener\AppUpdateEventListener;
use OCA\UpdateNotification\Listener\BeforeTemplateRenderedEventListener;
use OCA\UpdateNotification\Notification\AppUpdateNotifier;
use OCA\UpdateNotification\Notification\Notifier;
use OCA\UpdateNotification\UpdateChecker;
use OCP\App\Events\AppUpdateEvent;
use OCP\App\IAppManager;
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\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
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 {
public const APP_NAME = 'updatenotification';

public function __construct() {
parent::__construct('updatenotification', []);
parent::__construct(self::APP_NAME, []);
}

public function register(IRegistrationContext $context): void {
$context->registerNotifierService(Notifier::class);
$context->registerNotifierService(AppUpdateNotifier::class);

$context->registerEventListener(AppUpdateEvent::class, AppUpdateEventListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedEventListener::class);
}

public function boot(IBootContext $context): void {
$context->injectFn(function (IConfig $config,
IUserSession $userSession,
IAppManager $appManager,
IGroupManager $groupManager,
IAppContainer $appContainer,
ContainerInterface $container,
LoggerInterface $logger) {
if ($config->getSystemValue('updatechecker', true) !== true) {
// Updater check is disabled
Expand All @@ -75,8 +87,8 @@ public function boot(IBootContext $context): void {
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;
}
Expand Down
126 changes: 126 additions & 0 deletions apps/updatenotification/lib/BackgroundJob/AppUpdatedNotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
*
* @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\UpdateNotification\BackgroundJob;

use OCA\UpdateNotification\AppInfo\Application;
use OCA\UpdateNotification\Manager;
use OCP\App\IAppManager;
use OCP\AppFramework\Services\IAppConfig;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use Psr\Log\LoggerInterface;

class AppUpdatedNotifications extends QueuedJob {
public function __construct(
ITimeFactory $time,
private IConfig $config,
private IAppConfig $appConfig,
private IManager $notificationManager,
private IUserManager $userManager,
private IAppManager $appManager,
private LoggerInterface $logger,
private Manager $manager,
) {
parent::__construct($time);
}

/**
* @param array{appId: string, timestamp: int} $argument
*/
protected function run(mixed $argument): void {
$appId = $argument['appId'];
$timestamp = $argument['timestamp'];
$dateTime = $this->time->getDateTime();
$dateTime->setTimestamp($timestamp);

$this->logger->debug(
'Running background job to create app update notifications for "' . $appId . '"',
[
'app' => Application::APP_NAME,
],
);

if ($this->manager->getChangelogFile($appId, 'en') === null) {
$this->logger->debug('Skipping app updated notification - no changelog provided');
return;
}

$this->stopPreviousNotifications($appId);

// Create new notifications
$notification = $this->notificationManager->createNotification();
$notification->setApp(Application::APP_NAME)
->setDateTime($dateTime)
->setSubject('app_updated', [$appId])
->setObject('app_updated', $appId);

$this->notifyUsers($appId, $notification);
}

/**
* Stop all previous notifications users might not have dismissed until now
* @param string $appId The app to stop update notifications for
*/
private function stopPreviousNotifications(string $appId): void {
$notification = $this->notificationManager->createNotification();
$notification->setApp(Application::APP_NAME)
->setObject('app_updated', $appId);
$this->notificationManager->markProcessed($notification);
}

/**
* Notify all users for which the updated app is enabled
*/
private function notifyUsers(string $appId, INotification $notification): void {
$guestsEnabled = $this->appConfig->getAppValueBool('app_updated.notify_guests', false) && class_exists('\OCA\Guests\UserBackend');

$isDefer = $this->notificationManager->defer();

// Notify all seen users about the app update
$this->userManager->callForSeenUsers(function (IUser $user) use ($guestsEnabled, $appId, $notification) {
if (!$guestsEnabled && ($user->getBackendClassName() === '\OCA\Guests\UserBackend')) {
return;
}

if (!$this->appManager->isEnabledForUser($appId, $user)) {
return;
}

$notification->setUser($user->getUID());
$this->notificationManager->notify($notification);
});

// If we enabled the defer we call the flush
if ($isDefer) {
$this->notificationManager->flush();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @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
*
Expand All @@ -24,43 +25,43 @@
* 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 {
susnux marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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')) {
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');
}
}
Expand Down
Loading
Loading