Browse Source

implement torrent bookmarks feature

main
ghost 1 year ago
parent
commit
3d74818303
  1. 139
      src/Controller/TorrentController.php
  2. 80
      src/Entity/TorrentBookmark.php
  3. 40
      src/Repository/TorrentBookmarkRepository.php
  4. 40
      src/Service/TorrentService.php
  5. 14
      templates/default/torrent/info.html.twig

139
src/Controller/TorrentController.php

@ -59,6 +59,14 @@ class TorrentController extends AbstractController @@ -59,6 +59,14 @@ class TorrentController extends AbstractController
'added' => $torrent->getAdded(),
'locales' => $torrentService->findLastTorrentLocales($torrent->getId()),
'sensitive' => $torrentService->findLastTorrentSensitive($torrent->getId())->isValue(),
'bookmark' =>
[
'active' => $torrentService->findUserLastTorrentBookmarkValue(
$torrent->getId(),
$user->getId()
),
'total' => 0,
],
'pages' => []
],
'file' =>
@ -225,6 +233,62 @@ class TorrentController extends AbstractController @@ -225,6 +233,62 @@ class TorrentController extends AbstractController
);
}
// Torrent bookmark
#[Route(
'/{_locale}/torrent/{torrentId}/bookmark/toggle',
name: 'torrent_bookmark_toggle',
requirements:
[
'torrentId' => '\d+',
],
methods:
[
'GET'
]
)]
public function toggleBookmark(
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 torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{
throw $this->createNotFoundException();
}
// Update
$torrentService->toggleTorrentBookmark(
$torrent->getId(),
$user->getId(),
time()
);
// Redirect to info page created
return $this->redirectToRoute(
'torrent_info',
[
'_locale' => $request->get('_locale'),
'torrentId' => $torrent->getId()
]
);
}
// Torrent locales
#[Route(
'/{_locale}/torrent/{torrentId}/edit/locales/{torrentLocalesId}',
@ -271,16 +335,21 @@ class TorrentController extends AbstractController @@ -271,16 +335,21 @@ class TorrentController extends AbstractController
}
// Init torrent locales
$torrentLocalesValue = [];
$torrentLocalesCurrent = [
'userId' => null,
'value' => []
];
// Get from edition version requested
if ($request->get('torrentLocalesId'))
{
if ($torrentLocales = $torrentService->getTorrentLocales($request->get('torrentLocalesId')))
{
$torrentLocalesCurrent['userId'] = $torrentLocales->getUserId();
foreach ($torrentLocales->getValue() as $value)
{
$torrentLocalesValue[] = $value;
$torrentLocalesCurrent['value'][] = $value;
}
}
@ -295,9 +364,11 @@ class TorrentController extends AbstractController @@ -295,9 +364,11 @@ class TorrentController extends AbstractController
{
if ($torrentLocales = $torrentService->findLastTorrentLocales($torrent->getId()))
{
$torrentLocalesCurrent['userId'] = $torrentLocales->getUserId();
foreach ($torrentLocales->getValue() as $value)
{
$torrentLocalesValue[] = $value;
$torrentLocalesCurrent['value'][] = $value;
}
// Update active locale
@ -306,26 +377,26 @@ class TorrentController extends AbstractController @@ -306,26 +377,26 @@ class TorrentController extends AbstractController
else
{
$torrentLocalesValue[] = $request->get('_locale');
$torrentLocalesCurrent['value'][] = $request->get('_locale');
}
}
// Init edition history
$editions = [];
foreach ($torrentService->findTorrentLocales($torrent->getId()) as $torrentLocales)
foreach ($torrentService->findTorrentLocales($torrent->getId()) as $torrentLocalesEdition)
{
$editions[] =
[
'id' => $torrentLocales->getId(),
'added' => $torrentLocales->getAdded(),
'approved' => $torrentLocales->isApproved(),
'active' => $torrentLocales->getId() == $request->get('torrentLocalesId'),
'id' => $torrentLocalesEdition->getId(),
'added' => $torrentLocalesEdition->getAdded(),
'approved' => $torrentLocalesEdition->isApproved(),
'active' => $torrentLocalesEdition->getId() == $request->get('torrentLocalesId'),
'user' =>
[
'id' => $torrentLocales->getUserId(),
'id' => $torrentLocalesEdition->getUserId(),
'identicon' => $userService->identicon(
$userService->get(
$torrentLocales->getUserId()
$torrentLocalesEdition->getUserId()
)->getAddress()
),
]
@ -340,7 +411,7 @@ class TorrentController extends AbstractController @@ -340,7 +411,7 @@ class TorrentController extends AbstractController
'error' => [],
'attribute' =>
[
'value' => $request->get('locales') ? $request->get('locales') : $torrentLocalesValue,
'value' => $request->get('locales') ? $request->get('locales') : $torrentLocalesCurrent['value'],
'placeholder' => $translator->trans('Content language')
]
]
@ -402,7 +473,7 @@ class TorrentController extends AbstractController @@ -402,7 +473,7 @@ class TorrentController extends AbstractController
'session' =>
[
'moderator' => $user->isModerator(),
'owner' => $user->getId() === $torrentLocales->getUserId(),
'owner' => $torrentLocalesCurrent['userId'] === $user->getId(),
]
]
);
@ -582,10 +653,11 @@ class TorrentController extends AbstractController @@ -582,10 +653,11 @@ class TorrentController extends AbstractController
{
if ($torrentSensitive = $torrentService->getTorrentSensitive($request->get('torrentSensitiveId')))
{
$sensitive =
$torrentSensitiveCurrent =
[
'id' => $torrentSensitive->getId(),
'value' => $torrentSensitive->isValue(),
'id' => $torrentSensitive->getId(),
'userId' => $torrentSensitive->getUserId(),
'value' => $torrentSensitive->isValue(),
];
}
@ -598,38 +670,41 @@ class TorrentController extends AbstractController @@ -598,38 +670,41 @@ class TorrentController extends AbstractController
{
if ($torrentSensitive = $torrentService->findLastTorrentSensitive($request->get('torrentId')))
{
$sensitive =
$torrentSensitiveCurrent =
[
'id' => $torrentSensitive->getId(),
'value' => $torrentSensitive->isValue(),
]; }
'id' => $torrentSensitive->getId(),
'userId' => $torrentSensitive->getUserId(),
'value' => $torrentSensitive->isValue(),
];
}
else
{
$sensitive =
$torrentSensitiveCurrent =
[
'id' => null,
'value' => false,
'id' => null,
'userId' => null,
'value' => false,
];
}
}
// Init edition history
$editions = [];
foreach ($torrentService->findTorrentSensitive($torrent->getId()) as $torrentSensitive)
foreach ($torrentService->findTorrentSensitive($torrent->getId()) as $torrentSensitiveEdition)
{
$editions[] =
[
'id' => $torrentSensitive->getId(),
'added' => $torrentSensitive->getAdded(),
'approved' => $torrentSensitive->isApproved(),
'active' => $torrentSensitive->getId() == $sensitive['id'],
'id' => $torrentSensitiveEdition->getId(),
'added' => $torrentSensitiveEdition->getAdded(),
'approved' => $torrentSensitiveEdition->isApproved(),
'active' => $torrentSensitiveEdition->getId() == $torrentSensitiveCurrent['id'],
'user' =>
[
'id' => $torrentSensitive->getUserId(),
'id' => $torrentSensitiveEdition->getUserId(),
'identicon' => $userService->identicon(
$userService->get(
$torrentSensitive->getUserId()
$torrentSensitiveEdition->getUserId()
)->getAddress()
),
]
@ -644,7 +719,7 @@ class TorrentController extends AbstractController @@ -644,7 +719,7 @@ class TorrentController extends AbstractController
'error' => [],
'attribute' =>
[
'value' => $sensitive['value'],
'value' => $torrentSensitiveCurrent['value'],
'placeholder' => $translator->trans('Apply sensitive filters to publication')
]
]
@ -682,7 +757,7 @@ class TorrentController extends AbstractController @@ -682,7 +757,7 @@ class TorrentController extends AbstractController
'session' =>
[
'moderator' => $user->isModerator(),
'owner' => $user->getId() === $torrentSensitive->getUserId(),
'owner' => $torrentSensitiveCurrent['userId'] === $user->getId(),
]
]
);

