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(notifications): Add a dedicated exception when the notification i…
…s unknown to the notifier

Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Apr 12, 2024
commit c8e4a29dfa5665a99077b8eeed645786a946f375
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@
'OCP\\Notification\\INotifier' => $baseDir . '/lib/public/Notification/INotifier.php',
'OCP\\Notification\\IncompleteNotificationException' => $baseDir . '/lib/public/Notification/IncompleteNotificationException.php',
'OCP\\Notification\\InvalidValueException' => $baseDir . '/lib/public/Notification/InvalidValueException.php',
'OCP\\Notification\\UnknownNotificationException' => $baseDir . '/lib/public/Notification/UnknownNotificationException.php',
'OCP\\OCM\\Events\\ResourceTypeRegisterEvent' => $baseDir . '/lib/public/OCM/Events/ResourceTypeRegisterEvent.php',
'OCP\\OCM\\Exceptions\\OCMArgumentException' => $baseDir . '/lib/public/OCM/Exceptions/OCMArgumentException.php',
'OCP\\OCM\\Exceptions\\OCMProviderException' => $baseDir . '/lib/public/OCM/Exceptions/OCMProviderException.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 @@ -602,6 +602,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Notification\\INotifier' => __DIR__ . '/../../..' . '/lib/public/Notification/INotifier.php',
'OCP\\Notification\\IncompleteNotificationException' => __DIR__ . '/../../..' . '/lib/public/Notification/IncompleteNotificationException.php',
'OCP\\Notification\\InvalidValueException' => __DIR__ . '/../../..' . '/lib/public/Notification/InvalidValueException.php',
'OCP\\Notification\\UnknownNotificationException' => __DIR__ . '/../../..' . '/lib/public/Notification/UnknownNotificationException.php',
'OCP\\OCM\\Events\\ResourceTypeRegisterEvent' => __DIR__ . '/../../..' . '/lib/public/OCM/Events/ResourceTypeRegisterEvent.php',
'OCP\\OCM\\Exceptions\\OCMArgumentException' => __DIR__ . '/../../..' . '/lib/public/OCM/Exceptions/OCMArgumentException.php',
'OCP\\OCM\\Exceptions\\OCMProviderException' => __DIR__ . '/../../..' . '/lib/public/OCM/Exceptions/OCMProviderException.php',
Expand Down
25 changes: 17 additions & 8 deletions lib/private/Notification/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OCP\Notification\IncompleteNotificationException;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCP\Notification\UnknownNotificationException;
use OCP\RichObjectStrings\IValidator;
use OCP\Support\Subscription\IRegistry;
use Psr\Container\ContainerExceptionInterface;
Expand Down Expand Up @@ -343,24 +344,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 AlreadyProcessedException When the notification is not needed anymore and should be deleted
* @since 8.2.0
* {@inheritDoc}
*/
public function prepare(INotification $notification, string $languageCode): INotification {
$notifiers = $this->getNotifiers();

foreach ($notifiers as $notifier) {
try {
$notification = $notifier->prepare($notification, $languageCode);
} catch (\InvalidArgumentException $e) {
continue;
} catch (AlreadyProcessedException $e) {
$this->markProcessed($notification);
throw new \InvalidArgumentException('The given notification has been processed');
} catch (UnknownNotificationException) {
continue;
} catch (\InvalidArgumentException $e) {
// todo 33.0.0 Log as warning
// todo 39.0.0 Log as error
$this->logger->debug(get_class($notifier) . '::prepare() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\UnknownNotificationException when the notification is not known to your notifier and otherwise handle all \InvalidArgumentException yourself.');
continue;
}

if (!$notification->isValidParsed()) {
Expand Down Expand Up @@ -402,14 +403,22 @@ public function getCount(INotification $notification): int {
return $count;
}

/**
* {@inheritDoc}
*/
public function dismissNotification(INotification $notification): void {
$notifiers = $this->getNotifiers();

foreach ($notifiers as $notifier) {
if ($notifier instanceof IDismissableNotifier) {
try {
$notifier->dismissNotification($notification);
} catch (UnknownNotificationException) {
continue;
} catch (\InvalidArgumentException $e) {
// todo 33.0.0 Log as warning
// todo 39.0.0 Log as error
$this->logger->debug(get_class($notifier) . '::dismissNotification() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\UnknownNotificationException when the notification is not known to your notifier and otherwise handle all \InvalidArgumentException yourself.');
continue;
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/public/Notification/IDismissableNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
interface IDismissableNotifier extends INotifier {
/**
* @param INotification $notification
* @throws \InvalidArgumentException In case the handler can't handle the notification
* @throws UnknownNotificationException when the notifier is not in charge of the notification
*
* @since 18.0.0
* @since 30.0.0 Notifiers should throw {@see UnknownNotificationException} instead of \InvalidArgumentException
* when they did not handle the notification. Throwing \InvalidArgumentException directly is deprecated and will
* be logged as an error in Nextcloud 39.
*/
public function dismissNotification(INotification $notification): void;
}
7 changes: 5 additions & 2 deletions lib/public/Notification/INotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface INotifier {
public function getID(): string;

/**
* Human readable name describing the notifier
* Human-readable name describing the notifier
*
* @return string
* @since 17.0.0
Expand All @@ -50,9 +50,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
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
* @since 9.0.0
* @since 30.0.0 Notifiers should throw {@see UnknownNotificationException} instead of \InvalidArgumentException
* when they did not handle the notification. Throwing \InvalidArgumentException directly is deprecated and will
* be logged as an error in Nextcloud 39.
*/
public function prepare(INotification $notification, string $languageCode): INotification;
}
33 changes: 33 additions & 0 deletions lib/public/Notification/UnknownNotificationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\Notification;

/**
* @since 30.0.0
*/
class UnknownNotificationException extends \InvalidArgumentException {
}