repod/lib/Controller/EpisodesController.php

59 lines
2.0 KiB
PHP
Raw Normal View History

2023-08-24 10:48:10 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
2023-08-24 10:48:10 +00:00
use OCA\RePod\AppInfo\Application;
2023-12-24 15:59:34 +00:00
use OCA\RePod\Core\EpisodeAction\EpisodeActionExtraData;
2023-08-24 15:43:10 +00:00
use OCA\RePod\Core\EpisodeAction\EpisodeActionReader;
use OCA\RePod\Service\UserService;
2023-08-24 10:48:10 +00:00
use OCP\AppFramework\Controller;
2023-08-29 06:48:54 +00:00
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
2023-08-24 15:43:10 +00:00
use OCP\AppFramework\Http\JSONResponse;
use OCP\Http\Client\IClientService;
2023-08-24 10:48:10 +00:00
use OCP\IRequest;
class EpisodesController extends Controller
{
2023-08-24 15:43:10 +00:00
public function __construct(
IRequest $request,
2024-11-12 09:11:21 +00:00
private readonly EpisodeActionReader $episodeActionReader,
private readonly EpisodeActionRepository $episodeActionRepository,
private readonly IClientService $clientService,
private readonly UserService $userService
2023-08-24 15:43:10 +00:00
) {
2023-08-24 10:48:10 +00:00
parent::__construct(Application::APP_ID, $request);
}
2023-08-24 15:43:10 +00:00
#[NoAdminRequired]
#[NoCSRFRequired]
#[FrontpageRoute(verb: 'GET', url: '/episodes/list')]
2023-12-23 16:25:20 +00:00
public function list(string $url): JSONResponse {
2023-08-24 15:43:10 +00:00
$client = $this->clientService->newClient();
$feed = $client->get($url);
2023-12-24 15:59:34 +00:00
$episodes = $this->episodeActionReader->parseRssXml((string) $feed->getBody());
2024-11-10 12:18:53 +00:00
usort($episodes, fn (EpisodeActionExtraData $a, EpisodeActionExtraData $b): int => $b->getPubDate() <=> $a->getPubDate());
$episodes = array_values(array_intersect_key($episodes, array_unique(array_map(fn (EpisodeActionExtraData $episode): string => $episode->getGuid(), $episodes))));
2023-12-24 15:59:34 +00:00
return new JSONResponse($episodes, $feed->getStatusCode());
2023-08-24 15:43:10 +00:00
}
2023-08-29 06:48:54 +00:00
#[NoAdminRequired]
#[NoCSRFRequired]
#[FrontpageRoute(verb: 'GET', url: '/episodes/action')]
2023-12-23 16:25:20 +00:00
public function action(string $url): JSONResponse {
$action = $this->episodeActionRepository->findByEpisodeUrl($url, $this->userService->getUserUID());
2023-08-29 06:48:54 +00:00
if ($action) {
return new JSONResponse($action->toArray());
}
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
2023-08-24 10:48:10 +00:00
}