|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) 2016, Joas Schilling <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author Joas Schilling <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
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 |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\AnnouncementCenter; |
|
26
|
|
|
|
|
27
|
|
|
use OCA\AnnouncementCenter\Model\Announcement; |
|
28
|
|
|
use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException; |
|
29
|
|
|
use OCA\AnnouncementCenter\Model\AnnouncementMapper; |
|
30
|
|
|
use OCA\AnnouncementCenter\Model\Group; |
|
31
|
|
|
use OCA\AnnouncementCenter\Model\GroupMapper; |
|
32
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
33
|
|
|
use OCP\BackgroundJob\IJobList; |
|
34
|
|
|
use OCP\Comments\ICommentsManager; |
|
35
|
|
|
use OCP\IConfig; |
|
36
|
|
|
use OCP\IDBConnection; |
|
37
|
|
|
use OCP\IGroupManager; |
|
38
|
|
|
use OCP\Notification\IManager as INotificationManager; |
|
39
|
|
|
use OCP\IUser; |
|
40
|
|
|
use OCP\IUserSession; |
|
41
|
|
|
|
|
42
|
|
|
class Manager { |
|
43
|
|
|
|
|
44
|
|
|
/** @var IConfig */ |
|
45
|
|
|
protected $config; |
|
46
|
|
|
|
|
47
|
|
|
/** @var AnnouncementMapper */ |
|
48
|
|
|
protected $announcementMapper; |
|
49
|
|
|
|
|
50
|
|
|
/** @var GroupMapper */ |
|
51
|
|
|
protected $groupMapper; |
|
52
|
|
|
|
|
53
|
|
|
/** @var IGroupManager */ |
|
54
|
|
|
protected $groupManager; |
|
55
|
|
|
|
|
56
|
|
|
/** @var INotificationManager */ |
|
57
|
|
|
protected $notificationManager; |
|
58
|
|
|
|
|
59
|
|
|
/** @var ICommentsManager */ |
|
60
|
|
|
protected $commentsManager; |
|
61
|
|
|
|
|
62
|
|
|
/** @var IJobList */ |
|
63
|
|
|
protected $jobList; |
|
64
|
|
|
|
|
65
|
|
|
/** @var IUserSession */ |
|
66
|
|
|
protected $userSession; |
|
67
|
|
|
|
|
68
|
19 |
|
public function __construct(IConfig $config, |
|
69
|
|
|
AnnouncementMapper $announcementMapper, |
|
70
|
|
|
GroupMapper $groupMapper, |
|
71
|
|
|
IGroupManager $groupManager, |
|
72
|
|
|
INotificationManager $notificationManager, |
|
73
|
|
|
ICommentsManager $commentsManager, |
|
74
|
|
|
IJobList $jobList, |
|
75
|
|
|
IUserSession $userSession) { |
|
76
|
19 |
|
$this->config = $config; |
|
77
|
19 |
|
$this->announcementMapper = $announcementMapper; |
|
78
|
19 |
|
$this->groupMapper = $groupMapper; |
|
79
|
19 |
|
$this->groupManager = $groupManager; |
|
80
|
19 |
|
$this->notificationManager = $notificationManager; |
|
81
|
19 |
|
$this->commentsManager = $commentsManager; |
|
82
|
19 |
|
$this->jobList = $jobList; |
|
83
|
19 |
|
$this->userSession = $userSession; |
|
84
|
19 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $subject |
|
88
|
|
|
* @param string $message |
|
89
|
|
|
* @param string $user |
|
90
|
|
|
* @param int $time |
|
91
|
|
|
* @param string[] $groups |
|
92
|
|
|
* @param bool $comments |
|
93
|
|
|
* @return Announcement |
|
94
|
|
|
* @throws \InvalidArgumentException when the subject is empty or invalid |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function announce(string $subject, string $message, string $user, int $time, array $groups, bool $comments): Announcement { |
|
97
|
2 |
|
$subject = trim($subject); |
|
98
|
2 |
|
$message = trim($message); |
|
99
|
2 |
|
if (isset($subject[512])) { |
|
100
|
1 |
|
throw new \InvalidArgumentException('Invalid subject', 1); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
if ($subject === '') { |
|
104
|
1 |
|
throw new \InvalidArgumentException('Invalid subject', 2); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$announcement = new Announcement(); |
|
108
|
|
|
$announcement->setSubject($subject); |
|
109
|
|
|
$announcement->setMessage($message); |
|
110
|
|
|
$announcement->setUser($user); |
|
111
|
|
|
$announcement->setTime($time); |
|
112
|
|
|
$announcement->setAllowComments((int) $comments); |
|
113
|
|
|
$this->announcementMapper->insert($announcement); |
|
114
|
|
|
|
|
115
|
|
|
$addedGroups = 0; |
|
116
|
|
|
foreach ($groups as $group) { |
|
117
|
|
|
if ($this->groupManager->groupExists($group)) { |
|
118
|
|
|
$this->addGroupLink($announcement, $group); |
|
119
|
|
|
$addedGroups++; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ($addedGroups === 0) { |
|
124
|
|
|
$this->addGroupLink($announcement, 'everyone'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $announcement; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
protected function addGroupLink(Announcement $announcement, string $gid) { |
|
131
|
|
|
$group = new Group(); |
|
132
|
|
|
$group->setId($announcement->getId()); |
|
133
|
|
|
$group->setGroup($gid); |
|
134
|
|
|
$this->groupMapper->insert($group); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
public function delete(int $id) { |
|
138
|
|
|
// Delete notifications |
|
139
|
1 |
|
$notification = $this->notificationManager->createNotification(); |
|
140
|
1 |
|
$notification->setApp('announcementcenter') |
|
141
|
1 |
|
->setObject('announcement', (string)$id); |
|
142
|
1 |
|
$this->notificationManager->markProcessed($notification); |
|
143
|
|
|
|
|
144
|
|
|
// Delete comments |
|
145
|
1 |
|
$this->commentsManager->deleteCommentsAtObject('announcement', (string) $id); |
|
146
|
|
|
|
|
147
|
1 |
|
$announcement = $this->announcementMapper->getById($id); |
|
148
|
1 |
|
$this->announcementMapper->delete($announcement); |
|
149
|
1 |
|
$this->groupMapper->deleteGroupsForAnnouncement($announcement); |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param int $id |
|
154
|
|
|
* @param bool $ignorePermissions Permissions are ignored e.g. in background jobs to generate activities etc. |
|
155
|
|
|
* @return Announcement |
|
156
|
|
|
* @throws AnnouncementDoesNotExistException |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function getAnnouncement(int $id, bool $ignorePermissions = false): Announcement { |
|
159
|
|
|
try { |
|
160
|
1 |
|
$announcement = $this->announcementMapper->getById($id); |
|
161
|
1 |
|
} catch (DoesNotExistException $e) { |
|
|
|
|
|
|
162
|
1 |
|
throw new AnnouncementDoesNotExistException(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if ($ignorePermissions) { |
|
166
|
|
|
return $announcement; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$userGroups = $this->getUserGroups(); |
|
170
|
|
|
$memberOfAdminGroups = array_intersect($this->getAdminGroups(), $userGroups); |
|
171
|
|
|
if (!empty($memberOfAdminGroups)) { |
|
172
|
|
|
return $announcement; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$groups = $this->groupMapper->getGroupsForAnnouncement($announcement); |
|
176
|
|
|
$memberOfGroups = array_intersect($groups, $userGroups); |
|
177
|
|
|
|
|
178
|
|
|
if (empty($memberOfGroups)) { |
|
179
|
|
|
throw new AnnouncementDoesNotExistException(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $announcement; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
protected function getUserGroups(): array { |
|
186
|
|
|
$user = $this->userSession->getUser(); |
|
187
|
|
|
if ($user instanceof IUser) { |
|
|
|
|
|
|
188
|
|
|
$userGroups = $this->groupManager->getUserGroupIds($user); |
|
189
|
|
|
$userGroups[] = 'everyone'; |
|
190
|
|
|
} else { |
|
191
|
|
|
$userGroups = ['everyone']; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $userGroups; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param Announcement $announcement |
|
199
|
|
|
* @return string[] |
|
200
|
|
|
*/ |
|
201
|
2 |
|
public function getGroups(Announcement $announcement): array { |
|
202
|
2 |
|
return $this->groupMapper->getGroupsForAnnouncement($announcement); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param int $offsetId |
|
207
|
|
|
* @return Announcement[] |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getAnnouncements(int $offsetId = 0): array { |
|
210
|
|
|
|
|
211
|
|
|
$userGroups = $this->getUserGroups(); |
|
212
|
|
|
$memberOfAdminGroups = array_intersect($this->getAdminGroups(), $userGroups); |
|
213
|
|
|
if (!empty($memberOfAdminGroups)) { |
|
214
|
|
|
$userGroups = []; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$announcements = $this->announcementMapper->getAnnouncements($userGroups, $offsetId); |
|
218
|
|
|
|
|
219
|
|
|
return $announcements; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function markNotificationRead(int $id): void { |
|
223
|
|
|
$user = $this->userSession->getUser(); |
|
224
|
|
|
|
|
225
|
|
|
if ($user instanceof IUser) { |
|
|
|
|
|
|
226
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
227
|
|
|
$notification->setApp('announcementcenter') |
|
228
|
|
|
->setUser($user->getUID()) |
|
229
|
|
|
->setObject('announcement', (string)$id); |
|
230
|
|
|
$this->notificationManager->markProcessed($notification); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function getNumberOfComments(Announcement $announcement): int { |
|
235
|
|
|
return $this->commentsManager->getNumberOfCommentsForObject('announcement', (string) $announcement->getId()); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
4 |
|
public function hasNotifications(Announcement $announcement): bool { |
|
239
|
4 |
|
$hasJob = $this->jobList->has(BackgroundJob::class, [ |
|
240
|
4 |
|
'id' => $announcement->getId(), |
|
241
|
|
|
'activities' => true, |
|
242
|
|
|
'notifications' => true, |
|
243
|
|
|
]); |
|
244
|
|
|
|
|
245
|
4 |
|
$hasJob = $hasJob || $this->jobList->has(BackgroundJob::class, [ |
|
246
|
4 |
|
'id' => $announcement->getId(), |
|
247
|
|
|
'activities' => false, |
|
248
|
|
|
'notifications' => true, |
|
249
|
|
|
]); |
|
250
|
|
|
|
|
251
|
4 |
|
if ($hasJob) { |
|
252
|
2 |
|
return true; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
2 |
|
$notification = $this->notificationManager->createNotification(); |
|
256
|
2 |
|
$notification->setApp('announcementcenter') |
|
257
|
2 |
|
->setObject('announcement', (string)$announcement->getId()); |
|
258
|
2 |
|
return $this->notificationManager->getCount($notification) > 0; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
2 |
|
public function removeNotifications(int $id): void { |
|
262
|
2 |
|
if ($this->jobList->has(BackgroundJob::class, [ |
|
263
|
2 |
|
'id' => $id, |
|
264
|
|
|
'activities' => true, |
|
265
|
|
|
'notifications' => true, |
|
266
|
|
|
])) { |
|
267
|
|
|
// Delete the current background job and add a new one without notifications |
|
268
|
1 |
|
$this->jobList->remove(BackgroundJob::class, [ |
|
269
|
1 |
|
'id' => $id, |
|
270
|
|
|
'activities' => true, |
|
271
|
|
|
'notifications' => true, |
|
272
|
|
|
]); |
|
273
|
1 |
|
$this->jobList->add(BackgroundJob::class, [ |
|
274
|
1 |
|
'id' => $id, |
|
275
|
|
|
'activities' => true, |
|
276
|
|
|
'notifications' => false, |
|
277
|
|
|
]); |
|
278
|
|
|
|
|
279
|
|
|
} else { |
|
280
|
1 |
|
$this->jobList->remove(BackgroundJob::class, [ |
|
281
|
1 |
|
'id' => $id, |
|
282
|
|
|
'activities' => false, |
|
283
|
|
|
'notifications' => true, |
|
284
|
|
|
]); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
2 |
|
$notification = $this->notificationManager->createNotification(); |
|
288
|
2 |
|
$notification->setApp('announcementcenter') |
|
289
|
2 |
|
->setObject('announcement', (string)$id); |
|
290
|
2 |
|
$this->notificationManager->markProcessed($notification); |
|
291
|
2 |
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Check if the user is in the admin group |
|
295
|
|
|
*/ |
|
296
|
6 |
|
public function checkIsAdmin(): bool { |
|
297
|
6 |
|
$user = $this->userSession->getUser(); |
|
298
|
|
|
|
|
299
|
6 |
|
if ($user instanceof IUser) { |
|
|
|
|
|
|
300
|
5 |
|
$groups = $this->getAdminGroups(); |
|
301
|
5 |
|
foreach ($groups as $group) { |
|
302
|
5 |
|
if ($this->groupManager->isInGroup($user->getUID(), $group)) { |
|
303
|
3 |
|
return true; |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
3 |
|
return false; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @return string[] |
|
313
|
|
|
*/ |
|
314
|
5 |
|
protected function getAdminGroups(): array { |
|
315
|
5 |
|
$adminGroups = $this->config->getAppValue('announcementcenter', 'admin_groups', '["admin"]'); |
|
316
|
5 |
|
$adminGroups = json_decode($adminGroups, true); |
|
317
|
5 |
|
return $adminGroups; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
Scrutinizer analyzes your
composer.json/composer.lockfile if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.