80
src/Entity/TorrentBookmark.php

@ -0,0 +1,80 @@ @@ -0,0 +1,80 @@
<?php
namespace App\Entity;
use App\Repository\TorrentBookmarkRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TorrentBookmarkRepository::class)]
class TorrentBookmark
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column]
private ?int $torrentId = null;
#[ORM\Column]
private ?int $userId = null;
#[ORM\Column]
private ?int $added = null;
#[ORM\Column]
private ?bool $value = null;
public function getId(): ?int
{
return $this->id;
}
public function getTorrentId(): ?int
{
return $this->torrentId;
}
public function setTorrentId(int $torrentId): static
{
$this->torrentId = $torrentId;
return $this;
}
public function getUserId(): ?int
{
return $this->userId;
}
public function setUserId(int $userId): static
{
$this->userId = $userId;
return $this;
}
public function getAdded(): ?int
{
return $this->added;
}
public function setAdded(int $added): static
{
$this->added = $added;
return $this;
}
public function isValue(): ?bool
{
return $this->value;
}
public function setValue(bool $value): static
{
$this->value = $value;
return $this;
}
}

40
src/Repository/TorrentBookmarkRepository.php

@ -0,0 +1,40 @@ @@ -0,0 +1,40 @@
<?php
namespace App\Repository;
use App\Entity\TorrentBookmark;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<TorrentBookmark>
*
* @method TorrentBookmark|null find($id, $lockMode = null, $lockVersion = null)
* @method TorrentBookmark|null findOneBy(array $criteria, array $orderBy = null)
* @method TorrentBookmark[] findAll()
* @method TorrentBookmark[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TorrentBookmarkRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, TorrentBookmark::class);
}
public function findUserLastTorrentBookmark(
int $torrentId,
int $userId
): ?TorrentBookmark
{
return $this->createQueryBuilder('tb')
->where('tb.torrentId = :torrentId')
->andWhere('tb.userId = :userId')
->setParameter('torrentId', $torrentId)
->setParameter('userId', $userId)
->orderBy('tb.id', 'DESC') // same to ts.added
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
}

