fix(Notifications): test-notifier command

This commit is contained in:
2025-11-21 23:58:17 +02:00
parent 6647378e29
commit 8ecddc032f
2 changed files with 21 additions and 40 deletions

View File

@@ -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('<info>Testing Forum Notifier...</info>');
$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('<info>All tests passed! The notifier is working correctly.</info>');
$output->writeln('<info>Check the notifications in Nextcloud UI for user: ' . $username . '</info>');
return 0;
} catch (\Exception $e) {

View File

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