Skip to content

Commit

Permalink
feat(notifications): Migrate server INotifiers to new exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jun 25, 2024
1 parent 9496ce6 commit 714ca90
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 53 deletions.
11 changes: 6 additions & 5 deletions apps/comments/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand All @@ -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]);
Expand Down Expand Up @@ -128,7 +129,7 @@ public function prepare(INotification $notification, string $languageCode): INot
break;

default:
throw new \InvalidArgumentException('Invalid subject');
throw new UnknownNotificationException('Invalid subject');
}
}

Expand Down
12 changes: 7 additions & 5 deletions apps/comments/tests/Unit/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -304,7 +306,7 @@ public function testPrepareSuccessDeletedUser() {


public function testPrepareDifferentApp() {
$this->expectException(\InvalidArgumentException::class);
$this->expectException(UnknownNotificationException::class);

$this->folder
->expects($this->never())
Expand Down Expand Up @@ -341,7 +343,7 @@ public function testPrepareDifferentApp() {


public function testPrepareNotFound() {
$this->expectException(\InvalidArgumentException::class);
$this->expectException(UnknownNotificationException::class);

$this->folder
->expects($this->never())
Expand Down Expand Up @@ -379,7 +381,7 @@ public function testPrepareNotFound() {


public function testPrepareDifferentSubject() {
$this->expectException(\InvalidArgumentException::class);
$this->expectException(UnknownNotificationException::class);

$displayName = 'Huraga';

Expand Down Expand Up @@ -436,7 +438,7 @@ public function testPrepareDifferentSubject() {


public function testPrepareNotFiles() {
$this->expectException(\InvalidArgumentException::class);
$this->expectException(UnknownNotificationException::class);

$displayName = 'Huraga';

Expand Down Expand Up @@ -494,7 +496,7 @@ public function testPrepareNotFiles() {


public function testPrepareUnresolvableFileID() {
$this->expectException(\OCP\Notification\AlreadyProcessedException::class);
$this->expectException(AlreadyProcessedException::class);

$displayName = 'Huraga';

Expand Down
7 changes: 4 additions & 3 deletions apps/dav/lib/CalDAV/Reminder/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCP\Notification\UnknownNotificationException;

/**
* Class Notifier
Expand Down Expand Up @@ -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
Expand All @@ -95,7 +96,7 @@ public function prepare(INotification $notification,
return $this->prepareReminderNotification($notification);

default:
throw new \InvalidArgumentException('Unknown subject');
throw new UnknownNotificationException('Unknown subject');

}
}
Expand Down
5 changes: 3 additions & 2 deletions apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down
7 changes: 4 additions & 3 deletions apps/federatedfilesharing/lib/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -141,7 +142,7 @@ public function prepare(INotification $notification, string $languageCode): INot

default:
// Unknown subject => Unknown notification => throw
throw new \InvalidArgumentException();
throw new UnknownNotificationException();
}
}

Expand Down
9 changes: 5 additions & 4 deletions apps/files/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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()) {
Expand All @@ -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')
};
}

Expand Down Expand Up @@ -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.
Expand Down
12 changes: 5 additions & 7 deletions apps/files_reminders/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace OCA\FilesReminders\Notification;

use InvalidArgumentException;
use OCA\FilesReminders\AppInfo\Application;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
Expand All @@ -19,6 +18,7 @@
use OCP\Notification\IAction;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCP\Notification\UnknownNotificationException;

class Notifier implements INotifier {
public function __construct(
Expand All @@ -37,14 +37,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()) {
Expand All @@ -54,7 +53,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(), '/');
Expand Down Expand Up @@ -92,8 +91,7 @@ public function prepare(INotification $notification, string $languageCode): INot
$this->addActionButton($notification, $label);
break;
default:
throw new InvalidArgumentException();
break;
throw new UnknownNotificationException();
}

return $notification;
Expand Down
9 changes: 5 additions & 4 deletions apps/files_sharing/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,15 +79,15 @@ 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() !== '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);
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions apps/twofactor_backupcodes/lib/Notifications/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCP\L10N\IFactory;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCP\Notification\UnknownNotificationException;

class Notifier implements INotifier {

Expand Down Expand Up @@ -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
Expand All @@ -71,7 +72,7 @@ public function prepare(INotification $notification, string $languageCode): INot

default:
// Unknown subject => Unknown notification => throw
throw new \InvalidArgumentException();
throw new UnknownNotificationException();
}
}
}
Loading

0 comments on commit 714ca90

Please sign in to comment.