1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
5
|
|
|
* |
6
|
|
|
* @author Joas Schilling <[email protected]> |
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @license AGPL-3.0 |
10
|
|
|
* |
11
|
|
|
* This code is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
13
|
|
|
* as published by the Free Software Foundation. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\UpdateNotification\Notification; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
use OCP\IConfig; |
29
|
|
|
use OCP\IGroupManager; |
30
|
|
|
use OCP\IURLGenerator; |
31
|
|
|
use OCP\IUser; |
32
|
|
|
use OCP\IUserSession; |
33
|
|
|
use OCP\L10N\IFactory; |
34
|
|
|
use OCP\Notification\AlreadyProcessedException; |
35
|
|
|
use OCP\Notification\IManager; |
36
|
|
|
use OCP\Notification\INotification; |
37
|
|
|
use OCP\Notification\INotifier; |
38
|
|
|
use OCP\Util; |
39
|
|
|
|
40
|
|
|
class Notifier implements INotifier { |
41
|
|
|
|
42
|
|
|
/** @var IURLGenerator */ |
43
|
|
|
protected $url; |
44
|
|
|
|
45
|
|
|
/** @var IConfig */ |
46
|
|
|
protected $config; |
47
|
|
|
|
48
|
|
|
/** @var IManager */ |
49
|
|
|
protected $notificationManager; |
50
|
|
|
|
51
|
|
|
/** @var IFactory */ |
52
|
|
|
protected $l10NFactory; |
53
|
|
|
|
54
|
|
|
/** @var IUserSession */ |
55
|
|
|
protected $userSession; |
56
|
|
|
|
57
|
|
|
/** @var IGroupManager */ |
58
|
|
|
protected $groupManager; |
59
|
|
|
|
60
|
|
|
/** @var string[] */ |
61
|
|
|
protected $appVersions; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Notifier constructor. |
65
|
|
|
* |
66
|
|
|
* @param IURLGenerator $url |
67
|
|
|
* @param IConfig $config |
68
|
|
|
* @param IManager $notificationManager |
69
|
|
|
* @param IFactory $l10NFactory |
70
|
|
|
* @param IUserSession $userSession |
71
|
|
|
* @param IGroupManager $groupManager |
72
|
|
|
*/ |
73
|
|
|
public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) { |
74
|
|
|
$this->url = $url; |
75
|
|
|
$this->notificationManager = $notificationManager; |
76
|
|
|
$this->config = $config; |
77
|
|
|
$this->l10NFactory = $l10NFactory; |
78
|
|
|
$this->userSession = $userSession; |
79
|
|
|
$this->groupManager = $groupManager; |
80
|
|
|
$this->appVersions = $this->getAppVersions(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Identifier of the notifier, only use [a-z0-9_] |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
* @since 17.0.0 |
88
|
|
|
*/ |
89
|
|
|
public function getID(): string { |
90
|
|
|
return 'updatenotification'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Human readable name describing the notifier |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
* @since 17.0.0 |
98
|
|
|
*/ |
99
|
|
|
public function getName(): string { |
100
|
|
|
return $this->l10NFactory->get('updatenotification')->t('Update notifications'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param INotification $notification |
105
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification |
106
|
|
|
* @return INotification |
107
|
|
|
* @throws \InvalidArgumentException When the notification was not prepared by a notifier |
108
|
|
|
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted |
109
|
|
|
* @since 9.0.0 |
110
|
|
|
*/ |
111
|
|
|
public function prepare(INotification $notification, string $languageCode): INotification { |
112
|
|
|
if ($notification->getApp() !== 'updatenotification') { |
113
|
|
|
throw new \InvalidArgumentException('Unknown app id'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$l = $this->l10NFactory->get('updatenotification', $languageCode); |
117
|
|
|
if ($notification->getSubject() === 'connection_error') { |
118
|
|
|
$errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0); |
119
|
|
|
if ($errors === 0) { |
120
|
|
|
$this->notificationManager->markProcessed($notification); |
121
|
|
|
throw new \InvalidArgumentException('Update checked worked again'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors])) |
125
|
|
|
->setParsedMessage($l->t('Please check the Nextcloud and server log files for errors.')); |
126
|
|
|
} elseif ($notification->getObjectType() === 'core') { |
127
|
|
|
$this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions()); |
128
|
|
|
|
129
|
|
|
$parameters = $notification->getSubjectParameters(); |
130
|
|
|
$notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']])); |
131
|
|
|
|
132
|
|
|
if ($this->isAdmin()) { |
133
|
|
|
$notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version'); |
134
|
|
|
} |
135
|
|
|
} else { |
136
|
|
|
$appInfo = $this->getAppInfo($notification->getObjectType()); |
137
|
|
|
$appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name']; |
138
|
|
|
|
139
|
|
|
if (isset($this->appVersions[$notification->getObjectType()])) { |
140
|
|
|
$this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()])) |
144
|
|
|
->setRichSubject($l->t('Update for {app} to version %s is available.', [$notification->getObjectId()]), [ |
145
|
|
|
'app' => [ |
146
|
|
|
'type' => 'app', |
147
|
|
|
'id' => $notification->getObjectType(), |
148
|
|
|
'name' => $appName, |
149
|
|
|
] |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
if ($this->isAdmin()) { |
153
|
|
|
$notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg'))); |
158
|
|
|
|
159
|
|
|
return $notification; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Remove the notification and prevent rendering, when the update is installed |
164
|
|
|
* |
165
|
|
|
* @param INotification $notification |
166
|
|
|
* @param string $installedVersion |
167
|
|
|
* @throws AlreadyProcessedException When the update is already installed |
168
|
|
|
*/ |
169
|
|
|
protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) { |
170
|
|
|
if (version_compare($notification->getObjectId(), $installedVersion, '<=')) { |
171
|
|
|
throw new AlreadyProcessedException(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
protected function isAdmin(): bool { |
179
|
|
|
$user = $this->userSession->getUser(); |
180
|
|
|
|
181
|
|
|
if ($user instanceof IUser) { |
182
|
|
|
return $this->groupManager->isAdmin($user->getUID()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function getCoreVersions(): string { |
189
|
|
|
return implode('.', Util::getVersion()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function getAppVersions(): array { |
193
|
|
|
return \OC_App::getAppVersions(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
protected function getAppInfo($appId) { |
197
|
|
|
return \OC_App::getAppInfo($appId); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.