-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Move transfer to background job
Signed-off-by: Christopher Ng <chrng8@gmail.com>
- Loading branch information
Showing
5 changed files
with
164 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Guests; | ||
|
||
use OCA\Files\Exception\TransferOwnershipException; | ||
use OCA\Files\Service\OwnershipTransferService; | ||
use OCA\Guests\AppInfo\Application; | ||
use OCA\Guests\BackgroundJob\TransferJob; | ||
use OCA\Guests\Db\Transfer; | ||
use OCA\Guests\Db\TransferMapper; | ||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\AppFramework\QueryException; | ||
use OCP\BackgroundJob\IJobList; | ||
use OCP\IUser; | ||
use OCP\Notification\IManager as INotificationManager; | ||
use OCP\Share\IManager as IShareManager; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class TransferService { | ||
public function __construct( | ||
private ContainerInterface $container, | ||
private IShareManager $shareManager, | ||
private INotificationManager $notificationManager, | ||
private IJobList $jobList, | ||
private TransferMapper $transferMapper, | ||
private LoggerInterface $logger, | ||
) | ||
{ | ||
} | ||
|
||
public function transfer(IUser $guestUser, IUser $user): void { | ||
try { | ||
/** @var OwnershipTransferService $ownershipTransferService */ | ||
$ownershipTransferService = $this->container->get(OwnershipTransferService::class); | ||
} catch (QueryException $e) { | ||
$this->logger->error('Could not resolve ownership transfer service to import guest user data', [ | ||
'exception' => $e, | ||
]); | ||
throw $e; | ||
} | ||
|
||
try { | ||
$ownershipTransferService->transfer( | ||
$guestUser, | ||
$user, | ||
'/', | ||
null, | ||
true, | ||
true | ||
); | ||
} catch (TransferOwnershipException $e) { | ||
$this->logger->error('Could not import guest user data', [ | ||
'exception' => $e, | ||
]); | ||
throw $e; | ||
} | ||
|
||
// Update incomming shares | ||
$shares = $this->shareManager->getSharedWith($guestUser->getUID(), IShare::TYPE_USER); | ||
Check failure on line 67 in lib/TransferService.php GitHub Actions / Nextcloud dev-stable29UndefinedClass
|
||
foreach ($shares as $share) { | ||
$share->setSharedWith($user->getUID()); | ||
$this->shareManager->updateShare($share); | ||
} | ||
|
||
$notification = $this->notificationManager->createNotification(); | ||
$notification | ||
->setApp(Application::APP_ID) | ||
->setSubject('data_migrated_to_system_user') | ||
->setObject('user', $user->getEMailAddress()) | ||
->setDateTime(new \DateTime()) | ||
->setUser($user->getUID()); | ||
$this->notificationManager->notify($notification); | ||
} | ||
|
||
public function addTransferJob(IUser $author, IUser $source, string $target): void { | ||
$transfer = new Transfer(); | ||
$transfer->setAuthor($author->getUID()); | ||
$transfer->setSource($source->getUID()); | ||
$transfer->setTarget($target); | ||
$transfer->setStatus(Transfer::STATUS_WAITING); | ||
/** @var Transfer $transfer */ | ||
$transfer = $this->transferMapper->insert($transfer); | ||
|
||
$this->jobList->add(TransferJob::class, [ | ||
'id' => $transfer->getId(), | ||
]); | ||
} | ||
|
||
public function getTransfer(IUser $source, string $target): ?Transfer { | ||
try { | ||
$transfer = $this->transferMapper->getBySource($source->getUID()); | ||
return $transfer; | ||
} catch (DoesNotExistException $e) { | ||
// Allow as this just means there is no pending transfer | ||
} | ||
|
||
try { | ||
$transfer = $this->transferMapper->getByTarget($target); | ||
return $transfer; | ||
} catch (DoesNotExistException $e) { | ||
// Allow as this just means there is no pending transfer | ||
} | ||
|
||
return null; | ||
} | ||
} |