|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Tom Needham <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
|
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\Files\Service\TransferOwnership; |
|
23
|
|
|
|
|
24
|
|
|
use OCA\Files\BackgroundJob\TransferOwnership; |
|
25
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
26
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
27
|
|
|
use OCP\BackgroundJob\IJobList; |
|
28
|
|
|
use OCP\Files\IRootFolder; |
|
29
|
|
|
use OCP\Files\NotFoundException; |
|
30
|
|
|
use OCP\Files\NotPermittedException; |
|
31
|
|
|
use OCP\Files\Storage\IPersistentLockingStorage; |
|
32
|
|
|
use OCP\IL10N; |
|
33
|
|
|
use OCP\IURLGenerator; |
|
34
|
|
|
use OCP\IUser; |
|
35
|
|
|
use OCP\IUserManager; |
|
36
|
|
|
use OCP\L10N\IFactory; |
|
37
|
|
|
use OCP\Lock\Persistent\ILock; |
|
38
|
|
|
use OCP\Notification\IManager; |
|
39
|
|
|
use OCP\Notification\INotification; |
|
40
|
|
|
use OCP\Notification\INotifier; |
|
41
|
|
|
use OCP\Util; |
|
42
|
|
|
|
|
43
|
|
|
class TransferRequestManager implements INotifier { |
|
44
|
|
|
|
|
45
|
|
|
/** @var IRootFolder */ |
|
46
|
|
|
protected $rootFolder; |
|
47
|
|
|
/** @var IManager */ |
|
48
|
|
|
protected $notificationManager; |
|
49
|
|
|
/** @var IUserManager */ |
|
50
|
|
|
protected $userManager; |
|
51
|
|
|
/** @var ITimeFactory */ |
|
52
|
|
|
protected $timeFactory; |
|
53
|
|
|
/** @var TransferRequestMapper */ |
|
54
|
|
|
protected $requestMapper; |
|
55
|
|
|
/** @var IURLGenerator */ |
|
56
|
|
|
protected $urlGenerator; |
|
57
|
|
|
/** @var IFactory */ |
|
58
|
|
|
protected $factory; |
|
59
|
|
|
/** @var IJobList */ |
|
60
|
|
|
protected $jobList; |
|
61
|
|
|
|
|
62
|
|
|
public function __construct( |
|
63
|
|
|
IRootFolder $rootFolder, |
|
64
|
|
|
IManager $notificationManager, |
|
65
|
|
|
IUserManager $userManager, |
|
66
|
|
|
ITimeFactory $timeFactory, |
|
67
|
|
|
TransferRequestMapper $requestMapper, |
|
68
|
|
|
IURLGenerator $urlGenerator, |
|
69
|
|
|
IFactory $factory, |
|
70
|
|
|
IJobList $jobList) { |
|
71
|
|
|
$this->rootFolder = $rootFolder; |
|
72
|
|
|
$this->notificationManager = $notificationManager; |
|
73
|
|
|
$this->userManager = $userManager; |
|
74
|
|
|
$this->timeFactory = $timeFactory; |
|
75
|
|
|
$this->requestMapper = $requestMapper; |
|
76
|
|
|
$this->urlGenerator = $urlGenerator; |
|
77
|
|
|
$this->factory = $factory; |
|
78
|
|
|
$this->jobList = $jobList; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param IUser $sourceUser |
|
83
|
|
|
* @param IUser $destinationUser |
|
84
|
|
|
* @param $fileId |
|
85
|
|
|
* @throws NotFoundException |
|
86
|
|
|
* @throws \Exception |
|
87
|
|
|
*/ |
|
88
|
|
|
public function newTransferRequest(IUser $sourceUser, IUser $destinationUser, $fileId) { |
|
89
|
|
|
// Cannot give to self |
|
90
|
|
|
if ($sourceUser->getUID() === $destinationUser->getUID()) { |
|
91
|
|
|
throw new \Exception('Cannot transfer to self'); |
|
92
|
|
|
} |
|
93
|
|
|
// Check node exists |
|
94
|
|
|
$sourceFolder = $this->rootFolder->getById($fileId)[0]; |
|
95
|
|
|
// Check source user owns the node |
|
96
|
|
|
if ($sourceFolder->getOwner()->getUID() !== $sourceUser->getUID()) { |
|
97
|
|
|
throw new NotPermittedException('Cannot move a file you dont own'); |
|
98
|
|
|
} |
|
99
|
|
|
// Check the folder is on persistent lockable storage otherwise we can't do this in the background |
|
100
|
|
|
if (!$sourceFolder->getStorage() instanceof IPersistentLockingStorage) { |
|
101
|
|
|
throw new \Exception('Source folder storage not lockable'); |
|
102
|
|
|
} |
|
103
|
|
|
// Check therer is no request with the same signature |
|
104
|
|
|
if (count($this->requestMapper->findRequestWithSameSignature($sourceUser->getUID(), $destinationUser->getUID(), $fileId)) > 0) { |
|
105
|
|
|
// There is |
|
106
|
|
|
throw new \Exception('There is already a request to transfer this file/folder'); |
|
107
|
|
|
} |
|
108
|
|
|
// Check we are not trying to request a transfer for a folder that is inside a current request |
|
109
|
|
|
$folder = $sourceFolder; |
|
110
|
|
|
$fileids = [$folder->getId()]; |
|
111
|
|
|
while($folder->getPath() !== '/') { |
|
112
|
|
|
$folder = $folder->getParent(); |
|
113
|
|
|
$fileids[] = $folder->getId(); |
|
114
|
|
|
} |
|
115
|
|
|
if (count($this->requestMapper->findOpenRequestForGivenFiles($fileids)) > 0) { |
|
116
|
|
|
throw new \Exception('This file/folder is already pending an existing transfer'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Create the transfer request object |
|
120
|
|
|
$request = new TransferRequest(); |
|
121
|
|
|
$request->setRequestedTime($this->timeFactory->getTime()); |
|
122
|
|
|
$request->setSourceUserId($sourceUser->getUID()); |
|
123
|
|
|
$request->setDestinationUserId($destinationUser->getUID()); |
|
124
|
|
|
$request->setFileId($fileId); |
|
125
|
|
|
$request = $this->requestMapper->insert($request); |
|
126
|
|
|
|
|
127
|
|
|
/** @var IPersistentLockingStorage $storage */ |
|
128
|
|
|
$storage = $sourceFolder->getStorage(); |
|
129
|
|
|
try { |
|
130
|
|
|
$storage->lockNodePersistent($sourceFolder->getInternalPath(), [ |
|
131
|
|
|
'depth' => ILock::LOCK_DEPTH_INFINITE, |
|
132
|
|
|
'token' => $this->getLockTokenFromRequest($request), |
|
|
|
|
|
|
133
|
|
|
'timeout' => 60*60*24 // 24 hours to allow a cron run and acceptance |
|
134
|
|
|
]); |
|
135
|
|
|
} catch (\Exception $e) { |
|
136
|
|
|
// Cleanup transfer request and fail |
|
137
|
|
|
$this->requestMapper->delete($request); |
|
138
|
|
|
throw $e; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->sendRequestNotification($request); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function acceptRequest(TransferRequest $request) { |
|
145
|
|
View Code Duplication |
if ($request->getAcceptedTime() !== null || $request->getActionedTime() !== null || $request->getRejectedTime() !== null) { |
|
|
|
|
|
|
146
|
|
|
throw new \Exception('Already actioned, accepted or rejected'); |
|
147
|
|
|
} |
|
148
|
|
|
// Create a background job, update accepted time |
|
149
|
|
|
$request->setAcceptedTime($this->timeFactory->getTime()); |
|
150
|
|
|
$this->requestMapper->update($request); |
|
151
|
|
|
$sourcePath = $this->rootFolder->getUserFolder( |
|
152
|
|
|
$request->getSourceUserId())->getById($request->getFileId())[0]->getInternalPath(); |
|
153
|
|
|
$this->jobList->add(TransferOwnership::class, json_encode([ |
|
154
|
|
|
'requestId' => $request->getId(), |
|
155
|
|
|
])); |
|
156
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
157
|
|
|
$notification->setApp('files') |
|
158
|
|
|
->setUser($request->getDestinationUserId()) |
|
159
|
|
|
->setObject('transfer_request', $request->getId()); |
|
160
|
|
|
$this->notificationManager->markProcessed($notification); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function rejectRequest(TransferRequest $request) { |
|
164
|
|
|
// Cleanup the lock, save reject timestamp |
|
165
|
|
View Code Duplication |
if ($request->getAcceptedTime() !== null || $request->getActionedTime() !== null || $request->getRejectedTime() !== null) { |
|
|
|
|
|
|
166
|
|
|
throw new \Exception('Already actioned, accepted or rejected'); |
|
167
|
|
|
} |
|
168
|
|
|
$request->setRejectedTime($this->timeFactory->getTime()); |
|
169
|
|
|
$this->requestMapper->update($request); |
|
170
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
171
|
|
|
$notification->setApp('files') |
|
172
|
|
|
->setUser($request->getDestinationUserId()) |
|
173
|
|
|
->setObject('transfer_request', $request->getId()); |
|
174
|
|
|
$this->notificationManager->markProcessed($notification); |
|
175
|
|
|
$file = $this->rootFolder->getUserFolder($request->getSourceUserId())->getById($request->getFileId())[0]; |
|
176
|
|
|
/** @var IPersistentLockingStorage $storage */ |
|
177
|
|
|
$storage = $file->getStorage(); |
|
178
|
|
|
$storage->unlockNodePersistent($file->getInternalPath(), ['token' => $this->getLockTokenFromRequest($request)]); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function deleteRequest(TransferRequest $request) { |
|
182
|
|
|
// Cleanup the lock and the notification |
|
183
|
|
|
$this->requestMapper->delete($request); |
|
184
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
185
|
|
|
$notification->setApp('files') |
|
186
|
|
|
->setUser($request->getDestinationUserId()) |
|
187
|
|
|
->setObject('transfer_request', $request->getId()); |
|
188
|
|
|
$this->notificationManager->markProcessed($notification); |
|
189
|
|
|
$file = $this->rootFolder->getById($request->getFileId())[0]; |
|
190
|
|
|
/** @var IPersistentLockingStorage $storage */ |
|
191
|
|
|
$storage = $file->getStorage(); |
|
192
|
|
|
$storage->unlockNodePersistent($file->getInternalPath(), ['token' => $this->getLockTokenFromRequest($request)]); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param TransferRequest $request the request object |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function sendRequestNotification(TransferRequest $request) { |
|
200
|
|
|
$time = new \DateTime(); |
|
201
|
|
|
$time->setTimestamp($this->timeFactory->getTime()); |
|
202
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
203
|
|
|
$notification->setApp('files') |
|
204
|
|
|
->setUser($request->getDestinationUserId()) |
|
205
|
|
|
->setDateTime($time) |
|
206
|
|
|
->setObject('transfer_request', $request->getId()); |
|
207
|
|
|
|
|
208
|
|
|
$notification->setIcon( |
|
209
|
|
|
$this->urlGenerator->imagePath('core', 'actions/give.svg') |
|
210
|
|
|
); |
|
211
|
|
|
|
|
212
|
|
|
$sourceUser = $this->userManager->get($request->getSourceUserId()); |
|
213
|
|
|
$sourceUserFolder = $this->rootFolder->getUserFolder($sourceUser->getUID()); |
|
214
|
|
|
$folder = $sourceUserFolder->getById($request->getFileId())[0]; |
|
215
|
|
|
$notification->setSubject("new_transfer_request"); |
|
216
|
|
|
$notification->setMessage("new_transfer_request", [$sourceUser->getDisplayName(), $folder->getName(), Util::humanFileSize($folder->getSize())]); |
|
217
|
|
|
|
|
218
|
|
|
$endpoint = $this->urlGenerator->linkToRouteAbsolute( |
|
219
|
|
|
'files.Transfer.accept', |
|
220
|
|
|
['requestId' => $request->getId()] |
|
221
|
|
|
); |
|
222
|
|
|
$declineAction = $notification->createAction(); |
|
223
|
|
|
$declineAction->setLabel('reject'); |
|
224
|
|
|
$declineAction->setLink($endpoint, 'DELETE'); |
|
225
|
|
|
$notification->addAction($declineAction); |
|
226
|
|
|
|
|
227
|
|
|
$acceptAction = $notification->createAction(); |
|
228
|
|
|
$acceptAction->setLabel('accept'); |
|
229
|
|
|
$acceptAction->setLink($endpoint, 'POST'); |
|
230
|
|
|
$acceptAction->setPrimary(true); |
|
231
|
|
|
$notification->addAction($acceptAction); |
|
232
|
|
|
|
|
233
|
|
|
$this->notificationManager->notify($notification); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function prepare(INotification $notification, $languageCode) { |
|
237
|
|
|
if ($notification->getApp() !== 'files') { |
|
238
|
|
|
throw new \InvalidArgumentException(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
// Read the language from the notification |
|
242
|
|
|
$l = $this->factory->get('files', $languageCode); |
|
243
|
|
|
|
|
244
|
|
|
switch ($notification->getObjectType()) { |
|
245
|
|
|
case 'transfer_request': |
|
246
|
|
|
$requestId = $notification->getObjectId(); |
|
247
|
|
|
try { |
|
248
|
|
|
$this->requestMapper->findById($requestId); |
|
249
|
|
|
} catch (DoesNotExistException $ex) { |
|
250
|
|
|
$this->notificationManager->markProcessed($notification); |
|
251
|
|
|
throw new \InvalidArgumentException(); |
|
252
|
|
|
} |
|
253
|
|
|
return $this->formatNotification($notification, $l); |
|
254
|
|
|
|
|
255
|
|
|
default: |
|
256
|
|
|
throw new \InvalidArgumentException(); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
protected function formatNotification(INotification $notification, IL10N $l) { |
|
261
|
|
|
switch($notification->getSubject()) { |
|
262
|
|
|
case 'new_transfer_request': |
|
263
|
|
|
$notification->setParsedSubject((string) $l->t('A user would like to transfer a folder to you')); |
|
264
|
|
|
$notification->setParsedMessage( |
|
265
|
|
|
(string) $l->t( |
|
266
|
|
|
'"%1$s" requested to transfer "%2$s" to you (%3$s)"', |
|
267
|
|
|
$notification->getMessageParameters()) |
|
268
|
|
|
); |
|
269
|
|
View Code Duplication |
foreach ($notification->getActions() as $action) { |
|
|
|
|
|
|
270
|
|
|
switch ($action->getLabel()) { |
|
271
|
|
|
case 'accept': |
|
272
|
|
|
$action->setParsedLabel( |
|
273
|
|
|
(string) $l->t('Accept') |
|
274
|
|
|
); |
|
275
|
|
|
break; |
|
276
|
|
|
case 'reject': |
|
277
|
|
|
$action->setParsedLabel( |
|
278
|
|
|
(string) $l->t('Decline') |
|
279
|
|
|
); |
|
280
|
|
|
break; |
|
281
|
|
|
} |
|
282
|
|
|
$notification->addParsedAction($action); |
|
283
|
|
|
} |
|
284
|
|
|
return $notification; |
|
285
|
|
|
break; |
|
|
|
|
|
|
286
|
|
View Code Duplication |
case 'transfer_request_actioned_source': |
|
|
|
|
|
|
287
|
|
|
$notification->setParsedSubject((string) $l->t('Transfer completed')); |
|
288
|
|
|
$notification->setParsedMessage( |
|
289
|
|
|
(string) $l->t( |
|
290
|
|
|
'"%1$s" accepted your transfer of "%2$s" and it was completed', |
|
291
|
|
|
$notification->getMessageParameters()) |
|
292
|
|
|
); |
|
293
|
|
|
return $notification; |
|
294
|
|
|
break; |
|
|
|
|
|
|
295
|
|
View Code Duplication |
case 'transfer_request_actioned_destination': |
|
|
|
|
|
|
296
|
|
|
$notification->setParsedSubject((string) $l->t('Transfer completed')); |
|
297
|
|
|
$notification->setParsedMessage( |
|
298
|
|
|
(string) $l->t( |
|
299
|
|
|
'"%1$s" was transferred to you from "%2$s"', |
|
300
|
|
|
$notification->getMessageParameters()) |
|
301
|
|
|
); |
|
302
|
|
|
return $notification; |
|
303
|
|
|
break; |
|
|
|
|
|
|
304
|
|
View Code Duplication |
case 'transfer_request_failed_destination': |
|
|
|
|
|
|
305
|
|
|
$notification->setParsedSubject((string) $l->t('Transfer failed')); |
|
306
|
|
|
$notification->setParsedMessage( |
|
307
|
|
|
(string) $l->t( |
|
308
|
|
|
'The transfer of "%1$s" from "%2$s failed. Ask the sender to try again."', |
|
309
|
|
|
$notification->getMessageParameters()) |
|
310
|
|
|
); |
|
311
|
|
|
return $notification; |
|
312
|
|
|
break; |
|
|
|
|
|
|
313
|
|
View Code Duplication |
case 'transfer_request_failed_source': |
|
|
|
|
|
|
314
|
|
|
$notification->setParsedSubject((string) $l->t('Transfer failed')); |
|
315
|
|
|
$notification->setParsedMessage( |
|
316
|
|
|
(string) $l->t( |
|
317
|
|
|
'The transfer of "%1$s" to "%2$s" failed with message: "%3$s"', |
|
318
|
|
|
$notification->getMessageParameters()) |
|
319
|
|
|
); |
|
320
|
|
|
return $notification; |
|
321
|
|
|
break; |
|
|
|
|
|
|
322
|
|
|
default: |
|
323
|
|
|
throw new \InvalidArgumentException('Not a notifcation that can be formatted by this class'); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
public function actionRequest(TransferRequest $request) { |
|
329
|
|
|
$request->setActionedTime($this->timeFactory->getTime()); |
|
330
|
|
|
$this->requestMapper->update($request); |
|
331
|
|
|
// Notify the source user it was accepted |
|
332
|
|
|
$this->notifyActioned($request); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Cleanup and notify |
|
337
|
|
|
* @param TransferRequest $request |
|
338
|
|
|
* @throws NotFoundException |
|
339
|
|
|
* @throws \OCP\Files\InvalidPathException |
|
340
|
|
|
*/ |
|
341
|
|
|
public function actionRequestFailure(TransferRequest $request, $message = null) { |
|
342
|
|
|
// Notify the users that it failed |
|
343
|
|
|
$this->notifyActionedFailure($request, $message); |
|
344
|
|
|
$this->cleanupRequest($request); |
|
345
|
|
|
|
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Tell the source user and destination user that the transfer has happened |
|
350
|
|
|
* @param TransferRequest $request |
|
351
|
|
|
* @throws NotFoundException |
|
352
|
|
|
* @throws \OCP\Files\InvalidPathException |
|
353
|
|
|
*/ |
|
354
|
|
|
public function notifyActioned(TransferRequest $request) { |
|
355
|
|
|
// Set to now |
|
356
|
|
|
$time = new \DateTime(); |
|
357
|
|
|
$time->setTimestamp($request->getActionedTime()); |
|
358
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
359
|
|
|
$notification->setApp('files') |
|
360
|
|
|
->setUser($request->getSourceUserId()) |
|
361
|
|
|
->setDateTime($time) |
|
362
|
|
|
->setObject('transfer_request', $request->getId()); |
|
363
|
|
|
|
|
364
|
|
|
$notification->setIcon( |
|
365
|
|
|
$this->urlGenerator->imagePath('core', 'actions/give.svg') |
|
366
|
|
|
); |
|
367
|
|
|
|
|
368
|
|
|
$destinationUser = $this->userManager->get($request->getDestinationUserId()); |
|
369
|
|
|
$folder = $this->rootFolder->getById($request->getFileId())[0]; |
|
370
|
|
|
$notification->setSubject("transfer_request_actioned_source"); |
|
371
|
|
|
$notification->setMessage("transfer_request_actioned_source", [$destinationUser->getDisplayName(), $folder->getName()]); |
|
372
|
|
|
$this->notificationManager->notify($notification); |
|
373
|
|
|
|
|
374
|
|
|
// Set to now |
|
375
|
|
|
$time = new \DateTime(); |
|
376
|
|
|
$time->setTimestamp($request->getActionedTime()); |
|
377
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
378
|
|
|
$notification->setApp('files') |
|
379
|
|
|
->setUser($request->getDestinationUserId()) |
|
380
|
|
|
->setDateTime($time) |
|
381
|
|
|
->setObject('transfer_request', $request->getId()); |
|
382
|
|
|
|
|
383
|
|
|
$notification->setIcon( |
|
384
|
|
|
$this->urlGenerator->imagePath('core', 'actions/give.svg') |
|
385
|
|
|
); |
|
386
|
|
|
|
|
387
|
|
|
$sourceUser = $this->userManager->get($request->getSourceUserId()); |
|
388
|
|
|
$folder = $this->rootFolder->getById($request->getFileId())[0]; |
|
389
|
|
|
$notification->setSubject("transfer_request_actioned_destination"); |
|
390
|
|
|
$notification->setMessage("transfer_request_actioned_destination", [$folder->getName(), $sourceUser->getDisplayName()]); |
|
391
|
|
|
$notification->setLink($this->urlGenerator->getAbsoluteURL('/f/'.$folder->getId())); |
|
392
|
|
|
$this->notificationManager->notify($notification); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Tell the source user and destination user that the transfer failed |
|
397
|
|
|
* @param TransferRequest $request |
|
398
|
|
|
* @throws NotFoundException |
|
399
|
|
|
* @throws \OCP\Files\InvalidPathException |
|
400
|
|
|
*/ |
|
401
|
|
|
public function notifyActionedFailure(TransferRequest $request, $message = null) { |
|
402
|
|
|
// Set to now |
|
403
|
|
|
$time = new \DateTime(); |
|
404
|
|
|
$time->setTimestamp($this->timeFactory->getTime()); |
|
405
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
406
|
|
|
$notification->setApp('files') |
|
407
|
|
|
->setUser($request->getSourceUserId()) |
|
408
|
|
|
->setDateTime($time) |
|
409
|
|
|
->setObject('transfer_request', $request->getId()); |
|
410
|
|
|
|
|
411
|
|
|
$notification->setIcon( |
|
412
|
|
|
$this->urlGenerator->imagePath('core', 'actions/give.svg') |
|
413
|
|
|
); |
|
414
|
|
|
|
|
415
|
|
|
$destinationUser = $this->userManager->get($request->getDestinationUserId()); |
|
416
|
|
|
$folder = $this->rootFolder->getById($request->getFileId())[0]; |
|
417
|
|
|
$notification->setSubject("transfer_request_failed_source"); |
|
418
|
|
|
$notification->setMessage("transfer_request_failed_source", [$folder->getName(), $destinationUser->getDisplayName(), $message]); |
|
419
|
|
|
$this->notificationManager->notify($notification); |
|
420
|
|
|
|
|
421
|
|
|
// Set to now |
|
422
|
|
|
$time = new \DateTime(); |
|
423
|
|
|
$time->setTimestamp($this->timeFactory->getTime()); |
|
424
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
425
|
|
|
$notification->setApp('files') |
|
426
|
|
|
->setUser($request->getDestinationUserId()) |
|
427
|
|
|
->setDateTime($time) |
|
428
|
|
|
->setObject('transfer_request', $request->getId()); |
|
429
|
|
|
|
|
430
|
|
|
$notification->setIcon( |
|
431
|
|
|
$this->urlGenerator->imagePath('core', 'actions/give.svg') |
|
432
|
|
|
); |
|
433
|
|
|
|
|
434
|
|
|
$sourceUser = $this->userManager->get($request->getSourceUserId()); |
|
435
|
|
|
$folder = $this->rootFolder->getById($request->getFileId())[0]; |
|
436
|
|
|
$notification->setSubject("transfer_request_failed_destination"); |
|
437
|
|
|
$notification->setMessage("transfer_request_failed_destination", [$folder->getName(), $sourceUser->getDisplayName()]); |
|
438
|
|
|
$this->notificationManager->notify($notification); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* Background job for cleaning up old requests, removes notifications, request and locks |
|
443
|
|
|
*/ |
|
444
|
|
|
public function cleanup() { |
|
445
|
|
|
// Delete request that are older than 24 hours |
|
446
|
|
|
$oldRequests = $this->requestMapper->findOpenRequestsOlderThan(1); |
|
447
|
|
|
/** @var TransferRequest $request */ |
|
448
|
|
|
foreach ($oldRequests as $request) { |
|
449
|
|
|
$this->cleanupRequest($request); |
|
450
|
|
|
} |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* Delete request, delete lock, and delete requested notification |
|
455
|
|
|
* @param TransferRequest $request |
|
456
|
|
|
*/ |
|
457
|
|
|
protected function cleanupRequest(TransferRequest $request) { |
|
458
|
|
|
// Remove the lock |
|
459
|
|
|
try { |
|
460
|
|
|
$file = $this->rootFolder->getById($request->getFileId())[0]; |
|
461
|
|
|
/** @var IPersistentLockingStorage $storage */ |
|
462
|
|
|
$storage = $file->getStorage(); |
|
463
|
|
|
$storage->unlockNodePersistent($file->getInternalPath(), ['token' => $this->getLockTokenFromRequest($request)]); |
|
464
|
|
|
} catch (\Exception $e) { |
|
465
|
|
|
\OC::$server->getLogger()->logException($e, ['app' => 'files']); |
|
466
|
|
|
} |
|
467
|
|
|
// Now remove the request |
|
468
|
|
|
$this->requestMapper->delete($request); |
|
469
|
|
|
// And lets remove the notification to save confusion |
|
470
|
|
|
$notification = $this->notificationManager->createNotification(); |
|
471
|
|
|
$notification->setApp('files') |
|
472
|
|
|
->setUser($request->getDestinationUserId()) |
|
473
|
|
|
->setObject('transfer_request', $request->getId()); |
|
474
|
|
|
$this->notificationManager->markProcessed($notification); |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
/** |
|
478
|
|
|
* Helper to get the lock token id associated with a request |
|
479
|
|
|
* @param TransferRequest $request |
|
480
|
|
|
* @return string |
|
481
|
|
|
*/ |
|
482
|
|
|
protected function getLockTokenFromRequest(TransferRequest $request) { |
|
483
|
|
|
return 'transfer-request-'.$request->getId(); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
/** |
|
487
|
|
|
* Helper to get a request object from the mapper without another dep injection |
|
488
|
|
|
* @param $requestId |
|
489
|
|
|
* @return TransferRequest|\OCP\AppFramework\Db\Entity |
|
490
|
|
|
* @throws DoesNotExistException |
|
491
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
|
492
|
|
|
*/ |
|
493
|
|
|
public function getRequestById($requestId) { |
|
494
|
|
|
return $this->requestMapper->findById($requestId); |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
|
|
498
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.