repod/lib/Service/SearchProvider.php

66 lines
1.6 KiB
PHP
Raw Normal View History

2024-01-18 10:43:58 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
use OCA\RePod\AppInfo\Application;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
class SearchProvider implements IProvider
{
public function __construct(
2024-11-12 09:11:21 +00:00
private readonly IL10N $l10n,
private readonly IURLGenerator $urlGenerator,
private readonly MultiPodService $multiPodService
2024-01-18 10:43:58 +00:00
) {}
public function getId(): string {
return Application::APP_ID;
}
public function getName(): string {
2024-01-18 16:24:06 +00:00
return $this->l10n->t('Podcast');
2024-01-18 10:43:58 +00:00
}
public function getOrder(string $route, array $routeParameters): int {
2024-11-10 12:18:53 +00:00
if (str_starts_with($route, Application::APP_ID.'.')) {
2024-01-18 10:43:58 +00:00
// Active app, prefer my results
return -1;
}
return 25;
}
public function search(IUser $user, ISearchQuery $query): SearchResult {
$podcasts = $this->multiPodService->search($query->getTerm());
$searchResults = [];
foreach ($podcasts as $podcast) {
$title = $podcast->getTitle();
$link = $podcast->getLink();
2024-01-18 16:42:20 +00:00
if (isset($title, $link)) {
2024-01-18 10:43:58 +00:00
$searchResults[] = new SearchResultEntry(
$podcast->getImageUrl() ?? $this->urlGenerator->linkTo(Application::APP_ID, 'img/app.svg'),
$title,
$podcast->getAuthor() ?? '',
$this->urlGenerator->linkToRoute('repod.page.index').'/#/'.urlencode(base64_encode($link)),
$this->urlGenerator->linkTo(Application::APP_ID, 'img/app.svg')
);
}
}
return SearchResult::complete(
2024-01-18 16:24:06 +00:00
$this->l10n->t('Podcast'),
2024-01-18 10:43:58 +00:00
$searchResults
);
}
}