RePod-Nextcloud-App/lib/Controller/EpisodesController.php
2023-12-24 16:59:34 +01:00

48 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\RePod\AppInfo\Application;
use OCA\RePod\Core\EpisodeAction\EpisodeActionExtraData;
use OCA\RePod\Core\EpisodeAction\EpisodeActionReader;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
class EpisodesController extends Controller
{
public function __construct(
IRequest $request,
private IClientService $clientService,
private EpisodeActionReader $episodeActionReader
) {
parent::__construct(Application::APP_ID, $request);
}
public function list(string $url): JSONResponse {
$client = $this->clientService->newClient();
$feed = $client->get($url);
$episodes = $this->episodeActionReader->parseRssXml((string) $feed->getBody());
usort($episodes, fn (EpisodeActionExtraData $a, EpisodeActionExtraData $b) => $b->getFetchedAtUnix() <=> $a->getFetchedAtUnix());
$episodes = array_intersect_key($episodes, array_unique(array_map(fn (EpisodeActionExtraData $episode) => $episode->getEpisodeGuid(), $episodes)));
return new JSONResponse($episodes, $feed->getStatusCode());
}
public function action(string $url): JSONResponse {
$action = $this->episodeActionReader->findByEpisodeUrl($url);
if ($action) {
return new JSONResponse($action->toArray());
}
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
}