From ed6c4ea4154aab18b7ea3a9f36e1cf7ee5599ffe Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 13 Oct 2023 02:28:50 +0300 Subject: [PATCH] add torrent approved moderation tools --- src/Controller/ActivityController.php | 54 +++ src/Controller/TorrentController.php | 85 +++- src/Entity/Activity.php | 27 +- src/Service/ActivityService.php | 87 ++++ src/Service/TorrentService.php | 24 ++ .../event/torrent/approve/add.html.twig | 16 + .../event/torrent/approve/delete.html.twig | 16 + templates/default/torrent/info.html.twig | 371 ++++++++++-------- 8 files changed, 493 insertions(+), 187 deletions(-) create mode 100644 templates/default/activity/event/torrent/approve/add.html.twig create mode 100644 templates/default/activity/event/torrent/approve/delete.html.twig diff --git a/src/Controller/ActivityController.php b/src/Controller/ActivityController.php index 7a65eea..c543492 100644 --- a/src/Controller/ActivityController.php +++ b/src/Controller/ActivityController.php @@ -372,6 +372,60 @@ class ActivityController extends AbstractController break; + case $activity::EVENT_TORRENT_APPROVE_ADD: + + return $this->render( + 'default/activity/event/torrent/approve/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName() + ] + ] + ); + + break; + + case $activity::EVENT_TORRENT_APPROVE_DELETE: + + return $this->render( + 'default/activity/event/torrent/approve/delete.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName() + ] + ] + ); + + break; + // Torrent Download case $activity::EVENT_TORRENT_DOWNLOAD_FILE_ADD: diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index 0a143fd..c01bed0 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -76,7 +76,13 @@ class TorrentController extends AbstractController $page = $request->get('page') ? (int) $request->get('page') : 1; // Render template - return $this->render('default/torrent/info.html.twig', [ + return $this->render('default/torrent/info.html.twig', + [ + 'user' => + [ + 'id' => $user->getId(), + 'moderator' => $user->isModerator() + ], 'torrent' => [ 'id' => $torrent->getId(), @@ -90,6 +96,7 @@ class TorrentController extends AbstractController ], 'locales' => $torrent->getLocales(), 'sensitive' => $torrent->isSensitive(), + 'approved' => $torrent->isApproved(), 'download' => [ 'file' => @@ -564,6 +571,82 @@ class TorrentController extends AbstractController ); } + + #[Route( + '/{_locale}/torrent/{torrentId}/approve/toggle', + name: 'torrent_approve_toggle', + requirements: + [ + 'torrentId' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function approve( + Request $request, + UserService $userService, + TorrentService $torrentService, + ActivityService $activityService + ): Response + { + // Init user + $user = $this->initUser( + $request, + $userService, + $activityService + ); + + // Init torrent + if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) + { + throw $this->createNotFoundException(); + } + + // Check permissions + if (!$user->isModerator()) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Register activity event + if (!$torrent->isApproved()) + { + $activityService->addEventTorrentApproveAdd( + $user->getId(), + $torrent->getId(), + time() + ); + } + + else + { + $activityService->addEventTorrentApproveDelete( + $user->getId(), + $torrent->getId(), + time() + ); + } + + // Update approved + $torrentService->toggleTorrentApproved( + $torrent->getId() + ); + + // Redirect back to form + return $this->redirectToRoute( + 'torrent_info', + [ + '_locale' => $request->get('_locale'), + 'torrentId' => $torrent->getId() + ] + ); + } + // Torrent locales #[Route( '/{_locale}/torrent/{torrentId}/edit/locales/{torrentLocalesId}', diff --git a/src/Entity/Activity.php b/src/Entity/Activity.php index 27e1440..a08cfcb 100644 --- a/src/Entity/Activity.php +++ b/src/Entity/Activity.php @@ -37,22 +37,25 @@ class Activity /// Torrent public const EVENT_TORRENT_ADD = 2000; - public const EVENT_TORRENT_LOCALES_ADD = 2100; - public const EVENT_TORRENT_LOCALES_DELETE = 2101; - public const EVENT_TORRENT_LOCALES_APPROVE_ADD = 2110; - public const EVENT_TORRENT_LOCALES_APPROVE_DELETE = 2111; + public const EVENT_TORRENT_APPROVE_ADD = 1100; + public const EVENT_TORRENT_APPROVE_DELETE = 1101; - public const EVENT_TORRENT_SENSITIVE_ADD = 2200; - public const EVENT_TORRENT_SENSITIVE_DELETE = 2201; - public const EVENT_TORRENT_SENSITIVE_APPROVE_ADD = 2210; - public const EVENT_TORRENT_SENSITIVE_APPROVE_DELETE = 2211; + public const EVENT_TORRENT_LOCALES_ADD = 2200; + public const EVENT_TORRENT_LOCALES_DELETE = 2201; + public const EVENT_TORRENT_LOCALES_APPROVE_ADD = 2210; + public const EVENT_TORRENT_LOCALES_APPROVE_DELETE = 2211; - public const EVENT_TORRENT_STAR_ADD = 2300; - public const EVENT_TORRENT_STAR_DELETE = 2301; + public const EVENT_TORRENT_SENSITIVE_ADD = 2300; + public const EVENT_TORRENT_SENSITIVE_DELETE = 2301; + public const EVENT_TORRENT_SENSITIVE_APPROVE_ADD = 2310; + public const EVENT_TORRENT_SENSITIVE_APPROVE_DELETE = 2311; - public const EVENT_TORRENT_DOWNLOAD_FILE_ADD = 2400; + public const EVENT_TORRENT_STAR_ADD = 2400; + public const EVENT_TORRENT_STAR_DELETE = 2401; - public const EVENT_TORRENT_DOWNLOAD_MAGNET_ADD = 2500; + public const EVENT_TORRENT_DOWNLOAD_FILE_ADD = 2500; + + public const EVENT_TORRENT_DOWNLOAD_MAGNET_ADD = 2600; // ... diff --git a/src/Service/ActivityService.php b/src/Service/ActivityService.php index 6b84ea8..14eaa9f 100644 --- a/src/Service/ActivityService.php +++ b/src/Service/ActivityService.php @@ -43,6 +43,9 @@ class ActivityService // Torrents Activity::EVENT_TORRENT_ADD, + Activity::EVENT_TORRENT_APPROVE_ADD, + Activity::EVENT_TORRENT_APPROVE_DELETE, + Activity::EVENT_TORRENT_LOCALES_ADD, Activity::EVENT_TORRENT_LOCALES_DELETE, Activity::EVENT_TORRENT_LOCALES_APPROVE_ADD, @@ -191,6 +194,30 @@ class ActivityService break; + case Activity::EVENT_TORRENT_APPROVE_ADD: + + $events + [ + $this->translatorInterface->trans('Torrents') + ] + [ + $this->translatorInterface->trans('Approved') + ] = $code; + + break; + + case Activity::EVENT_TORRENT_APPROVE_DELETE: + + $events + [ + $this->translatorInterface->trans('Torrents') + ] + [ + $this->translatorInterface->trans('Disapproved') + ] = $code; + + break; + /// Torrent locales case Activity::EVENT_TORRENT_LOCALES_ADD: @@ -760,6 +787,66 @@ class ActivityService return $activity; } + public function addEventTorrentApproveAdd( + int $userId, + int $torrentId, + int $added + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_APPROVE_ADD + ); + + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventTorrentApproveDelete( + int $userId, + int $torrentId, + int $added + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_APPROVE_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + /// Torrent Download public function addEventTorrentDownloadFileAdd( int $userId, diff --git a/src/Service/TorrentService.php b/src/Service/TorrentService.php index e06d747..d37bedb 100644 --- a/src/Service/TorrentService.php +++ b/src/Service/TorrentService.php @@ -298,6 +298,30 @@ class TorrentService return $torrent; } + public function toggleTorrentApproved( + int $torrentId + ): ?Torrent + { + $torrent = $this->getTorrent($torrentId); + + $torrent->setApproved( + !$torrent->isApproved() // toggle current value + ); + + $this->entityManagerInterface->persist($torrent); + $this->entityManagerInterface->flush(); + + $this->updateTorrentLocales( + $torrent->getId() + ); + + $this->updateTorrentSensitive( + $torrent->getId() + ); + + return $torrent; + } + public function getTorrentScrapeQueue(): ?Torrent { return $this->entityManagerInterface diff --git a/templates/default/activity/event/torrent/approve/add.html.twig b/templates/default/activity/event/torrent/approve/add.html.twig new file mode 100644 index 0000000..f1b227e --- /dev/null +++ b/templates/default/activity/event/torrent/approve/add.html.twig @@ -0,0 +1,16 @@ +
+
+ + {{ 'identicon' | trans }} + + + {{ 'approved torrent' | trans }} + + + {{ torrent.name }} + +
+
+ {{ added | format_ago }} +
+
\ No newline at end of file diff --git a/templates/default/activity/event/torrent/approve/delete.html.twig b/templates/default/activity/event/torrent/approve/delete.html.twig new file mode 100644 index 0000000..6abaa30 --- /dev/null +++ b/templates/default/activity/event/torrent/approve/delete.html.twig @@ -0,0 +1,16 @@ +
+
+ + {{ 'identicon' | trans }} + + + {{ 'disapproved torrent' | trans }} + + + {{ torrent.name }} + +
+
+ {{ added | format_ago }} +
+
\ No newline at end of file diff --git a/templates/default/torrent/info.html.twig b/templates/default/torrent/info.html.twig index 68c3f07..5ed04ce 100644 --- a/templates/default/torrent/info.html.twig +++ b/templates/default/torrent/info.html.twig @@ -23,225 +23,248 @@ {% block title %}{{ file.name }} - {{ 'Torrent' | trans }} #{{ torrent.id }}{% if pagination.page > 1 %} - {{ 'Page' | trans }} {{ pagination.page }}{% endif %} - {{ name }}{% endblock %} {% block main_content %}
-
-

