repod/lib/Controller/EpisodesController.php
Michel Roux bebd6bf5ed
All checks were successful
repod / xml (push) Successful in 13s
repod / php (push) Successful in 34s
repod / nodejs (push) Successful in 1m55s
repod / release (push) Has been skipped
fix: 🔒 app should not require admin access (closes #76)
2024-03-16 17:42:28 +01:00

58 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
use OCA\RePod\AppInfo\Application;
use OCA\RePod\Core\EpisodeAction\EpisodeActionExtraData;
use OCA\RePod\Core\EpisodeAction\EpisodeActionReader;
use OCA\RePod\Service\UserService;
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 EpisodeActionReader $episodeActionReader,
private EpisodeActionRepository $episodeActionRepository,
private IClientService $clientService,
private UserService $userService
) {
parent::__construct(Application::APP_ID, $request);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
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->getPubDate() <=> $a->getPubDate());
$episodes = array_values(array_intersect_key($episodes, array_unique(array_map(fn (EpisodeActionExtraData $episode) => $episode->getGuid(), $episodes))));
return new JSONResponse($episodes, $feed->getStatusCode());
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function action(string $url): JSONResponse {
$action = $this->episodeActionRepository->findByEpisodeUrl($url, $this->userService->getUserUID());
if ($action) {
return new JSONResponse($action->toArray());
}
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
}