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; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
26
|
|
|
use OCP\IDBConnection; |
27
|
|
|
use OCP\Notification\IAction; |
28
|
|
|
use OCP\Notification\IManager; |
29
|
|
|
use OCP\Notification\INotification; |
30
|
|
|
|
31
|
|
|
class Handler { |
32
|
|
|
/** @var IDBConnection */ |
33
|
|
|
protected $connection; |
34
|
|
|
|
35
|
|
|
/** @var IManager */ |
36
|
|
|
protected $manager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param IDBConnection $connection |
40
|
|
|
* @param IManager $manager |
41
|
|
|
*/ |
42
|
8 |
|
public function __construct(IDBConnection $connection, IManager $manager) { |
43
|
8 |
|
$this->connection = $connection; |
44
|
8 |
|
$this->manager = $manager; |
45
|
8 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add a new notification to the database |
49
|
|
|
* |
50
|
|
|
* @param INotification $notification |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
2 |
|
public function add(INotification $notification) { |
54
|
2 |
|
$sql = $this->connection->getQueryBuilder(); |
55
|
2 |
|
$sql->insert('notifications'); |
56
|
2 |
|
$this->sqlInsert($sql, $notification); |
57
|
2 |
|
$sql->execute(); |
58
|
|
|
|
59
|
2 |
|
return $sql->getLastInsertId(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Count the notifications matching the given Notification |
64
|
|
|
* |
65
|
|
|
* @param INotification $notification |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
2 |
|
public function count(INotification $notification) { |
69
|
2 |
|
$sql = $this->connection->getQueryBuilder(); |
70
|
2 |
|
$sql->select($sql->createFunction('COUNT(*)')) |
71
|
2 |
|
->from('notifications'); |
72
|
|
|
|
73
|
2 |
|
$this->sqlWhere($sql, $notification); |
74
|
|
|
|
75
|
2 |
|
$statement = $sql->execute(); |
76
|
2 |
|
$count = (int) $statement->fetchColumn(); |
77
|
2 |
|
$statement->closeCursor(); |
78
|
|
|
|
79
|
2 |
|
return $count; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Delete the notifications matching the given Notification |
84
|
|
|
* |
85
|
|
|
* @param INotification $notification |
86
|
|
|
*/ |
87
|
2 |
|
public function delete(INotification $notification) { |
88
|
2 |
|
$sql = $this->connection->getQueryBuilder(); |
89
|
2 |
|
$sql->delete('notifications'); |
90
|
2 |
|
$this->sqlWhere($sql, $notification); |
91
|
2 |
|
$sql->execute(); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Delete the notification of a given user |
96
|
|
|
* |
97
|
|
|
* @param string $user |
98
|
|
|
*/ |
99
|
|
|
public function deleteByUser(string $user) { |
100
|
|
|
$notification = $this->manager->createNotification(); |
101
|
|
|
try { |
102
|
|
|
$notification->setUser($user); |
103
|
|
|
} catch (\InvalidArgumentException $e) { |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
$this->delete($notification); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Delete the notification matching the given id |
111
|
|
|
* |
112
|
|
|
* @param int $id |
113
|
|
|
* @param string $user |
114
|
|
|
*/ |
115
|
1 |
|
public function deleteById($id, $user) { |
116
|
1 |
|
$sql = $this->connection->getQueryBuilder(); |
117
|
1 |
|
$sql->delete('notifications') |
118
|
1 |
|
->where($sql->expr()->eq('notification_id', $sql->createParameter('id'))) |
119
|
1 |
|
->setParameter('id', $id) |
120
|
1 |
|
->andWhere($sql->expr()->eq('user', $sql->createParameter('user'))) |
121
|
1 |
|
->setParameter('user', $user); |
122
|
1 |
|
$sql->execute(); |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the notification matching the given id |
127
|
|
|
* |
128
|
|
|
* @param int $id |
129
|
|
|
* @param string $user |
130
|
|
|
* @return null|INotification |
131
|
|
|
*/ |
132
|
1 |
|
public function getById($id, $user) { |
133
|
1 |
|
$sql = $this->connection->getQueryBuilder(); |
134
|
1 |
|
$sql->select('*') |
135
|
1 |
|
->from('notifications') |
136
|
1 |
|
->where($sql->expr()->eq('notification_id', $sql->createParameter('id'))) |
137
|
1 |
|
->setParameter('id', $id) |
138
|
1 |
|
->andWhere($sql->expr()->eq('user', $sql->createParameter('user'))) |
139
|
1 |
|
->setParameter('user', $user); |
140
|
1 |
|
$statement = $sql->execute(); |
141
|
|
|
|
142
|
1 |
|
$notification = null; |
143
|
1 |
|
if ($row = $statement->fetch()) { |
144
|
1 |
|
$notification = $this->notificationFromRow($row); |
145
|
|
|
} |
146
|
1 |
|
$statement->closeCursor(); |
147
|
|
|
|
148
|
1 |
|
return $notification; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return the notifications matching the given Notification |
153
|
|
|
* |
154
|
|
|
* @param INotification $notification |
155
|
|
|
* @param int $limit |
156
|
|
|
* @return array [notification_id => INotification] |
157
|
|
|
*/ |
158
|
2 |
|
public function get(INotification $notification, $limit = 25) { |
159
|
2 |
|
$sql = $this->connection->getQueryBuilder(); |
160
|
2 |
|
$sql->select('*') |
161
|
2 |
|
->from('notifications') |
162
|
2 |
|
->orderBy('notification_id', 'DESC') |
163
|
2 |
|
->setMaxResults($limit); |
164
|
|
|
|
165
|
2 |
|
$this->sqlWhere($sql, $notification); |
166
|
2 |
|
$statement = $sql->execute(); |
167
|
|
|
|
168
|
2 |
|
$notifications = []; |
169
|
2 |
|
while ($row = $statement->fetch()) { |
170
|
2 |
|
$notifications[(int) $row['notification_id']] = $this->notificationFromRow($row); |
171
|
|
|
} |
172
|
2 |
|
$statement->closeCursor(); |
173
|
|
|
|
174
|
2 |
|
return $notifications; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Add where statements to a query builder matching the given notification |
179
|
|
|
* |
180
|
|
|
* @param IQueryBuilder $sql |
181
|
|
|
* @param INotification $notification |
182
|
|
|
*/ |
183
|
2 |
|
protected function sqlWhere(IQueryBuilder $sql, INotification $notification) { |
184
|
2 |
|
if ($notification->getApp() !== '') { |
185
|
2 |
|
$sql->andWhere($sql->expr()->eq('app', $sql->createParameter('app'))); |
186
|
2 |
|
$sql->setParameter('app', $notification->getApp()); |
187
|
|
|
} |
188
|
|
|
|
189
|
2 |
|
if ($notification->getUser() !== '') { |
190
|
2 |
|
$sql->andWhere($sql->expr()->eq('user', $sql->createParameter('user'))) |
191
|
2 |
|
->setParameter('user', $notification->getUser()); |
192
|
|
|
} |
193
|
|
|
|
194
|
2 |
|
if ($notification->getDateTime()->getTimestamp() !== 0) { |
195
|
1 |
|
$sql->andWhere($sql->expr()->eq('timestamp', $sql->createParameter('timestamp'))) |
196
|
1 |
|
->setParameter('timestamp', $notification->getDateTime()->getTimestamp()); |
197
|
|
|
} |
198
|
|
|
|
199
|
2 |
|
if ($notification->getObjectType() !== '') { |
200
|
1 |
|
$sql->andWhere($sql->expr()->eq('object_type', $sql->createParameter('objectType'))) |
201
|
1 |
|
->setParameter('objectType', $notification->getObjectType()); |
202
|
|
|
} |
203
|
|
|
|
204
|
2 |
|
if ($notification->getObjectId() !== '') { |
205
|
1 |
|
$sql->andWhere($sql->expr()->eq('object_id', $sql->createParameter('objectId'))) |
206
|
1 |
|
->setParameter('objectId', $notification->getObjectId()); |
207
|
|
|
} |
208
|
|
|
|
209
|
2 |
|
if ($notification->getSubject() !== '') { |
210
|
1 |
|
$sql->andWhere($sql->expr()->eq('subject', $sql->createParameter('subject'))) |
211
|
1 |
|
->setParameter('subject', $notification->getSubject()); |
212
|
|
|
} |
213
|
|
|
|
214
|
2 |
|
if ($notification->getMessage() !== '') { |
215
|
1 |
|
$sql->andWhere($sql->expr()->eq('message', $sql->createParameter('message'))) |
216
|
1 |
|
->setParameter('message', $notification->getMessage()); |
217
|
|
|
} |
218
|
|
|
|
219
|
2 |
|
if ($notification->getLink() !== '') { |
220
|
1 |
|
$sql->andWhere($sql->expr()->eq('link', $sql->createParameter('link'))) |
221
|
1 |
|
->setParameter('link', $notification->getLink()); |
222
|
|
|
} |
223
|
|
|
|
224
|
2 |
|
if (method_exists($notification, 'getIcon') && $notification->getIcon() !== '') { |
225
|
1 |
|
$sql->andWhere($sql->expr()->eq('icon', $sql->createParameter('icon'))) |
226
|
1 |
|
->setParameter('icon', $notification->getIcon()); |
227
|
|
|
} |
228
|
2 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Turn a notification into an input statement |
232
|
|
|
* |
233
|
|
|
* @param IQueryBuilder $sql |
234
|
|
|
* @param INotification $notification |
235
|
|
|
*/ |
236
|
2 |
|
protected function sqlInsert(IQueryBuilder $sql, INotification $notification) { |
237
|
2 |
|
$sql->setValue('app', $sql->createParameter('app')) |
238
|
2 |
|
->setParameter('app', $notification->getApp()); |
239
|
|
|
|
240
|
2 |
|
$sql->setValue('user', $sql->createParameter('user')) |
241
|
2 |
|
->setParameter('user', $notification->getUser()); |
242
|
|
|
|
243
|
2 |
|
$sql->setValue('timestamp', $sql->createParameter('timestamp')) |
244
|
2 |
|
->setParameter('timestamp', $notification->getDateTime()->getTimestamp()); |
245
|
|
|
|
246
|
2 |
|
$sql->setValue('object_type', $sql->createParameter('objectType')) |
247
|
2 |
|
->setParameter('objectType', $notification->getObjectType()); |
248
|
|
|
|
249
|
2 |
|
$sql->setValue('object_id', $sql->createParameter('objectId')) |
250
|
2 |
|
->setParameter('objectId', $notification->getObjectId()); |
251
|
|
|
|
252
|
2 |
|
$sql->setValue('subject', $sql->createParameter('subject')) |
253
|
2 |
|
->setParameter('subject', $notification->getSubject()); |
254
|
|
|
|
255
|
2 |
|
$sql->setValue('subject_parameters', $sql->createParameter('subject_parameters')) |
256
|
2 |
|
->setParameter('subject_parameters', json_encode($notification->getSubjectParameters())); |
257
|
|
|
|
258
|
2 |
|
$sql->setValue('message', $sql->createParameter('message')) |
259
|
2 |
|
->setParameter('message', $notification->getMessage()); |
260
|
|
|
|
261
|
2 |
|
$sql->setValue('message_parameters', $sql->createParameter('message_parameters')) |
262
|
2 |
|
->setParameter('message_parameters', json_encode($notification->getMessageParameters())); |
263
|
|
|
|
264
|
2 |
|
$sql->setValue('link', $sql->createParameter('link')) |
265
|
2 |
|
->setParameter('link', $notification->getLink()); |
266
|
|
|
|
267
|
2 |
|
if (method_exists($notification, 'getIcon')) { |
268
|
2 |
|
$sql->setValue('icon', $sql->createParameter('icon')) |
269
|
2 |
|
->setParameter('icon', $notification->getIcon()); |
270
|
|
|
} else { |
271
|
|
|
$sql->setValue('icon', $sql->createParameter('icon')) |
272
|
|
|
->setParameter('icon', ''); |
273
|
|
|
} |
274
|
|
|
|
275
|
2 |
|
$actions = []; |
276
|
2 |
|
foreach ($notification->getActions() as $action) { |
277
|
|
|
/** @var IAction $action */ |
278
|
2 |
|
$actions[] = [ |
279
|
2 |
|
'label' => $action->getLabel(), |
280
|
2 |
|
'link' => $action->getLink(), |
281
|
2 |
|
'type' => $action->getRequestType(), |
282
|
2 |
|
'primary' => $action->isPrimary(), |
283
|
|
|
]; |
284
|
|
|
} |
285
|
2 |
|
$sql->setValue('actions', $sql->createParameter('actions')) |
286
|
2 |
|
->setParameter('actions', json_encode($actions)); |
287
|
2 |
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Turn a database row into a INotification |
291
|
|
|
* |
292
|
|
|
* @param array $row |
293
|
|
|
* @return INotification |
294
|
|
|
*/ |
295
|
2 |
|
protected function notificationFromRow(array $row) { |
296
|
2 |
|
$dateTime = new \DateTime(); |
297
|
2 |
|
$dateTime->setTimestamp((int) $row['timestamp']); |
298
|
|
|
|
299
|
2 |
|
$notification = $this->manager->createNotification(); |
300
|
2 |
|
$notification->setApp($row['app']) |
301
|
2 |
|
->setUser($row['user']) |
302
|
2 |
|
->setDateTime($dateTime) |
303
|
2 |
|
->setObject($row['object_type'], $row['object_id']) |
304
|
2 |
|
->setSubject($row['subject'], (array) json_decode($row['subject_parameters'], true)); |
305
|
|
|
|
306
|
2 |
|
if ($row['message'] !== '') { |
307
|
2 |
|
$notification->setMessage($row['message'], (array) json_decode($row['message_parameters'], true)); |
308
|
|
|
} |
309
|
2 |
|
if ($row['link'] !== '' && $row['link'] !== null) { |
310
|
2 |
|
$notification->setLink($row['link']); |
311
|
|
|
} |
312
|
2 |
|
if ($row['icon'] !== '' && $row['icon'] !== null) { |
313
|
2 |
|
$notification->setIcon($row['icon']); |
314
|
|
|
} |
315
|
|
|
|
316
|
2 |
|
$actions = (array) json_decode($row['actions'], true); |
317
|
2 |
|
foreach ($actions as $actionData) { |
318
|
2 |
|
$action = $notification->createAction(); |
319
|
2 |
|
$action->setLabel($actionData['label']) |
320
|
2 |
|
->setLink($actionData['link'], $actionData['type']); |
321
|
2 |
|
if (isset($actionData['primary'])) { |
322
|
2 |
|
$action->setPrimary($actionData['primary']); |
323
|
|
|
} |
324
|
2 |
|
$notification->addAction($action); |
325
|
|
|
} |
326
|
|
|
|
327
|
2 |
|
return $notification; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|