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