diff options
Diffstat (limited to 'apps/files/lib/Notification/Notifier.php')
-rw-r--r-- | apps/files/lib/Notification/Notifier.php | 164 |
1 files changed, 72 insertions, 92 deletions
diff --git a/apps/files/lib/Notification/Notifier.php b/apps/files/lib/Notification/Notifier.php index 8f885a995f9..6acc312c126 100644 --- a/apps/files/lib/Notification/Notifier.php +++ b/apps/files/lib/Notification/Notifier.php @@ -3,33 +3,16 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Sascha Wiswedel <sascha.wiswedel@nextcloud.com> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ 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; @@ -39,35 +22,18 @@ 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 { - - /** @var IFactory */ - protected $l10nFactory; - - /** @var IURLGenerator */ - protected $urlGenerator; - /** @var TransferOwnershipMapper */ - private $mapper; - /** @var IManager */ - private $notificationManager; - /** @var IUserManager */ - private $userManager; - /** @var ITimeFactory */ - private $timeFactory; - - 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; + 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 { @@ -82,30 +48,26 @@ class Notifier implements INotifier, IDismissableNotifier { * @param INotification $notification * @param string $languageCode The code of the language that should be used to prepare the notification * @return INotification - * @throws \InvalidArgumentException When the notification was not prepared by a notifier + * @throws UnknownNotificationException When the notification was not prepared by a notifier */ public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'files') { - throw new \InvalidArgumentException('Unhandled app'); - } - - if ($notification->getSubject() === 'transferownershipRequest') { - return $this->handleTransferownershipRequest($notification, $languageCode); - } - if ($notification->getSubject() === 'transferOwnershipFailedSource') { - return $this->handleTransferOwnershipFailedSource($notification, $languageCode); - } - if ($notification->getSubject() === 'transferOwnershipFailedTarget') { - return $this->handleTransferOwnershipFailedTarget($notification, $languageCode); - } - if ($notification->getSubject() === 'transferOwnershipDoneSource') { - return $this->handleTransferOwnershipDoneSource($notification, $languageCode); - } - if ($notification->getSubject() === 'transferOwnershipDoneTarget') { - return $this->handleTransferOwnershipDoneTarget($notification, $languageCode); + throw new UnknownNotificationException('Unhandled app'); } - throw new \InvalidArgumentException('Unhandled subject'); + $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 { @@ -151,7 +113,6 @@ class Notifier implements INotifier, IDismissableNotifier { 'name' => $sourceUser->getDisplayName(), ], ]) - ->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."), [ @@ -160,20 +121,40 @@ class Notifier implements INotifier, IDismissableNotifier { 'id' => $param['targetUser'] . '::' . $param['nodeName'], 'name' => $param['nodeName'], ] - ]) - ->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."))); + ]); return $notification; } - public function handleTransferOwnershipFailedSource(INotification $notification, string $languageCode): INotification { + 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 failed')) - ->setParsedSubject($l->t('Ownership transfer failed')) + $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.'), [ @@ -187,19 +168,16 @@ class Notifier implements INotifier, IDismissableNotifier { 'id' => $targetUser->getUID(), 'name' => $targetUser->getDisplayName(), ], - ]) - ->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 { + 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')) - ->setRichMessage( $l->t('The ownership transfer of {path} from {user} failed.'), [ @@ -213,20 +191,17 @@ class Notifier implements INotifier, IDismissableNotifier { 'id' => $sourceUser->getUID(), 'name' => $sourceUser->getDisplayName(), ], - ]) - ->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $sourceUser->getDisplayName()], $l->t('The ownership transfer of {path} from {user} failed.'))); + ]); return $notification; } - public function handleTransferOwnershipDoneSource(INotification $notification, string $languageCode): INotification { + 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')) - ->setParsedSubject($l->t('Ownership transfer done')) - ->setRichMessage( $l->t('Your ownership transfer of {path} to {user} has completed.'), [ @@ -240,20 +215,17 @@ class Notifier implements INotifier, IDismissableNotifier { 'id' => $targetUser->getUID(), 'name' => $targetUser->getDisplayName(), ], - ]) - ->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $targetUser->getDisplayName()], $l->t('Your ownership transfer of {path} to {user} has completed.'))); + ]); return $notification; } - public function handleTransferOwnershipDoneTarget(INotification $notification, string $languageCode): INotification { + 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')) - ->setParsedSubject($l->t('Ownership transfer done')) - ->setRichMessage( $l->t('The ownership transfer of {path} from {user} has completed.'), [ @@ -267,24 +239,32 @@ class Notifier implements INotifier, IDismissableNotifier { 'id' => $sourceUser->getUID(), 'name' => $sourceUser->getDisplayName(), ], - ]) - ->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $sourceUser->getDisplayName()], $l->t('The ownership transfer of {path} from {user} has completed.'))); + ]); return $notification; } public function dismissNotification(INotification $notification): void { if ($notification->getApp() !== 'files') { - throw new \InvalidArgumentException('Unhandled app'); + throw new UnknownNotificationException('Unhandled app'); + } + if ($notification->getSubject() !== 'transferownershipRequest') { + throw new UnknownNotificationException('Unhandled notification type'); } - // TODO: This should all be moved to a service that also the transferownershipContoller uses. + // TODO: This should all be moved to a service that also the transferownershipController uses. 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') |