Skip to content

Commit

Permalink
fix: send the correct body when chat notification
Browse files Browse the repository at this point in the history
  • Loading branch information
maelgangloff committed Aug 28, 2024
1 parent 83a97b4 commit 02c9ce7
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 103 deletions.
6 changes: 2 additions & 4 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ identifiers:
- type: url
value: >-
https://github.com/maelgangloff/domain-watcher/releases
description: Release of domain-watchdog
description: Release of Domain Watchdog
repository-code: 'https://github.com/maelgangloff/domain-watchdog'
abstract: An app that utilizes RDAP to gather publicly accessible information about domain names, track their history, and automatically purchase them
abstract: An app that uses RDAP to collect publicly available info about domains, track their history, and purchase them
keywords:
- DOMAIN
- RDAP
- WHOIS
license: AGPL-3.0-or-later
version: 0.1.0
date-released: '2024-08-08'
license-url: 'https://github.com/maelgangloff/domain-watchdog/blob/master/LICENSE'
53 changes: 5 additions & 48 deletions src/Controller/WatchListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Controller;

use App\Config\Provider\AbstractProvider;
use App\Config\WebhookScheme;
use App\Entity\Connector;
use App\Entity\Domain;
use App\Entity\DomainEntity;
Expand All @@ -12,6 +11,7 @@
use App\Entity\WatchList;
use App\Notifier\TestChatNotification;
use App\Repository\WatchListRepository;
use App\Service\ChatNotificationService;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\ORMException;
Expand Down Expand Up @@ -39,9 +39,6 @@
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -55,7 +52,8 @@ public function __construct(
private readonly LoggerInterface $logger,
private readonly HttpClientInterface $httpClient,
private readonly CacheItemPoolInterface $cacheItemPool,
private readonly KernelInterface $kernel
private readonly KernelInterface $kernel,
private readonly ChatNotificationService $chatNotificationService
) {
}

Expand Down Expand Up @@ -124,7 +122,7 @@ public function createWatchList(Request $request): WatchList
}
}

$this->verifyWebhookDSN($watchList);
$this->chatNotificationService->sendChatNotification($watchList, new TestChatNotification());
$this->verifyConnector($watchList, $watchList->getConnector());

$this->logger->info('User {username} registers a Watchlist ({token}).', [
Expand Down Expand Up @@ -155,47 +153,6 @@ public function getWatchLists(): Collection
return $user->getWatchLists();
}

private function verifyWebhookDSN(WatchList $watchList): void
{
$webhookDsn = $watchList->getWebhookDsn();
if (null !== $webhookDsn && 0 !== count($webhookDsn)) {
foreach ($webhookDsn as $dsnString) {
try {
$dsn = new Dsn($dsnString);
} catch (InvalidArgumentException $exception) {
throw new BadRequestHttpException($exception->getMessage());
}

$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();

$push = (new TestChatNotification())->asPushMessage();
$chat = (new TestChatNotification())->asChatMessage();

try {
$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) {
throw new BadRequestHttpException($exception->getMessage());
}
}
}
}

/**
* @throws \Exception
*/
Expand Down Expand Up @@ -285,7 +242,7 @@ public function putWatchList(WatchList $watchList): WatchList
}
}

$this->verifyWebhookDSN($watchList);
$this->chatNotificationService->sendChatNotification($watchList, new TestChatNotification());
$this->verifyConnector($watchList, $watchList->getConnector());

