repod/lib/Controller/SearchController.php
Michel Roux f17e795004
All checks were successful
repod / xml (push) Successful in 13s
repod / php (push) Successful in 49s
repod / nodejs (push) Successful in 1m42s
repod / release (push) Has been skipped
fix: don't crash if a duplicate is found on search and episode
2024-01-16 10:30:26 +01:00

45 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\GPodderSync\Core\PodcastData\PodcastData;
use OCA\RePod\AppInfo\Application;
use OCA\RePod\Service\FyydService;
use OCA\RePod\Service\ItunesService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
class SearchController extends Controller
{
public function __construct(
IRequest $request,
private LoggerInterface $logger,
private FyydService $fyydService,
private ItunesService $itunesService
) {
parent::__construct(Application::APP_ID, $request);
}
public function index(string $value): JSONResponse {
$podcasts = [];
$providers = [$this->fyydService, $this->itunesService];
foreach ($providers as $provider) {
try {
$podcasts = [...$podcasts, ...$provider->search($value)];
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), $e->getTrace());
}
}
usort($podcasts, fn (PodcastData $a, PodcastData $b) => $b->getFetchedAtUnix() <=> $a->getFetchedAtUnix());
$podcasts = array_values(array_intersect_key($podcasts, array_unique(array_map(fn (PodcastData $feed) => $feed->getLink(), $podcasts))));
return new JSONResponse($podcasts);
}
}