From 8f83953ff144fe4d0f74353bcf4e027bfee939ff Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 17 Apr 2024 13:13:10 +0200 Subject: [PATCH] fix(activity): Add a dedicated exception when not all fields are set while publishing an activity Signed-off-by: Joas Schilling --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Activity/Manager.php | 14 ++---- .../IncompleteActivityException.php | 43 +++++++++++++++++++ lib/public/Activity/IManager.php | 5 ++- tests/lib/Activity/ManagerTest.php | 9 ++-- 6 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 lib/public/Activity/Exceptions/IncompleteActivityException.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 8d6412c724f..20ea47bbb3c 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -14,6 +14,7 @@ return array( 'OCP\\Accounts\\PropertyDoesNotExistException' => $baseDir . '/lib/public/Accounts/PropertyDoesNotExistException.php', 'OCP\\Accounts\\UserUpdatedEvent' => $baseDir . '/lib/public/Accounts/UserUpdatedEvent.php', 'OCP\\Activity\\ActivitySettings' => $baseDir . '/lib/public/Activity/ActivitySettings.php', + 'OCP\\Activity\\Exceptions\\IncompleteActivityException' => $baseDir . '/lib/public/Activity/Exceptions/IncompleteActivityException.php', 'OCP\\Activity\\Exceptions\\InvalidValueException' => $baseDir . '/lib/public/Activity/Exceptions/InvalidValueException.php', 'OCP\\Activity\\IConsumer' => $baseDir . '/lib/public/Activity/IConsumer.php', 'OCP\\Activity\\IEvent' => $baseDir . '/lib/public/Activity/IEvent.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 31583ef51a3..e48cae907ae 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -47,6 +47,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Accounts\\PropertyDoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/Accounts/PropertyDoesNotExistException.php', 'OCP\\Accounts\\UserUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/Accounts/UserUpdatedEvent.php', 'OCP\\Activity\\ActivitySettings' => __DIR__ . '/../../..' . '/lib/public/Activity/ActivitySettings.php', + 'OCP\\Activity\\Exceptions\\IncompleteActivityException' => __DIR__ . '/../../..' . '/lib/public/Activity/Exceptions/IncompleteActivityException.php', 'OCP\\Activity\\Exceptions\\InvalidValueException' => __DIR__ . '/../../..' . '/lib/public/Activity/Exceptions/InvalidValueException.php', 'OCP\\Activity\\IConsumer' => __DIR__ . '/../../..' . '/lib/public/Activity/IConsumer.php', 'OCP\\Activity\\IEvent' => __DIR__ . '/../../..' . '/lib/public/Activity/IEvent.php', diff --git a/lib/private/Activity/Manager.php b/lib/private/Activity/Manager.php index b8568a18b81..3458ea041c3 100644 --- a/lib/private/Activity/Manager.php +++ b/lib/private/Activity/Manager.php @@ -29,6 +29,7 @@ namespace OC\Activity; use OCP\Activity\ActivitySettings; +use OCP\Activity\Exceptions\IncompleteActivityException; use OCP\Activity\IConsumer; use OCP\Activity\IEvent; use OCP\Activity\IFilter; @@ -127,16 +128,7 @@ class Manager implements IManager { } /** - * Publish an event to the activity consumers - * - * Make sure to call at least the following methods before sending an Event: - * - setApp() - * - setType() - * - setAffectedUser() - * - setSubject() - * - * @param IEvent $event - * @throws \BadMethodCallException if required values have not been set + * {@inheritDoc} */ public function publish(IEvent $event): void { if ($event->getAuthor() === '') { @@ -150,7 +142,7 @@ class Manager implements IManager { } if (!$event->isValid()) { - throw new \BadMethodCallException('The given event is invalid'); + throw new IncompleteActivityException('The given event is invalid'); } foreach ($this->getConsumers() as $c) { diff --git a/lib/public/Activity/Exceptions/IncompleteActivityException.php b/lib/public/Activity/Exceptions/IncompleteActivityException.php new file mode 100644 index 00000000000..14cb6e7e5d6 --- /dev/null +++ b/lib/public/Activity/Exceptions/IncompleteActivityException.php @@ -0,0 +1,43 @@ + + * + * @author Joas Schilling + * + * @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 . + * + */ + +namespace OCP\Activity\Exceptions; + +/** + * Thrown when {@see \OCP\Notification\IManager::notify()} is called with a notification + * that does not have all required fields set: + * + * - app + * - type + * - affectedUser + * - subject + * - objectType + * - objectId + * + * @since 30.0.0 + */ +class IncompleteActivityException extends \BadMethodCallException { +} diff --git a/lib/public/Activity/IManager.php b/lib/public/Activity/IManager.php index 8340b54f757..6de21d4347e 100644 --- a/lib/public/Activity/IManager.php +++ b/lib/public/Activity/IManager.php @@ -28,6 +28,8 @@ declare(strict_types=1); */ namespace OCP\Activity; +use OCP\Activity\Exceptions\IncompleteActivityException; + /** * Interface IManager * @@ -61,8 +63,9 @@ interface IManager { * - setObject() * * @param IEvent $event - * @throws \BadMethodCallException if required values have not been set + * @throws IncompleteActivityException if required values have not been set * @since 8.2.0 + * @since 30.0.0 throws {@see IncompleteActivityException} instead of \BadMethodCallException */ public function publish(IEvent $event): void; diff --git a/tests/lib/Activity/ManagerTest.php b/tests/lib/Activity/ManagerTest.php index 9a39f21f4e5..0a38e3dd993 100644 --- a/tests/lib/Activity/ManagerTest.php +++ b/tests/lib/Activity/ManagerTest.php @@ -23,6 +23,7 @@ namespace Test\Activity; +use OCP\Activity\Exceptions\IncompleteActivityException; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -167,7 +168,7 @@ class ManagerTest extends TestCase { public function testPublishExceptionNoApp() { - $this->expectException(\BadMethodCallException::class); + $this->expectException(IncompleteActivityException::class); $event = $this->activityManager->generateEvent(); $this->activityManager->publish($event); @@ -175,7 +176,7 @@ class ManagerTest extends TestCase { public function testPublishExceptionNoType() { - $this->expectException(\BadMethodCallException::class); + $this->expectException(IncompleteActivityException::class); $event = $this->activityManager->generateEvent(); $event->setApp('test'); @@ -184,7 +185,7 @@ class ManagerTest extends TestCase { public function testPublishExceptionNoAffectedUser() { - $this->expectException(\BadMethodCallException::class); + $this->expectException(IncompleteActivityException::class); $event = $this->activityManager->generateEvent(); $event->setApp('test') @@ -194,7 +195,7 @@ class ManagerTest extends TestCase { public function testPublishExceptionNoSubject() { - $this->expectException(\BadMethodCallException::class); + $this->expectException(IncompleteActivityException::class); $event = $this->activityManager->generateEvent(); $event->setApp('test')