From 8130968a352bcf66606a076598fa4a58a291bc6d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 25 Jun 2024 11:20:48 +0200 Subject: [PATCH] feat(notifications): Migrate server INotifiers to new exceptions Signed-off-by: Joas Schilling --- apps/comments/lib/Notification/Notifier.php | 11 ++++++----- .../tests/Unit/Notification/NotifierTest.php | 12 +++++++----- apps/dav/lib/CalDAV/Reminder/Notifier.php | 7 ++++--- .../dav/tests/unit/CalDAV/Reminder/NotifierTest.php | 5 +++-- apps/federatedfilesharing/lib/Notifier.php | 7 ++++--- apps/files/lib/Notification/Notifier.php | 9 +++++---- apps/files_reminders/lib/Notification/Notifier.php | 13 +++++-------- apps/files_sharing/lib/Notification/Notifier.php | 9 +++++---- .../lib/Notifications/Notifier.php | 5 +++-- .../lib/Notification/AppUpdateNotifier.php | 13 ++++++++----- .../lib/Notification/Notifier.php | 10 +++++----- apps/user_ldap/lib/Notification/Notifier.php | 7 ++++--- core/Notification/CoreNotifier.php | 5 +++-- .../Authentication/Notifications/Notifier.php | 6 +++--- 14 files changed, 65 insertions(+), 54 deletions(-) diff --git a/apps/comments/lib/Notification/Notifier.php b/apps/comments/lib/Notification/Notifier.php index 4f114f22ef8f5..d5563ef7d8557 100644 --- a/apps/comments/lib/Notification/Notifier.php +++ b/apps/comments/lib/Notification/Notifier.php @@ -17,6 +17,7 @@ use OCP\Notification\AlreadyProcessedException; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; class Notifier implements INotifier { public function __construct( @@ -52,19 +53,19 @@ public function getName(): string { * @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 * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted * @since 9.0.0 */ public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'comments') { - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } try { $comment = $this->commentsManager->get($notification->getObjectId()); } catch (NotFoundException $e) { // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all - throw new \InvalidArgumentException('Comment not found', 0, $e); + throw new UnknownNotificationException('Comment not found', 0, $e); } $l = $this->l10nFactory->get('comments', $languageCode); $displayName = $comment->getActorId(); @@ -80,7 +81,7 @@ public function prepare(INotification $notification, string $languageCode): INot case 'mention': $parameters = $notification->getSubjectParameters(); if ($parameters[0] !== 'files') { - throw new \InvalidArgumentException('Unsupported comment object'); + throw new UnknownNotificationException('Unsupported comment object'); } $userFolder = $this->rootFolder->getUserFolder($notification->getUser()); $nodes = $userFolder->getById((int)$parameters[1]); @@ -128,7 +129,7 @@ public function prepare(INotification $notification, string $languageCode): INot break; default: - throw new \InvalidArgumentException('Invalid subject'); + throw new UnknownNotificationException('Invalid subject'); } } diff --git a/apps/comments/tests/Unit/Notification/NotifierTest.php b/apps/comments/tests/Unit/Notification/NotifierTest.php index 8830ca37265ca..a01c08a6760fb 100644 --- a/apps/comments/tests/Unit/Notification/NotifierTest.php +++ b/apps/comments/tests/Unit/Notification/NotifierTest.php @@ -16,7 +16,9 @@ use OCP\IURLGenerator; use OCP\IUserManager; use OCP\L10N\IFactory; +use OCP\Notification\AlreadyProcessedException; use OCP\Notification\INotification; +use OCP\Notification\UnknownNotificationException; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -304,7 +306,7 @@ public function testPrepareSuccessDeletedUser() { public function testPrepareDifferentApp() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UnknownNotificationException::class); $this->folder ->expects($this->never()) @@ -341,7 +343,7 @@ public function testPrepareDifferentApp() { public function testPrepareNotFound() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UnknownNotificationException::class); $this->folder ->expects($this->never()) @@ -379,7 +381,7 @@ public function testPrepareNotFound() { public function testPrepareDifferentSubject() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UnknownNotificationException::class); $displayName = 'Huraga'; @@ -436,7 +438,7 @@ public function testPrepareDifferentSubject() { public function testPrepareNotFiles() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UnknownNotificationException::class); $displayName = 'Huraga'; @@ -494,7 +496,7 @@ public function testPrepareNotFiles() { public function testPrepareUnresolvableFileID() { - $this->expectException(\OCP\Notification\AlreadyProcessedException::class); + $this->expectException(AlreadyProcessedException::class); $displayName = 'Huraga'; diff --git a/apps/dav/lib/CalDAV/Reminder/Notifier.php b/apps/dav/lib/CalDAV/Reminder/Notifier.php index 8863c165be1e6..f3c784ea21f8a 100644 --- a/apps/dav/lib/CalDAV/Reminder/Notifier.php +++ b/apps/dav/lib/CalDAV/Reminder/Notifier.php @@ -17,6 +17,7 @@ use OCP\Notification\AlreadyProcessedException; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; /** * Class Notifier @@ -78,12 +79,12 @@ public function getName():string { * @param INotification $notification * @param string $languageCode The code of the language that should be used to prepare the notification * @return INotification - * @throws \Exception + * @throws UnknownNotificationException */ public function prepare(INotification $notification, string $languageCode):INotification { if ($notification->getApp() !== Application::APP_ID) { - throw new \InvalidArgumentException('Notification not from this app'); + throw new UnknownNotificationException('Notification not from this app'); } // Read the language from the notification @@ -95,7 +96,7 @@ public function prepare(INotification $notification, return $this->prepareReminderNotification($notification); default: - throw new \InvalidArgumentException('Unknown subject'); + throw new UnknownNotificationException('Unknown subject'); } } diff --git a/apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php b/apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php index 6f319766d2146..dcf11a1a6b833 100644 --- a/apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php +++ b/apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php @@ -16,6 +16,7 @@ use OCP\L10N\IFactory; use OCP\Notification\AlreadyProcessedException; use OCP\Notification\INotification; +use OCP\Notification\UnknownNotificationException; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -88,7 +89,7 @@ public function testGetName():void { public function testPrepareWrongApp(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UnknownNotificationException::class); $this->expectExceptionMessage('Notification not from this app'); /** @var INotification|MockObject $notification */ @@ -105,7 +106,7 @@ public function testPrepareWrongApp(): void { public function testPrepareWrongSubject(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UnknownNotificationException::class); $this->expectExceptionMessage('Unknown subject'); /** @var INotification|MockObject $notification */ diff --git a/apps/federatedfilesharing/lib/Notifier.php b/apps/federatedfilesharing/lib/Notifier.php index 44e46b6a3ab48..4c4380d0875dd 100644 --- a/apps/federatedfilesharing/lib/Notifier.php +++ b/apps/federatedfilesharing/lib/Notifier.php @@ -15,6 +15,7 @@ use OCP\L10N\IFactory; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; class Notifier implements INotifier { /** @var IFactory */ @@ -65,12 +66,12 @@ public function getName(): string { * @param INotification $notification * @param string $languageCode The code of the language that should be used to prepare the notification * @return INotification - * @throws \InvalidArgumentException + * @throws UnknownNotificationException */ public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'files_sharing' || $notification->getObjectType() !== 'remote_share') { // Not my app => throw - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } // Read the language from the notification @@ -141,7 +142,7 @@ public function prepare(INotification $notification, string $languageCode): INot default: // Unknown subject => Unknown notification => throw - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } } diff --git a/apps/files/lib/Notification/Notifier.php b/apps/files/lib/Notification/Notifier.php index b9cda6c907abf..bae3a513df745 100644 --- a/apps/files/lib/Notification/Notifier.php +++ b/apps/files/lib/Notification/Notifier.php @@ -20,6 +20,7 @@ use OCP\Notification\IManager; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; class Notifier implements INotifier, IDismissableNotifier { /** @var IFactory */ @@ -62,11 +63,11 @@ public function getName(): string { * @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'); + throw new UnknownNotificationException('Unhandled app'); } return match($notification->getSubject()) { @@ -76,7 +77,7 @@ public function prepare(INotification $notification, string $languageCode): INot 'transferOwnershipFailedTarget' => $this->handleTransferOwnershipFailedTarget($notification, $languageCode), 'transferOwnershipDoneSource' => $this->handleTransferOwnershipDoneSource($notification, $languageCode), 'transferOwnershipDoneTarget' => $this->handleTransferOwnershipDoneTarget($notification, $languageCode), - default => throw new \InvalidArgumentException('Unhandled subject') + default => throw new UnknownNotificationException('Unhandled subject') }; } @@ -256,7 +257,7 @@ public function handleTransferOwnershipDoneTarget(INotification $notification, s public function dismissNotification(INotification $notification): void { if ($notification->getApp() !== 'files') { - throw new \InvalidArgumentException('Unhandled app'); + throw new UnknownNotificationException('Unhandled app'); } // TODO: This should all be moved to a service that also the transferownershipController uses. diff --git a/apps/files_reminders/lib/Notification/Notifier.php b/apps/files_reminders/lib/Notification/Notifier.php index 7f0c2d693223c..000b04119cd01 100644 --- a/apps/files_reminders/lib/Notification/Notifier.php +++ b/apps/files_reminders/lib/Notification/Notifier.php @@ -9,16 +9,15 @@ namespace OCA\FilesReminders\Notification; -use InvalidArgumentException; use OCA\FilesReminders\AppInfo\Application; use OCP\Files\FileInfo; use OCP\Files\IRootFolder; use OCP\IURLGenerator; use OCP\L10N\IFactory; -use OCP\Notification\AlreadyProcessedException; use OCP\Notification\IAction; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; class Notifier implements INotifier { public function __construct( @@ -37,14 +36,13 @@ public function getName(): string { } /** - * @throws InvalidArgumentException - * @throws AlreadyProcessedException + * @throws UnknownNotificationException */ public function prepare(INotification $notification, string $languageCode): INotification { $l = $this->l10nFactory->get(Application::APP_ID, $languageCode); if ($notification->getApp() !== Application::APP_ID) { - throw new InvalidArgumentException(); + throw new UnknownNotificationException(); } switch ($notification->getSubject()) { @@ -54,7 +52,7 @@ public function prepare(INotification $notification, string $languageCode): INot $node = $this->root->getUserFolder($notification->getUser())->getFirstNodeById($fileId); if (!$node) { - throw new InvalidArgumentException(); + throw new UnknownNotificationException(); } $path = rtrim($node->getPath(), '/'); @@ -92,8 +90,7 @@ public function prepare(INotification $notification, string $languageCode): INot $this->addActionButton($notification, $label); break; default: - throw new InvalidArgumentException(); - break; + throw new UnknownNotificationException(); } return $notification; diff --git a/apps/files_sharing/lib/Notification/Notifier.php b/apps/files_sharing/lib/Notification/Notifier.php index 42dc21b0b4aa3..3947e5aa5f223 100644 --- a/apps/files_sharing/lib/Notification/Notifier.php +++ b/apps/files_sharing/lib/Notification/Notifier.php @@ -19,6 +19,7 @@ use OCP\Notification\AlreadyProcessedException; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager; use OCP\Share\IShare; @@ -78,7 +79,7 @@ public function getName(): string { * @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 * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted * @since 9.0.0 */ @@ -86,7 +87,7 @@ public function prepare(INotification $notification, string $languageCode): INot if ($notification->getApp() !== 'files_sharing' || ($notification->getSubject() !== 'expiresTomorrow' && $notification->getObjectType() !== 'share')) { - throw new \InvalidArgumentException('Unhandled app or subject'); + throw new UnknownNotificationException('Unhandled app or subject'); } $l = $this->l10nFactory->get('files_sharing', $languageCode); @@ -145,7 +146,7 @@ protected function parseShareInvitation(IShare $share, INotification $notificati throw new AlreadyProcessedException(); } } else { - throw new \InvalidArgumentException('Invalid share type'); + throw new UnknownNotificationException('Invalid share type'); } switch ($notification->getSubject()) { @@ -216,7 +217,7 @@ protected function parseShareInvitation(IShare $share, INotification $notificati break; default: - throw new \InvalidArgumentException('Invalid subject'); + throw new UnknownNotificationException('Invalid subject'); } $notification->setRichSubject($subject, $subjectParameters) diff --git a/apps/twofactor_backupcodes/lib/Notifications/Notifier.php b/apps/twofactor_backupcodes/lib/Notifications/Notifier.php index cc8b0f026a031..ec24a59641fa5 100644 --- a/apps/twofactor_backupcodes/lib/Notifications/Notifier.php +++ b/apps/twofactor_backupcodes/lib/Notifications/Notifier.php @@ -12,6 +12,7 @@ use OCP\L10N\IFactory; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; class Notifier implements INotifier { @@ -49,7 +50,7 @@ public function getName(): string { public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'twofactor_backupcodes') { // Not my app => throw - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } // Read the language from the notification @@ -71,7 +72,7 @@ public function prepare(INotification $notification, string $languageCode): INot default: // Unknown subject => Unknown notification => throw - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } } } diff --git a/apps/updatenotification/lib/Notification/AppUpdateNotifier.php b/apps/updatenotification/lib/Notification/AppUpdateNotifier.php index 25777db6367e5..353ca883aba44 100644 --- a/apps/updatenotification/lib/Notification/AppUpdateNotifier.php +++ b/apps/updatenotification/lib/Notification/AppUpdateNotifier.php @@ -13,10 +13,12 @@ use OCP\IURLGenerator; use OCP\IUserManager; use OCP\L10N\IFactory; +use OCP\Notification\AlreadyProcessedException; use OCP\Notification\IAction; use OCP\Notification\IManager as INotificationManager; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; use Psr\Log\LoggerInterface; class AppUpdateNotifier implements INotifier { @@ -46,26 +48,27 @@ public function getName(): string { * @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 + * @throws AlreadyProcessedException When the app is no longer known */ public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== Application::APP_NAME) { - throw new \InvalidArgumentException('Unknown app'); + throw new UnknownNotificationException('Unknown app'); } if ($notification->getSubject() !== 'app_updated') { - throw new \InvalidArgumentException('Unknown subject'); + throw new UnknownNotificationException('Unknown subject'); } $appId = $notification->getSubjectParameters()[0]; $appInfo = $this->appManager->getAppInfo($appId, lang:$languageCode); if ($appInfo === null) { - throw new \InvalidArgumentException('App info not found'); + throw new AlreadyProcessedException(); } // Prepare translation factory for requested language $l = $this->l10nFactory->get(Application::APP_NAME, $languageCode); - + $icon = $this->appManager->getAppIcon($appId, true); if ($icon === null) { $icon = $this->urlGenerator->imagePath('core', 'actions/change.svg'); diff --git a/apps/updatenotification/lib/Notification/Notifier.php b/apps/updatenotification/lib/Notification/Notifier.php index 87f22d60f60b4..6f1e8514b11a3 100644 --- a/apps/updatenotification/lib/Notification/Notifier.php +++ b/apps/updatenotification/lib/Notification/Notifier.php @@ -19,6 +19,7 @@ use OCP\Notification\IManager; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; use OCP\Util; class Notifier implements INotifier { @@ -87,25 +88,24 @@ public function getName(): string { * @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 * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted * @since 9.0.0 */ public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'updatenotification') { - throw new \InvalidArgumentException('Unknown app id'); + throw new UnknownNotificationException('Unknown app id'); } if ($notification->getSubject() !== 'update_available' && $notification->getSubject() !== 'connection_error') { - throw new \InvalidArgumentException('Unknown subject'); + throw new UnknownNotificationException('Unknown subject'); } $l = $this->l10NFactory->get('updatenotification', $languageCode); if ($notification->getSubject() === 'connection_error') { $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', '0'); if ($errors === 0) { - $this->notificationManager->markProcessed($notification); - throw new \InvalidArgumentException('Update checked worked again'); + throw new AlreadyProcessedException(); } $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors])) diff --git a/apps/user_ldap/lib/Notification/Notifier.php b/apps/user_ldap/lib/Notification/Notifier.php index 82050bd2596d9..ca0fa6f033562 100644 --- a/apps/user_ldap/lib/Notification/Notifier.php +++ b/apps/user_ldap/lib/Notification/Notifier.php @@ -8,6 +8,7 @@ use OCP\L10N\IFactory; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; class Notifier implements INotifier { @@ -45,12 +46,12 @@ public function getName(): string { * @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() !== 'user_ldap') { // Not my app => throw - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } // Read the language from the notification @@ -76,7 +77,7 @@ public function prepare(INotification $notification, string $languageCode): INot default: // Unknown subject => Unknown notification => throw - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } } } diff --git a/core/Notification/CoreNotifier.php b/core/Notification/CoreNotifier.php index dfe48286432db..83a86513e03c1 100644 --- a/core/Notification/CoreNotifier.php +++ b/core/Notification/CoreNotifier.php @@ -14,6 +14,7 @@ use OCP\Notification\IAction; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; class CoreNotifier implements INotifier { public function __construct( @@ -45,7 +46,7 @@ public function getName(): string { public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'core') { - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } $l = $this->factory->get('core', $languageCode); @@ -71,6 +72,6 @@ public function prepare(INotification $notification, string $languageCode): INot return $notification; } - throw new \InvalidArgumentException('Invalid subject'); + throw new UnknownNotificationException('Invalid subject'); } } diff --git a/lib/private/Authentication/Notifications/Notifier.php b/lib/private/Authentication/Notifications/Notifier.php index 3b6c9b3e61041..a81e385d8b179 100644 --- a/lib/private/Authentication/Notifications/Notifier.php +++ b/lib/private/Authentication/Notifications/Notifier.php @@ -8,10 +8,10 @@ */ namespace OC\Authentication\Notifications; -use InvalidArgumentException; use OCP\L10N\IFactory as IL10nFactory; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; class Notifier implements INotifier { /** @var IL10nFactory */ @@ -27,7 +27,7 @@ public function __construct(IL10nFactory $l10nFactory) { public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'auth') { // Not my app => throw - throw new InvalidArgumentException(); + throw new UnknownNotificationException(); } // Read the language from the notification @@ -52,7 +52,7 @@ public function prepare(INotification $notification, string $languageCode): INot return $notification; default: // Unknown subject => Unknown notification => throw - throw new InvalidArgumentException(); + throw new UnknownNotificationException(); } }