$this->logger->info('User {username} updates a Watchlist ({token}).', [
Expand Down
3 changes: 1 addition & 2 deletions src/MessageHandler/OrderDomainHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ public function __invoke(OrderDomain $message): void
$connectorProvider = new $connectorProviderClass($connector->getAuthData(), $this->client, $this->cacheItemPool, $this->kernel);

$connectorProvider->orderDomain($domain, $this->kernel->isDebug());
$this->statService->incrementStat('stats.domain.purchased');

$notification = (new DomainOrderNotification($this->sender, $domain, $connector));
$this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage());
$this->chatNotificationService->sendChatNotification($watchList, $notification);

$this->statService->incrementStat('stats.domain.purchased');
} catch (\Throwable $exception) {
$this->logger->warning('Unable to complete purchase. An error message is sent to user {username}.', [
'username' => $watchList->getUser()->getUserIdentifier(),
Expand Down
5 changes: 1 addition & 4 deletions src/Notifier/DomainOrderErrorNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface;

class DomainOrderErrorNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface, PushNotificationInterface
class DomainOrderErrorNotification extends DomainWatchdogNotification
{
public function __construct(
private readonly Address $sender,
Expand Down
5 changes: 1 addition & 4 deletions src/Notifier/DomainOrderNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface;

class DomainOrderNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface, PushNotificationInterface
class DomainOrderNotification extends DomainWatchdogNotification
{
public function __construct(
private readonly Address $sender,
Expand Down
5 changes: 1 addition & 4 deletions src/Notifier/DomainUpdateErrorNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface;

class DomainUpdateErrorNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface, PushNotificationInterface
class DomainUpdateErrorNotification extends DomainWatchdogNotification
{
public function __construct(
private readonly Address $sender,
Expand Down
5 changes: 1 addition & 4 deletions src/Notifier/DomainUpdateNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface;

class DomainUpdateNotification extends Notification implements ChatNotificationInterface, EmailNotificationInterface, PushNotificationInterface
class DomainUpdateNotification extends DomainWatchdogNotification
{
public function __construct(
private readonly Address $sender,
Expand Down
11 changes: 11 additions & 0 deletions src/Notifier/DomainWatchdogNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Notifier;

use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;

abstract class DomainWatchdogNotification extends Notification implements ChatNotificationInterface, PushNotificationInterface
{
}
4 changes: 1 addition & 3 deletions src/Notifier/TestChatNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface;

class TestChatNotification extends Notification implements ChatNotificationInterface, PushNotificationInterface
class TestChatNotification extends DomainWatchdogNotification
{
public function asChatMessage(?RecipientInterface $recipient = null, ?string $transport = null): ?ChatMessage
{
Expand Down
68 changes: 38 additions & 30 deletions src/Service/ChatNotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use App\Config\WebhookScheme;
use App\Entity\WatchList;
use App\Notifier\TestChatNotification;
use App\Notifier\DomainWatchdogNotification;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Recipient\NoRecipient;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

Expand All @@ -18,47 +19,54 @@ public function __construct(
) {
}

public function sendChatNotification(WatchList $watchList, ChatNotificationInterface $notification): void
public function sendChatNotification(WatchList $watchList, DomainWatchdogNotification $notification): void
{
$webhookDsn = $watchList->getWebhookDsn();
if (null !== $webhookDsn && 0 !== count($webhookDsn)) {
foreach ($webhookDsn as $dsnString) {
$dsn = new Dsn($dsnString);
try {
$dsn = new Dsn($dsnString);
} catch (InvalidArgumentException $exception) {
throw new BadRequestHttpException($exception->getMessage());
}

$scheme = $dsn->getScheme();
$webhookScheme = WebhookScheme::tryFrom($scheme);

if (null !== $webhookScheme) {
$transportFactoryClass = $webhookScheme->getChatTransportFactory();
/** @var AbstractTransportFactory $transportFactory */
$transportFactory = new $transportFactoryClass();
if (null === $webhookScheme) {
throw new BadRequestHttpException("The DSN scheme ($scheme) is not supported");
}

$push = (new TestChatNotification())->asPushMessage();
$chat = (new TestChatNotification())->asChatMessage();
$transportFactoryClass = $webhookScheme->getChatTransportFactory();
/** @var AbstractTransportFactory $transportFactory */
$transportFactory = new $transportFactoryClass();

try {
$factory = $transportFactory->create($dsn);
$push = $notification->asPushMessage(new NoRecipient());
$chat = $notification->asChatMessage(new NoRecipient());

if ($factory->supports($push)) {
$factory->send($push);
} elseif ($factory->supports($chat)) {
$factory->send($chat);
} else {
throw new BadRequestHttpException('Unsupported message type');
}
try {
$factory = $transportFactory->create($dsn);

$this->logger->info('Chat message sent with {schema} for Watchlist {token}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
} catch (\Throwable) {
$this->logger->error('Unable to send a chat message to {scheme} for Watchlist {token}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
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}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
} catch (\Throwable $exception) {
$this->logger->error('Unable to send a chat message to {scheme} for Watchlist {token}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
throw new BadRequestHttpException($exception->getMessage());
}
}
}
Expand Down

0 comments on commit 02c9ce7

Please sign in to comment.