diff options
Diffstat (limited to 'services/app/src/App.php')
| -rw-r--r-- | services/app/src/App.php | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/services/app/src/App.php b/services/app/src/App.php index 9d6cc70..9502976 100644 --- a/services/app/src/App.php +++ b/services/app/src/App.php @@ -26,6 +26,9 @@ use Nsfisis\Albatross\Middlewares\CacheControlPrivateMiddleware; use Nsfisis\Albatross\Middlewares\CurrentUserMiddleware; use Nsfisis\Albatross\Middlewares\TrailingSlash; use Nsfisis\Albatross\Middlewares\TwigMiddleware; +use Nsfisis\Albatross\Models\AggregatedExecutionStatus; +use Nsfisis\Albatross\Models\Answer; +use Nsfisis\Albatross\Models\Quiz; use Nsfisis\Albatross\Models\User; use Nsfisis\Albatross\Repositories\AnswerRepository; use Nsfisis\Albatross\Repositories\QuizRepository; @@ -385,11 +388,16 @@ final class App throw new HttpForbiddenException($request); } + if ($currentUser !== null) { + $phper_token = $this->obtainPhperToken($quiz, $answer, $currentUser); + } + return $this->render($request, $response, 'answer_view.html.twig', [ 'page_title' => "問題 #{$quiz->quiz_id} - 回答 #{$answer->answer_number}", 'quiz' => $quiz, 'answer' => $answer, 'testcase_executions' => $testcaseExecutionRepo->listByAnswerId($answer->answer_id), + 'phper_token' => $phper_token ?? null, ]); } @@ -881,6 +889,8 @@ final class App $testcaseExecutions = $testcaseExecutionRepo->listByAnswerId($answer->answer_id); + $phper_token = $this->obtainPhperToken($quiz, $answer, $currentUser); + return $this->makeJsonResponse($response, [ 'aggregated_status' => [ 'label' => $answer->execution_status->label(), @@ -893,6 +903,7 @@ final class App 'show_loading_indicator' => $ex->status->showLoadingIndicator(), ], ], $testcaseExecutions), + 'phper_token' => $phper_token, ])->withStatus(200); } @@ -944,6 +955,23 @@ final class App ->withStatus(200); } + private function obtainPhperToken(Quiz $quiz, Answer $answer, User $currentUser): ?string + { + if ($answer->author_id !== $currentUser->user_id) { + return null; + } + if ($answer->execution_status !== AggregatedExecutionStatus::OK) { + return null; + } + if ($quiz->birdie_code_size === null) { + return null; + } + if ($answer->code_size > $quiz->birdie_code_size) { + return null; + } + return '#albatros-' . md5('KORE-HA-TEKITO-NA-SORUTO-DESU' . $quiz->slug); + } + private function makeJsonResponse(ResponseInterface $response, mixed $data): ResponseInterface { $payload = json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
