ttskch /
esaba
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace App\Controller; |
||
| 6 | |||
| 7 | use App\Esa\HtmlHandler; |
||
| 8 | use App\Esa\Proxy; |
||
| 9 | use App\Esa\WebhookValidator; |
||
| 10 | use App\Service\AccessController; |
||
| 11 | use App\Service\AssetResolver; |
||
| 12 | use JsonException; |
||
| 13 | use Polidog\Esa\Exception\ClientException; |
||
| 14 | use Psr\Log\LoggerInterface; |
||
| 15 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||
| 16 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
| 17 | use Symfony\Component\HttpFoundation\Request; |
||
| 18 | use Symfony\Component\HttpFoundation\Response; |
||
| 19 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
| 20 | use Symfony\Component\Routing\Annotation\Route; |
||
| 21 | |||
| 22 | #[Route('/', name: 'default_')] |
||
| 23 | final class DefaultController extends AbstractController |
||
| 24 | { |
||
| 25 | 1 | #[Route('/', name: 'index', methods: ['GET'])] |
|
| 26 | public function index(Request $request): Response |
||
| 27 | { |
||
| 28 | 1 | if ($postId = $request->query->get('post_id')) { |
|
| 29 | 1 | return $this->redirectToRoute('default_post', ['id' => $postId]); |
|
| 30 | } |
||
| 31 | |||
| 32 | 1 | return $this->render('default/index.html.twig'); |
|
| 33 | } |
||
| 34 | |||
| 35 | 1 | #[Route('/post/{id}', name: 'post', requirements: ['id' => '\d+'], methods: ['GET'])] |
|
| 36 | public function post( |
||
| 37 | Request $request, |
||
| 38 | int $id, |
||
| 39 | Proxy $esa, |
||
| 40 | AccessController $accessController, |
||
| 41 | HtmlHandler $htmlHandler, |
||
| 42 | AssetResolver $assetResolver, |
||
| 43 | array $htmlReplacements, |
||
| 44 | ): Response { |
||
| 45 | 1 | $force = $request->query->getBoolean('force'); |
|
| 46 | |||
| 47 | try { |
||
| 48 | 1 | $post = $esa->getPost($id, $force); |
|
| 49 | 1 | } catch (ClientException $e) { |
|
| 50 | 1 | throw new NotFoundHttpException('', $e); |
|
| 51 | } |
||
| 52 | |||
| 53 | 1 | if (!$accessController->isPublic($post['category'], $post['tags'])) { |
|
| 54 | 1 | throw new NotFoundHttpException(); |
|
| 55 | } |
||
| 56 | |||
| 57 | // fix body_html |
||
| 58 | $htmlHandler |
||
| 59 | 1 | ->initialize($post['body_html']) |
|
| 60 | 1 | ->replacePostUrls('default_post', 'id') |
|
| 61 | 1 | ->disableMentionLinks() |
|
| 62 | 1 | ->replaceEmojiCodes() |
|
| 63 | 1 | ->replaceHtml($htmlReplacements) |
|
| 64 | 1 | ->dumpHtml() |
|
| 65 | ; |
||
| 66 | 1 | $post['body_html'] = $htmlHandler->dumpHtml(); |
|
| 67 | 1 | $toc = $htmlHandler->getToc(); |
|
| 68 | |||
| 69 | 1 | $assetPaths = $assetResolver->getAssetPaths($post['category'], $post['tags']); |
|
| 70 | |||
| 71 | 1 | if ($force) { |
|
| 72 | 1 | return $this->redirectToRoute('default_post', ['id' => $id]); |
|
| 73 | } |
||
| 74 | |||
| 75 | 1 | return $this->render('default/post.html.twig', [ |
|
| 76 | 'post' => $post, |
||
| 77 | 'toc' => $toc, |
||
| 78 | 1 | 'css' => $assetPaths['css'], |
|
| 79 | 1 | 'js' => $assetPaths['js'], |
|
| 80 | ]); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @throws JsonException |
||
| 85 | */ |
||
| 86 | 1 | #[Route('/webhook', name: 'webhook', methods: ['POST'])] |
|
| 87 | public function webhook( |
||
| 88 | Request $request, |
||
| 89 | WebhookValidator $validator, |
||
| 90 | Proxy $esa, |
||
| 91 | LoggerInterface $logger |
||
| 92 | ): Response { |
||
| 93 | 1 | $payload = $request->getContent(); |
|
| 94 | 1 | $signature = $request->headers->get('X-Esa-Signature'); |
|
| 95 | |||
| 96 | 1 | if ($signature && !$validator->isValid($payload, $signature)) { |
|
| 97 | 1 | throw new NotFoundHttpException(); |
|
| 98 | } |
||
| 99 | |||
| 100 | 1 | $body = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 101 | |||
| 102 | 1 | switch ($body['kind']) { |
|
| 103 | 1 | case 'post_create': |
|
| 104 | 1 | case 'post_update': |
|
| 105 | 1 | $esa->getPost($body['post']['number'], true); |
|
| 106 | 1 | $logger->debug(sprintf('Cache for post %d is warmed up!', $body['post']['number'])); |
|
| 107 | 1 | break; |
|
| 108 | default: |
||
| 109 | 1 | break; |
|
| 110 | } |
||
| 111 | |||
| 112 | 1 | return new JsonResponse('OK'); |
|
| 113 | } |
||
| 114 | } |
||
| 115 |