4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-14 21:26:37 +00:00

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

View File

@ -5,8 +5,11 @@ declare(strict_types=1);
namespace App\Sync\NowPlaying\Task; namespace App\Sync\NowPlaying\Task;
use App\Doctrine\ReloadableEntityManagerInterface; use App\Doctrine\ReloadableEntityManagerInterface;
use App\Entity;
use App\Entity\Api\NowPlaying\NowPlaying; 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\Entity\Station;
use App\Environment; use App\Environment;
use App\Event\Radio\GenerateRawNowPlaying; use App\Event\Radio\GenerateRawNowPlaying;
@ -32,9 +35,9 @@ final class NowPlayingTask implements NowPlayingTaskInterface, EventSubscriberIn
private readonly EventDispatcherInterface $eventDispatcher, private readonly EventDispatcherInterface $eventDispatcher,
private readonly MessageBus $messageBus, private readonly MessageBus $messageBus,
private readonly RouterInterface $router, private readonly RouterInterface $router,
private readonly Entity\Repository\ListenerRepository $listenerRepo, private readonly ListenerRepository $listenerRepo,
private readonly Entity\Repository\SettingsRepository $settingsRepo, private readonly SettingsRepository $settingsRepo,
private readonly Entity\ApiGenerator\NowPlayingApiGenerator $nowPlayingApiGenerator, private readonly NowPlayingApiGenerator $nowPlayingApiGenerator,
private readonly ReloadableEntityManagerInterface $em, private readonly ReloadableEntityManagerInterface $em,
private readonly LoggerInterface $logger, private readonly LoggerInterface $logger,
private readonly HlsListeners $hlsListeners, private readonly HlsListeners $hlsListeners,
@ -171,7 +174,7 @@ final class NowPlayingTask implements NowPlayingTaskInterface, EventSubscriberIn
} }
private function dispatchWebhooks( private function dispatchWebhooks(
Entity\Station $station, Station $station,
NowPlaying $npOriginal NowPlaying $npOriginal
): void { ): void {
/** @var NowPlaying $np */ /** @var NowPlaying $np */
@ -181,30 +184,30 @@ final class NowPlayingTask implements NowPlayingTaskInterface, EventSubscriberIn
$npOld = $station->getNowplaying(); $npOld = $station->getNowplaying();
$triggers = [ $triggers = [
Entity\StationWebhook::TRIGGER_ALL, WebhookTriggers::All->value,
]; ];
if ($npOld instanceof NowPlaying) { if ($npOld instanceof NowPlaying) {
if ($npOld->now_playing?->song?->id !== $np->now_playing?->song?->id) { 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) { if ($npOld->listeners->current > $np->listeners->current) {
$triggers[] = Entity\StationWebhook::TRIGGER_LISTENER_LOST; $triggers[] = WebhookTriggers::ListenerLost->value;
} elseif ($npOld->listeners->current < $np->listeners->current) { } 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) { 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) { } 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) { if ($npOld->is_online && !$np->is_online) {
$triggers[] = Entity\StationWebhook::TRIGGER_STATION_OFFLINE; $triggers[] = WebhookTriggers::StationOffline->value;
} elseif (!$npOld->is_online && $np->is_online) { } 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; namespace App\Webhook\Connector;
use App\Entity; use App\Entity\Api\NowPlaying\NowPlaying;
use App\Entity\Station;
use App\Entity\StationWebhook;
use Monolog\Level; use Monolog\Level;
/* /*
@ -67,9 +69,9 @@ final class Discord extends AbstractConnector
* @inheritDoc * @inheritDoc
*/ */
public function dispatch( public function dispatch(
Entity\Station $station, Station $station,
Entity\StationWebhook $webhook, StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np, NowPlaying $np,
array $triggers array $triggers
): void { ): void {
$config = $webhook->getConfig(); $config = $webhook->getConfig();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,24 +4,27 @@ declare(strict_types=1);
namespace App\Webhook\Connector; 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 final class TuneIn extends AbstractConnector
{ {
public const NAME = 'tunein'; 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 * @inheritDoc
*/ */
public function dispatch( public function dispatch(
Entity\Station $station, Station $station,
Entity\StationWebhook $webhook, StationWebhook $webhook,
Entity\Api\NowPlaying\NowPlaying $np, NowPlaying $np,
array $triggers array $triggers
): void { ): void {
$config = $webhook->getConfig(); $config = $webhook->getConfig();

View File

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

View File

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