-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: Provide app-updated notifications for users #43967
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d9d3448
fix(updatenotification): Replace deprecated code and move background …
susnux fa14daf
feat(updatenotification): Add notification for users when apps are up…
susnux 9c1a87a
fix(updatenotification): Update tests to match with changed constructors
susnux 31d24a5
fix(updatenotification): Resolve static code analysis errors
susnux 40ae129
fix(updatenotification): Add migration step to replace and remove ren…
susnux 2672884
feat(updatenotification): Allow to disable the app updated notificati…
susnux 876e2d6
feat(AppManager): Provide `getAppIcon` function
susnux b969eab
chore: Compile assets
susnux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| * @author Lukas Reschke <[email protected]> | ||
| * @author Morris Jobke <[email protected]> | ||
| * @author Roeland Jago Douma <[email protected]> | ||
| * @author Ferdinand Thiessen <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
|
|
@@ -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 | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
126 changes: 126 additions & 0 deletions
126
apps/updatenotification/lib/BackgroundJob/AppUpdatedNotifications.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 <[email protected]> | ||
| * | ||
| * @author Ferdinand Thiessen <[email protected]> | ||
| * | ||
| * @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(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| * @author Christoph Wurst <[email protected]> | ||
| * @author Joas Schilling <[email protected]> | ||
| * @author Lukas Reschke <[email protected]> | ||
| * @author Ferdinand Thiessen <[email protected]> | ||
| * | ||
| * @license AGPL-3.0 | ||
| * | ||
|
|
@@ -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 { | ||
|
|
||
| /** | ||
| * @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'); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.