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