40
src/Service/TorrentService.php

@ -5,10 +5,12 @@ namespace App\Service; @@ -5,10 +5,12 @@ namespace App\Service;
use App\Entity\Torrent;
use App\Entity\TorrentLocales;
use App\Entity\TorrentSensitive;
use App\Entity\TorrentBookmark;
use App\Repository\TorrentRepository;
use App\Repository\TorrentLocalesRepository;
use App\Repository\TorrentSensitiveRepository;
use App\Repository\TorrentBookmarkRepository;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Filesystem\Filesystem;
@ -158,6 +160,19 @@ class TorrentService @@ -158,6 +160,19 @@ class TorrentService
->findTorrentSensitive($torrentId);
}
/// Bookmark
public function findUserLastTorrentBookmarkValue(int $torrentId, int $userId): bool
{
if ($torrentBookmark = $this->entityManagerInterface
->getRepository(TorrentBookmark::class)
->findUserLastTorrentBookmark($torrentId, $userId))
{
return $torrentBookmark->isValue();
}
return false;
}
// Update
public function toggleTorrentLocalesApproved(
int $id
@ -204,6 +219,31 @@ class TorrentService @@ -204,6 +219,31 @@ class TorrentService
return $torrentLocales;
}
public function toggleTorrentBookmark(
int $torrentId,
int $userId,
int $added
): ?TorrentBookmark
{
$torrentBookmark = new TorrentBookmark();
$torrentBookmark->setTorrentId($torrentId);
$torrentBookmark->setUserId($userId);
$torrentBookmark->setAdded($added);
$torrentBookmark->setValue(
!$this->findUserLastTorrentBookmarkValue(
$torrentId,
$userId
)
);
$this->entityManagerInterface->persist($torrentBookmark);
$this->entityManagerInterface->flush();
return $torrentBookmark;
}
public function deleteTorrentSensitive(
int $id
): ?TorrentSensitive

14
templates/default/torrent/info.html.twig

@ -27,10 +27,16 @@ @@ -27,10 +27,16 @@
<h1>
{{ 'Torrent'|trans }} #{{ torrent.id }}
</h1>
<a class="float-right margin-l-8-px" href="#" title="{{ 'Bookmark'|trans }}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"/>
</svg>
<a class="float-right margin-l-8-px" href="{{ path('torrent_bookmark_toggle', {torrentId : torrent.id}) }}" title="{{ 'Bookmark'|trans }}">
{% if torrent.bookmark.active %}
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"/>
</svg>
{% else %}
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M2.866 14.85c-.078.444.36.791.746.593l4.39-2.256 4.389 2.256c.386.198.824-.149.746-.592l-.83-4.73 3.522-3.356c.33-.314.16-.888-.282-.95l-4.898-.696L8.465.792a.513.513 0 0 0-.927 0L5.354 5.12l-4.898.696c-.441.062-.612.636-.283.95l3.523 3.356-.83 4.73zm4.905-2.767-3.686 1.894.694-3.957a.565.565 0 0 0-.163-.505L1.71 6.745l4.052-.576a.525.525 0 0 0 .393-.288L8 2.223l1.847 3.658a.525.525 0 0 0 .393.288l4.052.575-2.906 2.77a.565.565 0 0 0-.163.506l.694 3.957-3.686-1.894a.503.503 0 0 0-.461 0z"/>
</svg>
{% endif %}
</a>
{#
<a class="float-right margin-l-8-px" href="#" title="{{ 'Torrent'|trans }}">

Loading…
Cancel
Save