From 8bf557e823e21d78e58b3f68302aa0404a990931 Mon Sep 17 00:00:00 2001 From: "Buster \"Silver Eagle\" Neece" Date: Mon, 20 Jun 2022 11:33:20 -0500 Subject: [PATCH] Reduce redundant code on public player. --- .../Frontend/PublicPages/PlayerAction.php | 71 +++++++++++++++---- templates/frontend/public/embed.phtml | 37 ---------- templates/frontend/public/index.phtml | 27 +------ 3 files changed, 61 insertions(+), 74 deletions(-) delete mode 100644 templates/frontend/public/embed.phtml diff --git a/src/Controller/Frontend/PublicPages/PlayerAction.php b/src/Controller/Frontend/PublicPages/PlayerAction.php index c5b6b77af..397a68a0b 100644 --- a/src/Controller/Frontend/PublicPages/PlayerAction.php +++ b/src/Controller/Frontend/PublicPages/PlayerAction.php @@ -26,6 +26,10 @@ final class PlayerAction string $station_id, ?string $embed = null, ): ResponseInterface { + $response = $response + ->withHeader('X-Frame-Options', '*') + ->withHeader('X-Robots-Tag', 'index, nofollow'); + $station = $request->getStation(); if (!$station->getEnablePublicPage()) { @@ -40,24 +44,67 @@ final class PlayerAction $defaultAlbumArtUri = $this->stationRepo->getDefaultAlbumArtUrl($station); $defaultAlbumArt = Router::resolveUri($baseUrl, $defaultAlbumArtUri, true); - $autoplay = !empty($request->getQueryParam('autoplay')); + // Build Vue props. + $customization = $request->getCustomization(); + $router = $request->getRouter(); - $templateName = (!empty($embed)) - ? 'frontend/public/embed' - : 'frontend/public/index'; + $props = [ + 'initialNowPlaying' => $np, + 'showAlbumArt' => !$customization->hideAlbumArt(), + 'autoplay' => !empty($request->getQueryParam('autoplay')), + ]; + + if ($customization->useWebSocketsForNowPlaying()) { + $props['useNchan'] = true; + $props['nowPlayingUri'] = '/api/live/nowplaying/' . urlencode($station->getShortName()); + } else { + $props['useNchan'] = false; + $props['nowPlayingUri'] = (string)$router->named( + 'api:nowplaying:index', + ['station_id' => $station->getId()] + ); + } + + // Render embedded player. + if (!empty($embed)) { + $pageClasses = []; + $pageClasses[] = 'page-station-public-player-embed station-' . $station->getShortName(); + $pageClasses[] = ('social' === $embed) ? 'embed-social' : 'embed'; + + return $request->getView()->renderVuePage( + response: $response, + component: 'Vue_PublicPlayer', + id: 'station-nowplaying', + layout: 'minimal', + title: $station->getName(), + layoutParams: [ + 'page_class' => implode(' ', $pageClasses), + 'hide_footer' => true, + ], + props: $props, + ); + } + + // Render full page player. + $props['stationName'] = $station->getName(); + $props['enableRequests'] = $station->getEnableRequests(); + $props['downloadPlaylistUri'] = (string)$router->named( + 'public:playlist', + ['station_id' => $station->getShortName(), 'format' => 'pls'] + ); + $props['requestListUri'] = (string)$router->named( + 'api:requests:list', + ['station_id' => $station->getId()] + ); + $props['customFields'] = $this->customFieldRepo->fetchArray(); return $request->getView()->renderToResponse( - $response - ->withHeader('X-Frame-Options', '*') - ->withHeader('X-Robots-Tag', 'index, nofollow'), - $templateName, + $response, + 'frontend/public/index', [ - 'isSocial' => ('social' === $embed), - 'autoplay' => $autoplay, 'station' => $station, + 'props' => $props, 'defaultAlbumArt' => $defaultAlbumArt, - 'nowplaying' => $np, - 'customFields' => $this->customFieldRepo->fetchArray(), ] ); } diff --git a/templates/frontend/public/embed.phtml b/templates/frontend/public/embed.phtml deleted file mode 100644 index 000acf1e5..000000000 --- a/templates/frontend/public/embed.phtml +++ /dev/null @@ -1,37 +0,0 @@ -getShortName(); -$pageClasses[] = ($isSocial) ? 'embed-social' : 'embed'; - -$this->layout( - 'minimal', - [ - 'page_class' => implode(' ', $pageClasses), - 'title' => $this->e($station->getName()), - 'hide_footer' => true, - ] -); - -$props = [ - 'initialNowPlaying' => $nowplaying, - 'showAlbumArt' => !$customization->hideAlbumArt(), - 'autoplay' => $autoplay, -]; - -if ($customization->useWebSocketsForNowPlaying()) { - $props['useNchan'] = true; - $props['nowPlayingUri'] = '/api/live/nowplaying/' . urlencode($station->getShortName()); -} else { - $props['useNchan'] = false; - $props['nowPlayingUri'] = (string)$router->named('api:nowplaying:index', ['station_id' => $station->getId()]); -} - -/** @var \App\Assets $assets */ -$assets->addVueRender('Vue_PublicPlayer', '#station-nowplaying', $props); -?> - -
-
-
diff --git a/templates/frontend/public/index.phtml b/templates/frontend/public/index.phtml index 37c8cac2a..e6c3faf7b 100644 --- a/templates/frontend/public/index.phtml +++ b/templates/frontend/public/index.phtml @@ -8,35 +8,12 @@ $this->layout( 'minimal', [ 'page_class' => 'page-station-public-player station-' . $station->getShortName(), - 'title' => $this->e($station->getName()), + 'title' => $this->e($station->getName()), ] ); -$props = [ - 'stationName' => $station->getName(), - 'enableRequests' => $station->getEnableRequests(), - 'autoplay' => $autoplay, - 'downloadPlaylistUri' => (string)$router->named( - 'public:playlist', - ['station_id' => $station->getShortName(), 'format' => 'pls'] - ), - 'requestListUri' => (string)$router->named('api:requests:list', ['station_id' => $station->getId()]), - 'customFields' => $customFields, - 'initialNowPlaying' => $nowplaying, - 'showAlbumArt' => !$customization->hideAlbumArt(), -]; - -if ($customization->useWebSocketsForNowPlaying()) { - $props['useNchan'] = true; - $props['nowPlayingUri'] = '/api/live/nowplaying/' . urlencode($station->getShortName()); -} else { - $props['useNchan'] = false; - $props['nowPlayingUri'] = (string)$router->named('api:nowplaying:index', ['station_id' => $station->getId()]); -} - /** @var \App\Assets $assets */ -$assets - ->addVueRender('Vue_PublicFullPlayer', '#public-radio-player', $props); +$assets->addVueRender('Vue_PublicFullPlayer', '#public-radio-player', $props); // Register PWA service worker $swJsRoute = (string)$router->named('public:sw');