From 8ecddc032f11eae72ba6a12447313d87b2d01570 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Fri, 21 Nov 2025 23:58:17 +0200 Subject: [PATCH] fix(Notifications): test-notifier command --- lib/Command/TestNotifier.php | 53 +++++++---------------------- lib/Service/NotificationService.php | 8 +++++ 2 files changed, 21 insertions(+), 40 deletions(-) diff --git a/lib/Command/TestNotifier.php b/lib/Command/TestNotifier.php index e9d5dbf..cc25096 100644 --- a/lib/Command/TestNotifier.php +++ b/lib/Command/TestNotifier.php @@ -7,17 +7,15 @@ declare(strict_types=1); namespace OCA\Forum\Command; -use OCA\Forum\Notification\Notifier; -use OCP\L10N\IFactory; -use OCP\Notification\IManager as INotificationManager; +use OCA\Forum\Service\NotificationService; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class TestNotifier extends Command { public function __construct( - private INotificationManager $notificationManager, - private IFactory $l10nFactory, + private NotificationService $notificationService, ) { parent::__construct(); } @@ -25,49 +23,24 @@ class TestNotifier extends Command { protected function configure(): void { parent::configure(); $this->setName('forum:test-notifier') - ->setDescription('Test the forum notification system'); + ->setDescription('Test the forum notification system') + ->addArgument('username', InputArgument::REQUIRED, 'The username to send the test notification to'); } protected function execute(InputInterface $input, OutputInterface $output): int { try { + $username = $input->getArgument('username'); $output->writeln('Testing Forum Notifier...'); + $output->writeln(' Target user: ' . $username); - // Instantiate the notifier - $notifier = new Notifier($this->l10nFactory); - - $output->writeln('✓ Notifier instantiated successfully'); - $output->writeln(' ID: ' . $notifier->getID()); - $output->writeln(' Name: ' . $notifier->getName()); - - // Create a test notification (matching production structure) - $notification = $this->notificationManager->createNotification(); - - $notification->setApp('forum') - ->setUser('admin') - ->setDateTime(new \DateTime()) - ->setObject('thread', '1') - ->setSubject('new_posts', [ - 'threadId' => 1, - 'threadTitle' => 'Test Thread', - 'threadSlug' => 'test-thread', - 'lastPostId' => 1, - 'postCount' => 1, - ]) - ->setLink('http://localhost/apps/forum/t/test-thread') - ->setIcon('http://localhost/apps/forum/img/app-dark.svg'); - - $output->writeln('✓ Test notification created'); - - // Try to prepare it - $prepared = $notifier->prepare($notification, 'en'); - - $output->writeln('✓ Notification prepared successfully'); - $output->writeln(' Subject: ' . $prepared->getParsedSubject()); - $output->writeln(' Link: ' . $prepared->getLink()); - $output->writeln(' Icon: ' . $prepared->getIcon()); + // Send a test notification using the production notification service + $this->notificationService->sendTestNotification($username); + $output->writeln('✓ Test notification sent successfully'); + $output->writeln(' Thread: Test Thread'); + $output->writeln(' Slug: test-thread'); $output->writeln(''); - $output->writeln('All tests passed! The notifier is working correctly.'); + $output->writeln('Check the notifications in Nextcloud UI for user: ' . $username . ''); return 0; } catch (\Exception $e) { diff --git a/lib/Service/NotificationService.php b/lib/Service/NotificationService.php index fff0fa5..ed30166 100644 --- a/lib/Service/NotificationService.php +++ b/lib/Service/NotificationService.php @@ -59,6 +59,14 @@ class NotificationService { } } + /** + * Send a test notification to a user + * Useful for testing the notification system + */ + public function sendTestNotification(string $userId): void { + $this->createOrUpdateNotification($userId, 1, 1, 'Test Thread', 'test-thread'); + } + /** * Create or update a notification for a user about a thread * This allows collating multiple posts into a single notification