|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Joas Schilling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\AnnouncementCenter; |
|
25
|
|
|
|
|
26
|
|
|
use OC\BackgroundJob\QueuedJob; |
|
27
|
|
|
use OCP\Activity\IEvent; |
|
28
|
|
|
use OCP\Activity\IManager; |
|
29
|
|
|
use OCP\IGroup; |
|
30
|
|
|
use OCP\IGroupManager; |
|
31
|
|
|
use OCP\IURLGenerator; |
|
32
|
|
|
use OCP\IUser; |
|
33
|
|
|
use OCP\IUserManager; |
|
34
|
|
|
use OCP\Notification\IManager as INotificationManager; |
|
35
|
|
|
use OCP\Notification\INotification; |
|
36
|
|
|
|
|
37
|
|
|
class BackgroundJob extends QueuedJob { |
|
38
|
|
|
/** @var INotificationManager */ |
|
39
|
|
|
protected $notificationManager; |
|
40
|
|
|
|
|
41
|
|
|
/** @var IUserManager */ |
|
42
|
|
|
private $userManager; |
|
43
|
|
|
|
|
44
|
|
|
/** @var IGroupManager */ |
|
45
|
|
|
private $groupManager; |
|
46
|
|
|
|
|
47
|
|
|
/** @var IURLGenerator */ |
|
48
|
|
|
private $urlGenerator; |
|
49
|
|
|
|
|
50
|
|
|
/** @var Manager */ |
|
51
|
|
|
private $manager; |
|
52
|
|
|
|
|
53
|
|
|
/** @var IManager */ |
|
54
|
|
|
private $activityManager; |
|
55
|
|
|
|
|
56
|
|
|
/** @var array */ |
|
57
|
|
|
protected $notifiedUsers = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param IUserManager $userManager |
|
61
|
|
|
* @param IGroupManager $groupManager |
|
62
|
|
|
* @param IManager $activityManager |
|
63
|
|
|
* @param INotificationManager $notificationManager |
|
64
|
|
|
* @param IURLGenerator $urlGenerator |
|
65
|
|
|
* @param Manager $manager |
|
66
|
|
|
*/ |
|
67
|
9 |
|
public function __construct( |
|
68
|
|
|
IUserManager $userManager, |
|
69
|
|
|
IGroupManager $groupManager, |
|
70
|
|
|
IManager $activityManager, |
|
71
|
|
|
INotificationManager $notificationManager, |
|
72
|
|
|
IURLGenerator $urlGenerator, |
|
73
|
|
|
Manager $manager) { |
|
74
|
9 |
|
$this->userManager = $userManager; |
|
75
|
9 |
|
$this->groupManager = $groupManager; |
|
76
|
9 |
|
$this->activityManager = $activityManager; |
|
77
|
9 |
|
$this->notificationManager = $notificationManager; |
|
78
|
9 |
|
$this->urlGenerator = $urlGenerator; |
|
79
|
9 |
|
$this->manager = $manager; |
|
80
|
9 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param array $arguments |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public function run($arguments) { |
|
86
|
|
|
try { |
|
87
|
3 |
|
$announcement = $this->manager->getAnnouncement($arguments['id'], false, true); |
|
88
|
1 |
|
} catch (\InvalidArgumentException $e) { |
|
89
|
|
|
// Announcement was deleted in the meantime, so no need to announce it anymore |
|
90
|
|
|
// So we die silently |
|
91
|
1 |
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
$this->createPublicity($announcement['id'], $announcement['author'], $announcement['time'], $announcement['groups'], $arguments); |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param int $id |
|
99
|
|
|
* @param string $authorId |
|
100
|
|
|
* @param int $timeStamp |
|
101
|
|
|
* @param string[] $groups |
|
102
|
|
|
* @param array $publicity |
|
103
|
|
|
*/ |
|
104
|
2 |
|
protected function createPublicity($id, $authorId, $timeStamp, array $groups, array $publicity) { |
|
105
|
2 |
|
$event = $this->activityManager->generateEvent(); |
|
106
|
2 |
|
$event->setApp('announcementcenter') |
|
107
|
2 |
|
->setType('announcementcenter') |
|
108
|
2 |
|
->setAuthor($authorId) |
|
109
|
2 |
|
->setTimestamp($timeStamp) |
|
110
|
2 |
|
->setSubject('announcementsubject#' . $id, [$authorId]) |
|
111
|
2 |
|
->setMessage('announcementmessage#' . $id, [$authorId]) |
|
112
|
2 |
|
->setObject('announcement', $id); |
|
113
|
|
|
|
|
114
|
2 |
|
$dateTime = new \DateTime(); |
|
115
|
2 |
|
$dateTime->setTimestamp($timeStamp); |
|
116
|
|
|
|
|
117
|
2 |
|
$notification = $this->notificationManager->createNotification(); |
|
118
|
2 |
|
$notification->setApp('announcementcenter') |
|
119
|
2 |
|
->setDateTime($dateTime) |
|
120
|
2 |
|
->setObject('announcement', $id) |
|
121
|
2 |
|
->setSubject('announced', [$authorId]) |
|
122
|
2 |
|
->setLink($this->urlGenerator->linkToRoute('announcementcenter.page.index')); |
|
123
|
|
|
|
|
124
|
2 |
|
if (in_array('everyone', $groups)) { |
|
125
|
1 |
|
$this->createPublicityEveryone($authorId, $event, $notification, $publicity); |
|
126
|
|
|
} else { |
|
127
|
1 |
|
$this->createPublicityGroups($authorId, $event, $notification, $groups, $publicity); |
|
128
|
|
|
} |
|
129
|
2 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $authorId |
|
133
|
|
|
* @param IEvent $event |
|
134
|
|
|
* @param INotification $notification |
|
135
|
|
|
* @param array $publicity |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function createPublicityEveryone($authorId, IEvent $event, INotification $notification, array $publicity) { |
|
138
|
2 |
|
$this->userManager->callForAllUsers(function(IUser $user) use ($authorId, $event, $notification, $publicity) { |
|
139
|
2 |
|
if (!empty($publicity['activities'])) { |
|
140
|
1 |
|
$event->setAffectedUser($user->getUID()); |
|
141
|
1 |
|
$this->activityManager->publish($event); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
View Code Duplication |
if (!empty($publicity['notifications']) && $authorId !== $user->getUID()) { |
|
|
|
|
|
|
145
|
1 |
|
$notification->setUser($user->getUID()); |
|
146
|
1 |
|
$this->notificationManager->notify($notification); |
|
147
|
|
|
} |
|
148
|
2 |
|
}); |
|
149
|
2 |
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $authorId |
|
153
|
|
|
* @param IEvent $event |
|
154
|
|
|
* @param INotification $notification |
|
155
|
|
|
* @param string[] $groups |
|
156
|
|
|
* @param array $publicity |
|
157
|
|
|
*/ |
|
158
|
2 |
|
protected function createPublicityGroups($authorId, IEvent $event, INotification $notification, array $groups, array $publicity) { |
|
159
|
2 |
|
foreach ($groups as $gid) { |
|
160
|
2 |
|
$group = $this->groupManager->get($gid); |
|
161
|
2 |
|
if (!($group instanceof IGroup)) { |
|
|
|
|
|
|
162
|
2 |
|
continue; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
2 |
|
$users = $group->getUsers(); |
|
166
|
2 |
|
foreach ($users as $user) { |
|
167
|
2 |
|
$uid = $user->getUID(); |
|
168
|
2 |
|
if (isset($this->notifiedUsers[$uid])) { |
|
169
|
2 |
|
continue; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
2 |
|
if (!empty($publicity['activities'])) { |
|
173
|
1 |
|
$event->setAffectedUser($uid); |
|
174
|
1 |
|
$this->activityManager->publish($event); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
2 |
View Code Duplication |
if (!empty($publicity['notifications']) && $authorId !== $uid) { |
|
|
|
|
|
|
178
|
1 |
|
$notification->setUser($uid); |
|
179
|
1 |
|
$this->notificationManager->notify($notification); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
2 |
|
$this->notifiedUsers[$uid] = true; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
2 |
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.