@@ -38,141 +38,141 @@ |
||
38 | 38 | |
39 | 39 | class Notifier implements INotifier { |
40 | 40 | |
41 | - /** @var IURLGenerator */ |
|
42 | - protected $url; |
|
43 | - |
|
44 | - /** @var IConfig */ |
|
45 | - protected $config; |
|
46 | - |
|
47 | - /** @var IManager */ |
|
48 | - protected $notificationManager; |
|
49 | - |
|
50 | - /** @var IFactory */ |
|
51 | - protected $l10NFactory; |
|
52 | - |
|
53 | - /** @var IUserSession */ |
|
54 | - protected $userSession; |
|
55 | - |
|
56 | - /** @var IGroupManager */ |
|
57 | - protected $groupManager; |
|
58 | - |
|
59 | - /** @var string[] */ |
|
60 | - protected $appVersions; |
|
61 | - |
|
62 | - /** |
|
63 | - * Notifier constructor. |
|
64 | - * |
|
65 | - * @param IURLGenerator $url |
|
66 | - * @param IConfig $config |
|
67 | - * @param IManager $notificationManager |
|
68 | - * @param IFactory $l10NFactory |
|
69 | - * @param IUserSession $userSession |
|
70 | - * @param IGroupManager $groupManager |
|
71 | - */ |
|
72 | - public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) { |
|
73 | - $this->url = $url; |
|
74 | - $this->notificationManager = $notificationManager; |
|
75 | - $this->config = $config; |
|
76 | - $this->l10NFactory = $l10NFactory; |
|
77 | - $this->userSession = $userSession; |
|
78 | - $this->groupManager = $groupManager; |
|
79 | - $this->appVersions = $this->getAppVersions(); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param INotification $notification |
|
84 | - * @param string $languageCode The code of the language that should be used to prepare the notification |
|
85 | - * @return INotification |
|
86 | - * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
87 | - * @since 9.0.0 |
|
88 | - */ |
|
89 | - public function prepare(INotification $notification, $languageCode): INotification { |
|
90 | - if ($notification->getApp() !== 'updatenotification') { |
|
91 | - throw new \InvalidArgumentException('Unknown app id'); |
|
92 | - } |
|
93 | - |
|
94 | - $l = $this->l10NFactory->get('updatenotification', $languageCode); |
|
95 | - if ($notification->getSubject() === 'connection_error') { |
|
96 | - $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0); |
|
97 | - if ($errors === 0) { |
|
98 | - $this->notificationManager->markProcessed($notification); |
|
99 | - throw new \InvalidArgumentException('Update checked worked again'); |
|
100 | - } |
|
101 | - |
|
102 | - $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors])) |
|
103 | - ->setParsedMessage($l->t('Please check the Nextcloud and server log files for errors.')); |
|
104 | - } elseif ($notification->getObjectType() === 'core') { |
|
105 | - $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions()); |
|
106 | - |
|
107 | - $parameters = $notification->getSubjectParameters(); |
|
108 | - $notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']])); |
|
109 | - |
|
110 | - if ($this->isAdmin()) { |
|
111 | - $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version'); |
|
112 | - } |
|
113 | - } else { |
|
114 | - $appInfo = $this->getAppInfo($notification->getObjectType()); |
|
115 | - $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name']; |
|
116 | - |
|
117 | - if (isset($this->appVersions[$notification->getObjectType()])) { |
|
118 | - $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]); |
|
119 | - } |
|
120 | - |
|
121 | - $notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()])) |
|
122 | - ->setRichSubject($l->t('Update for {app} to version %s is available.', [$notification->getObjectId()]), [ |
|
123 | - 'app' => [ |
|
124 | - 'type' => 'app', |
|
125 | - 'id' => $notification->getObjectType(), |
|
126 | - 'name' => $appName, |
|
127 | - ] |
|
128 | - ]); |
|
129 | - |
|
130 | - if ($this->isAdmin()) { |
|
131 | - $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType()); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg'))); |
|
136 | - |
|
137 | - return $notification; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Remove the notification and prevent rendering, when the update is installed |
|
142 | - * |
|
143 | - * @param INotification $notification |
|
144 | - * @param string $installedVersion |
|
145 | - * @throws \InvalidArgumentException When the update is already installed |
|
146 | - */ |
|
147 | - protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) { |
|
148 | - if (version_compare($notification->getObjectId(), $installedVersion, '<=')) { |
|
149 | - $this->notificationManager->markProcessed($notification); |
|
150 | - throw new \InvalidArgumentException('Update already installed'); |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @return bool |
|
156 | - */ |
|
157 | - protected function isAdmin(): bool { |
|
158 | - $user = $this->userSession->getUser(); |
|
159 | - |
|
160 | - if ($user instanceof IUser) { |
|
161 | - return $this->groupManager->isAdmin($user->getUID()); |
|
162 | - } |
|
163 | - |
|
164 | - return false; |
|
165 | - } |
|
166 | - |
|
167 | - protected function getCoreVersions(): string { |
|
168 | - return implode('.', Util::getVersion()); |
|
169 | - } |
|
170 | - |
|
171 | - protected function getAppVersions(): array { |
|
172 | - return \OC_App::getAppVersions(); |
|
173 | - } |
|
174 | - |
|
175 | - protected function getAppInfo($appId) { |
|
176 | - return \OC_App::getAppInfo($appId); |
|
177 | - } |
|
41 | + /** @var IURLGenerator */ |
|
42 | + protected $url; |
|
43 | + |
|
44 | + /** @var IConfig */ |
|
45 | + protected $config; |
|
46 | + |
|
47 | + /** @var IManager */ |
|
48 | + protected $notificationManager; |
|
49 | + |
|
50 | + /** @var IFactory */ |
|
51 | + protected $l10NFactory; |
|
52 | + |
|
53 | + /** @var IUserSession */ |
|
54 | + protected $userSession; |
|
55 | + |
|
56 | + /** @var IGroupManager */ |
|
57 | + protected $groupManager; |
|
58 | + |
|
59 | + /** @var string[] */ |
|
60 | + protected $appVersions; |
|
61 | + |
|
62 | + /** |
|
63 | + * Notifier constructor. |
|
64 | + * |
|
65 | + * @param IURLGenerator $url |
|
66 | + * @param IConfig $config |
|
67 | + * @param IManager $notificationManager |
|
68 | + * @param IFactory $l10NFactory |
|
69 | + * @param IUserSession $userSession |
|
70 | + * @param IGroupManager $groupManager |
|
71 | + */ |
|
72 | + public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) { |
|
73 | + $this->url = $url; |
|
74 | + $this->notificationManager = $notificationManager; |
|
75 | + $this->config = $config; |
|
76 | + $this->l10NFactory = $l10NFactory; |
|
77 | + $this->userSession = $userSession; |
|
78 | + $this->groupManager = $groupManager; |
|
79 | + $this->appVersions = $this->getAppVersions(); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param INotification $notification |
|
84 | + * @param string $languageCode The code of the language that should be used to prepare the notification |
|
85 | + * @return INotification |
|
86 | + * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
87 | + * @since 9.0.0 |
|
88 | + */ |
|
89 | + public function prepare(INotification $notification, $languageCode): INotification { |
|
90 | + if ($notification->getApp() !== 'updatenotification') { |
|
91 | + throw new \InvalidArgumentException('Unknown app id'); |
|
92 | + } |
|
93 | + |
|
94 | + $l = $this->l10NFactory->get('updatenotification', $languageCode); |
|
95 | + if ($notification->getSubject() === 'connection_error') { |
|
96 | + $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0); |
|
97 | + if ($errors === 0) { |
|
98 | + $this->notificationManager->markProcessed($notification); |
|
99 | + throw new \InvalidArgumentException('Update checked worked again'); |
|
100 | + } |
|
101 | + |
|
102 | + $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors])) |
|
103 | + ->setParsedMessage($l->t('Please check the Nextcloud and server log files for errors.')); |
|
104 | + } elseif ($notification->getObjectType() === 'core') { |
|
105 | + $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions()); |
|
106 | + |
|
107 | + $parameters = $notification->getSubjectParameters(); |
|
108 | + $notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']])); |
|
109 | + |
|
110 | + if ($this->isAdmin()) { |
|
111 | + $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version'); |
|
112 | + } |
|
113 | + } else { |
|
114 | + $appInfo = $this->getAppInfo($notification->getObjectType()); |
|
115 | + $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name']; |
|
116 | + |
|
117 | + if (isset($this->appVersions[$notification->getObjectType()])) { |
|
118 | + $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]); |
|
119 | + } |
|
120 | + |
|
121 | + $notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()])) |
|
122 | + ->setRichSubject($l->t('Update for {app} to version %s is available.', [$notification->getObjectId()]), [ |
|
123 | + 'app' => [ |
|
124 | + 'type' => 'app', |
|
125 | + 'id' => $notification->getObjectType(), |
|
126 | + 'name' => $appName, |
|
127 | + ] |
|
128 | + ]); |
|
129 | + |
|
130 | + if ($this->isAdmin()) { |
|
131 | + $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType()); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg'))); |
|
136 | + |
|
137 | + return $notification; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Remove the notification and prevent rendering, when the update is installed |
|
142 | + * |
|
143 | + * @param INotification $notification |
|
144 | + * @param string $installedVersion |
|
145 | + * @throws \InvalidArgumentException When the update is already installed |
|
146 | + */ |
|
147 | + protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) { |
|
148 | + if (version_compare($notification->getObjectId(), $installedVersion, '<=')) { |
|
149 | + $this->notificationManager->markProcessed($notification); |
|
150 | + throw new \InvalidArgumentException('Update already installed'); |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @return bool |
|
156 | + */ |
|
157 | + protected function isAdmin(): bool { |
|
158 | + $user = $this->userSession->getUser(); |
|
159 | + |
|
160 | + if ($user instanceof IUser) { |
|
161 | + return $this->groupManager->isAdmin($user->getUID()); |
|
162 | + } |
|
163 | + |
|
164 | + return false; |
|
165 | + } |
|
166 | + |
|
167 | + protected function getCoreVersions(): string { |
|
168 | + return implode('.', Util::getVersion()); |
|
169 | + } |
|
170 | + |
|
171 | + protected function getAppVersions(): array { |
|
172 | + return \OC_App::getAppVersions(); |
|
173 | + } |
|
174 | + |
|
175 | + protected function getAppInfo($appId) { |
|
176 | + return \OC_App::getAppInfo($appId); |
|
177 | + } |
|
178 | 178 | } |