- Monitoring chapters
- possibilities for ToolBarButton.html.twig to have tailwind classes see ChapterListToolbar.php
- Makefile scheduler command
- Makefile make:message command
This commit is contained in:
Jérémy Guillot
2024-07-20 19:40:48 +02:00
parent 7fc2f15f6b
commit ff59aa5d77
21 changed files with 818 additions and 283 deletions

View File

@@ -81,7 +81,7 @@ class MangaController extends AbstractController
return $this->render('manga/show_chapters.html.twig', [
'manga' => $manga,
'toolbar' => $this->toolbarFactory->createToolbar('chapter_list', ['mangaId' => $manga->getId()])->getGroups(),
'toolbar' => $this->toolbarFactory->createToolbar('chapter_list', ['mangaId' => $manga->getId(), 'isMonitored' => (int) $manga->isMonitored()])->getGroups(),
]);
}
@@ -388,6 +388,22 @@ class MangaController extends AbstractController
return new JsonResponse(['success' => 'Metadata refresh started...'], 200);
}
#[Route('/toggle_monitored', name: 'toggle_monitored')]
public function toogleMonitored(Request $request): JsonResponse
{
$id = json_decode($request->getContent(), true)['mangaId'];
$manga = $this->mangaRepository->find($id);
if (!$manga) {
return new JsonResponse(['error' => 'Manga Not Found.'], 400);
}
$manga->setMonitored(!$manga->isMonitored());
$this->entityManager->persist($manga);
$this->entityManager->flush();
return new JsonResponse(['success' => 'Monitored status updated.', 'isMonitored' => $manga->isMonitored()], 200);
}
private function isFullVolume(Chapter $chapter): bool
{
$volumeChapters = $this->chapterRepository->findBy([

View File

@@ -56,6 +56,9 @@ class Manga
#[ORM\Column(length: 255, nullable: true)]
private ?string $thumbnailUrl = null;
#[ORM\Column]
private ?bool $monitored = null;
public function __construct()
{
$this->chapters = new ArrayCollection();
@@ -250,4 +253,16 @@ class Manga
return $this;
}
public function isMonitored(): ?bool
{
return $this->monitored;
}
public function setMonitored(bool $monitored): static
{
$this->monitored = $monitored;
return $this;
}
}

View File

@@ -9,6 +9,9 @@ class ChapterListToolbar extends Toolbar
{
public function __construct(array $contextData = [])
{
$monitoredTitle = $contextData['isMonitored'] ? 'Monitored' : 'Monitoring';
$monitoredColor = $contextData['isMonitored'] ? 'text-green-500' : 'text-white';
$this
->addToLeftGroup(new ToolbarButton('arrows-rotate', 'Refresh metadata', 'toolbar#refreshMetadata', $contextData))
->addToLeftGroup(new ToolbarDivider())
@@ -17,7 +20,7 @@ class ChapterListToolbar extends Toolbar
->addToLeftGroup(new ToolbarButton('history', 'History', 'toolbar#history', $contextData))
->addToRightGroup(new ToolbarButton('bookmark', 'Monitoring', 'toolbar#monitoring', $contextData))
->addToRightGroup(new ToolbarButton('bookmark', $monitoredTitle, 'toolbar#monitoring', array_merge($contextData, ['buttonClass' => $monitoredColor])))
->addToRightGroup(new ToolbarButton('wrench', 'Edit', 'toolbar#editManga', $contextData))
->addToRightGroup(new ToolbarButton('trash-can', 'Delete', 'toolbar#deleteManga', $contextData))
->addToRightGroup(new ToolbarDivider())

View File

@@ -10,10 +10,10 @@ class MangaListToolbar extends Toolbar
{
public function __construct(array $contextData = [])
{
$this->addToLeftGroup(new ToolbarButton('arrows-rotate', 'Refresh', 'refreshMetadata'))
->addToLeftGroup(new ToolbarButton('search', 'Search', 'searchLastChapter'))
$this->addToLeftGroup(new ToolbarButton('arrows-rotate', 'Refresh', 'toolbar#refreshMetadata'))
->addToLeftGroup(new ToolbarButton('search', 'Search', 'toolbar#searchLastChapter'))
->addToRightGroup(new ToolbarButton('th-large', 'Options', 'options'))
->addToRightGroup(new ToolbarButton('th-large', 'Options', 'toolbar#options'))
->addToRightGroup(new ToolbarDivider())
->addToRightGroup(new ToolbarDropdown('eye', 'View', 'changeView', [
['text' => 'Poster View', 'action' => 'changeView', 'data' => ['view' => 'poster']],

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Message;
final class RefreshAndDownloadChapters
{
/*
* Add whatever properties and methods you need
* to hold the data for this message class.
*/
// private $name;
// public function __construct(string $name)
// {
// $this->name = $name;
// }
// public function getName(): string
// {
// return $this->name;
// }
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\MessageHandler;
use App\Entity\Chapter;
use App\Entity\Manga;
use App\Message\DownloadChapter;
use App\Message\RefreshAndDownloadChapters;
use App\Repository\MangaRepository;
use App\Service\MangadexProvider;
use App\Service\NotificationService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;
#[AsMessageHandler]
final readonly class RefreshAndDownloadChaptersHandler
{
public function __construct(
private MangaRepository $mangaRepository,
private MangadexProvider $mangadexProvider,
private EntityManagerInterface $entityManager,
private MessageBusInterface $bus
)
{
}
public function __invoke(RefreshAndDownloadChapters $message): void
{
$mangas = $this->mangaRepository->findBy(['monitored' => true]);
foreach ($mangas as $manga) {
$chapters = $this->refreshMangas($manga);
if (empty($chapters)) {
continue;
}
/** @var Chapter $chapter */
foreach ($chapters as $chapter) {
$this->bus->dispatch(new DownloadChapter($chapter->getId()));
}
}
}
private function refreshMangas(Manga $manga): array
{
$lastChapters = $this->mangadexProvider->addAllChaptersToManga($manga);
foreach ($lastChapters as $chapter) {
$this->entityManager->persist($chapter);
}
$this->entityManager->persist($manga);
$this->entityManager->flush();
return $lastChapters;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Scheduler;
use App\Message\RefreshAndDownloadChapters;
use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;
use Symfony\Contracts\Cache\CacheInterface;
#[AsSchedule]
class MainSchedule implements ScheduleProviderInterface
{
public function __construct(private CacheInterface $cache)
{
}
#[\Override] public function getSchedule(): Schedule
{
return (new Schedule())->add(
RecurringMessage::every('6 hours', new RefreshAndDownloadChapters())
)
->stateful($this->cache);
}
}

View File

@@ -12,9 +12,6 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;