1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Circles - Bring cloud-users closer together. |
8
|
|
|
* |
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
10
|
|
|
* later. See the COPYING file. |
11
|
|
|
* |
12
|
|
|
* @author Maxence Lange <[email protected]> |
13
|
|
|
* @author Vinicius Cubas Brand <[email protected]> |
14
|
|
|
* @author Daniel Tygel <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @copyright 2017 |
17
|
|
|
* @license GNU AGPL version 3 or any later version |
18
|
|
|
* |
19
|
|
|
* This program is free software: you can redistribute it and/or modify |
20
|
|
|
* it under the terms of the GNU Affero General Public License as |
21
|
|
|
* published by the Free Software Foundation, either version 3 of the |
22
|
|
|
* License, or (at your option) any later version. |
23
|
|
|
* |
24
|
|
|
* This program is distributed in the hope that it will be useful, |
25
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
26
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
27
|
|
|
* GNU Affero General Public License for more details. |
28
|
|
|
* |
29
|
|
|
* You should have received a copy of the GNU Affero General Public License |
30
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
namespace OCA\Circles\Service; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger; |
39
|
|
|
use OCA\Circles\AppInfo\Application; |
40
|
|
|
use OCA\Circles\Db\MemberRequest; |
41
|
|
|
use OCA\Circles\Exceptions\RequestBuilderException; |
42
|
|
|
use OCA\Circles\Model\Member; |
43
|
|
|
use OCP\IURLGenerator; |
44
|
|
|
use OCP\Notification\IManager as INotificationManager; |
45
|
|
|
use OCP\Notification\INotification; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Class NotificationService |
50
|
|
|
* |
51
|
|
|
* @package OCA\Circles\Service |
52
|
|
|
*/ |
53
|
|
|
class NotificationService { |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
use TNC22Logger; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** @var IURLGenerator */ |
60
|
|
|
private $urlGenerator; |
61
|
|
|
|
62
|
|
|
/** @var INotificationManager */ |
63
|
|
|
private $notificationManager; |
64
|
|
|
|
65
|
|
|
/** @var MemberRequest */ |
66
|
|
|
private $memberRequest; |
67
|
|
|
|
68
|
|
|
/** @var TimezoneService */ |
69
|
|
|
private $timezoneService; |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* NotificationService constructor. |
74
|
|
|
* |
75
|
|
|
* @param IURLGenerator $urlGenerator |
76
|
|
|
* @param INotificationManager $notificationManager |
77
|
|
|
* @param MemberRequest $memberRequest |
78
|
|
|
* @param TimezoneService $timezoneService |
79
|
|
|
*/ |
80
|
|
|
public function __construct( |
81
|
|
|
IURLGenerator $urlGenerator, |
82
|
|
|
INotificationManager $notificationManager, |
83
|
|
|
MemberRequest $memberRequest, |
84
|
|
|
TimezoneService $timezoneService |
85
|
|
|
) { |
86
|
|
|
$this->urlGenerator = $urlGenerator; |
87
|
|
|
$this->notificationManager = $notificationManager; |
88
|
|
|
$this->memberRequest = $memberRequest; |
89
|
|
|
$this->timezoneService = $timezoneService; |
90
|
|
|
|
91
|
|
|
$this->setup('app', Application::APP_ID); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Member $member |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function notificationInvited(Member $member): void { |
|
|
|
|
99
|
|
|
if ($member->getUserType() !== Member::TYPE_USER || !$member->isLocal()) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->deleteNotification('member', $member->getId()); |
104
|
|
|
$notification = $this->createMemberNotification( |
105
|
|
|
$member->getUserId(), |
106
|
|
|
$member->getId(), |
107
|
|
|
'invitation' |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$declineAction = $notification->createAction(); |
111
|
|
|
$declineUrl = $this->linkToOCS('circles.Local.circleLeave', ['circleId' => $member->getCircleId()]); |
112
|
|
|
$declineAction->setLabel('refuse') |
113
|
|
|
->setLink($this->urlGenerator->getAbsoluteURL($declineUrl), 'PUT'); |
114
|
|
|
$notification->addAction($declineAction); |
115
|
|
|
|
116
|
|
|
$acceptAction = $notification->createAction(); |
117
|
|
|
$acceptUrl = $this->linkToOCS('circles.Local.circleJoin', ['circleId' => $member->getCircleId()]); |
118
|
|
|
$acceptAction->setLabel('accept') |
119
|
|
|
->setLink($this->urlGenerator->getAbsoluteURL($acceptUrl), 'PUT'); |
120
|
|
|
$notification->addAction($acceptAction); |
121
|
|
|
|
122
|
|
|
$this->notificationManager->notify($notification); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param Member $member |
128
|
|
|
* |
129
|
|
|
* @throws RequestBuilderException |
130
|
|
|
*/ |
131
|
|
|
public function notificationRequested(Member $member): void { |
132
|
|
|
// if ($member->getUserType() !== Member::TYPE_USER || !$member->isLocal()) { |
133
|
|
|
// return; |
134
|
|
|
// } |
135
|
|
|
|
136
|
|
|
$this->deleteNotification('member', $member->getId()); |
137
|
|
|
|
138
|
|
|
$moderators = $this->memberRequest->getInheritedMembers( |
139
|
|
|
$member->getCircleId(), |
140
|
|
|
false, |
141
|
|
|
Member::LEVEL_MODERATOR |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
foreach ($moderators as $moderator) { |
145
|
|
|
if ($moderator->getUserType() !== Member::TYPE_USER || !$moderator->isLocal()) { |
146
|
|
|
continue; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->log(3, '### ' . json_encode($moderator)); |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
$notification = $this->createMemberNotification( |
153
|
|
|
$moderator->getUserId(), |
154
|
|
|
$member->getId(), |
155
|
|
|
'joinRequest' |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$declineAction = $notification->createAction(); |
159
|
|
|
$declineUrl = $this->linkToOCS( |
160
|
|
|
'circles.Local.memberRemove', |
161
|
|
|
[ |
162
|
|
|
'circleId' => $member->getCircleId(), |
163
|
|
|
'memberId' => $member->getId() |
164
|
|
|
] |
165
|
|
|
); |
166
|
|
|
$declineAction->setLabel('refuse') |
167
|
|
|
->setLink($this->urlGenerator->getAbsoluteURL($declineUrl), 'DELETE'); |
168
|
|
|
$notification->addAction($declineAction); |
169
|
|
|
|
170
|
|
|
$acceptAction = $notification->createAction(); |
171
|
|
|
$acceptUrl = $this->linkToOCS( |
172
|
|
|
'circles.Local.memberConfirm', |
173
|
|
|
[ |
174
|
|
|
'circleId' => $member->getCircleId(), |
175
|
|
|
'memberId' => $member->getId() |
176
|
|
|
] |
177
|
|
|
); |
178
|
|
|
$acceptAction->setLabel('accept') |
179
|
|
|
->setLink($this->urlGenerator->getAbsoluteURL($acceptUrl), 'PUT'); |
180
|
|
|
$notification->addAction($acceptAction); |
181
|
|
|
|
182
|
|
|
$this->notificationManager->notify($notification); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $object |
190
|
|
|
* @param string $objectId |
191
|
|
|
*/ |
192
|
|
|
public function deleteNotification(string $object, string $objectId) { |
|
|
|
|
193
|
|
|
// if ($objectId === '') { |
194
|
|
|
// return; |
195
|
|
|
// } |
196
|
|
|
// |
197
|
|
|
// $notification = $this->notificationManager->createNotification(); |
198
|
|
|
// $notification->setApp('circles') |
199
|
|
|
// ->setObject($object, $objectId); |
200
|
|
|
// |
201
|
|
|
// $this->notificationManager->markProcessed($notification); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $userId |
207
|
|
|
* @param string $memberId |
208
|
|
|
* @param string $subject |
209
|
|
|
* |
210
|
|
|
* @return INotification |
211
|
|
|
*/ |
212
|
|
|
private function createMemberNotification( |
213
|
|
|
string $userId, |
214
|
|
|
string $memberId, |
215
|
|
|
string $subject |
216
|
|
|
): INotification { |
217
|
|
|
$notification = $this->notificationManager->createNotification(); |
218
|
|
|
$notification->setApp('circles') |
219
|
|
|
->setDateTime($this->timezoneService->getDateTime()) |
220
|
|
|
->setUser($userId) |
221
|
|
|
->setObject('member', $memberId) |
222
|
|
|
->setSubject($subject); |
223
|
|
|
|
224
|
|
|
return $notification; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param string $route |
230
|
|
|
* @param array $params |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
private function linkToOCS(string $route, array $params = []): string { |
235
|
|
|
$absolute = $this->urlGenerator->linkToOCSRouteAbsolute($route, $params); |
236
|
|
|
|
237
|
|
|
return parse_url($absolute, PHP_URL_PATH); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
|
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.