- {{ file.name }} - {#{{ 'Torrent' | trans }} #{{ torrent.id }}#} -

- - {# - - - - - +

+ {{ file.name }} + {#{{ 'Torrent' | trans }} #{{ torrent.id }}#} +

+
-
- {{ 'Common'|trans }} + + {{ torrent.download.magnet.total }} + + + {% if torrent.download.file.exist %} + + + + {% else %} + + + + {% endif %} + + + {{ torrent.download.file.total }} + + + {% if torrent.star.exist %} + + + + {% else %} + + + + {% endif %} + + + {{ torrent.star.total }} +
-
- - +
+ + {% if user.moderator %} + + + + + + + + {% endif %} + + + + + + + + + + + + {% if file.hash.v1 %} + {% endif %} + {% if file.hash.v2 %} - {% if file.hash.v1 %} - - - - - {% endif %} - {% if file.hash.v2 %} - - - - - {% endif %} - {# visible in H1 + {% endif %} + {# visible in H1 + + + + + #} + {% if file.created %} - #} - {% if file.created %} - - - - - {% endif %} + {% endif %} - {% if file.size %} - - - - - {% endif %} - {% if file.pieces %} - - - - - {% endif %} - {% if file.source %} - - - - - {% endif %} - {% if file.software %} - - - - - {% endif %} - {% if file.comment %} - - - - - {% endif %} + {% if file.size %} + {% endif %} + {% if file.pieces %} - + + {% endif %} + {% if file.source %} - - + {% endif %} + {% if file.software %} + {% endif %} + {% if file.comment %} - -
+
+ {{ 'Moderation' | trans }} +
+
+ {{ 'Approved' | trans }} + + {% if torrent.approved %} + {{ 'Yes' | trans }} + + + + + + {% else %} + {{ 'No' | trans }} + + + + + + {% endif %} +
+
+ {{ 'Common' | trans }} +
+
+ {{ 'ID' | trans }} + + #{{ torrent.id }} +
+ {{ 'MD5' | trans }} + + {{ torrent.md5file }} +
- {{ 'ID'|trans }} + {{ 'Info hash v1' | trans }} - #{{ torrent.id }} + {{ file.hash.v1 }}
- {{ 'MD5'|trans }} + {{ 'Info hash v2' | trans }} - {{ torrent.md5file }} + {{ file.hash.v2 }}
- {{ 'Info hash v1'|trans }} - - {{ file.hash.v1 }} -
- {{ 'Info hash v2'|trans }} - - {{ file.hash.v2 }} -
+ {{ 'Filename'|trans }} + + {{ file.name }} +
- {{ 'Filename'|trans }} + {{ 'Created' | trans }} - {{ file.name }} + {{ file.created | format_date }}
- {{ 'Created'|trans }} - - {{ file.created | format_date }} -
- {{ 'Size'|trans }} - - {{ file.size | format_bytes }} -
- {{ 'Pieces'|trans }} - - {{ file.pieces | format_number }} -
- {{ 'Source'|trans }} - - {{ file.source }} -
- {{ 'Software'|trans }} - - {{ file.software }} -
- {{ 'Comment'|trans }} - - {{ file.comment }} -
- {{ 'Contributors'|trans }} + {{ 'Size' | trans }} - {% for id, identicon in torrent.contributors %} - - {{'identicon'|trans }} - - {% endfor %} + {{ file.size | format_bytes }}
-
- {{ 'Scrape'|trans }} -
+
+ {{ 'Pieces' | trans }} + + {{ file.pieces | format_number }}
- {{ 'Seeders'|trans }} + + {{ 'Source' | trans }} - {{ torrent.scrape.seeders }} + + {{ file.source }}
- {{ 'Peers'|trans }} + {{ 'Software' | trans }} - {{ torrent.scrape.peers }} + {{ file.software }}
- {{ 'Leechers'|trans }} + {{ 'Comment' | trans }} - {{ torrent.scrape.leechers }} + {{ file.comment }}
-
+ {% endif %} + + + {{ 'Contributors' | trans }} + + + {% for id, identicon in torrent.contributors %} + + {{'identicon'|trans }} + + {% endfor %} + + + + +
+ {{ 'Scrape' | trans }} +
+ + + + + {{ 'Seeders' | trans }} + + + {{ torrent.scrape.seeders }} + + + + + {{ 'Peers' | trans }} + + + {{ torrent.scrape.peers }} + + + + + {{ 'Leechers' | trans }} + + + {{ torrent.scrape.leechers }} + + + +
- {{ 'Files'|trans }} + {{ 'Files' | trans }}
{% for key, value in file.tree %} @@ -258,7 +281,7 @@ {% endfor %}
- {{ 'Trackers'|trans }} + {{ 'Trackers' | trans }}
{% for tracker in trackers %} @@ -303,7 +326,7 @@ - {{ 'Sensitive'|trans }} + {{ 'Sensitive' | trans }}
{% if torrent.sensitive %}