Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to use the display name of file transfers #20360

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions apps/files/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Notification\IAction;
use OCP\Notification\IDismissableNotifier;
Expand All @@ -50,22 +52,22 @@ class Notifier implements INotifier, IDismissableNotifier {
private $mapper;
/** @var IManager */
private $notificationManager;
/** @var IUserManager */
private $userManager;
/** @var ITimeFactory */
private $timeFactory;

/**
* @param IFactory $l10nFactory
* @param IURLGenerator $urlGenerator
*/
public function __construct(IFactory $l10nFactory,
IURLGenerator $urlGenerator,
TransferOwnershipMapper $mapper,
IManager $notificationManager,
IUserManager $userManager,
ITimeFactory $timeFactory) {
$this->l10nFactory = $l10nFactory;
$this->urlGenerator = $urlGenerator;
$this->mapper = $mapper;
$this->notificationManager = $notificationManager;
$this->userManager = $userManager;
$this->timeFactory = $timeFactory;
}

Expand Down Expand Up @@ -138,18 +140,19 @@ public function handleTransferownershipRequest(INotification $notification, stri
IAction::TYPE_DELETE
);

$sourceUser = $this->getUser($param['sourceUser']);
$notification->addParsedAction($approveAction)
->addParsedAction($disapproveAction)
->setRichSubject(
$l->t('Incoming ownership transfer from {user}'),
[
'user' => [
'type' => 'user',
'id' => $param['sourceUser'],
'name' => $param['sourceUser'],
'id' => $sourceUser->getUID(),
'name' => $sourceUser->getDisplayName(),
],
])
->setParsedSubject(str_replace('{user}', $param['sourceUser'], $l->t('Incoming ownership transfer from {user}')))
->setParsedSubject(str_replace('{user}', $sourceUser->getDisplayName(), $l->t('Incoming ownership transfer from {user}')))
->setRichMessage(
$l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour."),
[
Expand All @@ -168,6 +171,7 @@ public function handleTransferOwnershipFailedSource(INotification $notification,
$l = $this->l10nFactory->get('files', $languageCode);
$param = $notification->getSubjectParameters();

$targetUser = $this->getUser($param['targetUser']);
$notification->setRichSubject($l->t('Ownership transfer failed'))
->setParsedSubject($l->t('Ownership transfer failed'))

Expand All @@ -181,18 +185,19 @@ public function handleTransferOwnershipFailedSource(INotification $notification,
],
'user' => [
'type' => 'user',
'id' => $param['targetUser'],
'name' => $param['targetUser'],
'id' => $targetUser->getUID(),
'name' => $targetUser->getDisplayName(),
],
])
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['targetUser']], $l->t('Your ownership transfer of {path} to {user} failed.')));
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $targetUser->getDisplayName()], $l->t('Your ownership transfer of {path} to {user} failed.')));
return $notification;
}

public function handleTransferOwnershipFailedTarget(INotification $notification, string $languageCode): INotification {
$l = $this->l10nFactory->get('files', $languageCode);
$param = $notification->getSubjectParameters();

$sourceUser = $this->getUser($param['sourceUser']);
$notification->setRichSubject($l->t('Ownership transfer failed'))
->setParsedSubject($l->t('Ownership transfer failed'))

Expand All @@ -206,11 +211,11 @@ public function handleTransferOwnershipFailedTarget(INotification $notification,
],
'user' => [
'type' => 'user',
'id' => $param['sourceUser'],
'name' => $param['sourceUser'],
'id' => $sourceUser->getUID(),
'name' => $sourceUser->getDisplayName(),
],
])
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['sourceUser']], $l->t('The ownership transfer of {path} from {user} failed.')));
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $sourceUser->getDisplayName()], $l->t('The ownership transfer of {path} from {user} failed.')));

