Move WebhookTriggers to Enum and clean up dispatch classes.

This commit is contained in:
Buster Neece 2022-11-26 00:02:42 -06:00
parent 741dc48830
commit 60312ee45b
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
15 changed files with 139 additions and 91 deletions

View File

@ -4,24 +4,26 @@
* Webhook Configuration
*/
use App\Entity\StationWebhook;
use App\Entity\Enums\WebhookTriggers;
use App\Webhook\Connector;
$triggers = [
StationWebhook::TRIGGER_SONG_CHANGED => __('Any time the currently playing song changes'),
StationWebhook::TRIGGER_LISTENER_GAINED => __('Any time the listener count increases'),
StationWebhook::TRIGGER_LISTENER_LOST => __('Any time the listener count decreases'),
StationWebhook::TRIGGER_LIVE_CONNECT => __('Any time a live streamer/DJ connects to the stream'),
StationWebhook::TRIGGER_LIVE_DISCONNECT => __('Any time a live streamer/DJ disconnects from the stream'),
StationWebhook::TRIGGER_STATION_OFFLINE => __('When the station broadcast goes offline.'),
StationWebhook::TRIGGER_STATION_ONLINE => __('When the station broadcast comes online.'),
$allTriggers = [
WebhookTriggers::SongChanged->value,
WebhookTriggers::ListenerGained->value,
WebhookTriggers::ListenerLost->value,
WebhookTriggers::LiveConnect->value,
WebhookTriggers::LiveDisconnect->value,
WebhookTriggers::StationOffline->value,
WebhookTriggers::StationOnline->value,
];
$allTriggers = array_keys($triggers);
$allTriggersExceptListeners = array_diff($allTriggers, [
StationWebhook::TRIGGER_LISTENER_GAINED,
StationWebhook::TRIGGER_LISTENER_LOST,
]);
$allTriggersExceptListeners = array_diff(
$allTriggers,
[
WebhookTriggers::ListenerGained->value,
WebhookTriggers::ListenerLost->value,
]
);
return [
'webhooks' => [
@ -59,13 +61,13 @@ return [
'class' => Connector\Twitter::class,
'name' => __('Twitter Post'),
'description' => __('Automatically send a tweet.'),
'triggers' => $allTriggers,
'triggers' => $allTriggersExceptListeners,
],
Connector\Mastodon::NAME => [
'class' => Connector\Mastodon::class,
'name' => __('Mastodon Post'),
'description' => __('Automatically publish to a Mastodon instance.'),
'triggers' => [],
'triggers' => $allTriggersExceptListeners,
],
Connector\GoogleAnalytics::NAME => [
'class' => Connector\GoogleAnalytics::class,
@ -82,5 +84,13 @@ return [
],
// The triggers that can be selected for a web hook to trigger.
'triggers' => $triggers,
'triggers' => [
WebhookTriggers::SongChanged->value => __('Any time the currently playing song changes'),
WebhookTriggers::ListenerGained->value => __('Any time the listener count increases'),
WebhookTriggers::ListenerLost->value => __('Any time the listener count decreases'),
WebhookTriggers::LiveConnect->value => __('Any time a live streamer/DJ connects to the stream'),
WebhookTriggers::LiveDisconnect->value => __('Any time a live streamer/DJ disconnects from the stream'),
WebhookTriggers::StationOffline->value => __('When the station broadcast goes offline.'),
WebhookTriggers::StationOnline->value => __('When the station broadcast comes online.'),
],
];

View File

@ -171,7 +171,7 @@ export default {
bot_token: '',
chat_id: '',
api: '',
text: '',
text: this.langTelegramDefaultContent,
parse_mode: 'Markdown'
}
},

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Entity\Enums;
enum WebhookTriggers: string
{
case All = 'all';
case SongChanged = 'song_changed';
case ListenerGained = 'listener_gained';
case ListenerLost = 'listener_lost';
case LiveConnect = 'live_connect';
case LiveDisconnect = 'live_disconnect';
case StationOffline = 'station_offline';
case StationOnline = 'station_online';
}

View File

@ -25,15 +25,6 @@ class StationWebhook implements
public const LAST_SENT_TIMESTAMP_KEY = 'last_message_sent';
public const TRIGGER_ALL = 'all';
public const TRIGGER_SONG_CHANGED = 'song_changed';
public const TRIGGER_LISTENER_GAINED = 'listener_gained';
public const TRIGGER_LISTENER_LOST = 'listener_lost';
public const TRIGGER_LIVE_CONNECT = 'live_connect';
public const TRIGGER_LIVE_DISCONNECT = 'live_disconnect';
public const TRIGGER_STATION_OFFLINE = 'station_offline';
public const TRIGGER_STATION_ONLINE = 'station_online';
#[ORM\Column(nullable: false)]
protected int $station_id;

View File

@ -5,8 +5,11 @@ declare(strict_types=1);
namespace App\Sync\NowPlaying\Task;
use App\Doctrine\ReloadableEntityManagerInterface;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\ApiGenerator\NowPlayingApiGenerator;
use App\Entity\Enums\WebhookTriggers;
use App\Entity\Repository\ListenerRepository;
use App\Entity\Repository\SettingsRepository;
use App\Entity\Station;
use App\Environment;
use App\Event\Radio\GenerateRawNowPlaying;
@ -32,9 +35,9 @@ final class NowPlayingTask implements NowPlayingTaskInterface, EventSubscriberIn
private readonly EventDispatcherInterface $eventDispatcher,
private readonly MessageBus $messageBus,
private readonly RouterInterface $router,
private readonly Entity\Repository\ListenerRepository $listenerRepo,
private readonly Entity\Repository\SettingsRepository $settingsRepo,
private readonly Entity\ApiGenerator\NowPlayingApiGenerator $nowPlayingApiGenerator,
private readonly ListenerRepository $listenerRepo,
private readonly SettingsRepository $settingsRepo,
private readonly NowPlayingApiGenerator $nowPlayingApiGenerator,
private readonly ReloadableEntityManagerInterface $em,
private readonly LoggerInterface $logger,
private readonly HlsListeners $hlsListeners,
@ -171,7 +174,7 @@ final class NowPlayingTask implements NowPlayingTaskInterface, EventSubscriberIn
}
private function dispatchWebhooks(
Entity\Station $station,
Station $station,
NowPlaying $npOriginal
): void {
/** @var NowPlaying $np */
@ -181,30 +184,30 @@ final class NowPlayingTask implements NowPlayingTaskInterface, EventSubscriberIn
$npOld = $station->getNowplaying();
$triggers = [
Entity\StationWebhook::TRIGGER_ALL,
WebhookTriggers::All->value,
];
if ($npOld instanceof NowPlaying) {
if ($npOld->now_playing?->song?->id !== $np->now_playing?->song?->id) {
$triggers[] = Entity\StationWebhook::TRIGGER_SONG_CHANGED;
$triggers[] = WebhookTriggers::SongChanged->value;
}
if ($npOld->listeners->current > $np->listeners->current) {
$triggers[] = Entity\StationWebhook::TRIGGER_LISTENER_LOST;
$triggers[] = WebhookTriggers::ListenerLost->value;
} elseif ($npOld->listeners->current < $np->listeners->current) {
$triggers[] = Entity\StationWebhook::TRIGGER_LISTENER_GAINED;
$triggers[] = WebhookTriggers::ListenerGained->value;
}
if (!$npOld->live->is_live && $np->live->is_live) {
$triggers[] = Entity\StationWebhook::TRIGGER_LIVE_CONNECT;
$triggers[] = WebhookTriggers::LiveConnect->value;
} elseif ($npOld->live->is_live && !$np->live->is_live) {
$triggers[] = Entity\StationWebhook::TRIGGER_LIVE_DISCONNECT;
$triggers[] = WebhookTriggers::LiveDisconnect->value;
}
if ($npOld->is_online && !$np->is_online) {
$triggers[] = Entity\StationWebhook::TRIGGER_STATION_OFFLINE;
$triggers[] = WebhookTriggers::StationOffline->value;
} elseif (!$npOld->is_online && $np->is_online) {
$triggers[] = Entity\StationWebhook::TRIGGER_STATION_ONLINE;
$triggers[] = WebhookTriggers::StationOnline->value;
}
}

View File

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Webhook\Connector;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Station;
use App\Entity\StationWebhook;
use Monolog\Level;
/*
@ -67,9 +69,9 @@ final class Discord extends AbstractConnector
* @inheritDoc
*/
public function dispatch(
Entity\Station $station,
Entity\StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np,
Station $station,
StationWebhook $webhook,
NowPlaying $np,
array $triggers
): void {
$config = $webhook->getConfig();

View File

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Webhook\Connector;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Station;
use App\Entity\StationWebhook;
use App\Service\Mail;
use GuzzleHttp\Client;
use Monolog\Logger;
@ -25,9 +27,9 @@ final class Email extends AbstractConnector
* @inheritDoc
*/
public function dispatch(
Entity\Station $station,
Entity\StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np,
Station $station,
StationWebhook $webhook,
NowPlaying $np,
array $triggers
): void {
if (!$this->mail->isEnabled()) {

View File

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Webhook\Connector;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Station;
use App\Entity\StationWebhook;
final class Generic extends AbstractConnector
{
@ -14,9 +16,9 @@ final class Generic extends AbstractConnector
* @inheritDoc
*/
public function dispatch(
Entity\Station $station,
Entity\StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np,
Station $station,
StationWebhook $webhook,
NowPlaying $np,
array $triggers
): void {
$config = $webhook->getConfig();

View File

@ -4,7 +4,10 @@ declare(strict_types=1);
namespace App\Webhook\Connector;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Repository\ListenerRepository;
use App\Entity\Station;
use App\Entity\StationWebhook;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
use Monolog\Logger;
@ -18,7 +21,7 @@ final class GoogleAnalytics extends AbstractConnector
public function __construct(
Logger $logger,
Client $httpClient,
private readonly Entity\Repository\ListenerRepository $listenerRepo
private readonly ListenerRepository $listenerRepo
) {
parent::__construct($logger, $httpClient);
}
@ -27,9 +30,9 @@ final class GoogleAnalytics extends AbstractConnector
* @inheritDoc
*/
public function dispatch(
Entity\Station $station,
Entity\StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np,
Station $station,
StationWebhook $webhook,
NowPlaying $np,
array $triggers
): void {
$config = $webhook->getConfig();

View File

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Webhook\Connector;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Station;
use App\Entity\StationWebhook;
use App\Utilities\Urls;
/**
@ -14,7 +16,7 @@ final class Mastodon extends AbstractConnector
{
public const NAME = 'mastodon';
protected function getRateLimitTime(Entity\StationWebhook $webhook): ?int
protected function getRateLimitTime(StationWebhook $webhook): ?int
{
$config = $webhook->getConfig();
$rateLimitSeconds = (int)($config['rate_limit'] ?? 0);
@ -23,9 +25,9 @@ final class Mastodon extends AbstractConnector
}
public function dispatch(
Entity\Station $station,
Entity\StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np,
Station $station,
StationWebhook $webhook,
NowPlaying $np,
array $triggers
): void {
$config = $webhook->getConfig();

View File

@ -4,7 +4,10 @@ declare(strict_types=1);
namespace App\Webhook\Connector;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Repository\ListenerRepository;
use App\Entity\Station;
use App\Entity\StationWebhook;
use App\Http\RouterInterface;
use App\Utilities\Urls;
use GuzzleHttp\Client;
@ -19,7 +22,7 @@ final class MatomoAnalytics extends AbstractConnector
Logger $logger,
Client $httpClient,
private readonly RouterInterface $router,
private readonly Entity\Repository\ListenerRepository $listenerRepo
private readonly ListenerRepository $listenerRepo
) {
parent::__construct($logger, $httpClient);
}
@ -28,9 +31,9 @@ final class MatomoAnalytics extends AbstractConnector
* @inheritDoc
*/
public function dispatch(
Entity\Station $station,
Entity\StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np,
Station $station,
StationWebhook $webhook,
NowPlaying $np,
array $triggers
): void {
$config = $webhook->getConfig();

View File

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Webhook\Connector;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Station;
use App\Entity\StationWebhook;
/**
* Telegram web hook connector.
@ -19,9 +21,9 @@ final class Telegram extends AbstractConnector
* @inheritDoc
*/
public function dispatch(
Entity\Station $station,
Entity\StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np,
Station $station,
StationWebhook $webhook,
NowPlaying $np,
array $triggers
): void {
$config = $webhook->getConfig();

View File

@ -4,24 +4,27 @@ declare(strict_types=1);
namespace App\Webhook\Connector;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Enums\WebhookTriggers;
use App\Entity\Station;
use App\Entity\StationWebhook;
final class TuneIn extends AbstractConnector
{
public const NAME = 'tunein';
protected function webhookShouldTrigger(Entity\StationWebhook $webhook, array $triggers = []): bool
protected function webhookShouldTrigger(StationWebhook $webhook, array $triggers = []): bool
{
return in_array(Entity\StationWebhook::TRIGGER_SONG_CHANGED, $triggers, true);
return in_array(WebhookTriggers::SongChanged->value, $triggers, true);
}
/**
* @inheritDoc
*/
public function dispatch(
Entity\Station $station,
Entity\StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np,
Station $station,
StationWebhook $webhook,
NowPlaying $np,
array $triggers
): void {
$config = $webhook->getConfig();

View File

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Webhook\Connector;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Station;
use App\Entity\StationWebhook;
use App\Service\GuzzleFactory;
use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
@ -22,7 +24,7 @@ final class Twitter extends AbstractConnector
parent::__construct($logger, $httpClient);
}
protected function getRateLimitTime(Entity\StationWebhook $webhook): ?int
protected function getRateLimitTime(StationWebhook $webhook): ?int
{
$config = $webhook->getConfig();
$rateLimitSeconds = (int)($config['rate_limit'] ?? 0);
@ -34,9 +36,9 @@ final class Twitter extends AbstractConnector
* @inheritDoc
*/
public function dispatch(
Entity\Station $station,
Entity\StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np,
Station $station,
StationWebhook $webhook,
NowPlaying $np,
array $triggers
): void {
$config = $webhook->getConfig();

View File

@ -4,7 +4,10 @@ declare(strict_types=1);
namespace App\Webhook;
use App\Entity;
use App\Entity\ApiGenerator\NowPlayingApiGenerator;
use App\Entity\Enums\WebhookTriggers;
use App\Entity\Station;
use App\Entity\StationWebhook;
use App\Environment;
use App\Http\RouterInterface;
use App\Message;
@ -22,7 +25,7 @@ final class Dispatcher
private readonly RouterInterface $router,
private readonly LocalWebhookHandler $localHandler,
private readonly ConnectorLocator $connectors,
private readonly Entity\ApiGenerator\NowPlayingApiGenerator $nowPlayingApiGen
private readonly NowPlayingApiGenerator $nowPlayingApiGen
) {
}
@ -42,8 +45,8 @@ final class Dispatcher
private function handleDispatch(Message\DispatchWebhookMessage $message): void
{
$station = $this->em->find(Entity\Station::class, $message->station_id);
if (!$station instanceof Entity\Station) {
$station = $this->em->find(Station::class, $message->station_id);
if (!$station instanceof Station) {
return;
}
@ -67,9 +70,9 @@ final class Dispatcher
return;
}
/** @var Entity\StationWebhook[] $enabledWebhooks */
/** @var StationWebhook[] $enabledWebhooks */
$enabledWebhooks = $station->getWebhooks()->filter(
function (Entity\StationWebhook $webhook) {
function (StationWebhook $webhook) {
return $webhook->getIsEnabled();
}
);
@ -111,8 +114,8 @@ final class Dispatcher
}
try {
$webhook = $this->em->find(Entity\StationWebhook::class, $message->webhookId);
if (!($webhook instanceof Entity\StationWebhook)) {
$webhook = $this->em->find(StationWebhook::class, $message->webhookId);
if (!($webhook instanceof StationWebhook)) {
return;
}
@ -122,7 +125,9 @@ final class Dispatcher
$np->cache = 'event';
$connectorObj = $this->connectors->getConnector($webhook->getType());
$connectorObj->dispatch($station, $webhook, $np, [Entity\StationWebhook::TRIGGER_ALL]);
$connectorObj->dispatch($station, $webhook, $np, [
WebhookTriggers::All->value,
]);
} catch (\Throwable $e) {
$this->logger->error(
sprintf(