update torrent features

This commit is contained in:
ghost 2023-10-13 00:08:45 +03:00
parent d1f8c126b0
commit e713c17333
16 changed files with 661 additions and 329 deletions

View file

@ -9,175 +9,21 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Service\UserService;
use App\Service\ArticleService;
use App\Service\TorrentService;
use App\Service\ActivityService;
class SearchController extends AbstractController
{
#[Route(
'/{_locale}/search',
name: 'search_index',
methods:
[
'GET'
]
)]
public function index(
Request $request,
UserService $userService,
ArticleService $articleService,
TorrentService $torrentService,
ActivityService $activityService
): Response
{
// Init user
$user = $this->initUser(
$request,
$userService,
$activityService
);
$article = $request->query->get('article') ? (int) $request->query->get('article') : 1;
switch ($request->query->get('type'))
{
case 'article':
break;
case 'torrent':
$total = 0; // @TODO pagination
$torrents = [];
foreach ($torrentService->searchTorrents($request->query->get('query')) as $torrent)
{
// Apply locales filter
if ($lastTorrentLocales = $torrentService->findLastTorrentLocalesByTorrentIdApproved($torrent->getId()))
{
if (!count(
array_intersect(
$lastTorrentLocales->getValue(),
$user->getLocales()
)
)) {
$total--;
continue;
}
}
// Apply sensitive filters
if ($lastTorrentSensitive = $torrentService->findLastTorrentSensitiveByTorrentIdApproved($torrent->getId()))
{
if ($user->isSensitive() && $lastTorrentSensitive->isValue())
{
$total--;
continue;
}
}
// Read file
if (!$file = $torrentService->readTorrentFileByTorrentId($torrent->getId()))
{
$total--;
continue; // @TODO exception
}
// Generate keywords
$keywords = [];
$query = explode(' ', mb_strtolower($request->query->get('query')));
foreach (explode(',', $torrent->getKeywords()) as $keyword)
{
if (in_array($keyword, $query))
{
$keywords[] = urlencode($keyword);
}
}
$torrents[] =
[
'id' => $torrent->getId(),
'added' => $torrent->getAdded(),
'file' =>
[
'name' => $file->getName(),
'size' => $file->getSize(),
],
'scrape' =>
[
'seeders' => (int) $torrent->getSeeders(),
'peers' => (int) $torrent->getPeers(),
'leechers' => (int) $torrent->getLeechers(),
],
'user' =>
[
'id' => $torrent->getUserId(),
'identicon' => $userService->identicon(
$userService->getUser(
$torrent->getUserId()
)->getAddress()
)
],
'keywords' => $keywords,
'download' =>
[
'file' =>
[
'exist' => (bool) $torrentService->findTorrentDownloadFile(
$torrent->getId(),
$user->getId()
),
'total' => $torrentService->findTorrentDownloadFilesTotalByTorrentId(
$torrent->getId()
)
],
'magnet' =>
[
'exist' => (bool) $torrentService->findTorrentDownloadMagnet(
$torrent->getId(),
$user->getId()
),
'total' => $torrentService->findTorrentDownloadMagnetsTotalByTorrentId(
$torrent->getId()
)
]
],
'star' =>
[
'exist' => (bool) $torrentService->findTorrentStar(
$torrent->getId(),
$user->getId()
),
'total' => $torrentService->findTorrentStarsTotalByTorrentId(
$torrent->getId()
)
],
];
}
return $this->render('default/search/torrent.html.twig', [
'query' => $request->query->get('query'),
'torrents' => $torrents
]);
break;
default:
throw $this->createNotFoundException();
}
}
public function module(
?string $query,
?string $type
): Response
{
return $this->render('default/search/module.html.twig', [
'query' => $query,
'type' => $type,
]);
return $this->render(
'default/search/module.html.twig',
[
'query' => $query,
]
);
}
private function initUser(