Add streamer avatar to NP API.

This commit is contained in:
Buster "Silver Eagle" Neece 2022-06-07 04:00:39 -05:00
parent 0bd53a65a3
commit 18fe4060e7
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
4 changed files with 47 additions and 40 deletions

View File

@ -4,13 +4,16 @@ declare(strict_types=1);
namespace App\Entity\Api\NowPlaying; namespace App\Entity\Api\NowPlaying;
use App\Entity\Api\ResolvableUrlInterface;
use App\Http\Router;
use OpenApi\Attributes as OA; use OpenApi\Attributes as OA;
use Psr\Http\Message\UriInterface;
#[OA\Schema( #[OA\Schema(
schema: 'Api_NowPlaying_Live', schema: 'Api_NowPlaying_Live',
type: 'object' type: 'object'
)] )]
class Live class Live implements ResolvableUrlInterface
{ {
#[OA\Property( #[OA\Property(
description: 'Whether the stream is known to currently have a live DJ.', description: 'Whether the stream is known to currently have a live DJ.',
@ -21,8 +24,7 @@ class Live
#[OA\Property( #[OA\Property(
description: 'The current active streamer/DJ, if one is available.', description: 'The current active streamer/DJ, if one is available.',
example: 'DJ Jazzy Jeff' example: 'DJ Jazzy Jeff'
) )]
]
public string $streamer_name = ''; public string $streamer_name = '';
#[OA\Property( #[OA\Property(
@ -31,13 +33,17 @@ class Live
)] )]
public ?int $broadcast_start = null; public ?int $broadcast_start = null;
public function __construct( /** @var string|UriInterface|null */
bool $is_live = false, #[OA\Property(
string $streamer_name = '', description: 'URL to the streamer artwork (if available).',
?int $broadcast_start = null example: 'https://picsum.photos/1200/1200'
) { )]
$this->is_live = $is_live; public $art = null;
$this->streamer_name = $streamer_name;
$this->broadcast_start = $broadcast_start; public function resolveUrls(UriInterface $base): void
{
$this->art = (null !== $this->art)
? (string)Router::resolveUri($base, $this->art, true)
: null;
} }
} }

View File

@ -69,6 +69,8 @@ class NowPlaying implements ResolvableUrlInterface
{ {
$this->station->resolveUrls($base); $this->station->resolveUrls($base);
$this->live->resolveUrls($base);
if ($this->now_playing instanceof ResolvableUrlInterface) { if ($this->now_playing instanceof ResolvableUrlInterface) {
$this->now_playing->resolveUrls($base); $this->now_playing->resolveUrls($base);
} }

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Entity\ApiGenerator; namespace App\Entity\ApiGenerator;
use App\Entity; use App\Entity;
use App\Http\Router;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use NowPlaying\Result\CurrentSong; use NowPlaying\Result\CurrentSong;
use NowPlaying\Result\Result; use NowPlaying\Result\Result;
@ -21,7 +22,8 @@ class NowPlayingApiGenerator
protected Entity\Repository\SongHistoryRepository $historyRepo, protected Entity\Repository\SongHistoryRepository $historyRepo,
protected Entity\Repository\StationQueueRepository $queueRepo, protected Entity\Repository\StationQueueRepository $queueRepo,
protected Entity\Repository\StationStreamerBroadcastRepository $broadcastRepo, 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 // Detect and report live DJ status
if ($station->getIsStreamerLive()) { $currentStreamer = $station->getCurrentStreamer();
$current_streamer = $station->getCurrentStreamer();
$streamer_name = ($current_streamer instanceof Entity\StationStreamer)
? $current_streamer->getDisplayName()
: 'Live DJ';
if (null !== $currentStreamer) {
$broadcastStart = $this->broadcastRepo->getLatestBroadcast($station)?->getTimestampStart(); $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 { } else {
$np->live = new Entity\Api\NowPlaying\Live(false); $np->live = new Entity\Api\NowPlaying\Live();
} }
$apiSongHistory = ($this->songHistoryApiGenerator)( $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(); $np->update();
return $np; return $np;

View File

@ -14,28 +14,13 @@ use Psr\Http\Message\UriInterface;
class SongApiGenerator class SongApiGenerator
{ {
protected EntityManagerInterface $em;
protected Router $router;
protected Entity\Repository\StationRepository $stationRepo;
protected Entity\Repository\CustomFieldRepository $customFieldRepo;
protected RemoteAlbumArt $remoteAlbumArt;
public function __construct( public function __construct(
EntityManagerInterface $em, protected EntityManagerInterface $em,
Router $router, protected Router $router,
Entity\Repository\StationRepository $stationRepo, protected Entity\Repository\StationRepository $stationRepo,
Entity\Repository\CustomFieldRepository $customFieldRepo, protected Entity\Repository\CustomFieldRepository $customFieldRepo,
RemoteAlbumArt $remoteAlbumArt protected RemoteAlbumArt $remoteAlbumArt
) { ) {
$this->em = $em;
$this->router = $router;
$this->stationRepo = $stationRepo;
$this->customFieldRepo = $customFieldRepo;
$this->remoteAlbumArt = $remoteAlbumArt;
} }
public function __invoke( public function __invoke(