From 18fe4060e7dc822209f62477a94fe6b64b84444d Mon Sep 17 00:00:00 2001 From: "Buster \"Silver Eagle\" Neece" Date: Tue, 7 Jun 2022 04:00:39 -0500 Subject: [PATCH] Add streamer avatar to NP API. --- src/Entity/Api/NowPlaying/Live.php | 28 +++++++++------- src/Entity/Api/NowPlaying/NowPlaying.php | 2 ++ .../ApiGenerator/NowPlayingApiGenerator.php | 32 +++++++++++++------ src/Entity/ApiGenerator/SongApiGenerator.php | 25 +++------------ 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/Entity/Api/NowPlaying/Live.php b/src/Entity/Api/NowPlaying/Live.php index e10f42422..72d77577f 100644 --- a/src/Entity/Api/NowPlaying/Live.php +++ b/src/Entity/Api/NowPlaying/Live.php @@ -4,13 +4,16 @@ declare(strict_types=1); namespace App\Entity\Api\NowPlaying; +use App\Entity\Api\ResolvableUrlInterface; +use App\Http\Router; use OpenApi\Attributes as OA; +use Psr\Http\Message\UriInterface; #[OA\Schema( schema: 'Api_NowPlaying_Live', type: 'object' )] -class Live +class Live implements ResolvableUrlInterface { #[OA\Property( description: 'Whether the stream is known to currently have a live DJ.', @@ -21,8 +24,7 @@ class Live #[OA\Property( description: 'The current active streamer/DJ, if one is available.', example: 'DJ Jazzy Jeff' - ) - ] + )] public string $streamer_name = ''; #[OA\Property( @@ -31,13 +33,17 @@ class Live )] public ?int $broadcast_start = null; - public function __construct( - bool $is_live = false, - string $streamer_name = '', - ?int $broadcast_start = null - ) { - $this->is_live = $is_live; - $this->streamer_name = $streamer_name; - $this->broadcast_start = $broadcast_start; + /** @var string|UriInterface|null */ + #[OA\Property( + description: 'URL to the streamer artwork (if available).', + example: 'https://picsum.photos/1200/1200' + )] + public $art = null; + + public function resolveUrls(UriInterface $base): void + { + $this->art = (null !== $this->art) + ? (string)Router::resolveUri($base, $this->art, true) + : null; } } diff --git a/src/Entity/Api/NowPlaying/NowPlaying.php b/src/Entity/Api/NowPlaying/NowPlaying.php index 6cf78a4a0..d2937ce80 100644 --- a/src/Entity/Api/NowPlaying/NowPlaying.php +++ b/src/Entity/Api/NowPlaying/NowPlaying.php @@ -69,6 +69,8 @@ class NowPlaying implements ResolvableUrlInterface { $this->station->resolveUrls($base); + $this->live->resolveUrls($base); + if ($this->now_playing instanceof ResolvableUrlInterface) { $this->now_playing->resolveUrls($base); } diff --git a/src/Entity/ApiGenerator/NowPlayingApiGenerator.php b/src/Entity/ApiGenerator/NowPlayingApiGenerator.php index 5718c0d92..eb88a98f2 100644 --- a/src/Entity/ApiGenerator/NowPlayingApiGenerator.php +++ b/src/Entity/ApiGenerator/NowPlayingApiGenerator.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Entity\ApiGenerator; use App\Entity; +use App\Http\Router; use GuzzleHttp\Psr7\Uri; use NowPlaying\Result\CurrentSong; use NowPlaying\Result\Result; @@ -21,7 +22,8 @@ class NowPlayingApiGenerator protected Entity\Repository\SongHistoryRepository $historyRepo, protected Entity\Repository\StationQueueRepository $queueRepo, protected Entity\Repository\StationStreamerBroadcastRepository $broadcastRepo, - protected EventDispatcherInterface $eventDispatcher + protected EventDispatcherInterface $eventDispatcher, + protected Router $router, ) { } @@ -89,17 +91,29 @@ class NowPlayingApiGenerator } // Detect and report live DJ status - if ($station->getIsStreamerLive()) { - $current_streamer = $station->getCurrentStreamer(); - $streamer_name = ($current_streamer instanceof Entity\StationStreamer) - ? $current_streamer->getDisplayName() - : 'Live DJ'; + $currentStreamer = $station->getCurrentStreamer(); + if (null !== $currentStreamer) { $broadcastStart = $this->broadcastRepo->getLatestBroadcast($station)?->getTimestampStart(); - $np->live = new Entity\Api\NowPlaying\Live(true, $streamer_name, $broadcastStart); + $live = new Entity\Api\NowPlaying\Live(); + $live->is_live = true; + $live->streamer_name = $currentStreamer->getDisplayName(); + $live->broadcast_start = $broadcastStart; + + if (0 !== $currentStreamer->getArtUpdatedAt()) { + $live->art = $this->router->named( + route_name: 'api:stations:streamer:art', + route_params: [ + 'station_id' => $station->getIdRequired(), + 'streamer_id' => $currentStreamer->getIdRequired() . '|' . $currentStreamer->getArtUpdatedAt(), + ], + ); + } + + $np->live = $live; } else { - $np->live = new Entity\Api\NowPlaying\Live(false); + $np->live = new Entity\Api\NowPlaying\Live(); } $apiSongHistory = ($this->songHistoryApiGenerator)( @@ -169,7 +183,7 @@ class NowPlayingApiGenerator ); } - $np->live = new Entity\Api\NowPlaying\Live(false); + $np->live = new Entity\Api\NowPlaying\Live(); $np->update(); return $np; diff --git a/src/Entity/ApiGenerator/SongApiGenerator.php b/src/Entity/ApiGenerator/SongApiGenerator.php index bb81298f1..e45c56dbe 100644 --- a/src/Entity/ApiGenerator/SongApiGenerator.php +++ b/src/Entity/ApiGenerator/SongApiGenerator.php @@ -14,28 +14,13 @@ use Psr\Http\Message\UriInterface; class SongApiGenerator { - protected EntityManagerInterface $em; - - protected Router $router; - - protected Entity\Repository\StationRepository $stationRepo; - - protected Entity\Repository\CustomFieldRepository $customFieldRepo; - - protected RemoteAlbumArt $remoteAlbumArt; - public function __construct( - EntityManagerInterface $em, - Router $router, - Entity\Repository\StationRepository $stationRepo, - Entity\Repository\CustomFieldRepository $customFieldRepo, - RemoteAlbumArt $remoteAlbumArt + protected EntityManagerInterface $em, + protected Router $router, + protected Entity\Repository\StationRepository $stationRepo, + protected Entity\Repository\CustomFieldRepository $customFieldRepo, + protected RemoteAlbumArt $remoteAlbumArt ) { - $this->em = $em; - $this->router = $router; - $this->stationRepo = $stationRepo; - $this->customFieldRepo = $customFieldRepo; - $this->remoteAlbumArt = $remoteAlbumArt; } public function __invoke(