From e713c173331a99e5a7e57e953bf8da1a72e70a35 Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 13 Oct 2023 00:08:45 +0300 Subject: [PATCH] update torrent features --- public/asset/default/css/framework.css | 2 +- src/Controller/ActivityController.php | 75 +++++ src/Controller/SearchController.php | 166 +--------- src/Controller/TorrentController.php | 300 +++++++++++++++--- src/Controller/UserController.php | 46 +-- src/Entity/Torrent.php | 39 ++- src/Entity/TorrentLocales.php | 2 +- src/Entity/User.php | 4 +- src/Repository/TorrentRepository.php | 92 +++++- src/Service/TorrentService.php | 221 +++++++++---- .../list.html.twig} | 6 +- templates/default/layout.html.twig | 2 +- templates/default/search/module.html.twig | 6 +- templates/default/torrent/info.html.twig | 2 +- .../list.html.twig} | 4 +- templates/default/user/module.html.twig | 19 +- 16 files changed, 659 insertions(+), 327 deletions(-) rename templates/default/{user/dashboard.html.twig => activity/list.html.twig} (91%) rename templates/default/{search/torrent.html.twig => torrent/list.html.twig} (97%) diff --git a/public/asset/default/css/framework.css b/public/asset/default/css/framework.css index 3b63c64..037ea91 100644 --- a/public/asset/default/css/framework.css +++ b/public/asset/default/css/framework.css @@ -1,7 +1,7 @@ .container { position: relative; overflow: hidden; - max-width: 732px; + max-width: 748px; margin: 0 auto; } diff --git a/src/Controller/ActivityController.php b/src/Controller/ActivityController.php index 0749b06..7664ea2 100644 --- a/src/Controller/ActivityController.php +++ b/src/Controller/ActivityController.php @@ -15,6 +15,50 @@ use App\Service\TorrentService; class ActivityController extends AbstractController { + #[Route( + '/{_locale}/activity', + name: 'activity_all', + methods: + [ + 'GET' + ] + )] + public function all( + Request $request, + UserService $userService, + ActivityService $activityService + ): Response + { + $user = $this->initUser( + $request, + $userService, + $activityService + ); + + $total = $activityService->findActivitiesTotal( + $user->getEvents() + ); + + $page = $request->get('page') ? (int) $request->get('page') : 1; + + return $this->render( + 'default/activity/list.html.twig', + [ + 'activities' => $activityService->findLastActivities( // @TODO locale/sensitive filters + $user->getEvents(), + $this->getParameter('app.pagination'), + ($page - 1) * $this->getParameter('app.pagination') + ), + 'pagination' => + [ + 'page' => $page, + 'pages' => ceil($total / $this->getParameter('app.pagination')), + 'total' => $total + ] + ] + ); + } + public function event( $activity, ActivityService $activityService, @@ -727,4 +771,35 @@ class ActivityController extends AbstractController ); } } + + private function initUser( + Request $request, + UserService $userService, + ActivityService $activityService + ): ?\App\Entity\User + { + // Init user + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $activityService->getEventCodes(), + $this->getParameter('app.theme'), + $this->getParameter('app.sensitive'), + $this->getParameter('app.yggdrasil'), + $this->getParameter('app.approved') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } + + return $user; + } } \ No newline at end of file diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index e789610..30b06b6 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -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( diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index 8373b16..465a672 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -57,27 +57,11 @@ class TorrentController extends AbstractController // Get contributors $contributors = []; - - $contributors[$torrent->getUserId()] = $userService->identicon( - $userService->getUser( - $torrent->getUserId() - )->getAddress() - ); - - if ($torrentLocales = $torrentService->findLastTorrentLocalesByTorrentId($torrent->getId())) - { - $contributors[$torrentLocales->getUserId()] = $userService->identicon( - $userService->getUser( - $torrentLocales->getUserId() - )->getAddress() - ); - } - - if ($torrentSensitive = $torrentService->findLastTorrentSensitiveByTorrentId($torrent->getId())) + foreach ($torrentService->getTorrentContributors($torrent) as $userId) { - $contributors[$torrentSensitive->getUserId()] = $userService->identicon( + $contributors[$userId] = $userService->identicon( $userService->getUser( - $torrentSensitive->getUserId() + $userId )->getAddress() ); } @@ -98,25 +82,14 @@ class TorrentController extends AbstractController 'id' => $torrent->getId(), 'md5file' => $torrent->getMd5File(), 'added' => $torrent->getAdded(), - /* - 'user' => - [ - 'id' => $torrent->getUserId(), - 'identicon' => $userService->identicon( - $userService->getUser( - $torrent->getUserId() - )->getAddress() - ), - ], - */ 'scrape' => [ 'seeders' => (int) $torrent->getSeeders(), 'peers' => (int) $torrent->getPeers(), 'leechers' => (int) $torrent->getLeechers(), ], - 'locales' => $torrentService->findLastTorrentLocalesByTorrentId($torrent->getId()), - 'sensitive' => $torrentService->findLastTorrentSensitiveByTorrentId($torrent->getId())->isValue(), + 'locales' => $torrent->getLocales(), + 'sensitive' => $torrent->isSensitive(), 'download' => [ 'file' => @@ -172,8 +145,6 @@ class TorrentController extends AbstractController 'v1' => $file->getInfoHashV1(false), 'v2' => $file->getInfoHashV2(false) ], - // @TODO use download action to filter announcement URL - // 'magnet' => $file->getMagnetLink() ], 'trackers' => explode('|', $this->getParameter('app.trackers')), 'activities' => $activityService->findLastActivitiesByTorrentId( @@ -192,7 +163,263 @@ class TorrentController extends AbstractController } #[Route( - '/{_locale}/submit/torrent', + '/{_locale}/search', + name: 'torrent_search', + methods: + [ + 'GET' + ] + )] + public function search( + Request $request, + UserService $userService, + TorrentService $torrentService, + ActivityService $activityService + ): Response + { + // Init user + $user = $this->initUser( + $request, + $userService, + $activityService + ); + + // Init request + $query = $request->get('query') ? explode(' ', $request->get('query')) : []; + $page = $request->get('page') ? (int) $request->get('page') : 1; + + // Get total torrents + $total = $torrentService->findTorrentsTotal( + $query, + $user->getLocales(), + $user->isSensitive() ? false : null, // hide on sensitive mode enabled or show all + $user->isModerator() ? null : true, // show approved content only for regular users + ); + + $torrents = []; + foreach ($torrentService->findTorrents( + $query, + $user->getLocales(), + $user->isSensitive() ? false : null, // hide on sensitive mode enabled or show all + $user->isModerator() ? null : true, // show approved content only for regular users + $this->getParameter('app.pagination'), + ($page - 1) * $this->getParameter('app.pagination') + ) as $torrent) + { + // Read file + if (!$file = $torrentService->readTorrentFileByTorrentId($torrent->getId())) + { + throw $this->createNotFoundException(); // @TODO exception + } + + // Generate keywords + $keywords = []; + foreach ($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/torrent/list.html.twig', [ + 'query' => $request->query->get('query'), + 'torrents' => $torrents + ]); + } + + #[Route( + '/{_locale}', + name: 'torrent_recent', + methods: + [ + 'GET' + ] + )] + public function recent( + Request $request, + UserService $userService, + TorrentService $torrentService, + ActivityService $activityService + ): Response + { + // Init user + $user = $this->initUser( + $request, + $userService, + $activityService + ); + + // Init page + $page = $request->get('page') ? (int) $request->get('page') : 1; + + // Get total torrents + $total = $torrentService->findTorrentsTotal( + [], + $user->getLocales(), + $user->isSensitive() ? false : null, // hide on sensitive mode enabled or show all + $user->isModerator() ? null : true, // show approved content only for regular users + ); + + // Create torrents list + $torrents = []; + foreach ($torrentService->findTorrents( + [], + $user->getLocales(), + $user->isSensitive() ? false : null, // hide on sensitive mode enabled or show all + $user->isModerator() ? null : true, // show approved content only for regular users + $this->getParameter('app.pagination'), + ($page - 1) * $this->getParameter('app.pagination') + ) as $torrent) + { + // Read file + if (!$file = $torrentService->readTorrentFileByTorrentId($torrent->getId())) + { + throw $this->createNotFoundException(); // @TODO exception + } + + // Generate keywords + $keywords = []; + $query = explode(' ', mb_strtolower($request->query->get('query'))); + foreach ($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/torrent/list.html.twig', [ + 'query' => $request->query->get('query'), + 'torrents' => $torrents + ]); + } + + // Forms + #[Route( + '/{_locale}/submit', name: 'torrent_submit', methods: [ @@ -310,6 +537,7 @@ class TorrentController extends AbstractController $user->isApproved() ); + // Add activity event $activityService->addEventTorrentAdd( $user->getId(), time(), @@ -765,7 +993,7 @@ class TorrentController extends AbstractController } else { - if ($torrentSensitive = $torrentService->findLastTorrentSensitiveByTorrentId($request->get('torrentId'))) + if ($torrentSensitive = $torrentService->findLastTorrentSensitiveByTorrentId($torrent->getId())) { $torrentSensitiveCurrent = [ diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 7e56711..8b7a9f0 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -31,57 +31,13 @@ class UserController extends AbstractController ); return $this->redirectToRoute( - 'user_dashboard', + 'torrent_recent', [ '_locale' => $user->getLocale() ] ); } - #[Route( - '/{_locale}', - name: 'user_dashboard', - methods: - [ - 'GET' - ] - )] - public function index( - Request $request, - UserService $userService, - ActivityService $activityService - ): Response - { - $user = $this->initUser( - $request, - $userService, - $activityService - ); - - $total = $activityService->findActivitiesTotal( - $user->getEvents() - ); - - $page = $request->get('page') ? (int) $request->get('page') : 1; - - return $this->render( - 'default/user/dashboard.html.twig', - [ - 'activities' => $activityService->findLastActivities( // @TODO locale/sensitive filters - $user->getEvents(), - $this->getParameter('app.pagination'), - ($page - 1) * $this->getParameter('app.pagination') - ), - 'pagination' => - [ - 'page' => $page, - 'pages' => ceil($total / $this->getParameter('app.pagination')), - 'total' => $total - ] - ] - ); - } - #[Route( '/{_locale}/settings', name: 'user_settings', diff --git a/src/Entity/Torrent.php b/src/Entity/Torrent.php index 38cc8d6..876bd1e 100644 --- a/src/Entity/Torrent.php +++ b/src/Entity/Torrent.php @@ -6,7 +6,6 @@ use App\Repository\TorrentRepository; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; -// @TODO #[ORM\Index(columns: ['keywords'], name: 'keywords_idx', flags: ['fulltext'])] #[ORM\Entity(repositoryClass: TorrentRepository::class)] class Torrent @@ -25,14 +24,20 @@ class Torrent #[ORM\Column(nullable: true)] private ?int $scraped = null; + #[ORM\Column(type: Types::SIMPLE_ARRAY)] + private array $locales = []; + + #[ORM\Column] + private ?bool $sensitive = null; + #[ORM\Column] private ?bool $approved = null; #[ORM\Column(length: 32)] private ?string $md5file = null; - #[ORM\Column(type: Types::TEXT, nullable: true)] - private ?string $keywords = null; + #[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: true)] + private ?array $keywords = null; #[ORM\Column(nullable: true)] private ?int $seeders = null; @@ -103,18 +108,42 @@ class Torrent return $this; } - public function getKeywords(): ?string + public function getKeywords(): ?array { return $this->keywords; } - public function setKeywords(?string $keywords): static + public function setKeywords(?array $keywords): static { $this->keywords = $keywords; return $this; } + public function getLocales(): array + { + return $this->locales; + } + + public function setLocales(array $locales): static + { + $this->locales = $locales; + + return $this; + } + + public function isSensitive(): ?bool + { + return $this->sensitive; + } + + public function setSensitive(bool $sensitive): static + { + $this->sensitive = $sensitive; + + return $this; + } + public function isApproved(): ?bool { return $this->approved; diff --git a/src/Entity/TorrentLocales.php b/src/Entity/TorrentLocales.php index 5e6c10f..f5322d9 100644 --- a/src/Entity/TorrentLocales.php +++ b/src/Entity/TorrentLocales.php @@ -23,7 +23,7 @@ class TorrentLocales #[ORM\Column] private ?int $added = null; - #[ORM\Column(type: Types::ARRAY)] + #[ORM\Column(type: Types::SIMPLE_ARRAY)] private array $value = []; #[ORM\Column] diff --git a/src/Entity/User.php b/src/Entity/User.php index 56246d5..7339f65 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -32,10 +32,10 @@ class User #[ORM\Column(length: 2)] private ?string $locale = null; - #[ORM\Column(type: Types::ARRAY)] + #[ORM\Column(type: Types::SIMPLE_ARRAY)] private array $locales = []; - #[ORM\Column(type: Types::ARRAY)] + #[ORM\Column(type: Types::SIMPLE_ARRAY)] private array $events = []; #[ORM\Column(length: 255)] diff --git a/src/Repository/TorrentRepository.php b/src/Repository/TorrentRepository.php index 0df9bf5..a8f12f6 100644 --- a/src/Repository/TorrentRepository.php +++ b/src/Repository/TorrentRepository.php @@ -21,20 +21,94 @@ class TorrentRepository extends ServiceEntityRepository parent::__construct($registry, Torrent::class); } - public function searchByKeywords( - array $keywords - ): ?array + public function findTorrentsTotal( + array $keywords, + array $locales, + ?bool $sensitive = null, + ?bool $approved = null, + int $limit = 0, + int $offset = 10 + ): int + { + return $this->getTorrentsQueryByFilter( + $keywords, + $locales, + $sensitive, + $approved, + )->select('count(t.id)') + ->getQuery() + ->getSingleScalarResult(); + } + + public function findTorrents( + array $keywords, + array $locales, + ?bool $sensitive = null, + ?bool $approved = null, + int $limit = 0, + int $offset = 10 + ): array + { + return $this->getTorrentsQueryByFilter( + $keywords, + $locales, + $sensitive, + $approved, + )->setMaxResults($limit) + ->setFirstResult($offset) + ->orderBy('t.id', 'DESC') // same as t.added + ->getQuery() + ->getResult(); + } + + private function getTorrentsQueryByFilter( + array $keywords, + array $locales, + ?bool $sensitive = null, + ?bool $approved = null, + ): \Doctrine\ORM\QueryBuilder { $query = $this->createQueryBuilder('t'); - foreach ($keywords as $keyword) + if ($keywords) // @TODO ANY or DTS { - $query->orWhere('t.keywords LIKE :query') - ->setParameter('query', "%{$keyword}%"); + $orX = $query->expr()->orX(); + + foreach ($keywords as $i => $keyword) + { + $orX->add("t.keywords LIKE :keyword{$i}"); + $query->setParameter(":keyword{$i}", "%{$keyword}%"); + } + + $query->andWhere($orX); } - return $query->orderBy('t.id', 'ASC') // same as t.added - ->getQuery() - ->getResult(); + if ($locales) // @TODO ANY or DTS + { + $orX = $query->expr()->orX(); + + foreach ($locales as $i => $locale) + { + $orX->add("t.locales LIKE :locale{$i}"); + + $query->setParameter(":locale{$i}", "%{$locale}%"); + } + + $query->andWhere($orX); + } + + if (is_bool($sensitive)) + { + $query->andWhere('t.sensitive = :sensitive') + ->setParameter('sensitive', $sensitive); + } + + if (is_bool($approved)) + { + $query->andWhere('t.approved = :approved') + ->setParameter('approved', $approved); + } + + return $query; } } diff --git a/src/Service/TorrentService.php b/src/Service/TorrentService.php index 76899d1..2a71e58 100644 --- a/src/Service/TorrentService.php +++ b/src/Service/TorrentService.php @@ -150,7 +150,7 @@ class TorrentService public function generateTorrentKeywordsByTorrentFilepath( string $filepath, int $minLength = 3 - ): string + ): array { $keywords = []; @@ -175,17 +175,17 @@ class TorrentService { unset($words[$key]); } + + else + { + $words[$key] = mb_strtolower($value); + } } $keywords = array_merge($keywords, $words); } - return mb_strtolower( - implode( - ',', - array_unique($keywords) - ) - ); + return array_unique($keywords); } public function getStorageFilepathById(int $id): string @@ -197,6 +197,25 @@ class TorrentService ); } + public function getTorrentContributors(Torrent $torrent): array + { + $contributors = []; + + foreach ($this->findTorrentLocalesByTorrentId($torrent->getUserId()) as $torrentLocale) + { + $contributors[] = $torrentLocale->getUserId(); + } + + foreach ($this->findTorrentSensitiveByTorrentId($torrent->getUserId()) as $torrentSensitive) + { + $contributors[] = $torrentSensitive->getUserId(); + } + + $contributors[] = $torrent->getUserId(); + + return array_unique($contributors); + } + public function add( string $filepath, int $userId, @@ -213,6 +232,8 @@ class TorrentService $this->generateTorrentKeywordsByTorrentFilepath( $filepath ), + $locales, + $sensitive, $approved ); @@ -224,16 +245,13 @@ class TorrentService ) ); - if (!empty($locales)) - { - $this->addTorrentLocales( - $torrent->getId(), - $userId, - $added, - $locales, - $approved - ); - } + $this->addTorrentLocales( + $torrent->getId(), + $userId, + $added, + $locales, + $approved + ); $this->addTorrentSensitive( $torrent->getId(), @@ -258,7 +276,9 @@ class TorrentService int $userId, int $added, string $md5file, - string $keywords, + array $keywords, + array $locales, + bool $sensitive, bool $approved ): ?Torrent { @@ -268,6 +288,8 @@ class TorrentService $torrent->setAdded($added); $torrent->setMd5File($md5file); $torrent->setKeywords($keywords); + $torrent->setLocales($locales); + $torrent->setSensitive($sensitive); $torrent->setApproved($approved); $this->entityManagerInterface->persist($torrent); @@ -288,12 +310,41 @@ class TorrentService ); } - public function searchTorrents(string $query) : array + public function findTorrents( + array $keywords, + array $locales, + ?bool $sensitive, + ?bool $approved, + int $limit, + int $offset + ) : array { return $this->entityManagerInterface ->getRepository(Torrent::class) - ->searchByKeywords( - explode(' ', $query) + ->findTorrents( + $keywords, + $locales, + $sensitive, + $approved, + $limit, + $offset + ); + } + + public function findTorrentsTotal( + array $keywords, + array $locales, + ?bool $sensitive, + ?bool $approved + ) : int + { + return $this->entityManagerInterface + ->getRepository(Torrent::class) + ->findTorrentsTotal( + $keywords, + $locales, + $sensitive, + $approved ); } @@ -308,6 +359,74 @@ class TorrentService ); } + public function updateTorrentSensitive( + int $torrentId, + ): void + { + if ($torrent = $this->getTorrent($torrentId)) + { + if ($torrentSensitive = $this->entityManagerInterface + ->getRepository(TorrentSensitive::class) + ->findOneBy( + [ + 'torrentId' => $torrentId, + 'approved' => true, + ], + [ + 'id' => 'DESC' + ] + )) + { + $torrent->setSensitive( + $torrentSensitive->isValue() + ); + + $this->entityManagerInterface->persist($torrent); + $this->entityManagerInterface->flush(); + } + } + } + + public function updateTorrentLocales( + int $torrentId + ): void + { + if ($torrent = $this->getTorrent($torrentId)) + { + if ($torrentLocales = $this->entityManagerInterface + ->getRepository(TorrentLocales::class) + ->findOneBy( + [ + 'torrentId' => $torrentId, + 'approved' => true, + ], + [ + 'id' => 'DESC' + ] + )) + { + $torrent->setLocales($torrentLocales->getValue()); + + $this->entityManagerInterface->persist($torrent); + $this->entityManagerInterface->flush(); + } + } + } + + public function setTorrentApprovedByTorrentId( + int $torrentId, + bool $value + ): void + { + if ($torrent = $this->getTorrent($torrentId)) + { + $torrent->setApproved($value); + + $this->entityManagerInterface->persist($torrent); + $this->entityManagerInterface->flush(); + } + } + public function setTorrentsApprovedByUserId( int $userId, bool $value @@ -355,24 +474,6 @@ class TorrentService ); } - public function findLastTorrentLocalesByTorrentIdApproved( - int $torrentId, - bool $approved = true - ): ?TorrentLocales - { - return $this->entityManagerInterface - ->getRepository(TorrentLocales::class) - ->findOneBy( - [ - 'torrentId' => $torrentId, - 'approved' => $approved - ], - [ - 'id' => 'DESC' - ] - ); - } - public function findTorrentLocalesByTorrentId(int $torrentId): array { return $this->entityManagerInterface @@ -400,6 +501,10 @@ class TorrentService $this->entityManagerInterface->persist($torrentLocales); $this->entityManagerInterface->flush(); + $this->updateTorrentLocales( + $torrentLocales->getTorrentId() + ); + return $torrentLocales; } @@ -412,6 +517,10 @@ class TorrentService $this->entityManagerInterface->remove($torrentLocales); $this->entityManagerInterface->flush(); + $this->updateTorrentLocales( + $torrentLocales->getTorrentId() + ); + return $torrentLocales; } @@ -434,6 +543,10 @@ class TorrentService $this->entityManagerInterface->persist($torrentLocales); $this->entityManagerInterface->flush(); + $this->updateTorrentLocales( + $torrentId + ); + return $torrentLocales; } @@ -484,24 +597,6 @@ class TorrentService ); } - public function findLastTorrentSensitiveByTorrentIdApproved( - int $torrentId, - bool $approved = true - ): ?TorrentSensitive - { - return $this->entityManagerInterface - ->getRepository(TorrentSensitive::class) - ->findOneBy( - [ - 'torrentId' => $torrentId, - 'approved' => $approved, - ], - [ - 'id' => 'DESC' - ] - ); - } - public function findTorrentSensitiveByTorrentId(int $torrentId): array { return $this->entityManagerInterface @@ -531,6 +626,10 @@ class TorrentService $this->entityManagerInterface->persist($torrentSensitive); $this->entityManagerInterface->flush(); + $this->updateTorrentSensitive( + $torrentSensitive->getTorrentId() + ); + return $torrentSensitive; } @@ -545,6 +644,10 @@ class TorrentService $this->entityManagerInterface->remove($torrentSensitive); $this->entityManagerInterface->flush(); + $this->updateTorrentSensitive( + $torrentSensitive->getTorrentId() + ); + return $torrentSensitive; } @@ -567,6 +670,10 @@ class TorrentService $this->entityManagerInterface->persist($torrentSensitive); $this->entityManagerInterface->flush(); + $this->updateTorrentSensitive( + $torrentId + ); + return $torrentSensitive; } diff --git a/templates/default/user/dashboard.html.twig b/templates/default/activity/list.html.twig similarity index 91% rename from templates/default/user/dashboard.html.twig rename to templates/default/activity/list.html.twig index 2d93192..caa7f45 100644 --- a/templates/default/user/dashboard.html.twig +++ b/templates/default/activity/list.html.twig @@ -26,17 +26,17 @@ {{ 'Page' | trans | lower }} {{ pagination.page }} / {{ pagination.pages }} {% if pagination.page > 1 %} {% if pagination.page == 2 %} - + {{ 'Back' | trans | lower }} {% else %} - + {{ 'Back' | trans | lower }} {% endif %} {% endif %} {% if pagination.page < pagination.pages %} - + {{ 'Next' | trans | lower }} {% endif %} diff --git a/templates/default/layout.html.twig b/templates/default/layout.html.twig index ee1308a..b6c1775 100644 --- a/templates/default/layout.html.twig +++ b/templates/default/layout.html.twig @@ -13,7 +13,7 @@
- {% block header_search %} diff --git a/templates/default/search/module.html.twig b/templates/default/search/module.html.twig index abc30dd..376d103 100644 --- a/templates/default/search/module.html.twig +++ b/templates/default/search/module.html.twig @@ -1,5 +1,6 @@ -
+ + {# - + #} +
\ No newline at end of file diff --git a/templates/default/torrent/info.html.twig b/templates/default/torrent/info.html.twig index 5dc872e..68c3f07 100644 --- a/templates/default/torrent/info.html.twig +++ b/templates/default/torrent/info.html.twig @@ -292,7 +292,7 @@
{% if torrent.locales %}
- {% for i, locale in torrent.locales.value %}{% if i > 0 %},{% endif %} {{ locale|locale_name(locale)|u.title }}{% endfor %} + {% for i, locale in torrent.locales %}{% if i > 0 %},{% endif %} {{ locale|locale_name(locale)|u.title }}{% endfor %}
{% endif %}
diff --git a/templates/default/search/torrent.html.twig b/templates/default/torrent/list.html.twig similarity index 97% rename from templates/default/search/torrent.html.twig rename to templates/default/torrent/list.html.twig index bafa83d..51a145b 100644 --- a/templates/default/search/torrent.html.twig +++ b/templates/default/torrent/list.html.twig @@ -1,5 +1,5 @@ {% extends 'default/layout.html.twig' %} -{% block title %}{% if query %}{{ query }}{% else %}{{ 'New' | trans }}{% endif %} - {{ 'Torrents' | trans }} - {{ name }}{% endblock %} +{% block title %}{% if query %}{{ query }}{% else %}{{ 'Recent' | trans }}{% endif %} - {{ 'Torrents' | trans }} - {{ name }}{% endblock %} {% block main_content %} {% if torrents %} {% for torrent in torrents %} @@ -21,7 +21,7 @@ {% if torrent.keywords %}
{% for keyword in torrent.keywords %} - + #{{ keyword }} {% endfor %} diff --git a/templates/default/user/module.html.twig b/templates/default/user/module.html.twig index 076a1bd..e47fe07 100644 --- a/templates/default/user/module.html.twig +++ b/templates/default/user/module.html.twig @@ -1,17 +1,32 @@
- {% if route == 'user_dashboard' %} + {% if route == 'torrent_recent' %} {% else %} - + {% endif %} + {% if route == 'activity_all' %} + + + + + + + {% else %} + + + + + + + {% endif %} {% if route == 'user_info' %}