|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Joas Schilling <[email protected]> |
|
9
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
10
|
|
|
* @author Sascha Wiswedel <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @license GNU AGPL version 3 or any later version |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
17
|
|
|
* License, or (at your option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace OCA\Files\Notification; |
|
30
|
|
|
|
|
31
|
|
|
use OCA\Files\Db\TransferOwnershipMapper; |
|
32
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
33
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
34
|
|
|
use OCP\IURLGenerator; |
|
35
|
|
|
use OCP\IUser; |
|
36
|
|
|
use OCP\IUserManager; |
|
37
|
|
|
use OCP\L10N\IFactory; |
|
38
|
|
|
use OCP\Notification\IAction; |
|
39
|
|
|
use OCP\Notification\IDismissableNotifier; |
|
40
|
|
|
use OCP\Notification\IManager; |
|
41
|
|
|
use OCP\Notification\INotification; |
|
42
|
|
|
use OCP\Notification\INotifier; |
|
43
|
|
|
|
|
44
|
|
|
class Notifier implements INotifier, IDismissableNotifier { |
|
45
|
|
|
|
|
46
|
|
|
/** @var IFactory */ |
|
47
|
|
|
protected $l10nFactory; |
|
48
|
|
|
|
|
49
|
|
|
/** @var IURLGenerator */ |
|
50
|
|
|
protected $urlGenerator; |
|
51
|
|
|
/** @var TransferOwnershipMapper */ |
|
52
|
|
|
private $mapper; |
|
53
|
|
|
/** @var IManager */ |
|
54
|
|
|
private $notificationManager; |
|
55
|
|
|
/** @var IUserManager */ |
|
56
|
|
|
private $userManager; |
|
57
|
|
|
/** @var ITimeFactory */ |
|
58
|
|
|
private $timeFactory; |
|
59
|
|
|
|
|
60
|
|
|
public function __construct(IFactory $l10nFactory, |
|
61
|
|
|
IURLGenerator $urlGenerator, |
|
62
|
|
|
TransferOwnershipMapper $mapper, |
|
63
|
|
|
IManager $notificationManager, |
|
64
|
|
|
IUserManager $userManager, |
|
65
|
|
|
ITimeFactory $timeFactory) { |
|
66
|
|
|
$this->l10nFactory = $l10nFactory; |
|
67
|
|
|
$this->urlGenerator = $urlGenerator; |
|
68
|
|
|
$this->mapper = $mapper; |
|
69
|
|
|
$this->notificationManager = $notificationManager; |
|
70
|
|
|
$this->userManager = $userManager; |
|
71
|
|
|
$this->timeFactory = $timeFactory; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getID(): string { |
|
75
|
|
|
return 'files'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getName(): string { |
|
79
|
|
|
return $this->l10nFactory->get('files')->t('Files'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param INotification $notification |
|
84
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification |
|
85
|
|
|
* @return INotification |
|
86
|
|
|
* @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
87
|
|
|
*/ |
|
88
|
|
|
public function prepare(INotification $notification, string $languageCode): INotification { |
|
89
|
|
|
if ($notification->getApp() !== 'files') { |
|
90
|
|
|
throw new \InvalidArgumentException('Unhandled app'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($notification->getSubject() === 'transferownershipRequest') { |
|
94
|
|
|
return $this->handleTransferownershipRequest($notification, $languageCode); |
|
95
|
|
|
} |
|
96
|
|
|
if ($notification->getSubject() === 'transferOwnershipFailedSource') { |
|
97
|
|
|
return $this->handleTransferOwnershipFailedSource($notification, $languageCode); |
|
98
|
|
|
} |
|
99
|
|
|
if ($notification->getSubject() === 'transferOwnershipFailedTarget') { |
|
100
|
|
|
return $this->handleTransferOwnershipFailedTarget($notification, $languageCode); |
|
101
|
|
|
} |
|
102
|
|
|
if ($notification->getSubject() === 'transferOwnershipDoneSource') { |
|
103
|
|
|
return $this->handleTransferOwnershipDoneSource($notification, $languageCode); |
|
104
|
|
|
} |
|
105
|
|
|
if ($notification->getSubject() === 'transferOwnershipDoneTarget') { |
|
106
|
|
|
return $this->handleTransferOwnershipDoneTarget($notification, $languageCode); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
throw new \InvalidArgumentException('Unhandled subject'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function handleTransferownershipRequest(INotification $notification, string $languageCode): INotification { |
|
113
|
|
|
$l = $this->l10nFactory->get('files', $languageCode); |
|
114
|
|
|
$id = $notification->getObjectId(); |
|
115
|
|
|
$param = $notification->getSubjectParameters(); |
|
116
|
|
|
|
|
117
|
|
|
$approveAction = $notification->createAction() |
|
118
|
|
|
->setParsedLabel($l->t('Accept')) |
|
119
|
|
|
->setPrimary(true) |
|
120
|
|
|
->setLink( |
|
121
|
|
|
$this->urlGenerator->getAbsoluteURL( |
|
122
|
|
|
$this->urlGenerator->linkTo( |
|
123
|
|
|
'', |
|
124
|
|
|
'ocs/v2.php/apps/files/api/v1/transferownership/' . $id |
|
125
|
|
|
) |
|
126
|
|
|
), |
|
127
|
|
|
IAction::TYPE_POST |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
$disapproveAction = $notification->createAction() |
|
131
|
|
|
->setParsedLabel($l->t('Reject')) |
|
132
|
|
|
->setPrimary(false) |
|
133
|
|
|
->setLink( |
|
134
|
|
|
$this->urlGenerator->getAbsoluteURL( |
|
135
|
|
|
$this->urlGenerator->linkTo( |
|
136
|
|
|
'', |
|
137
|
|
|
'ocs/v2.php/apps/files/api/v1/transferownership/' . $id |
|
138
|
|
|
) |
|
139
|
|
|
), |
|
140
|
|
|
IAction::TYPE_DELETE |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$sourceUser = $this->getUser($param['sourceUser']); |
|
144
|
|
|
$notification->addParsedAction($approveAction) |
|
145
|
|
|
->addParsedAction($disapproveAction) |
|
146
|
|
|
->setRichSubject( |
|
147
|
|
|
$l->t('Incoming ownership transfer from {user}'), |
|
148
|
|
|
[ |
|
149
|
|
|
'user' => [ |
|
150
|
|
|
'type' => 'user', |
|
151
|
|
|
'id' => $sourceUser->getUID(), |
|
152
|
|
|
'name' => $sourceUser->getDisplayName(), |
|
153
|
|
|
], |
|
154
|
|
|
]) |
|
155
|
|
|
->setParsedSubject(str_replace('{user}', $sourceUser->getDisplayName(), $l->t('Incoming ownership transfer from {user}'))) |
|
156
|
|
|
->setRichMessage( |
|
157
|
|
|
$l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour."), |
|
158
|
|
|
[ |
|
159
|
|
|
'path' => [ |
|
160
|
|
|
'type' => 'highlight', |
|
161
|
|
|
'id' => $param['targetUser'] . '::' . $param['nodeName'], |
|
162
|
|
|
'name' => $param['nodeName'], |
|
163
|
|
|
] |
|
164
|
|
|
]) |
|
165
|
|
|
->setParsedMessage(str_replace('{path}', $param['nodeName'], $l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour."))); |
|
166
|
|
|
|
|
167
|
|
|
return $notification; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function handleTransferOwnershipFailedSource(INotification $notification, string $languageCode): INotification { |
|
171
|
|
|
$l = $this->l10nFactory->get('files', $languageCode); |
|
172
|
|
|
$param = $notification->getSubjectParameters(); |
|
173
|
|
|
|
|
174
|
|
|
$targetUser = $this->getUser($param['targetUser']); |
|
175
|
|
|
$notification->setRichSubject($l->t('Ownership transfer failed')) |
|
176
|
|
|
->setParsedSubject($l->t('Ownership transfer failed')) |
|
177
|
|
|
|
|
178
|
|
|
->setRichMessage( |
|
179
|
|
|
$l->t('Your ownership transfer of {path} to {user} failed.'), |
|
180
|
|
|
[ |
|
181
|
|
|
'path' => [ |
|
182
|
|
|
'type' => 'highlight', |
|
183
|
|
|
'id' => $param['targetUser'] . '::' . $param['nodeName'], |
|
184
|
|
|
'name' => $param['nodeName'], |
|
185
|
|
|
], |
|
186
|
|
|
'user' => [ |
|
187
|
|
|
'type' => 'user', |
|
188
|
|
|
'id' => $targetUser->getUID(), |
|
189
|
|
|
'name' => $targetUser->getDisplayName(), |
|
190
|
|
|
], |
|
191
|
|
|
]) |
|
192
|
|
|
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $targetUser->getDisplayName()], $l->t('Your ownership transfer of {path} to {user} failed.'))); |
|
193
|
|
|
return $notification; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function handleTransferOwnershipFailedTarget(INotification $notification, string $languageCode): INotification { |
|
197
|
|
|
$l = $this->l10nFactory->get('files', $languageCode); |
|
198
|
|
|
$param = $notification->getSubjectParameters(); |
|
199
|
|
|
|
|
200
|
|
|
$sourceUser = $this->getUser($param['sourceUser']); |
|
201
|
|
|
$notification->setRichSubject($l->t('Ownership transfer failed')) |
|
202
|
|
|
->setParsedSubject($l->t('Ownership transfer failed')) |
|
203
|
|
|
|
|
204
|
|
|
->setRichMessage( |
|
205
|
|
|
$l->t('The ownership transfer of {path} from {user} failed.'), |
|
206
|
|
|
[ |
|
207
|
|
|
'path' => [ |
|
208
|
|
|
'type' => 'highlight', |
|
209
|
|
|
'id' => $param['sourceUser'] . '::' . $param['nodeName'], |
|
210
|
|
|
'name' => $param['nodeName'], |
|
211
|
|
|
], |
|
212
|
|
|
'user' => [ |
|
213
|
|
|
'type' => 'user', |
|
214
|
|
|
'id' => $sourceUser->getUID(), |
|
215
|
|
|
'name' => $sourceUser->getDisplayName(), |
|
216
|
|
|
], |
|
217
|
|
|
]) |
|
218
|
|
|
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $sourceUser->getDisplayName()], $l->t('The ownership transfer of {path} from {user} failed.'))); |
|
219
|
|
|
|
|
220
|
|
|
return $notification; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function handleTransferOwnershipDoneSource(INotification $notification, string $languageCode): INotification { |
|
224
|
|
|
$l = $this->l10nFactory->get('files', $languageCode); |
|
225
|
|
|
$param = $notification->getSubjectParameters(); |
|
226
|
|
|
|
|
227
|
|
|
$targetUser = $this->getUser($param['targetUser']); |
|
228
|
|
|
$notification->setRichSubject($l->t('Ownership transfer done')) |
|
229
|
|
|
->setParsedSubject($l->t('Ownership transfer done')) |
|
230
|
|
|
|
|
231
|
|
|
->setRichMessage( |
|
232
|
|
|
$l->t('Your ownership transfer of {path} to {user} has completed.'), |
|
233
|
|
|
[ |
|
234
|
|
|
'path' => [ |
|
235
|
|
|
'type' => 'highlight', |
|
236
|
|
|
'id' => $param['targetUser'] . '::' . $param['nodeName'], |
|
237
|
|
|
'name' => $param['nodeName'], |
|
238
|
|
|
], |
|
239
|
|
|
'user' => [ |
|
240
|
|
|
'type' => 'user', |
|
241
|
|
|
'id' => $targetUser->getUID(), |
|
242
|
|
|
'name' => $targetUser->getDisplayName(), |
|
243
|
|
|
], |
|
244
|
|
|
]) |
|
245
|
|
|
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $targetUser->getDisplayName()], $l->t('Your ownership transfer of {path} to {user} has completed.'))); |
|
246
|
|
|
|
|
247
|
|
|
return $notification; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function handleTransferOwnershipDoneTarget(INotification $notification, string $languageCode): INotification { |
|
251
|
|
|
$l = $this->l10nFactory->get('files', $languageCode); |
|
252
|
|
|
$param = $notification->getSubjectParameters(); |
|
253
|
|
|
|
|
254
|
|
|
$sourceUser = $this->getUser($param['sourceUser']); |
|
255
|
|
|
$notification->setRichSubject($l->t('Ownership transfer done')) |
|
256
|
|
|
->setParsedSubject($l->t('Ownership transfer done')) |
|
257
|
|
|
|
|
258
|
|
|
->setRichMessage( |
|
259
|
|
|
$l->t('The ownership transfer of {path} from {user} has completed.'), |
|
260
|
|
|
[ |
|
261
|
|
|
'path' => [ |
|
262
|
|
|
'type' => 'highlight', |
|
263
|
|
|
'id' => $param['sourceUser'] . '::' . $param['nodeName'], |
|
264
|
|
|
'name' => $param['nodeName'], |
|
265
|
|
|
], |
|
266
|
|
|
'user' => [ |
|
267
|
|
|
'type' => 'user', |
|
268
|
|
|
'id' => $sourceUser->getUID(), |
|
269
|
|
|
'name' => $sourceUser->getDisplayName(), |
|
270
|
|
|
], |
|
271
|
|
|
]) |
|
272
|
|
|
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $sourceUser->getDisplayName()], $l->t('The ownership transfer of {path} from {user} has completed.'))); |
|
273
|
|
|
|
|
274
|
|
|
return $notification; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
public function dismissNotification(INotification $notification): void { |
|
278
|
|
|
if ($notification->getApp() !== 'files') { |
|
279
|
|
|
throw new \InvalidArgumentException('Unhandled app'); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
// TODO: This should all be moved to a service that also the transferownershipContoller uses. |
|
283
|
|
|
try { |
|
284
|
|
|
$transferOwnership = $this->mapper->getById((int)$notification->getObjectId()); |
|
285
|
|
|
} catch (DoesNotExistException $e) { |
|
286
|
|
|
return; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
290
|
|
|
$notification->setUser($transferOwnership->getSourceUser()) |
|
291
|
|
|
->setApp('files') |
|
292
|
|
|
->setDateTime($this->timeFactory->getDateTime()) |
|
293
|
|
|
->setSubject('transferownershipRequestDenied', [ |
|
294
|
|
|
'sourceUser' => $transferOwnership->getSourceUser(), |
|
295
|
|
|
'targetUser' => $transferOwnership->getTargetUser(), |
|
296
|
|
|
'nodeName' => $transferOwnership->getNodeName() |
|
297
|
|
|
]) |
|
298
|
|
|
->setObject('transfer', (string)$transferOwnership->getId()); |
|
299
|
|
|
$this->notificationManager->notify($notification); |
|
300
|
|
|
|
|
301
|
|
|
$this->mapper->delete($transferOwnership); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
protected function getUser(string $userId): IUser { |
|
305
|
|
|
$user = $this->userManager->get($userId); |
|
306
|
|
|
if ($user instanceof IUser) { |
|
307
|
|
|
return $user; |
|
308
|
|
|
} |
|
309
|
|
|
throw new \InvalidArgumentException('User not found'); |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|