Allow Requests and OnDemand to work without Meilisearch.
This commit is contained in:
parent
01892a9c14
commit
d82f653718
|
@ -78,14 +78,14 @@ return static function (RouteCollectorProxy $group) {
|
||||||
/*
|
/*
|
||||||
* Song Requests
|
* Song Requests
|
||||||
*/
|
*/
|
||||||
$group->get('/requests', Controller\Api\Stations\RequestsController::class . ':listAction')
|
$group->get('/requests', Controller\Api\Stations\Requests\ListAction::class)
|
||||||
->add(new Middleware\StationSupportsFeature(StationFeatures::Requests))
|
->add(new Middleware\StationSupportsFeature(StationFeatures::Requests))
|
||||||
->setName('api:requests:list');
|
->setName('api:requests:list');
|
||||||
|
|
||||||
$group->map(
|
$group->map(
|
||||||
['GET', 'POST'],
|
['GET', 'POST'],
|
||||||
'/request/{media_id}',
|
'/request/{media_id}',
|
||||||
Controller\Api\Stations\RequestsController::class . ':submitAction'
|
Controller\Api\Stations\Requests\SubmitAction::class
|
||||||
)
|
)
|
||||||
->setName('api:requests:submit')
|
->setName('api:requests:submit')
|
||||||
->add(new Middleware\StationSupportsFeature(StationFeatures::Requests))
|
->add(new Middleware\StationSupportsFeature(StationFeatures::Requests))
|
||||||
|
|
|
@ -59,18 +59,7 @@
|
||||||
</template>
|
</template>
|
||||||
</template>
|
</template>
|
||||||
<template #cell(art)="row">
|
<template #cell(art)="row">
|
||||||
<a
|
<album-art :src="row.item.media.art" />
|
||||||
:href="row.item.media.art"
|
|
||||||
class="album-art"
|
|
||||||
target="_blank"
|
|
||||||
data-fancybox="gallery"
|
|
||||||
>
|
|
||||||
<img
|
|
||||||
class="media_manager_album_art"
|
|
||||||
:alt="$gettext('Album Art')"
|
|
||||||
:src="row.item.media.art"
|
|
||||||
>
|
|
||||||
</a>
|
|
||||||
</template>
|
</template>
|
||||||
<template #cell(size)="row">
|
<template #cell(size)="row">
|
||||||
<template v-if="!row.item.size">
|
<template v-if="!row.item.size">
|
||||||
|
@ -94,6 +83,7 @@ import Icon from '~/components/Common/Icon';
|
||||||
import PlayButton from "~/components/Common/PlayButton";
|
import PlayButton from "~/components/Common/PlayButton";
|
||||||
import {useTranslate} from "~/vendor/gettext";
|
import {useTranslate} from "~/vendor/gettext";
|
||||||
import formatFileSize from "../../functions/formatFileSize";
|
import formatFileSize from "../../functions/formatFileSize";
|
||||||
|
import AlbumArt from "~/components/Common/AlbumArt.vue";
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
listUrl: {
|
listUrl: {
|
||||||
|
@ -165,7 +155,7 @@ forEach(props.customFields.slice(), (field) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#station_on_demand_table {
|
#public_on_demand {
|
||||||
.datatable-main {
|
.datatable-main {
|
||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
}
|
}
|
||||||
|
@ -190,11 +180,5 @@ forEach(props.customFields.slice(), (field) => {
|
||||||
padding-left: 0.5rem;
|
padding-left: 0.5rem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
img.media_manager_album_art {
|
|
||||||
width: 40px;
|
|
||||||
height: auto;
|
|
||||||
border-radius: 5px;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import initBase from '~/base.js';
|
import initBase from '~/base.js';
|
||||||
|
|
||||||
|
import '~/vendor/fancybox';
|
||||||
|
|
||||||
import OnDemand from '~/components/Public/OnDemand.vue';
|
import OnDemand from '~/components/Public/OnDemand.vue';
|
||||||
|
|
||||||
export default initBase(OnDemand);
|
export default initBase(OnDemand);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import initBase from '~/base.js';
|
import initBase from '~/base.js';
|
||||||
|
|
||||||
|
import '~/vendor/fancybox';
|
||||||
|
|
||||||
import Requests from '~/components/Public/Requests.vue';
|
import Requests from '~/components/Public/Requests.vue';
|
||||||
|
|
||||||
export default initBase(Requests);
|
export default initBase(Requests);
|
||||||
|
|
|
@ -4,7 +4,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Controller\Api\Stations\OnDemand;
|
namespace App\Controller\Api\Stations\OnDemand;
|
||||||
|
|
||||||
|
use App\Doctrine\Paginator\HydratingAdapter;
|
||||||
use App\Entity;
|
use App\Entity;
|
||||||
|
use App\Entity\StationMedia;
|
||||||
use App\Http\Response;
|
use App\Http\Response;
|
||||||
use App\Http\ServerRequest;
|
use App\Http\ServerRequest;
|
||||||
use App\Paginator;
|
use App\Paginator;
|
||||||
|
@ -34,45 +36,88 @@ final class ListAction
|
||||||
->withJson(new Entity\Api\Error(403, __('This station does not support on-demand streaming.')));
|
->withJson(new Entity\Api\Error(403, __('This station does not support on-demand streaming.')));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->meilisearch->isSupported()) {
|
|
||||||
return $response->withStatus(403)
|
|
||||||
->withJson(new Entity\Api\Error(403, __('This feature is not supported on this installation.')));
|
|
||||||
}
|
|
||||||
|
|
||||||
$index = $this->meilisearch->getIndex($station->getMediaStorageLocation());
|
|
||||||
|
|
||||||
$queryParams = $request->getQueryParams();
|
$queryParams = $request->getQueryParams();
|
||||||
$searchPhrase = trim($queryParams['searchPhrase'] ?? '');
|
$searchPhrase = trim($queryParams['searchPhrase'] ?? '');
|
||||||
|
|
||||||
$searchParams = [];
|
$sortField = (string)($queryParams['sort'] ?? '');
|
||||||
if (!empty($queryParams['sort'])) {
|
$sortDirection = strtolower($queryParams['sortOrder'] ?? 'asc');
|
||||||
$sortField = (string)$queryParams['sort'];
|
|
||||||
$sortDirection = strtolower($queryParams['sortOrder'] ?? 'asc');
|
|
||||||
$searchParams['sort'] = [$sortField . ':' . $sortDirection];
|
|
||||||
}
|
|
||||||
|
|
||||||
$hydrateCallback = function (array $results) {
|
if ($this->meilisearch->isSupported()) {
|
||||||
$ids = array_column($results, 'id');
|
$index = $this->meilisearch->getIndex($station->getMediaStorageLocation());
|
||||||
|
|
||||||
return $this->em->createQuery(
|
$searchParams = [];
|
||||||
<<<'DQL'
|
if (!empty($sortField)) {
|
||||||
|
$searchParams['sort'] = [$sortField . ':' . $sortDirection];
|
||||||
|
}
|
||||||
|
|
||||||
|
$paginatorAdapter = $index->getOnDemandSearchPaginator(
|
||||||
|
$station,
|
||||||
|
$searchPhrase,
|
||||||
|
$searchParams,
|
||||||
|
);
|
||||||
|
|
||||||
|
$hydrateCallback = function (iterable $results) {
|
||||||
|
$ids = array_column([...$results], 'id');
|
||||||
|
|
||||||
|
return $this->em->createQuery(
|
||||||
|
<<<'DQL'
|
||||||
SELECT sm
|
SELECT sm
|
||||||
FROM App\Entity\StationMedia sm
|
FROM App\Entity\StationMedia sm
|
||||||
WHERE sm.id IN (:ids)
|
WHERE sm.id IN (:ids)
|
||||||
ORDER BY FIELD(sm.id, :ids)
|
ORDER BY FIELD(sm.id, :ids)
|
||||||
DQL
|
DQL
|
||||||
)->setParameter('ids', $ids)
|
)->setParameter('ids', $ids)
|
||||||
->toIterable();
|
->toIterable();
|
||||||
};
|
};
|
||||||
|
|
||||||
$paginatorAdapter = $index->getOnDemandSearchPaginator(
|
$hydrateAdapter = new HydratingAdapter(
|
||||||
$station,
|
$paginatorAdapter,
|
||||||
$hydrateCallback,
|
$hydrateCallback(...)
|
||||||
$searchPhrase,
|
);
|
||||||
$searchParams,
|
|
||||||
);
|
|
||||||
|
|
||||||
$paginator = Paginator::fromAdapter($paginatorAdapter, $request);
|
$paginator = Paginator::fromAdapter($hydrateAdapter, $request);
|
||||||
|
} else {
|
||||||
|
$playlistsRaw = $this->em->createQuery(
|
||||||
|
<<<'DQL'
|
||||||
|
SELECT sp.id FROM App\Entity\StationPlaylist sp
|
||||||
|
WHERE sp.station = :station
|
||||||
|
AND sp.is_enabled = 1 AND sp.include_in_on_demand = 1
|
||||||
|
DQL
|
||||||
|
)->setParameter('station', $station)
|
||||||
|
->getArrayResult();
|
||||||
|
|
||||||
|
$playlistIds = array_column($playlistsRaw, 'id');
|
||||||
|
|
||||||
|
$qb = $this->em->createQueryBuilder();
|
||||||
|
$qb->select('sm, spm, sp')
|
||||||
|
->from(StationMedia::class, 'sm')
|
||||||
|
->leftJoin('sm.playlists', 'spm')
|
||||||
|
->leftJoin('spm.playlist', 'sp')
|
||||||
|
->where('sm.storage_location = :storageLocation')
|
||||||
|
->andWhere('sp.id IN (:playlistIds)')
|
||||||
|
->setParameter('storageLocation', $station->getMediaStorageLocation())
|
||||||
|
->setParameter('playlistIds', $playlistIds);
|
||||||
|
|
||||||
|
if (!empty($sortField)) {
|
||||||
|
match ($sortField) {
|
||||||
|
'name', 'title' => $qb->addOrderBy('sm.title', $sortDirection),
|
||||||
|
'artist' => $qb->addOrderBy('sm.artist', $sortDirection),
|
||||||
|
'album' => $qb->addOrderBy('sm.album', $sortDirection),
|
||||||
|
'genre' => $qb->addOrderBy('sm.genre', $sortDirection),
|
||||||
|
default => null,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
$qb->orderBy('sm.artist', 'ASC')
|
||||||
|
->addOrderBy('sm.title', 'ASC');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($searchPhrase)) {
|
||||||
|
$qb->andWhere('(sm.title LIKE :query OR sm.artist LIKE :query OR sm.album LIKE :query)')
|
||||||
|
->setParameter('query', '%' . $searchPhrase . '%');
|
||||||
|
}
|
||||||
|
|
||||||
|
$paginator = Paginator::fromQueryBuilder($qb, $request);
|
||||||
|
}
|
||||||
|
|
||||||
$router = $request->getRouter();
|
$router = $request->getRouter();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,172 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller\Api\Stations\Requests;
|
||||||
|
|
||||||
|
use App\Doctrine\Paginator\HydratingAdapter;
|
||||||
|
use App\Entity\Api\Error;
|
||||||
|
use App\Entity\Api\StationRequest;
|
||||||
|
use App\Entity\ApiGenerator\SongApiGenerator;
|
||||||
|
use App\Entity\StationMedia;
|
||||||
|
use App\Http\Response;
|
||||||
|
use App\Http\ServerRequest;
|
||||||
|
use App\OpenApi;
|
||||||
|
use App\Paginator;
|
||||||
|
use App\Service\Meilisearch;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use OpenApi\Attributes as OA;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
#[
|
||||||
|
OA\Get(
|
||||||
|
path: '/station/{station_id}/requests',
|
||||||
|
operationId: 'getRequestableSongs',
|
||||||
|
description: 'Return a list of requestable songs.',
|
||||||
|
tags: ['Stations: Song Requests'],
|
||||||
|
parameters: [
|
||||||
|
new OA\Parameter(ref: OpenApi::REF_STATION_ID_REQUIRED),
|
||||||
|
],
|
||||||
|
responses: [
|
||||||
|
new OA\Response(
|
||||||
|
response: 200,
|
||||||
|
description: 'Success',
|
||||||
|
content: new OA\JsonContent(
|
||||||
|
type: 'array',
|
||||||
|
items: new OA\Items(ref: '#/components/schemas/Api_StationRequest')
|
||||||
|
)
|
||||||
|
),
|
||||||
|
new OA\Response(ref: OpenApi::REF_RESPONSE_ACCESS_DENIED, response: 403),
|
||||||
|
new OA\Response(ref: OpenApi::REF_RESPONSE_NOT_FOUND, response: 404),
|
||||||
|
new OA\Response(ref: OpenApi::REF_RESPONSE_GENERIC_ERROR, response: 500),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
final class ListAction
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $em,
|
||||||
|
private readonly SongApiGenerator $songApiGenerator,
|
||||||
|
private readonly Meilisearch $meilisearch
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke(
|
||||||
|
ServerRequest $request,
|
||||||
|
Response $response,
|
||||||
|
string $station_id
|
||||||
|
): ResponseInterface {
|
||||||
|
$station = $request->getStation();
|
||||||
|
|
||||||
|
// Verify that the station supports on-demand streaming.
|
||||||
|
if (!$station->getEnableRequests()) {
|
||||||
|
return $response->withStatus(403)
|
||||||
|
->withJson(new Error(403, __('This station does not support requests.')));
|
||||||
|
}
|
||||||
|
|
||||||
|
$queryParams = $request->getQueryParams();
|
||||||
|
$searchPhrase = trim($queryParams['searchPhrase'] ?? '');
|
||||||
|
$sortField = (string)($queryParams['sort'] ?? '');
|
||||||
|
$sortDirection = strtolower($queryParams['sortOrder'] ?? 'asc');
|
||||||
|
|
||||||
|
if ($this->meilisearch->isSupported()) {
|
||||||
|
$index = $this->meilisearch->getIndex($station->getMediaStorageLocation());
|
||||||
|
|
||||||
|
$searchParams = [];
|
||||||
|
if (!empty($sortField)) {
|
||||||
|
$searchParams['sort'] = [$sortField . ':' . $sortDirection];
|
||||||
|
}
|
||||||
|
|
||||||
|
$paginatorAdapter = $index->getRequestableSearchPaginator(
|
||||||
|
$station,
|
||||||
|
$searchPhrase,
|
||||||
|
$searchParams,
|
||||||
|
);
|
||||||
|
|
||||||
|
$hydrateCallback = function (iterable $results) {
|
||||||
|
$ids = array_column([...$results], 'id');
|
||||||
|
|
||||||
|
return $this->em->createQuery(
|
||||||
|
<<<'DQL'
|
||||||
|
SELECT sm
|
||||||
|
FROM App\Entity\StationMedia sm
|
||||||
|
WHERE sm.id IN (:ids)
|
||||||
|
ORDER BY FIELD(sm.id, :ids)
|
||||||
|
DQL
|
||||||
|
)->setParameter('ids', $ids)
|
||||||
|
->toIterable();
|
||||||
|
};
|
||||||
|
|
||||||
|
$hydratingAdapter = new HydratingAdapter(
|
||||||
|
$paginatorAdapter,
|
||||||
|
$hydrateCallback(...)
|
||||||
|
);
|
||||||
|
|
||||||
|
$paginator = Paginator::fromAdapter($hydratingAdapter, $request);
|
||||||
|
} else {
|
||||||
|
$playlistsRaw = $this->em->createQuery(
|
||||||
|
<<<'DQL'
|
||||||
|
SELECT sp.id FROM App\Entity\StationPlaylist sp
|
||||||
|
WHERE sp.station = :station
|
||||||
|
AND sp.is_enabled = 1 AND sp.include_in_requests = 1
|
||||||
|
DQL
|
||||||
|
)->setParameter('station', $station)
|
||||||
|
->getArrayResult();
|
||||||
|
|
||||||
|
$playlistIds = array_column($playlistsRaw, 'id');
|
||||||
|
|
||||||
|
$qb = $this->em->createQueryBuilder();
|
||||||
|
$qb->select('sm, spm, sp')
|
||||||
|
->from(StationMedia::class, 'sm')
|
||||||
|
->leftJoin('sm.playlists', 'spm')
|
||||||
|
->leftJoin('spm.playlist', 'sp')
|
||||||
|
->where('sm.storage_location = :storageLocation')
|
||||||
|
->andWhere('sp.id IN (:playlistIds)')
|
||||||
|
->setParameter('storageLocation', $station->getMediaStorageLocation())
|
||||||
|
->setParameter('playlistIds', $playlistIds);
|
||||||
|
|
||||||
|
if (!empty($sortField)) {
|
||||||
|
match ($sortField) {
|
||||||
|
'name', 'title' => $qb->addOrderBy('sm.title', $sortDirection),
|
||||||
|
'artist' => $qb->addOrderBy('sm.artist', $sortDirection),
|
||||||
|
'album' => $qb->addOrderBy('sm.album', $sortDirection),
|
||||||
|
'genre' => $qb->addOrderBy('sm.genre', $sortDirection),
|
||||||
|
default => null,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
$qb->orderBy('sm.artist', 'ASC')
|
||||||
|
->addOrderBy('sm.title', 'ASC');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($searchPhrase)) {
|
||||||
|
$qb->andWhere('(sm.title LIKE :query OR sm.artist LIKE :query OR sm.album LIKE :query)')
|
||||||
|
->setParameter('query', '%' . $searchPhrase . '%');
|
||||||
|
}
|
||||||
|
|
||||||
|
$paginator = Paginator::fromQueryBuilder($qb, $request);
|
||||||
|
}
|
||||||
|
|
||||||
|
$router = $request->getRouter();
|
||||||
|
|
||||||
|
$paginator->setPostprocessor(
|
||||||
|
function (StationMedia $media) use ($station, $router) {
|
||||||
|
$row = new StationRequest();
|
||||||
|
$row->song = ($this->songApiGenerator)($media, $station, $router->getBaseUrl());
|
||||||
|
$row->request_id = $media->getUniqueId();
|
||||||
|
$row->request_url = $router->named(
|
||||||
|
'api:requests:submit',
|
||||||
|
[
|
||||||
|
'station_id' => $station->getId(),
|
||||||
|
'media_id' => $media->getUniqueId(),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$row->resolveUrls($router->getBaseUrl());
|
||||||
|
|
||||||
|
return $row;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return $paginator->write($response);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller\Api\Stations\Requests;
|
||||||
|
|
||||||
|
use App\Entity\Api\Error;
|
||||||
|
use App\Entity\Api\Status;
|
||||||
|
use App\Entity\Repository\StationRequestRepository;
|
||||||
|
use App\Entity\User;
|
||||||
|
use App\Exception\InvalidRequestAttribute;
|
||||||
|
use App\Http\Response;
|
||||||
|
use App\Http\ServerRequest;
|
||||||
|
use App\OpenApi;
|
||||||
|
use Exception;
|
||||||
|
use OpenApi\Attributes as OA;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
#[
|
||||||
|
OA\Post(
|
||||||
|
path: '/station/{station_id}/request/{request_id}',
|
||||||
|
operationId: 'submitSongRequest',
|
||||||
|
description: 'Submit a song request.',
|
||||||
|
tags: ['Stations: Song Requests'],
|
||||||
|
parameters: [
|
||||||
|
new OA\Parameter(ref: OpenApi::REF_STATION_ID_REQUIRED),
|
||||||
|
new OA\Parameter(
|
||||||
|
name: 'request_id',
|
||||||
|
description: 'The requestable song ID',
|
||||||
|
in: 'path',
|
||||||
|
required: true,
|
||||||
|
schema: new OA\Schema(type: 'string')
|
||||||
|
),
|
||||||
|
],
|
||||||
|
responses: [
|
||||||
|
new OA\Response(ref: OpenApi::REF_RESPONSE_SUCCESS, response: 200),
|
||||||
|
new OA\Response(ref: OpenApi::REF_RESPONSE_ACCESS_DENIED, response: 403),
|
||||||
|
new OA\Response(ref: OpenApi::REF_RESPONSE_NOT_FOUND, response: 404),
|
||||||
|
new OA\Response(ref: OpenApi::REF_RESPONSE_GENERIC_ERROR, response: 500),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
final class SubmitAction
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly StationRequestRepository $requestRepo
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke(
|
||||||
|
ServerRequest $request,
|
||||||
|
Response $response,
|
||||||
|
string $station_id,
|
||||||
|
string $media_id
|
||||||
|
): ResponseInterface {
|
||||||
|
$station = $request->getStation();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$user = $request->getUser();
|
||||||
|
} catch (InvalidRequestAttribute) {
|
||||||
|
$user = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$isAuthenticated = ($user instanceof User);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->requestRepo->submit(
|
||||||
|
$station,
|
||||||
|
$media_id,
|
||||||
|
$isAuthenticated,
|
||||||
|
$request->getIp(),
|
||||||
|
$request->getHeaderLine('User-Agent')
|
||||||
|
);
|
||||||
|
|
||||||
|
return $response->withJson(Status::success());
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return $response->withStatus(400)
|
||||||
|
->withJson(Error::fromException($e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,182 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace App\Controller\Api\Stations;
|
|
||||||
|
|
||||||
use App\Entity;
|
|
||||||
use App\Exception;
|
|
||||||
use App\Http\Response;
|
|
||||||
use App\Http\ServerRequest;
|
|
||||||
use App\OpenApi;
|
|
||||||
use App\Paginator;
|
|
||||||
use App\Service\Meilisearch;
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
|
||||||
|
|
||||||
#[
|
|
||||||
OA\Get(
|
|
||||||
path: '/station/{station_id}/requests',
|
|
||||||
operationId: 'getRequestableSongs',
|
|
||||||
description: 'Return a list of requestable songs.',
|
|
||||||
tags: ['Stations: Song Requests'],
|
|
||||||
parameters: [
|
|
||||||
new OA\Parameter(ref: OpenApi::REF_STATION_ID_REQUIRED),
|
|
||||||
],
|
|
||||||
responses: [
|
|
||||||
new OA\Response(
|
|
||||||
response: 200,
|
|
||||||
description: 'Success',
|
|
||||||
content: new OA\JsonContent(
|
|
||||||
type: 'array',
|
|
||||||
items: new OA\Items(ref: '#/components/schemas/Api_StationRequest')
|
|
||||||
)
|
|
||||||
),
|
|
||||||
new OA\Response(ref: OpenApi::REF_RESPONSE_ACCESS_DENIED, response: 403),
|
|
||||||
new OA\Response(ref: OpenApi::REF_RESPONSE_NOT_FOUND, response: 404),
|
|
||||||
new OA\Response(ref: OpenApi::REF_RESPONSE_GENERIC_ERROR, response: 500),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
OA\Post(
|
|
||||||
path: '/station/{station_id}/request/{request_id}',
|
|
||||||
operationId: 'submitSongRequest',
|
|
||||||
description: 'Submit a song request.',
|
|
||||||
tags: ['Stations: Song Requests'],
|
|
||||||
parameters: [
|
|
||||||
new OA\Parameter(ref: OpenApi::REF_STATION_ID_REQUIRED),
|
|
||||||
new OA\Parameter(
|
|
||||||
name: 'request_id',
|
|
||||||
description: 'The requestable song ID',
|
|
||||||
in: 'path',
|
|
||||||
required: true,
|
|
||||||
schema: new OA\Schema(type: 'string')
|
|
||||||
),
|
|
||||||
],
|
|
||||||
responses: [
|
|
||||||
new OA\Response(ref: OpenApi::REF_RESPONSE_SUCCESS, response: 200),
|
|
||||||
new OA\Response(ref: OpenApi::REF_RESPONSE_ACCESS_DENIED, response: 403),
|
|
||||||
new OA\Response(ref: OpenApi::REF_RESPONSE_NOT_FOUND, response: 404),
|
|
||||||
new OA\Response(ref: OpenApi::REF_RESPONSE_GENERIC_ERROR, response: 500),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
final class RequestsController
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
private readonly EntityManagerInterface $em,
|
|
||||||
private readonly Entity\Repository\StationRequestRepository $requestRepo,
|
|
||||||
private readonly Entity\ApiGenerator\SongApiGenerator $songApiGenerator,
|
|
||||||
private readonly Meilisearch $meilisearch
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function listAction(
|
|
||||||
ServerRequest $request,
|
|
||||||
Response $response,
|
|
||||||
string $station_id
|
|
||||||
): ResponseInterface {
|
|
||||||
$station = $request->getStation();
|
|
||||||
|
|
||||||
// Verify that the station supports on-demand streaming.
|
|
||||||
if (!$station->getEnableRequests()) {
|
|
||||||
return $response->withStatus(403)
|
|
||||||
->withJson(new Entity\Api\Error(403, __('This station does not support requests.')));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->meilisearch->isSupported()) {
|
|
||||||
return $response->withStatus(403)
|
|
||||||
->withJson(new Entity\Api\Error(403, __('This feature is not supported on this installation.')));
|
|
||||||
}
|
|
||||||
|
|
||||||
$index = $this->meilisearch->getIndex($station->getMediaStorageLocation());
|
|
||||||
|
|
||||||
$queryParams = $request->getQueryParams();
|
|
||||||
$searchPhrase = trim($queryParams['searchPhrase'] ?? '');
|
|
||||||
|
|
||||||
$searchParams = [];
|
|
||||||
if (!empty($queryParams['sort'])) {
|
|
||||||
$sortField = (string)$queryParams['sort'];
|
|
||||||
$sortDirection = strtolower($queryParams['sortOrder'] ?? 'asc');
|
|
||||||
$searchParams['sort'] = [$sortField . ':' . $sortDirection];
|
|
||||||
}
|
|
||||||
|
|
||||||
$hydrateCallback = function (array $results) {
|
|
||||||
$ids = array_column($results, 'id');
|
|
||||||
|
|
||||||
return $this->em->createQuery(
|
|
||||||
<<<'DQL'
|
|
||||||
SELECT sm
|
|
||||||
FROM App\Entity\StationMedia sm
|
|
||||||
WHERE sm.id IN (:ids)
|
|
||||||
ORDER BY FIELD(sm.id, :ids)
|
|
||||||
DQL
|
|
||||||
)->setParameter('ids', $ids)
|
|
||||||
->toIterable();
|
|
||||||
};
|
|
||||||
|
|
||||||
$paginatorAdapter = $index->getOnDemandSearchPaginator(
|
|
||||||
$station,
|
|
||||||
$hydrateCallback,
|
|
||||||
$searchPhrase,
|
|
||||||
$searchParams,
|
|
||||||
);
|
|
||||||
|
|
||||||
$paginator = Paginator::fromAdapter($paginatorAdapter, $request);
|
|
||||||
|
|
||||||
$router = $request->getRouter();
|
|
||||||
|
|
||||||
$paginator->setPostprocessor(
|
|
||||||
function (Entity\StationMedia $media) use ($station, $router) {
|
|
||||||
$row = new Entity\Api\StationRequest();
|
|
||||||
$row->song = ($this->songApiGenerator)($media, $station, $router->getBaseUrl());
|
|
||||||
$row->request_id = $media->getUniqueId();
|
|
||||||
$row->request_url = $router->named(
|
|
||||||
'api:requests:submit',
|
|
||||||
[
|
|
||||||
'station_id' => $station->getId(),
|
|
||||||
'media_id' => $media->getUniqueId(),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$row->resolveUrls($router->getBaseUrl());
|
|
||||||
|
|
||||||
return $row;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return $paginator->write($response);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function submitAction(
|
|
||||||
ServerRequest $request,
|
|
||||||
Response $response,
|
|
||||||
string $station_id,
|
|
||||||
string $media_id
|
|
||||||
): ResponseInterface {
|
|
||||||
$station = $request->getStation();
|
|
||||||
|
|
||||||
try {
|
|
||||||
$user = $request->getUser();
|
|
||||||
} catch (Exception\InvalidRequestAttribute) {
|
|
||||||
$user = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$isAuthenticated = ($user instanceof Entity\User);
|
|
||||||
|
|
||||||
try {
|
|
||||||
$this->requestRepo->submit(
|
|
||||||
$station,
|
|
||||||
$media_id,
|
|
||||||
$isAuthenticated,
|
|
||||||
$request->getIp(),
|
|
||||||
$request->getHeaderLine('User-Agent')
|
|
||||||
);
|
|
||||||
|
|
||||||
return $response->withJson(Entity\Api\Status::success());
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return $response->withStatus(400)
|
|
||||||
->withJson(new Entity\Api\Error(400, $e->getMessage()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Doctrine\Paginator;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Pagerfanta\Adapter\AdapterInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adapter which hydrates paginated records with a callback query.
|
||||||
|
*
|
||||||
|
* @template T
|
||||||
|
* @implements AdapterInterface<T>
|
||||||
|
*/
|
||||||
|
final class HydratingAdapter implements AdapterInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param AdapterInterface<T> $wrapped
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
private readonly AdapterInterface $wrapped,
|
||||||
|
private readonly Closure $hydrateCallback,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getNbResults(): int
|
||||||
|
{
|
||||||
|
return $this->wrapped->getNbResults();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSlice(int $offset, int $length): iterable
|
||||||
|
{
|
||||||
|
$results = $this->wrapped->getSlice($offset, $length);
|
||||||
|
yield from ($this->hydrateCallback)($results);
|
||||||
|
}
|
||||||
|
}
|
|
@ -381,17 +381,15 @@ final class Index
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return PaginatorAdapter<int|string, mixed>
|
* @return PaginatorAdapter<array>
|
||||||
*/
|
*/
|
||||||
public function getRequestableSearchPaginator(
|
public function getRequestableSearchPaginator(
|
||||||
Station $station,
|
Station $station,
|
||||||
callable $hydrateCallback,
|
|
||||||
?string $query,
|
?string $query,
|
||||||
array $searchParams = [],
|
array $searchParams = [],
|
||||||
array $options = [],
|
array $options = [],
|
||||||
): PaginatorAdapter {
|
): PaginatorAdapter {
|
||||||
return $this->getSearchPaginator(
|
return $this->getSearchPaginator(
|
||||||
$hydrateCallback,
|
|
||||||
$query,
|
$query,
|
||||||
[
|
[
|
||||||
...$searchParams,
|
...$searchParams,
|
||||||
|
@ -406,17 +404,15 @@ final class Index
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return PaginatorAdapter<int|string, mixed>
|
* @return PaginatorAdapter<array>
|
||||||
*/
|
*/
|
||||||
public function getOnDemandSearchPaginator(
|
public function getOnDemandSearchPaginator(
|
||||||
Station $station,
|
Station $station,
|
||||||
callable $hydrateCallback,
|
|
||||||
?string $query,
|
?string $query,
|
||||||
array $searchParams = [],
|
array $searchParams = [],
|
||||||
array $options = [],
|
array $options = [],
|
||||||
): PaginatorAdapter {
|
): PaginatorAdapter {
|
||||||
return $this->getSearchPaginator(
|
return $this->getSearchPaginator(
|
||||||
$hydrateCallback,
|
|
||||||
$query,
|
$query,
|
||||||
[
|
[
|
||||||
...$searchParams,
|
...$searchParams,
|
||||||
|
@ -431,17 +427,15 @@ final class Index
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return PaginatorAdapter<int|string, mixed>
|
* @return PaginatorAdapter<array>
|
||||||
*/
|
*/
|
||||||
public function getSearchPaginator(
|
public function getSearchPaginator(
|
||||||
callable $hydrateCallback,
|
|
||||||
?string $query,
|
?string $query,
|
||||||
array $searchParams = [],
|
array $searchParams = [],
|
||||||
array $options = [],
|
array $options = [],
|
||||||
): PaginatorAdapter {
|
): PaginatorAdapter {
|
||||||
return new PaginatorAdapter(
|
return new PaginatorAdapter(
|
||||||
$this->indexClient,
|
$this->indexClient,
|
||||||
$hydrateCallback(...),
|
|
||||||
$query,
|
$query,
|
||||||
$searchParams,
|
$searchParams,
|
||||||
$options,
|
$options,
|
||||||
|
|
|
@ -4,23 +4,20 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Service\Meilisearch;
|
namespace App\Service\Meilisearch;
|
||||||
|
|
||||||
use Closure;
|
|
||||||
use Meilisearch\Endpoints\Indexes;
|
use Meilisearch\Endpoints\Indexes;
|
||||||
use Meilisearch\Search\SearchResult;
|
use Meilisearch\Search\SearchResult;
|
||||||
use Pagerfanta\Adapter\AdapterInterface;
|
use Pagerfanta\Adapter\AdapterInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapter which uses Meilisearch to perform a search, then uses a callback to hydrate with database records.
|
* Adapter which uses Meilisearch to perform a search.
|
||||||
*
|
*
|
||||||
* @template TKey of array-key
|
* @template T of array
|
||||||
* @template T
|
|
||||||
* @implements AdapterInterface<T>
|
* @implements AdapterInterface<T>
|
||||||
*/
|
*/
|
||||||
final class PaginatorAdapter implements AdapterInterface
|
final class PaginatorAdapter implements AdapterInterface
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly Indexes $indexClient,
|
private readonly Indexes $indexClient,
|
||||||
private readonly Closure $hydrateCallback,
|
|
||||||
private readonly ?string $query,
|
private readonly ?string $query,
|
||||||
private readonly array $searchParams = [],
|
private readonly array $searchParams = [],
|
||||||
private readonly array $options = [],
|
private readonly array $options = [],
|
||||||
|
@ -55,6 +52,6 @@ final class PaginatorAdapter implements AdapterInterface
|
||||||
$this->options
|
$this->options
|
||||||
);
|
);
|
||||||
|
|
||||||
return ($this->hydrateCallback)($results->getHits());
|
yield from $results->getHits();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue