fix(activity): Add a dedicated exception when not all fields are set while publishing an activity

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2024-04-17 13:13:10 +02:00
parent 661143a1d1
commit 8f83953ff1
No known key found for this signature in database
GPG Key ID: 74434EFE0D2E2205
6 changed files with 57 additions and 16 deletions

View File

@ -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',

View File

@ -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',

View File

@ -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) {

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2024 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.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/>.
*
*/
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 {
}

View File

@ -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;

View File

@ -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')