1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Joas Schilling <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Notifications\Controller; |
23
|
|
|
|
24
|
|
|
use OCA\Notifications\Exceptions\NotificationNotFoundException; |
25
|
|
|
use OCA\Notifications\Handler; |
26
|
|
|
use OCP\AppFramework\Http; |
27
|
|
|
use OCP\AppFramework\Http\DataResponse; |
28
|
|
|
use OCP\AppFramework\OCSController; |
29
|
|
|
use OCP\IConfig; |
30
|
|
|
use OCP\IRequest; |
31
|
|
|
use OCP\IUser; |
32
|
|
|
use OCP\IUserSession; |
33
|
|
|
use OCP\Notification\IAction; |
34
|
|
|
use OCP\Notification\IManager; |
35
|
|
|
use OCP\Notification\INotification; |
36
|
|
|
|
37
|
|
|
class EndpointController extends OCSController { |
38
|
|
|
/** @var Handler */ |
39
|
|
|
private $handler; |
40
|
|
|
|
41
|
|
|
/** @var IManager */ |
42
|
|
|
private $manager; |
43
|
|
|
|
44
|
|
|
/** @var IUserSession */ |
45
|
|
|
private $session; |
46
|
|
|
|
47
|
|
|
/** @var IConfig */ |
48
|
|
|
private $config; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $appName |
52
|
|
|
* @param IRequest $request |
53
|
|
|
* @param Handler $handler |
54
|
|
|
* @param IManager $manager |
55
|
|
|
* @param IConfig $config |
56
|
|
|
* @param IUserSession $session |
57
|
|
|
*/ |
58
|
25 |
|
public function __construct($appName, IRequest $request, Handler $handler, IManager $manager, IConfig $config, IUserSession $session) { |
59
|
25 |
|
parent::__construct($appName, $request); |
60
|
|
|
|
61
|
25 |
|
$this->handler = $handler; |
62
|
25 |
|
$this->manager = $manager; |
63
|
25 |
|
$this->config = $config; |
64
|
25 |
|
$this->session = $session; |
65
|
25 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @NoAdminRequired |
69
|
|
|
* @NoCSRFRequired |
70
|
|
|
* |
71
|
|
|
* @param string $apiVersion |
72
|
|
|
* @return DataResponse |
73
|
|
|
*/ |
74
|
6 |
|
public function listNotifications(string $apiVersion): DataResponse { |
75
|
|
|
// When there are no apps registered that use the notifications |
76
|
|
|
// We stop polling for them. |
77
|
6 |
|
if (!$this->manager->hasNotifiers()) { |
78
|
2 |
|
return new DataResponse(null, Http::STATUS_NO_CONTENT); |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
$filter = $this->manager->createNotification(); |
82
|
4 |
|
$filter->setUser($this->getCurrentUser()); |
83
|
4 |
|
$language = $this->config->getUserValue($this->getCurrentUser(), 'core', 'lang', null); |
84
|
|
|
|
85
|
4 |
|
$notifications = $this->handler->get($filter); |
86
|
|
|
|
87
|
4 |
|
$data = []; |
88
|
4 |
|
$notificationIds = []; |
89
|
4 |
|
foreach ($notifications as $notificationId => $notification) { |
90
|
|
|
/** @var INotification $notification */ |
91
|
|
|
try { |
92
|
3 |
|
$notification = $this->manager->prepare($notification, $language); |
93
|
1 |
|
} catch (\InvalidArgumentException $e) { |
94
|
|
|
// The app was disabled, skip the notification |
95
|
1 |
|
continue; |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
$notificationIds[] = $notificationId; |
99
|
3 |
|
$data[] = $this->notificationToArray($notificationId, $notification, $apiVersion); |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
$eTag = $this->generateETag($notificationIds); |
103
|
4 |
|
if ($apiVersion !== 'v1' && $this->request->getHeader('If-None-Match') === $eTag) { |
104
|
|
|
return new DataResponse([], Http::STATUS_NOT_MODIFIED); |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
return new DataResponse($data, Http::STATUS_OK, ['ETag' => $eTag]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @NoAdminRequired |
112
|
|
|
* @NoCSRFRequired |
113
|
|
|
* |
114
|
|
|
* @param string $apiVersion |
115
|
|
|
* @param int $id |
116
|
|
|
* @return DataResponse |
117
|
|
|
*/ |
118
|
6 |
|
public function getNotification(string $apiVersion, int $id): DataResponse { |
119
|
6 |
|
if (!$this->manager->hasNotifiers()) { |
120
|
1 |
|
return new DataResponse(null, Http::STATUS_NOT_FOUND); |
121
|
|
|
} |
122
|
|
|
|
123
|
5 |
|
if ($id === 0) { |
124
|
|
|
return new DataResponse(null, Http::STATUS_NOT_FOUND); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
try { |
128
|
5 |
|
$notification = $this->handler->getById($id, $this->getCurrentUser()); |
129
|
1 |
|
} catch (NotificationNotFoundException $e) { |
130
|
1 |
|
return new DataResponse(null, Http::STATUS_NOT_FOUND); |
131
|
|
|
} |
132
|
|
|
|
133
|
4 |
|
$language = $this->config->getUserValue($this->getCurrentUser(), 'core', 'lang', null); |
134
|
|
|
|
135
|
|
|
try { |
136
|
4 |
|
$notification = $this->manager->prepare($notification, $language); |
137
|
2 |
|
} catch (\InvalidArgumentException $e) { |
138
|
|
|
// The app was disabled |
139
|
2 |
|
return new DataResponse(null, Http::STATUS_NOT_FOUND); |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
return new DataResponse($this->notificationToArray($id, $notification, $apiVersion)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @NoAdminRequired |
147
|
|
|
* |
148
|
|
|
* @param int $id |
149
|
|
|
* @return DataResponse |
150
|
|
|
*/ |
151
|
3 |
|
public function deleteNotification(int $id): DataResponse { |
152
|
3 |
|
if ($id === 0) { |
153
|
1 |
|
return new DataResponse(null, Http::STATUS_NOT_FOUND); |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
$this->handler->deleteById($id, $this->getCurrentUser()); |
157
|
2 |
|
return new DataResponse(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @NoAdminRequired |
162
|
|
|
* |
163
|
|
|
* @return DataResponse |
164
|
|
|
*/ |
165
|
2 |
|
public function deleteAllNotifications(): DataResponse { |
166
|
2 |
|
$this->handler->deleteByUser($this->getCurrentUser()); |
167
|
2 |
|
return new DataResponse(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get an ETag for the notification ids |
172
|
|
|
* |
173
|
|
|
* @param array $notifications |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
4 |
|
protected function generateETag(array $notifications): string { |
177
|
4 |
|
return md5(json_encode($notifications)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param int $notificationId |
182
|
|
|
* @param INotification $notification |
183
|
|
|
* @param string $apiVersion |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
4 |
|
protected function notificationToArray(int $notificationId, INotification $notification, string $apiVersion): array { |
187
|
|
|
$data = [ |
188
|
4 |
|
'notification_id' => $notificationId, |
189
|
4 |
|
'app' => $notification->getApp(), |
190
|
4 |
|
'user' => $notification->getUser(), |
191
|
4 |
|
'datetime' => $notification->getDateTime()->format('c'), |
192
|
4 |
|
'object_type' => $notification->getObjectType(), |
193
|
4 |
|
'object_id' => $notification->getObjectId(), |
194
|
4 |
|
'subject' => $notification->getParsedSubject(), |
195
|
4 |
|
'message' => $notification->getParsedMessage(), |
196
|
4 |
|
'link' => $notification->getLink(), |
197
|
|
|
]; |
198
|
|
|
|
199
|
4 |
|
if ($apiVersion !== 'v1') { |
200
|
2 |
|
$data = array_merge($data, [ |
201
|
2 |
|
'subjectRich' => $notification->getRichSubject(), |
202
|
2 |
|
'subjectRichParameters' => $notification->getRichSubjectParameters(), |
203
|
2 |
|
'messageRich' => $notification->getRichMessage(), |
204
|
2 |
|
'messageRichParameters' => $notification->getRichMessageParameters(), |
205
|
2 |
|
'icon' => $notification->getIcon(), |
206
|
|
|
]); |
207
|
|
|
} |
208
|
|
|
|
209
|
4 |
|
$data['actions'] = []; |
210
|
4 |
|
foreach ($notification->getParsedActions() as $action) { |
211
|
2 |
|
$data['actions'][] = $this->actionToArray($action); |
212
|
|
|
} |
213
|
|
|
|
214
|
4 |
|
return $data; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param IAction $action |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
2 |
|
protected function actionToArray(IAction $action): array { |
222
|
|
|
return [ |
223
|
2 |
|
'label' => $action->getParsedLabel(), |
224
|
2 |
|
'link' => $action->getLink(), |
225
|
2 |
|
'type' => $action->getRequestType(), |
226
|
2 |
|
'primary' => $action->isPrimary(), |
227
|
|
|
]; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
13 |
|
protected function getCurrentUser(): string { |
234
|
13 |
|
$user = $this->session->getUser(); |
235
|
13 |
|
if ($user instanceof IUser) { |
|
|
|
|
236
|
13 |
|
$user = $user->getUID(); |
237
|
|
|
} |
238
|
|
|
|
239
|
13 |
|
return (string) $user; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.