fix: a push notification is not chat

This commit is contained in:
Maël Gangloff 2024-08-27 21:44:42 +02:00
parent 4b0148fe49
commit 83a97b429b
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
7 changed files with 94 additions and 8 deletions

View File

@ -177,8 +177,18 @@ class WatchListController extends AbstractController
/** @var AbstractTransportFactory $transportFactory */ /** @var AbstractTransportFactory $transportFactory */
$transportFactory = new $transportFactoryClass(); $transportFactory = new $transportFactoryClass();
$push = (new TestChatNotification())->asPushMessage();
$chat = (new TestChatNotification())->asChatMessage();
try { try {
$transportFactory->create($dsn)->send((new TestChatNotification())->asChatMessage()); $factory = $transportFactory->create($dsn);
if ($factory->supports($push)) {
$factory->send($push);
} elseif ($factory->supports($chat)) {
$factory->send($chat);
} else {
throw new BadRequestHttpException('Unsupported message type');
}
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
throw new BadRequestHttpException($exception->getMessage()); throw new BadRequestHttpException($exception->getMessage());
} }

View File

@ -7,13 +7,15 @@ use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Address;
use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\EmailMessage; use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface; use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface; use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification; use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface; use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface; use Symfony\Component\Notifier\Recipient\RecipientInterface;
class DomainOrderErrorNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface class DomainOrderErrorNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface, PushNotificationInterface
{ {
public function __construct( public function __construct(
private readonly Address $sender, private readonly Address $sender,
@ -32,6 +34,16 @@ class DomainOrderErrorNotification extends Notification implements ChatNotificat
return ChatMessage::fromNotification($this); return ChatMessage::fromNotification($this);
} }
public function asPushMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?PushMessage
{
$ldhName = $this->domain->getLdhName();
$this->subject("Error: Domain Order $ldhName")
->content("Domain name $ldhName tried to be purchased. The attempt failed.")
->importance(Notification::IMPORTANCE_HIGH);
return PushMessage::fromNotification($this);
}
public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): EmailMessage public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): EmailMessage
{ {
return new EmailMessage((new TemplatedEmail()) return new EmailMessage((new TemplatedEmail())

View File

@ -9,13 +9,15 @@ use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Email;
use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\EmailMessage; use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface; use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface; use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification; use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface; use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface; use Symfony\Component\Notifier\Recipient\RecipientInterface;
class DomainOrderNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface class DomainOrderNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface, PushNotificationInterface
{ {
public function __construct( public function __construct(
private readonly Address $sender, private readonly Address $sender,
@ -36,6 +38,17 @@ class DomainOrderNotification extends Notification implements ChatNotificationIn
return ChatMessage::fromNotification($this); return ChatMessage::fromNotification($this);
} }
public function asPushMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?PushMessage
{
$ldhName = $this->domain->getLdhName();
$this
->subject("Success: Domain Ordered $ldhName!")
->content("Domain name $ldhName has just been purchased. The API provider did not return an error.")
->importance(Notification::IMPORTANCE_HIGH);
return PushMessage::fromNotification($this);
}
public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): EmailMessage public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): EmailMessage
{ {
return new EmailMessage((new TemplatedEmail()) return new EmailMessage((new TemplatedEmail())

View File

@ -7,13 +7,15 @@ use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Address;
use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\EmailMessage; use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface; use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface; use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification; use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface; use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface; use Symfony\Component\Notifier\Recipient\RecipientInterface;
class DomainUpdateErrorNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface class DomainUpdateErrorNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface, PushNotificationInterface
{ {
public function __construct( public function __construct(
private readonly Address $sender, private readonly Address $sender,
@ -32,6 +34,16 @@ class DomainUpdateErrorNotification extends Notification implements ChatNotifica
return ChatMessage::fromNotification($this); return ChatMessage::fromNotification($this);
} }
public function asPushMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?PushMessage
{
$ldhName = $this->domain->getLdhName();
$this->subject("Error: Domain Update $ldhName")
->content("Domain name $ldhName tried to be updated. The attempt failed.")
->importance(Notification::IMPORTANCE_MEDIUM);
return PushMessage::fromNotification($this);
}
public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): EmailMessage public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): EmailMessage
{ {
return new EmailMessage((new TemplatedEmail()) return new EmailMessage((new TemplatedEmail())

View File

@ -8,13 +8,15 @@ use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Email;
use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\EmailMessage; use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface; use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface; use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification; use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface; use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface; use Symfony\Component\Notifier\Recipient\RecipientInterface;
class DomainUpdateNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface class DomainUpdateNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface, PushNotificationInterface
{ {
public function __construct( public function __construct(
private readonly Address $sender, private readonly Address $sender,
@ -34,6 +36,17 @@ class DomainUpdateNotification extends Notification implements ChatNotificationI
return ChatMessage::fromNotification($this); return ChatMessage::fromNotification($this);
} }
public function asPushMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?PushMessage
{
$ldhName = $this->domainEvent->getDomain()->getLdhName();
$action = $this->domainEvent->getAction();
$this->subject("Domain changed $ldhName ($action)")
->content("Domain name $ldhName information has been updated ($action).")
->importance(Notification::IMPORTANCE_HIGH);
return PushMessage::fromNotification($this);
}
public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): EmailMessage public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): EmailMessage
{ {
return new EmailMessage((new TemplatedEmail()) return new EmailMessage((new TemplatedEmail())

View File

@ -3,11 +3,13 @@
namespace App\Notifier; namespace App\Notifier;
use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface; use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification; use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface; use Symfony\Component\Notifier\Recipient\RecipientInterface;
class TestChatNotification extends Notification implements ChatNotificationInterface class TestChatNotification extends Notification implements ChatNotificationInterface, PushNotificationInterface
{ {
public function asChatMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?ChatMessage public function asChatMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?ChatMessage
{ {
@ -18,4 +20,14 @@ class TestChatNotification extends Notification implements ChatNotificationInter
return ChatMessage::fromNotification($this); return ChatMessage::fromNotification($this);
} }
public function asPushMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?PushMessage
{
$this
->subject('Test notification')
->content('This is a test message. If you can read me, this Webhook is configured correctly')
->importance(Notification::IMPORTANCE_LOW);
return PushMessage::fromNotification($this);
}
} }

View File

@ -4,9 +4,10 @@ namespace App\Service;
use App\Config\WebhookScheme; use App\Config\WebhookScheme;
use App\Entity\WatchList; use App\Entity\WatchList;
use App\Notifier\TestChatNotification;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface; use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn; use Symfony\Component\Notifier\Transport\Dsn;
@ -31,8 +32,21 @@ readonly class ChatNotificationService
$transportFactoryClass = $webhookScheme->getChatTransportFactory(); $transportFactoryClass = $webhookScheme->getChatTransportFactory();
/** @var AbstractTransportFactory $transportFactory */ /** @var AbstractTransportFactory $transportFactory */
$transportFactory = new $transportFactoryClass(); $transportFactory = new $transportFactoryClass();
$push = (new TestChatNotification())->asPushMessage();
$chat = (new TestChatNotification())->asChatMessage();
try { try {
$transportFactory->create($dsn)->send($notification->asChatMessage(new Recipient())); $factory = $transportFactory->create($dsn);
if ($factory->supports($push)) {
$factory->send($push);
} elseif ($factory->supports($chat)) {
$factory->send($chat);
} else {
throw new BadRequestHttpException('Unsupported message type');
}
$this->logger->info('Chat message sent with {schema} for Watchlist {token}', $this->logger->info('Chat message sent with {schema} for Watchlist {token}',
[ [
'scheme' => $webhookScheme->name, 'scheme' => $webhookScheme->name,