Use job_full & contest_entry in /src/:job
[gruntmaster-page.git] / lib / Plack / App / Gruntmaster.pm
CommitLineData
7dc32473
MG
1package Plack::App::Gruntmaster;
2
3use 5.014000;
4use strict;
7dc32473
MG
5our $VERSION = '5999.000_001';
6
3b69df7a 7use Encode qw/encode decode/;
f34254b8 8use File::Slurp qw/read_file/;
3b69df7a 9use JSON::MaybeXS qw/encode_json/;
594d53ba
MG
10use PerlX::Maybe;
11use Scope::Upper qw/unwind SUB UP/;
3b69df7a 12use Web::Simple;
594d53ba
MG
13
14use Gruntmaster::Data;
3b69df7a
MG
15use Plack::App::Gruntmaster::HTML;
16
17use warnings NONFATAL => 'all';
18no warnings 'illegalproto';
f34254b8 19
594d53ba
MG
20##################################################
21
594d53ba
MG
22use constant USER_REGEX => qr/^\w{2,20}$/a;
23
594d53ba
MG
24use constant FORMAT_EXTENSION => {
25 C => 'c',
26 CPP => 'cpp',
a5e355d6
MG
27 GCCGO => 'go',
28 GOLANG => 'go',
d39978bf 29 GOLFSCRIPT => 'gs',
a5e355d6 30 HASKELL => 'hs',
594d53ba
MG
31 MONO => 'cs',
32 JAVA => 'java',
33 PASCAL => 'pas',
34 PERL => 'pl',
35 PYTHON => 'py',
c83dca87 36 RUBY => 'rb',
e6d1bcd4 37 SBCL => 'l',
594d53ba
MG
38};
39
3ef32174 40use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
594d53ba 41
c039d63e 42my ($env, $privacy);
594d53ba
MG
43
44sub db { $env->{'gruntmaster.dbic'} }
45
46sub remote_user {
8d725691 47 my $user = $env->{REMOTE_USER};
594d53ba
MG
48 $user &&= db->user($user);
49 $user
50}
51
3b69df7a 52sub admin { remote_user && remote_user->admin }
594d53ba
MG
53sub contest { db->contest ($_{contest}) }
54sub problem { db->problem ($_{problem}) }
55sub job { db->job ($_{job}) }
56sub user { db->user ($_{user}) }
57
54bc2c5c 58sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
69c01de9 59sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
594d53ba 60sub response {
c039d63e 61 my ($template, $title, $params, $maxage) = @_;
594d53ba
MG
62 unless ($params) {
63 $params = $title;
64 $title = 'No title';
65 }
9a4806b3 66 $params->{time} = time;
39e2d01a 67 $params->{args} = {%_};
54bc2c5c 68 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600)}, __PACKAGE__.'::Response'
594d53ba 69}
3b69df7a 70
c039d63e
MG
71sub forbid {
72 my ($condition) = @_;
73 $privacy = 'private' if $condition;
74 return if !$condition || admin;
8d725691 75 unwind $env->{authcomplex}->unauthorized, SUB UP
594d53ba 76}
31d70015 77
594d53ba
MG
78sub dispatch_request{
79 $env = $_[PSGI_ENV];
c039d63e
MG
80 $privacy = 'public';
81
594d53ba 82 sub (GET) {
3ef32174 83 sub (/robots.txt) { NOT_FOUND },
3ef32174 84
594d53ba
MG
85 sub (/src/:job) {
86 return NOT_FOUND if !job;
02d17b02
MG
87 my $job = db->job_full($_{job});
88 my $isowner = remote_user && remote_user->id eq $job->{owner};
89 my $contest = $job->{contest} && db->contest_entry($job->{contest});
90 my $private = $job->{private} || $contest && ($contest->{started} && !$contest->{finished});
e547b147
MG
91 forbid !$isowner && $private;
92 my $privacy = $private ? 'private' : 'public';
218d6f7e 93 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
e547b147 94 push @headers, (Vary => 'Authorization') if $private;
02d17b02 95 [200, \@headers, [$job->{source}]]
594d53ba
MG
96 },
97
d3892d73 98 sub (?:format~) {
feaa8f5a 99 my $format = lc ($_{format} // '');
594d53ba
MG
100 response_filter {
101 my ($r) = @_;
102 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
928a611f
MG
103 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
104 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
69c01de9 105 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
928a611f 106 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
69c01de9 107 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
594d53ba
MG
108 },
109 },
110
594d53ba
MG
111 sub (/st/:contest) {
112 response st => 'Standings', {
7468a6a7 113 st => [ contest->standings ],
ebca729d
MG
114 problems => [
115 map { [$_->id, $_->name] }
116 sort { $a->value <=> $b->value }
117 map { $_->problem } contest->contest_problems],
c039d63e 118 }, 10
594d53ba
MG
119 },
120
645cfb7d 121 sub (/ed/:contest) {
90f613d3 122 forbid !contest->is_finished;
42243f04 123 my $pblist = db->problem_list(contest => $_{contest}, solution => 1);
54bc2c5c 124 response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial}, contest->is_finished(time - 86400) ? 60 : ();
645cfb7d
MG
125 },
126
462db4aa
MG
127 sub (/login) {
128 forbid !remote_user;
2beb67b4 129 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]]
462db4aa
MG
130 },
131
594d53ba
MG
132 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
133
134 sub (/us/) { response us => 'Users', {us => db->user_list} },
54bc2c5c 135 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_), 300 },
23e32638 136 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) {
b8a0fa71 137 forbid $_{private};
7e158d3c 138 response log => 'Job list', db->job_list(%_), 5
b8a0fa71 139 },
87ffd88b 140 sub (/pb/ + ?:owner~&:contest~&:private~) {
b8a0fa71 141 forbid $_{private};
2ca1b4a6 142 forbid contest && contest->is_pending;
ee38c972 143 response pb => 'Problems', db->problem_list(%_)
b8a0fa71 144 },
594d53ba
MG
145
146 sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
54bc2c5c 147 sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}), 60 },
28e89d6c
MG
148 sub (/log/:job) {
149 forbid job->private;
54bc2c5c 150 response log_entry => "Job $_{job}", db->job_entry($_{job}), 10
28e89d6c 151 },
84ca7535
MG
152 sub (/pb/:problem + ?contest~) {
153 my (undef, undef, $contest) = @_;
154 $_{contest} = $contest;
8a4acaca
MG
155 return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem});
156 forbid problem->private && !contest;
157 if (contest) {
158 return redirect "/pb/$_{problem}" unless contest->is_running;
5b76a57d
MG
159 forbid !remote_user;
160 $privacy = 'private';
161 }
7e158d3c 162 response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id), $_{contest} ? 10 : ();
594d53ba 163 },
e4d5bdf5 164 sub (/sol/:problem) {
8a4acaca 165 forbid problem->private;
e4d5bdf5
MG
166 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
167 },
594d53ba
MG
168
169 sub (/) { redispatch_to '/index' },
cb0122d7 170 sub (/favicon.ico) { redirect '/static/favicon.ico' },
928a611f 171 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
594d53ba
MG
172 },
173
174 sub (POST) {
08794667
MG
175 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
176 my (undef, undef, $prog) = @_;
594d53ba 177 forbid !remote_user;
8a4acaca 178 my $private = (problem->private && !contest) ? 1 : 0;
3b69df7a 179 return reply 'This contest has finished' if contest && contest->is_finished;
8a4acaca
MG
180 return reply 'This contest has not yet started' if contest && contest->is_pending;
181 return reply 'This problem is private' if !admin && $private;
182 return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem});
08794667 183 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
594d53ba
MG
184 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
185 remote_user->update({lastjob => time});
186
08794667
MG
187 my $source = $prog ? read_file $prog->path : $_{source_code};
188 unlink $prog->path if $prog;
31dc8096 189 my $newjob = db->jobs->create({
594d53ba 190 maybe contest => $_{contest},
cb6adaff 191 private => $private,
594d53ba
MG
192 date => time,
193 extension => FORMAT_EXTENSION->{$_{prog_format}},
194 format => $_{prog_format},
195 problem => $_{problem},
08794667 196 source => $source,
594d53ba
MG
197 owner => remote_user->id,
198 });
199
31dc8096 200 [303, [Location => '/log/' . $newjob->id], []]
3c434a02 201 },
594d53ba 202 }
7dc32473
MG
203}
204
594d53ba 205
7dc32473
MG
2061;
207__END__
This page took 0.049854 seconds and 4 git commands to generate.