diff options
author | Joas Schilling <coding@schilljs.com> | 2017-03-17 12:47:26 +0100 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2017-03-26 10:59:58 +0200 |
commit | 3668673d7b3f64a5aaa80f569856b88460d9a522 (patch) | |
tree | 1767767ac8889e6001b9fca4f7670c237c28b158 /apps/updatenotification/lib/Notification | |
parent | f3917cfea148d09832a727953c74ece952a47f84 (diff) | |
download | nextcloud-server-3668673d7b3f64a5aaa80f569856b88460d9a522.tar.gz nextcloud-server-3668673d7b3f64a5aaa80f569856b88460d9a522.zip |
Create a notification when the update server couldn't be reached for some days
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps/updatenotification/lib/Notification')
-rw-r--r-- | apps/updatenotification/lib/Notification/BackgroundJob.php | 54 | ||||
-rw-r--r-- | apps/updatenotification/lib/Notification/Notifier.php | 19 |
2 files changed, 69 insertions, 4 deletions
diff --git a/apps/updatenotification/lib/Notification/BackgroundJob.php b/apps/updatenotification/lib/Notification/BackgroundJob.php index 83a9bdb599a..c3dd748116c 100644 --- a/apps/updatenotification/lib/Notification/BackgroundJob.php +++ b/apps/updatenotification/lib/Notification/BackgroundJob.php @@ -34,6 +34,8 @@ use OCP\Notification\IManager; class BackgroundJob extends TimedJob { + protected $connectionNotifications = [3, 7, 14, 30]; + /** @var IConfig */ protected $config; @@ -81,7 +83,7 @@ class BackgroundJob extends TimedJob { * Check for ownCloud update */ protected function checkCoreUpdate() { - if (in_array($this->getChannel(), ['daily', 'git'])) { + if (in_array($this->getChannel(), ['daily', 'git'], true)) { // "These aren't the update channels you're looking for." - Ben Obi-Wan Kenobi return; } @@ -89,12 +91,60 @@ class BackgroundJob extends TimedJob { $updater = $this->createVersionCheck(); $status = $updater->check(); - if (isset($status['version'])) { + if ($status === false) { + $errors = 1 + (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0); + $this->config->setAppValue('updatenotification', 'update_check_errors', $errors); + + if (in_array($errors, $this->connectionNotifications, true)) { + $this->sendErrorNotifications($errors); + } + } else if (isset($status['version'])) { + $this->config->setAppValue('updatenotification', 'update_check_errors', 0); + $this->clearErrorNotifications(); + $this->createNotifications('core', $status['version'], $status['versionstring']); } } /** + * Send a message to the admin when the update server could not be reached + * @param int $numDays + */ + protected function sendErrorNotifications($numDays) { + $this->clearErrorNotifications(); + + $notification = $this->notificationManager->createNotification(); + try { + $notification->setApp('updatenotification') + ->setDateTime(new \DateTime()) + ->setObject('updatenotification', 'error') + ->setSubject('connection_error', ['days' => $numDays]); + + foreach ($this->getUsersToNotify() as $uid) { + $notification->setUser($uid); + $this->notificationManager->notify($notification); + } + } catch (\InvalidArgumentException $e) { + return; + } + } + + /** + * Remove error notifications again + */ + protected function clearErrorNotifications() { + $notification = $this->notificationManager->createNotification(); + try { + $notification->setApp('updatenotification') + ->setSubject('connection_error') + ->setObject('updatenotification', 'error'); + } catch (\InvalidArgumentException $e) { + return; + } + $this->notificationManager->markProcessed($notification); + } + + /** * Check all installed apps for updates */ protected function checkAppUpdates() { diff --git a/apps/updatenotification/lib/Notification/Notifier.php b/apps/updatenotification/lib/Notification/Notifier.php index 079ec4c5e0a..bef57b0d867 100644 --- a/apps/updatenotification/lib/Notification/Notifier.php +++ b/apps/updatenotification/lib/Notification/Notifier.php @@ -24,6 +24,7 @@ namespace OCA\UpdateNotification\Notification; +use OCP\IConfig; use OCP\IGroupManager; use OCP\IURLGenerator; use OCP\IUser; @@ -38,6 +39,9 @@ class Notifier implements INotifier { /** @var IURLGenerator */ protected $url; + /** @var IConfig */ + protected $config; + /** @var IManager */ protected $notificationManager; @@ -57,14 +61,16 @@ class Notifier implements INotifier { * 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, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, 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; @@ -84,7 +90,16 @@ class Notifier implements INotifier { } $l = $this->l10NFactory->get('updatenotification', $languageCode); - if ($notification->getObjectType() === 'core') { + if ($notification->getSubject() === 'connection_error') { + $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0); + if ($errors === 0) { + $this->notificationManager->markProcessed($notification); + throw new \InvalidArgumentException(); + } + + $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors])) + ->setParsedMessage($l->t('Please check the nextcloud and server log files for errors.')); + } elseif ($notification->getObjectType() === 'core') { $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions()); $parameters = $notification->getSubjectParameters(); |