Last commit july 5th

This commit is contained in:
2024-07-05 13:46:23 +02:00
parent dad0d86e8c
commit b0e4dfbb76
24982 changed files with 2621219 additions and 413 deletions

View File

@@ -52,6 +52,7 @@ class CommentCrudController extends AbstractCrudController
->setLabel('Photo')
->onlyOnIndex()
;
yield TextField::new('state');
$createdAt = DateTimeField::new('createdAt')->setFormTypeOptions([
'years' => range(date('Y'), date('Y') + 5),

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Controller;
use App\Entity\Comment;
use App\Message\CommentMessage;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Workflow\WorkflowInterface;
use Twig\Environment;
#[Route('/admin')]
class AdminController extends AbstractController
{
public function __construct(
private Environment $twig,
private EntityManagerInterface $entityManager,
private MessageBusInterface $bus,
) {
}
#[Route('/comment/review/{id}', name: 'review_comment')]
public function reviewComment(Request $request, Comment $comment, WorkflowInterface $commentStateMachine): Response
{
$accepted = !$request->query->get('reject');
if ($commentStateMachine->can($comment, 'publish')) {
$transition = $accepted ? 'publish' : 'reject';
} elseif ($commentStateMachine->can($comment, 'publish_ham')) {
$transition = $accepted ? 'publish_ham' : 'reject_ham';
} else {
return new Response('Comment already reviewed or not in the right state.');
}
$commentStateMachine->apply($comment, $transition);
$this->entityManager->flush();
if ($accepted) {
$reviewUrl = $this->generateUrl('review_comment', ['id' => $comment->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
$this->bus->dispatch(new CommentMessage($comment->getId(), $reviewUrl));
}
return new Response($this->twig->render('admin/review.html.twig', [
'transition' => $transition,
'comment' => $comment,
]));
}
#[Route('/http-cache/{uri<.*>}', methods: ['PURGE'])]
public function purgeHttpCache(KernelInterface $kernel, Request $request, string $uri, StoreInterface $store): Response
{
if ('prod' === $kernel->getEnvironment()) {
return new Response('KO', 400);
}
$store->purge($request->getSchemeAndHttpHost().'/'.$uri);
return new Response('Done');
}
}

View File

@@ -6,40 +6,63 @@ namespace App\Controller;
use App\Entity\Comment;
use App\Entity\Conference;
use App\Form\CommentType;
use App\Message\CommentMessage;
use App\Repository\CommentRepository;
use App\Repository\ConferenceRepository;
use App\SpamChecker;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ConferenceController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
) {
private MessageBusInterface $bus,
)
{
}
#[Route('/', name: 'homepage')]
#[Route('/')]
public function indexNoLocale(): Response
{
return $this->redirectToRoute('homepage', ['_locale' => 'en']);
}
#[Route('/{_locale<%app.supported_locales%>}/', name: 'homepage')]
public function index(ConferenceRepository $conferenceRepository): Response
{
return $this->render('conference/index.html.twig', [
'conferences' => $conferenceRepository->findAll(),
])->setSharedMaxAge(3600);
}
#[Route('/{_locale<%app.supported_locales%>}/conference_header', name: 'conference_header')]
public function conferenceHeader(ConferenceRepository $conferenceRepository): Response
{
return $this->render('conference/header.html.twig', [
'conferences' => $conferenceRepository->findAll(),
]);
}
#[Route('/conference/{slug}', name: 'conference')]
#[Route('/{_locale<%app.supported_locales%>}/conference/{slug}', name: 'conference')]
public function show(
Request $request,
Conference $conference,
CommentRepository $commentRepository,
SpamChecker $spamChecker,
ConferenceRepository $conferenceRepository,
Request $request,
Conference $conference,
CommentRepository $commentRepository,
NotifierInterface $notifier,
ConferenceRepository $conferenceRepository,
#[Autowire('%photo_dir%')] string $photoDir,
): Response {
): Response
{
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment);
@@ -47,25 +70,31 @@ class ConferenceController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$comment->setConference($conference);
if ($photo = $form['photo']->getData()) {
$filename = bin2hex(random_bytes(6)).'.'.$photo->guessExtension();
$filename = bin2hex(random_bytes(6)) . '.' . $photo->guessExtension();
$photo->move($photoDir, $filename);
$comment->setPhotoFilename($filename);
}
$this->entityManager->persist($comment);
$this->entityManager->flush();
$context = [
'user_ip' => $request->getClientIp(),
'user_agent' => $request->headers->get('user-agent'),
'referrer' => $request->headers->get('referer'),
'permalink' => $request->getUri(),
];
if (2 === $spamChecker->getSpamScore($comment, $context)) {
throw new \RuntimeException('Blatant spam, go away!');
}
$this->entityManager->flush();
$reviewUrl = $this->generateUrl('review_comment', ['id' => $comment->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
$this->bus->dispatch(new CommentMessage($comment->getId(), $reviewUrl, $context));
$notifier->send(new Notification('Thank you for the feedback; your comment will be posted after moderation.', ['browser']));
return $this->redirectToRoute('conference', ['slug' => $conference->getSlug()]);
}
if ($form->isSubmitted()) {
$notifier->send(new Notification('Can you check your submission? There are some problems with it.', ['browser']));
}
$offset = max(0, $request->query->getInt('offset', 0));
$paginator = $commentRepository->getCommentPaginator($conference, $offset);