From d2553a4f6e14181d7788e42d1c70285ebb40f736 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 4 May 2016 16:34:54 +0200 Subject: Dismiss the notifications when the update is already installed --- apps/updatenotification/appinfo/app.php | 3 +- .../lib/Notification/Notifier.php | 48 ++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/apps/updatenotification/appinfo/app.php b/apps/updatenotification/appinfo/app.php index 2457ec21018..de78da5da76 100644 --- a/apps/updatenotification/appinfo/app.php +++ b/apps/updatenotification/appinfo/app.php @@ -40,8 +40,9 @@ if(\OC::$server->getConfig()->getSystemValue('updatechecker', true) === true) { } $manager = \OC::$server->getNotificationManager(); - $manager->registerNotifier(function() { + $manager->registerNotifier(function() use ($manager) { return new \OCA\UpdateNotification\Notification\Notifier( + $manager, \OC::$server->getL10NFactory() ); }, function() use ($l) { diff --git a/apps/updatenotification/lib/Notification/Notifier.php b/apps/updatenotification/lib/Notification/Notifier.php index 94fe610dd25..62c4819fb1e 100644 --- a/apps/updatenotification/lib/Notification/Notifier.php +++ b/apps/updatenotification/lib/Notification/Notifier.php @@ -22,23 +22,33 @@ namespace OCA\UpdateNotification\Notification; -use OCP\App\IAppManager; use OCP\L10N\IFactory; +use OCP\Notification\IManager; use OCP\Notification\INotification; use OCP\Notification\INotifier; class Notifier implements INotifier { + /** @var IManager */ + protected $notificationManager; + /** @var IFactory */ protected $l10NFactory; + /** @var string[] */ + protected $appVersions; + /** * Notifier constructor. * + * @param IManager $notificationManager * @param IFactory $l10NFactory */ - public function __construct(IFactory $l10NFactory) { + public function __construct(IManager $notificationManager, IFactory $l10NFactory) { + $this->notificationManager = $notificationManager; $this->l10NFactory = $l10NFactory; + $this->appVersions = $this->getAppVersions(); + \OC::$server->getLogger()->error(json_encode($this->appVersions)); } /** @@ -56,12 +66,44 @@ class Notifier implements INotifier { $l = $this->l10NFactory->get('updatenotification', $languageCode); if ($notification->getObjectType() === 'core') { $appName = $l->t('ownCloud core'); + + $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions()); } else { - $appInfo = \OC_App::getAppInfo($notification->getObjectType()); + $appInfo = $this->getAppInfo($notification->getObjectType()); $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name']; + + if (isset($this->appVersions[$notification->getObjectType()])) { + $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]); + } } $notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()])); return $notification; } + + /** + * Remove the notification and prevent rendering, when the update is installed + * + * @param INotification $notification + * @param string $installedVersion + * @throws \InvalidArgumentException When the update is already installed + */ + protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) { + if (version_compare($notification->getObjectId(), $installedVersion, '<=')) { + $this->notificationManager->markProcessed($notification); + throw new \InvalidArgumentException(); + } + } + + protected function getCoreVersions() { + return implode('.', \OCP\Util::getVersion()); + } + + protected function getAppVersions() { + return \OC_App::getAppVersions(); + } + + protected function getAppInfo($appId) { + return \OC_App::getAppInfo($appId); + } } -- cgit v1.2.3