feat: add Webhook content message

This commit is contained in:
Maël Gangloff
2024-08-17 00:36:00 +02:00
parent 64aba20a93
commit bb96da8fe3
8 changed files with 85 additions and 43 deletions

View File

@@ -35,6 +35,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Routing\Attribute\Route;
@@ -50,6 +51,29 @@ class WatchListController extends AbstractController
) {
}
/**
* @throws TransportExceptionInterface
*/
private function verifyWebhookDSN(WatchList $watchList): void
{
if (null !== $watchList->getWebhookDsn()) {
foreach ($watchList->getWebhookDsn() as $dsnString) {
$dsn = new Dsn($dsnString);
$scheme = $dsn->getScheme();
$webhookScheme = WebhookScheme::tryFrom($scheme);
if (null === $webhookScheme) {
throw new BadRequestHttpException("The DSN scheme ($scheme) is not supported");
}
$transportFactoryClass = $webhookScheme->getChatTransportFactory();
/** @var AbstractTransportFactory $transportFactory */
$transportFactory = new $transportFactoryClass();
$transportFactory->create($dsn)->send((new TestChatNotification())->asChatMessage());
}
}
}
/**
* @throws \Exception
*/
@@ -70,22 +94,7 @@ class WatchListController extends AbstractController
$user = $this->getUser();
$watchList->setUser($user);
if (null !== $watchList->getWebhookDsn()) {
foreach ($watchList->getWebhookDsn() as $dsnString) {
$dsn = new Dsn($dsnString);
$scheme = $dsn->getScheme();
$webhookScheme = WebhookScheme::tryFrom($scheme);
if (null === $webhookScheme) {
throw new BadRequestHttpException("The DSN scheme ($scheme) is not supported");
}
$transportFactoryClass = $webhookScheme->getChatTransportFactory();
/** @var AbstractTransportFactory $transportFactory */
$transportFactory = new $transportFactoryClass();
$transportFactory->create($dsn)->send((new TestChatNotification())->asChatMessage());
}
}
$this->verifyWebhookDSN($watchList);
/*
* In the limited version, we do not want a user to be able to register the same domain more than once in their watchlists.
@@ -173,15 +182,7 @@ class WatchListController extends AbstractController
$user = $this->getUser();
$watchList->setUser($user);
if (null !== $watchList->getWebhookDsn()) {
foreach ($watchList->getWebhookDsn() as $dsnString) {
$scheme = (new Dsn($dsnString))->getScheme();
if (null === WebhookScheme::tryFrom($scheme)) {
throw new BadRequestHttpException("The DSN scheme ($scheme) is not supported");
}
}
}
$this->verifyWebhookDSN($watchList);
if ($this->getParameter('limited_features')) {
if ($watchList->getDomains()->count() > (int) $this->getParameter('limit_max_watchlist_domains')) {

View File

@@ -24,7 +24,10 @@ class DomainOrderErrorNotification extends Notification implements ChatNotificat
public function asChatMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?ChatMessage
{
$this->subject('Error: Domain Order');
$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 ChatMessage::fromNotification($this);
}

View File

@@ -27,7 +27,11 @@ class DomainOrderNotification extends Notification implements ChatNotificationIn
public function asChatMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?ChatMessage
{
$this->subject('Domain Ordered');
$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 ChatMessage::fromNotification($this);
}

View File

@@ -24,7 +24,10 @@ class DomainUpdateErrorNotification extends Notification implements ChatNotifica
public function asChatMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?ChatMessage
{
$this->subject('Error: Domain Update');
$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 ChatMessage::fromNotification($this);
}

View File

@@ -25,7 +25,11 @@ class DomainUpdateNotification extends Notification implements ChatNotificationI
public function asChatMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?ChatMessage
{
$this->subject('Domain Updated');
$ldhName = $this->domainEvent->getDomain()->getLdhName();
$action = $this->domainEvent->getAction();
$this->subject("Success: Domain Updated $ldhName")
->content("Domain name $ldhName information has been updated ($action).")
->importance(Notification::IMPORTANCE_HIGH);
return ChatMessage::fromNotification($this);
}

View File

@@ -11,8 +11,10 @@ class TestChatNotification extends Notification implements ChatNotificationInter
{
public function asChatMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?ChatMessage
{
$this->subject('Test notification');
$this->content('This is a test message. If you can read me, this Webhook is configured correctly');
$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 ChatMessage::fromNotification($this);
}