123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- declare(strict_types=1);
- namespace OCA\Files\Notification;
- use OCA\Files\BackgroundJob\TransferOwnership;
- use OCA\Files\Db\TransferOwnershipMapper;
- use OCP\AppFramework\Db\DoesNotExistException;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\BackgroundJob\IJobList;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\L10N\IFactory;
- use OCP\Notification\IAction;
- use OCP\Notification\IDismissableNotifier;
- use OCP\Notification\IManager;
- use OCP\Notification\INotification;
- use OCP\Notification\INotifier;
- use OCP\Notification\UnknownNotificationException;
- class Notifier implements INotifier, IDismissableNotifier {
- public function __construct(
- protected IFactory $l10nFactory,
- protected IURLGenerator $urlGenerator,
- private TransferOwnershipMapper $mapper,
- private IManager $notificationManager,
- private IUserManager $userManager,
- private IJobList $jobList,
- private ITimeFactory $timeFactory,
- ) {
- }
- public function getID(): string {
- return 'files';
- }
- public function getName(): string {
- return $this->l10nFactory->get('files')->t('Files');
- }
-
- public function prepare(INotification $notification, string $languageCode): INotification {
- if ($notification->getApp() !== 'files') {
- throw new UnknownNotificationException('Unhandled app');
- }
- $imagePath = $this->urlGenerator->imagePath('files', 'folder-move.svg');
- $iconUrl = $this->urlGenerator->getAbsoluteURL($imagePath);
- $notification->setIcon($iconUrl);
- return match($notification->getSubject()) {
- 'transferownershipRequest' => $this->handleTransferownershipRequest($notification, $languageCode),
- 'transferownershipRequestDenied' => $this->handleTransferOwnershipRequestDenied($notification, $languageCode),
- 'transferOwnershipFailedSource' => $this->handleTransferOwnershipFailedSource($notification, $languageCode),
- 'transferOwnershipFailedTarget' => $this->handleTransferOwnershipFailedTarget($notification, $languageCode),
- 'transferOwnershipDoneSource' => $this->handleTransferOwnershipDoneSource($notification, $languageCode),
- 'transferOwnershipDoneTarget' => $this->handleTransferOwnershipDoneTarget($notification, $languageCode),
- default => throw new UnknownNotificationException('Unhandled subject')
- };
- }
- public function handleTransferownershipRequest(INotification $notification, string $languageCode): INotification {
- $l = $this->l10nFactory->get('files', $languageCode);
- $id = $notification->getObjectId();
- $param = $notification->getSubjectParameters();
- $approveAction = $notification->createAction()
- ->setParsedLabel($l->t('Accept'))
- ->setPrimary(true)
- ->setLink(
- $this->urlGenerator->getAbsoluteURL(
- $this->urlGenerator->linkTo(
- '',
- 'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
- )
- ),
- IAction::TYPE_POST
- );
- $disapproveAction = $notification->createAction()
- ->setParsedLabel($l->t('Reject'))
- ->setPrimary(false)
- ->setLink(
- $this->urlGenerator->getAbsoluteURL(
- $this->urlGenerator->linkTo(
- '',
- 'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
- )
- ),
- 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' => $sourceUser->getUID(),
- 'name' => $sourceUser->getDisplayName(),
- ],
- ])
- ->setRichMessage(
- $l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour."),
- [
- 'path' => [
- 'type' => 'highlight',
- 'id' => $param['targetUser'] . '::' . $param['nodeName'],
- 'name' => $param['nodeName'],
- ]
- ]);
- return $notification;
- }
- public function handleTransferOwnershipRequestDenied(INotification $notification, string $languageCode): INotification {
- $l = $this->l10nFactory->get('files', $languageCode);
- $param = $notification->getSubjectParameters();
- $targetUser = $this->getUser($param['targetUser']);
- $notification->setRichSubject($l->t('Ownership transfer denied'))
- ->setRichMessage(
- $l->t('Your ownership transfer of {path} was denied by {user}.'),
- [
- 'path' => [
- 'type' => 'highlight',
- 'id' => $param['targetUser'] . '::' . $param['nodeName'],
- 'name' => $param['nodeName'],
- ],
- 'user' => [
- 'type' => 'user',
- 'id' => $targetUser->getUID(),
- 'name' => $targetUser->getDisplayName(),
- ],
- ]);
- return $notification;
- }
- public function handleTransferOwnershipFailedSource(INotification $notification, string $languageCode): INotification {
- $l = $this->l10nFactory->get('files', $languageCode);
- $param = $notification->getSubjectParameters();
- $targetUser = $this->getUser($param['targetUser']);
- $notification->setRichSubject($l->t('Ownership transfer failed'))
- ->setRichMessage(
- $l->t('Your ownership transfer of {path} to {user} failed.'),
- [
- 'path' => [
- 'type' => 'highlight',
- 'id' => $param['targetUser'] . '::' . $param['nodeName'],
- 'name' => $param['nodeName'],
- ],
- 'user' => [
- 'type' => 'user',
- 'id' => $targetUser->getUID(),
- 'name' => $targetUser->getDisplayName(),
- ],
- ]);
- 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'))
- ->setRichMessage(
- $l->t('The ownership transfer of {path} from {user} failed.'),
- [
- 'path' => [
- 'type' => 'highlight',
- 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
- 'name' => $param['nodeName'],
- ],
- 'user' => [
- 'type' => 'user',
- 'id' => $sourceUser->getUID(),
- 'name' => $sourceUser->getDisplayName(),
- ],
- ]);
- return $notification;
- }
- public function handleTransferOwnershipDoneSource(INotification $notification, string $languageCode): INotification {
- $l = $this->l10nFactory->get('files', $languageCode);
- $param = $notification->getSubjectParameters();
- $targetUser = $this->getUser($param['targetUser']);
- $notification->setRichSubject($l->t('Ownership transfer done'))
- ->setRichMessage(
- $l->t('Your ownership transfer of {path} to {user} has completed.'),
- [
- 'path' => [
- 'type' => 'highlight',
- 'id' => $param['targetUser'] . '::' . $param['nodeName'],
- 'name' => $param['nodeName'],
- ],
- 'user' => [
- 'type' => 'user',
- 'id' => $targetUser->getUID(),
- 'name' => $targetUser->getDisplayName(),
- ],
- ]);
- return $notification;
- }
- public function handleTransferOwnershipDoneTarget(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 done'))
- ->setRichMessage(
- $l->t('The ownership transfer of {path} from {user} has completed.'),
- [
- 'path' => [
- 'type' => 'highlight',
- 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
- 'name' => $param['nodeName'],
- ],
- 'user' => [
- 'type' => 'user',
- 'id' => $sourceUser->getUID(),
- 'name' => $sourceUser->getDisplayName(),
- ],
- ]);
- return $notification;
- }
- public function dismissNotification(INotification $notification): void {
- if ($notification->getApp() !== 'files') {
- throw new UnknownNotificationException('Unhandled app');
- }
- if ($notification->getSubject() !== 'transferownershipRequest') {
- throw new UnknownNotificationException('Unhandled notification type');
- }
-
- try {
- $transferOwnership = $this->mapper->getById((int)$notification->getObjectId());
- } catch (DoesNotExistException $e) {
- return;
- }
- if ($this->jobList->has(TransferOwnership::class, [
- 'id' => $transferOwnership->getId(),
- ])) {
- return;
- }
- $notification = $this->notificationManager->createNotification();
- $notification->setUser($transferOwnership->getSourceUser())
- ->setApp('files')
- ->setDateTime($this->timeFactory->getDateTime())
- ->setSubject('transferownershipRequestDenied', [
- 'sourceUser' => $transferOwnership->getSourceUser(),
- 'targetUser' => $transferOwnership->getTargetUser(),
- 'nodeName' => $transferOwnership->getNodeName()
- ])
- ->setObject('transfer', (string)$transferOwnership->getId());
- $this->notificationManager->notify($notification);
- $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');
- }
- }
|