From b81d973331b195200f997b21b08d03ff41789a6e Mon Sep 17 00:00:00 2001 From: ghost Date: Sun, 8 Oct 2023 16:02:38 +0300 Subject: [PATCH] implement torrent sensitive edit features #11 --- src/Controller/PageController.php | 2 +- src/Controller/TorrentController.php | 460 ++++++++++++++---- src/Service/TorrentService.php | 30 +- .../default/torrent/edit/locales.html.twig | 10 +- .../default/torrent/edit/sensitive.html.twig | 85 ++++ templates/default/torrent/info.html.twig | 6 +- templates/default/torrent/submit.html.twig | 6 +- 7 files changed, 495 insertions(+), 104 deletions(-) create mode 100644 templates/default/torrent/edit/sensitive.html.twig diff --git a/src/Controller/PageController.php b/src/Controller/PageController.php index bb7c7ed..a30785d 100644 --- a/src/Controller/PageController.php +++ b/src/Controller/PageController.php @@ -133,7 +133,7 @@ class PageController extends AbstractController 'attribute' => [ 'value' => $request->get('sensitive'), - 'placeholder' => $translator->trans('Apply sensitive filters for this publication'), + 'placeholder' => $translator->trans('Apply sensitive filters to publication'), ] ] ]; diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index 823f929..883e876 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -14,6 +14,7 @@ use App\Service\TorrentService; class TorrentController extends AbstractController { + // Torrent #[Route( '/{_locale}/torrent/{torrentId}', name: 'torrent_info', @@ -57,7 +58,7 @@ class TorrentController extends AbstractController 'id' => $torrent->getId(), 'added' => $torrent->getAdded(), 'locales' => $torrentService->findLastTorrentLocales($torrent->getId()), - 'sensitive' => $torrentService->findLastTorrentSensitive($torrent->getId()), + 'sensitive' => $torrentService->findLastTorrentSensitive($torrent->getId())->isValue(), 'pages' => [] ], 'file' => @@ -86,9 +87,148 @@ class TorrentController extends AbstractController ]); } + #[Route( + '/{_locale}/submit/torrent', + name: 'torrent_submit', + methods: + [ + 'GET', + 'POST' + ] + )] + public function submit( + Request $request, + TranslatorInterface $translator, + UserService $userService, + TorrentService $torrentService + ): Response + { + // Init user + $user = $userService->init( + $request->getClientIp() + ); + + if (!$user->isStatus()) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Init form + $form = + [ + 'locales' => + [ + 'error' => [], + 'attribute' => + [ + 'value' => $request->get('locales') ? $request->get('locales') : [$request->get('_locale')], + 'placeholder' => $translator->trans('Content language') + ] + ], + 'torrent' => + [ + 'error' => [], + 'attribute' => + [ + 'value' => null, // is local file, there is no values passed + 'placeholder' => $translator->trans('Select torrent file') + ] + ], + 'sensitive' => + [ + 'error' => [], + 'attribute' => + [ + 'value' => $request->get('sensitive'), + 'placeholder' => $translator->trans('Apply sensitive filters to publication'), + ] + ] + ]; + + // Process request + if ($request->isMethod('post')) + { + /// Locales + $locales = []; + if ($request->get('locales')) + { + foreach ((array) $request->get('locales') as $locale) + { + if (in_array($locale, explode('|', $this->getParameter('app.locales')))) + { + $locales[] = $locale; + } + } + } + + //// At least one valid locale required + if (!$locales) + { + $form['locales']['error'][] = $translator->trans('At least one locale required'); + } + + /// Torrent + if ($file = $request->files->get('torrent')) + { + //// Validate torrent file + if (filesize($file->getPathName()) > $this->getParameter('app.torrent.size.max')) + { + $form['torrent']['error'][] = $translator->trans('Torrent file out of size limit'); + } + + //// Validate torrent format + if (!$torrentService->readTorrentFileByFilepath($file->getPathName())) + { + $form['torrent']['error'][] = $translator->trans('Could not parse torrent file'); + } + } + + else + { + $form['torrent']['error'][] = $translator->trans('Torrent file required'); + } + + // Request is valid + if (empty($form['torrent']['error']) && empty($form['locales']['error'])) + { + // Save data + $torrent = $torrentService->add( + $file->getPathName(), + $user->getId(), + time(), + (array) $locales, + (bool) $request->get('sensitive'), + $user->isApproved() + ); + + // Redirect to info page created + return $this->redirectToRoute( + 'torrent_info', + [ + '_locale' => $request->get('_locale'), + 'torrentId' => $torrent->getId() + ] + ); + } + } + + // Render form template + return $this->render( + 'default/torrent/submit.html.twig', + [ + 'locales' => explode('|', $this->getParameter('app.locales')), + 'form' => $form, + ] + ); + } + + // Torrent locales #[Route( '/{_locale}/torrent/{torrentId}/edit/locales/{torrentLocalesId}', - name: 'torrent_edit_locales', + name: 'torrent_locales_edit', requirements: [ 'torrentId' => '\d+', @@ -270,7 +410,7 @@ class TorrentController extends AbstractController #[Route( '/{_locale}/torrent/{torrentId}/approve/locales/{torrentLocalesId}', - name: 'torrent_approve_locales', + name: 'torrent_locales_approve', requirements: [ 'torrentId' => '\d+', @@ -321,7 +461,7 @@ class TorrentController extends AbstractController // Redirect to info page created return $this->redirectToRoute( - 'torrent_edit_locales', + 'torrent_locales_edit', [ '_locale' => $request->get('_locale'), 'torrentId' => $torrent->getId(), @@ -332,7 +472,7 @@ class TorrentController extends AbstractController #[Route( '/{_locale}/torrent/{torrentId}/delete/locales/{torrentLocalesId}', - name: 'torrent_delete_locales', + name: 'torrent_locales_delete', requirements: [ 'torrentId' => '\d+', @@ -383,7 +523,7 @@ class TorrentController extends AbstractController // Redirect to info page created return $this->redirectToRoute( - 'torrent_edit_locales', + 'torrent_locales_edit', [ '_locale' => $request->get('_locale'), 'torrentId' => $torrent->getId(), @@ -392,16 +532,26 @@ class TorrentController extends AbstractController ); } + // Torrent sensitive #[Route( - '/{_locale}/submit/torrent', - name: 'torrent_submit', + '/{_locale}/torrent/{torrentId}/edit/sensitive/{torrentSensitiveId}', + name: 'torrent_sensitive_edit', + requirements: + [ + 'torrentId' => '\d+', + 'torrentSensitiveId' => '\d+', + ], + defaults: + [ + 'torrentSensitiveId' => null, + ], methods: [ 'GET', 'POST' ] )] - public function submit( + public function editSensitive( Request $request, TranslatorInterface $translator, UserService $userService, @@ -421,34 +571,81 @@ class TorrentController extends AbstractController ); } - // Init form - $form = - [ - 'locales' => - [ - 'error' => [], - 'attribute' => + // Init torrent + if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) + { + throw $this->createNotFoundException(); + } + + // Init sensitive value + if ($request->get('torrentSensitiveId')) + { + if ($torrentSensitive = $torrentService->getTorrentSensitive($request->get('torrentSensitiveId'))) + { + $sensitive = [ - 'value' => $request->get('locales') ? $request->get('locales') : [$request->get('_locale')], - 'placeholder' => $translator->trans('Content language') - ] - ], - 'torrent' => + 'id' => $torrentSensitive->getId(), + 'value' => $torrentSensitive->isValue(), + ]; + } + + else + { + throw $this->createNotFoundException(); + } + } + else + { + if ($torrentSensitive = $torrentService->findLastTorrentSensitive($request->get('torrentId'))) + { + $sensitive = + [ + 'id' => $torrentSensitive->getId(), + 'value' => $torrentSensitive->isValue(), + ]; } + + else + { + $sensitive = + [ + 'id' => null, + 'value' => false, + ]; + } + } + + // Init edition history + $editions = []; + foreach ($torrentService->findTorrentSensitive($torrent->getId()) as $torrentSensitive) + { + $editions[] = [ - 'error' => [], - 'attribute' => + 'id' => $torrentSensitive->getId(), + 'added' => $torrentSensitive->getAdded(), + 'approved' => $torrentSensitive->isApproved(), + 'active' => $torrentSensitive->getId() == $sensitive['id'], + 'user' => [ - 'value' => null, // is local file, there is no values passed - 'placeholder' => $translator->trans('Select torrent file') + 'id' => $torrentSensitive->getUserId(), + 'identicon' => $userService->identicon( + $userService->get( + $torrentSensitive->getUserId() + )->getAddress() + ), ] - ], + ]; + } + + // Init form + $form = + [ 'sensitive' => [ 'error' => [], 'attribute' => [ - 'value' => $request->get('sensitive'), - 'placeholder' => $translator->trans('Apply sensitive filters for this publication'), + 'value' => $sensitive['value'], + 'placeholder' => $translator->trans('Apply sensitive filters to publication') ] ] ]; @@ -456,76 +653,161 @@ class TorrentController extends AbstractController // Process request if ($request->isMethod('post')) { - /// Locales - $locales = []; - if ($request->get('locales')) - { - foreach ((array) $request->get('locales') as $locale) - { - if (in_array($locale, explode('|', $this->getParameter('app.locales')))) - { - $locales[] = $locale; - } - } - } + // Save data + $torrentService->addTorrentSensitive( + $torrent->getId(), + $user->getId(), + time(), + $request->get('sensitive') === 'true', + $user->isApproved() + ); - //// At least one valid locale required - if (!$locales) - { - $form['locales']['error'][] = $translator->trans('At least one locale required'); - } + // Redirect to info page created + return $this->redirectToRoute( + 'torrent_info', + [ + '_locale' => $request->get('_locale'), + 'torrentId' => $torrent->getId() + ] + ); + } - /// Torrent - if ($file = $request->files->get('torrent')) - { - //// Validate torrent file - if (filesize($file->getPathName()) > $this->getParameter('app.torrent.size.max')) - { - $form['torrent']['error'][] = $translator->trans('Torrent file out of size limit'); - } + // Render form template + return $this->render( + 'default/torrent/edit/sensitive.html.twig', + [ + 'torrentId' => $torrent->getId(), + 'editions' => $editions, + 'form' => $form, + 'session' => + [ + 'moderator' => $user->isModerator(), + 'owner' => $user->getId() === $torrentSensitive->getUserId(), + ] + ] + ); + } - //// Validate torrent format - if (!$torrentService->readTorrentFileByFilepath($file->getPathName())) - { - $form['torrent']['error'][] = $translator->trans('Could not parse torrent file'); - } - } + #[Route( + '/{_locale}/torrent/{torrentId}/approve/sensitive/{torrentSensitiveId}', + name: 'torrent_sensitive_approve', + requirements: + [ + 'torrentId' => '\d+', + 'torrentSensitiveId' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function approveSensitive( + Request $request, + TranslatorInterface $translator, + UserService $userService, + TorrentService $torrentService + ): Response + { + // Init user + $user = $userService->init( + $request->getClientIp() + ); - else - { - $form['torrent']['error'][] = $translator->trans('Torrent file required'); - } + // Init torrent + if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) + { + throw $this->createNotFoundException(); + } - // Request is valid - if (empty($form['torrent']['error']) && empty($form['locales']['error'])) - { - // Save data - $torrent = $torrentService->add( - $file->getPathName(), - $user->getId(), - time(), - (array) $locales, - (bool) $request->get('sensitive'), - $user->isApproved() - ); + // Init torrent sensitive + if (!$torrentSensitive = $torrentService->getTorrentSensitive($request->get('torrentSensitiveId'))) + { + throw $this->createNotFoundException(); + } - // Redirect to info page created - return $this->redirectToRoute( - 'torrent_info', - [ - '_locale' => $request->get('_locale'), - 'torrentId' => $torrent->getId() - ] - ); - } + // Check permissions + if (!$user->isModerator()) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); } - // Render form template - return $this->render( - 'default/torrent/submit.html.twig', + // Update approved + $torrentService->toggleTorrentSensitiveApproved( + $torrentSensitive->getId() + ); + + // Redirect to info page created + return $this->redirectToRoute( + 'torrent_sensitive_edit', [ - 'locales' => explode('|', $this->getParameter('app.locales')), - 'form' => $form, + '_locale' => $request->get('_locale'), + 'torrentId' => $torrent->getId(), + 'torrentSensitiveId' => $torrentSensitive->getId(), + ] + ); + } + + #[Route( + '/{_locale}/torrent/{torrentId}/delete/sensitive/{torrentSensitiveId}', + name: 'torrent_sensitive_delete', + requirements: + [ + 'torrentId' => '\d+', + 'torrentSensitiveId' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function deleteSensitive( + Request $request, + TranslatorInterface $translator, + UserService $userService, + TorrentService $torrentService + ): Response + { + // Init user + $user = $userService->init( + $request->getClientIp() + ); + + // Init torrent + if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) + { + throw $this->createNotFoundException(); + } + + // Init torrent sensitive + if (!$torrentSensitive = $torrentService->getTorrentSensitive($request->get('torrentSensitiveId'))) + { + throw $this->createNotFoundException(); + } + + // Check permissions + if (!($user->isModerator() || $user->getId() === $torrentSensitive->getUserId())) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Update approved + $torrentService->deleteTorrentSensitive( + $torrentSensitive->getId() + ); + + // Redirect to info page created + return $this->redirectToRoute( + 'torrent_sensitive_edit', + [ + '_locale' => $request->get('_locale'), + 'torrentId' => $torrent->getId(), + 'torrentSensitiveId' => $torrentSensitive->getId(), ] ); } diff --git a/src/Service/TorrentService.php b/src/Service/TorrentService.php index dd0314a..9cc62eb 100644 --- a/src/Service/TorrentService.php +++ b/src/Service/TorrentService.php @@ -141,7 +141,7 @@ class TorrentService { return $this->entityManagerInterface ->getRepository(TorrentSensitive::class) - ->getTorrentLocales($id); + ->getTorrentSensitive($id); } public function findLastTorrentSensitive(int $torrentId): ?TorrentSensitive @@ -175,6 +175,22 @@ class TorrentService return $torrentLocales; } + public function toggleTorrentSensitiveApproved( + int $id + ): ?TorrentSensitive + { + $torrentSensitive = $this->getTorrentSensitive($id); + + $torrentSensitive->setApproved( + !$torrentSensitive->isApproved() // toggle current value + ); + + $this->entityManagerInterface->persist($torrentSensitive); + $this->entityManagerInterface->flush(); + + return $torrentSensitive; + } + // Delete public function deleteTorrentLocales( int $id @@ -188,6 +204,18 @@ class TorrentService return $torrentLocales; } + public function deleteTorrentSensitive( + int $id + ): ?TorrentSensitive + { + $torrentSensitive = $this->getTorrentSensitive($id); + + $this->entityManagerInterface->remove($torrentSensitive); + $this->entityManagerInterface->flush(); + + return $torrentSensitive; + } + // Setters public function add( string $filepath, diff --git a/templates/default/torrent/edit/locales.html.twig b/templates/default/torrent/edit/locales.html.twig index e71ebab..5d70f70 100644 --- a/templates/default/torrent/edit/locales.html.twig +++ b/templates/default/torrent/edit/locales.html.twig @@ -8,7 +8,7 @@ #{{ torrentId }} -
+
- + @@ -210,7 +210,7 @@
- + diff --git a/templates/default/torrent/submit.html.twig b/templates/default/torrent/submit.html.twig index 1b9444e..81fe358 100644 --- a/templates/default/torrent/submit.html.twig +++ b/templates/default/torrent/submit.html.twig @@ -58,11 +58,7 @@
- +