2016-09-02 20:37:12 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-12-26 23:48:01 +00:00
|
|
|
namespace App\Controller\Api\Stations;
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
use App\Entity;
|
2020-04-06 23:32:50 +00:00
|
|
|
use App\Exception;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2020-12-20 01:51:53 +00:00
|
|
|
use App\Paginator;
|
2019-08-07 04:33:55 +00:00
|
|
|
use App\Utilities;
|
2020-06-26 20:22:53 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2019-04-07 08:40:55 +00:00
|
|
|
use OpenApi\Annotations as OA;
|
2019-08-07 04:33:55 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2021-12-12 11:01:53 +00:00
|
|
|
/**
|
|
|
|
* @OA\Get(path="/station/{station_id}/requests",
|
|
|
|
* operationId="getRequestableSongs",
|
|
|
|
* tags={"Stations: Song Requests"},
|
|
|
|
* description="Return a list of requestable songs.",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/station_id_required"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="array",
|
|
|
|
* @OA\Items(ref="#/components/schemas/Api_StationRequest")
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=404, description="Station not found"),
|
|
|
|
* @OA\Response(response=403, description="Station does not support requests")
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @OA\Post(path="/station/{station_id}/request/{request_id}",
|
|
|
|
* operationId="submitSongRequest",
|
|
|
|
* tags={"Stations: Song Requests"},
|
|
|
|
* description="Submit a song request.",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/station_id_required"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="request_id",
|
|
|
|
* description="The requestable song ID",
|
|
|
|
* in="path",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string"
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=200, description="Success"),
|
|
|
|
* @OA\Response(response=404, description="Station not found"),
|
|
|
|
* @OA\Response(response=403, description="Station does not support requests")
|
|
|
|
* )
|
|
|
|
*/
|
2018-02-01 11:49:40 +00:00
|
|
|
class RequestsController
|
2016-09-02 20:37:12 +00:00
|
|
|
{
|
2019-09-29 07:50:24 +00:00
|
|
|
public function __construct(
|
2021-04-23 05:24:12 +00:00
|
|
|
protected EntityManagerInterface $em,
|
|
|
|
protected Entity\Repository\StationRequestRepository $requestRepo,
|
|
|
|
protected Entity\ApiGenerator\SongApiGenerator $songApiGenerator
|
2019-09-29 07:50:24 +00:00
|
|
|
) {
|
2017-12-09 17:29:01 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 02:30:05 +00:00
|
|
|
public function listAction(ServerRequest $request, Response $response): ResponseInterface
|
2016-09-02 20:37:12 +00:00
|
|
|
{
|
2019-08-09 15:00:21 +00:00
|
|
|
$station = $request->getStation();
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2018-04-12 22:43:58 +00:00
|
|
|
// Verify that the station supports requests.
|
2019-08-09 15:00:21 +00:00
|
|
|
$ba = $request->getStationBackend();
|
2021-01-19 17:52:45 +00:00
|
|
|
if (!$ba->supportsRequests() || !$station->getEnableRequests()) {
|
2019-08-09 15:00:21 +00:00
|
|
|
return $response->withStatus(403)
|
2019-08-21 22:33:47 +00:00
|
|
|
->withJson(new Entity\Api\Error(403, __('This station does not accept requests currently.')));
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2016-11-21 23:01:42 +00:00
|
|
|
|
2018-08-21 00:48:03 +00:00
|
|
|
$qb = $this->em->createQueryBuilder();
|
|
|
|
|
2020-10-04 22:35:41 +00:00
|
|
|
$qb->select('sm, spm, sp')
|
2018-08-21 00:48:03 +00:00
|
|
|
->from(Entity\StationMedia::class, 'sm')
|
2019-04-25 04:53:06 +00:00
|
|
|
->leftJoin('sm.playlists', 'spm')
|
2018-08-21 00:48:03 +00:00
|
|
|
->leftJoin('spm.playlist', 'sp')
|
2020-11-10 03:06:48 +00:00
|
|
|
->where('sm.storage_location = :storageLocation')
|
2018-08-21 00:48:03 +00:00
|
|
|
->andWhere('sp.id IS NOT NULL')
|
2020-11-10 03:06:48 +00:00
|
|
|
->andWhere('sp.station = :station')
|
2018-08-21 00:48:03 +00:00
|
|
|
->andWhere('sp.is_enabled = 1')
|
|
|
|
->andWhere('sp.include_in_requests = 1')
|
2020-11-10 03:06:48 +00:00
|
|
|
->setParameter('storageLocation', $station->getMediaStorageLocation())
|
|
|
|
->setParameter('station', $station);
|
2018-08-21 00:48:03 +00:00
|
|
|
|
2019-08-07 04:33:55 +00:00
|
|
|
$params = $request->getQueryParams();
|
|
|
|
|
|
|
|
if (!empty($params['sort'])) {
|
2021-11-19 21:01:50 +00:00
|
|
|
$sortDirection = (($params['sortOrder'] ?? 'ASC') === 'ASC') ? 'ASC' : 'DESC';
|
2018-08-21 00:48:03 +00:00
|
|
|
|
2021-11-19 21:01:50 +00:00
|
|
|
match ($params['sort']) {
|
|
|
|
'name', 'song_title' => $qb->addOrderBy('sm.title', $sortDirection),
|
|
|
|
'song_artist' => $qb->addOrderBy('sm.artist', $sortDirection),
|
|
|
|
'song_album' => $qb->addOrderBy('sm.album', $sortDirection),
|
|
|
|
'song_genre' => $qb->addOrderBy('sm.genre', $sortDirection),
|
|
|
|
default => null,
|
|
|
|
};
|
2018-08-21 00:48:03 +00:00
|
|
|
} else {
|
|
|
|
$qb->orderBy('sm.artist', 'ASC')
|
|
|
|
->addOrderBy('sm.title', 'ASC');
|
|
|
|
}
|
|
|
|
|
2021-04-23 02:16:00 +00:00
|
|
|
$search_phrase = trim($params['searchPhrase'] ?? '');
|
2018-08-21 00:48:03 +00:00
|
|
|
if (!empty($search_phrase)) {
|
|
|
|
$qb->andWhere('(sm.title LIKE :query OR sm.artist LIKE :query OR sm.album LIKE :query)')
|
2019-09-04 18:00:51 +00:00
|
|
|
->setParameter('query', '%' . $search_phrase . '%');
|
2018-08-21 00:48:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 01:51:53 +00:00
|
|
|
$paginator = Paginator::fromQueryBuilder($qb, $request);
|
2018-08-21 00:48:03 +00:00
|
|
|
|
|
|
|
$is_bootgrid = $paginator->isFromBootgrid();
|
2019-08-09 15:00:21 +00:00
|
|
|
$router = $request->getRouter();
|
2018-08-21 00:48:03 +00:00
|
|
|
|
2020-12-11 02:43:58 +00:00
|
|
|
$paginator->setPostprocessor(
|
2021-04-06 11:32:36 +00:00
|
|
|
function (Entity\StationMedia $media_row) use ($station, $is_bootgrid, $router) {
|
2020-12-11 02:43:58 +00:00
|
|
|
$row = new Entity\Api\StationRequest();
|
2021-04-06 11:32:36 +00:00
|
|
|
$row->song = ($this->songApiGenerator)($media_row, $station);
|
2020-12-11 02:43:58 +00:00
|
|
|
$row->request_id = $media_row->getUniqueId();
|
|
|
|
$row->request_url = (string)$router->named(
|
|
|
|
'api:requests:submit',
|
|
|
|
[
|
|
|
|
'station_id' => $station->getId(),
|
|
|
|
'media_id' => $media_row->getUniqueId(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$row->resolveUrls($router->getBaseUrl());
|
|
|
|
|
|
|
|
if ($is_bootgrid) {
|
|
|
|
return Utilities\Arrays::flattenArray($row, '_');
|
|
|
|
}
|
2016-11-08 07:46:17 +00:00
|
|
|
|
2020-12-11 02:43:58 +00:00
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
);
|
2016-11-08 07:46:17 +00:00
|
|
|
|
2018-08-21 00:48:03 +00:00
|
|
|
return $paginator->write($response);
|
2016-09-02 20:37:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 23:27:46 +00:00
|
|
|
public function submitAction(ServerRequest $request, Response $response, string $media_id): ResponseInterface
|
2016-09-02 20:37:12 +00:00
|
|
|
{
|
2019-08-09 15:00:21 +00:00
|
|
|
$station = $request->getStation();
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2018-04-12 22:43:58 +00:00
|
|
|
// Verify that the station supports requests.
|
2019-08-09 15:00:21 +00:00
|
|
|
$ba = $request->getStationBackend();
|
2021-01-19 17:52:45 +00:00
|
|
|
if (!$ba->supportsRequests() || !$station->getEnableRequests()) {
|
2019-08-09 15:00:21 +00:00
|
|
|
return $response->withStatus(403)
|
2019-08-21 22:33:47 +00:00
|
|
|
->withJson(new Entity\Api\Error(403, __('This station does not accept requests currently.')));
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2016-11-21 23:01:42 +00:00
|
|
|
|
2020-04-06 23:32:50 +00:00
|
|
|
try {
|
|
|
|
$user = $request->getUser();
|
2021-06-08 06:40:49 +00:00
|
|
|
} catch (Exception\InvalidRequestAttribute) {
|
2020-04-06 23:32:50 +00:00
|
|
|
$user = null;
|
|
|
|
}
|
2020-06-26 20:22:53 +00:00
|
|
|
|
2020-04-06 23:32:50 +00:00
|
|
|
$isAuthenticated = ($user instanceof Entity\User);
|
2018-12-12 17:26:34 +00:00
|
|
|
|
2017-01-24 00:35:16 +00:00
|
|
|
try {
|
2020-12-11 02:43:58 +00:00
|
|
|
$this->requestRepo->submit(
|
|
|
|
$station,
|
|
|
|
$media_id,
|
|
|
|
$isAuthenticated,
|
|
|
|
$request->getIp(),
|
|
|
|
$request->getHeaderLine('User-Agent')
|
|
|
|
);
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2021-09-05 22:41:10 +00:00
|
|
|
return $response->withJson(Entity\Api\Status::success());
|
2019-09-04 18:00:51 +00:00
|
|
|
} catch (Exception $e) {
|
2019-08-09 15:00:21 +00:00
|
|
|
return $response->withStatus(400)
|
|
|
|
->withJson(new Entity\Api\Error(400, $e->getMessage()));
|
2016-09-02 20:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|