Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(activity): Add a dedicated exception when not all fields are set …
…while publishing an activity

Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Apr 17, 2024
commit 8f83953ff144fe4d0f74353bcf4e027bfee939ff
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 3 additions & 11 deletions lib/private/Activity/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,16 +128,7 @@ public function generateEvent(): IEvent {
}

/**
* 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() === '') {
Expand All @@ -150,7 +142,7 @@ public function publish(IEvent $event): void {
}

if (!$event->isValid()) {
throw new \BadMethodCallException('The given event is invalid');
throw new IncompleteActivityException('The given event is invalid');
}

foreach ($this->getConsumers() as $c) {
Expand Down
43 changes: 43 additions & 0 deletions lib/public/Activity/Exceptions/IncompleteActivityException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Joas Schilling <[email protected]>
*
* @author Joas Schilling <[email protected]>
*
* @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 {
}
5 changes: 4 additions & 1 deletion lib/public/Activity/IManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
namespace OCP\Activity;

use OCP\Activity\Exceptions\IncompleteActivityException;

/**
* Interface IManager
*
Expand Down Expand Up @@ -61,8 +63,9 @@ public function generateEvent(): IEvent;
* - 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;

Expand Down
9 changes: 5 additions & 4 deletions tests/lib/Activity/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace Test\Activity;

use OCP\Activity\Exceptions\IncompleteActivityException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
Expand Down Expand Up @@ -167,15 +168,15 @@ protected function mockUserSession($user) {


public function testPublishExceptionNoApp() {
$this->expectException(\BadMethodCallException::class);
$this->expectException(IncompleteActivityException::class);

$event = $this->activityManager->generateEvent();
$this->activityManager->publish($event);
}


public function testPublishExceptionNoType() {
$this->expectException(\BadMethodCallException::class);
$this->expectException(IncompleteActivityException::class);

$event = $this->activityManager->generateEvent();
$event->setApp('test');
Expand All @@ -184,7 +185,7 @@ public function testPublishExceptionNoType() {


public function testPublishExceptionNoAffectedUser() {
$this->expectException(\BadMethodCallException::class);
$this->expectException(IncompleteActivityException::class);

$event = $this->activityManager->generateEvent();
$event->setApp('test')
Expand All @@ -194,7 +195,7 @@ public function testPublishExceptionNoAffectedUser() {


public function testPublishExceptionNoSubject() {
$this->expectException(\BadMethodCallException::class);
$this->expectException(IncompleteActivityException::class);

$event = $this->activityManager->generateEvent();
$event->setApp('test')
Expand Down