return $notification;
}
Expand All @@ -219,6 +224,7 @@ public function handleTransferOwnershipDoneSource(INotification $notification,
$l = $this->l10nFactory->get('files', $languageCode);
$param = $notification->getSubjectParameters();

$targetUser = $this->getUser($param['targetUser']);
$notification->setRichSubject($l->t('Ownership transfer done'))
->setParsedSubject($l->t('Ownership transfer done'))

Expand All @@ -232,11 +238,11 @@ public function handleTransferOwnershipDoneSource(INotification $notification,
],
'user' => [
'type' => 'user',
'id' => $param['targetUser'],
'name' => $param['targetUser'],
'id' => $targetUser->getUID(),
'name' => $targetUser->getDisplayName(),
],
])
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['targetUser']], $l->t('Your ownership transfer of {path} to {user} has completed.')));
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $targetUser->getDisplayName()], $l->t('Your ownership transfer of {path} to {user} has completed.')));

return $notification;
}
Expand All @@ -245,6 +251,7 @@ public function handleTransferOwnershipDoneTarget(INotification $notification,
$l = $this->l10nFactory->get('files', $languageCode);
$param = $notification->getSubjectParameters();

$sourceUser = $this->getUser($param['sourceUser']);
$notification->setRichSubject($l->t('Ownership transfer done'))
->setParsedSubject($l->t('Ownership transfer done'))

Expand All @@ -258,11 +265,11 @@ public function handleTransferOwnershipDoneTarget(INotification $notification,
],
'user' => [
'type' => 'user',
'id' => $param['sourceUser'],
'name' => $param['sourceUser'],
'id' => $sourceUser->getUID(),
'name' => $sourceUser->getDisplayName(),
],
])
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['sourceUser']], $l->t('The ownership transfer of {path} from {user} has completed.')));
->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $sourceUser->getDisplayName()], $l->t('The ownership transfer of {path} from {user} has completed.')));

return $notification;
}
Expand Down Expand Up @@ -293,4 +300,12 @@ public function dismissNotification(INotification $notification): void {

$this->mapper->delete($transferOwnership);
}

protected function getUser(string $userId): IUser {
$user = $this->userManager->get($userId);
if ($user instanceof IUser) {
return $user;
}
throw new \InvalidArgumentException('User not found');
}
}
26 changes: 20 additions & 6 deletions apps/files/lib/Service/OwnershipTransferService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\Encryption\IManager as IEncryptionManager;
use OCP\Files\FileInfo;
use OCP\Files\IHomeStorage;
use OCP\Files\InvalidPathException;
use OCP\Files\Mount\IMountManager;
use OCP\IUser;
use OCP\Share\IManager as IShareManager;
Expand Down Expand Up @@ -94,18 +95,31 @@ public function transfer(IUser $sourceUser,
throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to have logged in once.", 2);
}

// setup filesystem
Filesystem::initMountPoints($sourceUid);
Filesystem::initMountPoints($destinationUid);

$view = new View();

if ($move) {
$finalTarget = "$destinationUid/files/";
} else {
$date = date('Y-m-d H-i-s');
$finalTarget = "$destinationUid/files/transferred from $sourceUid on $date";
}

// setup filesystem
Filesystem::initMountPoints($sourceUid);
Filesystem::initMountPoints($destinationUid);
// Remove some characters which are prone to cause errors
$cleanUserName = str_replace(['\\', '/', ':', '.', '?', '#', '\'', '"'], '-', $sourceUser->getDisplayName());
// Replace multiple dashes with one dash
$cleanUserName = preg_replace('/-{2,}/s', '-', $cleanUserName);
$cleanUserName = $cleanUserName ?: $sourceUid;

$finalTarget = "$destinationUid/files/transferred from $cleanUserName on $date";
try {
$view->verifyPath(dirname($finalTarget), basename($finalTarget));
} catch (InvalidPathException $e) {
$finalTarget = "$destinationUid/files/transferred from $sourceUid on $date";
}
}

$view = new View();
if (!($view->is_dir($sourcePath) || $view->is_file($sourcePath))) {
throw new TransferOwnershipException("Unknown path provided: $path", 1);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ public static function normalizePath($path, $stripTrailingSlash = true, $isAbsol
$patterns = [
'/\\\\/s', // no windows style slashes
'/\/\.(\/\.)?\//s', // remove '/./'
'/\/{2,}/s', // remove squence of slashes
'/\/{2,}/s', // remove sequence of slashes
'/\/\.$/s', // remove trailing /.
];

Expand Down