Sort problems in standings by value and fix standings JSON
[plack-app-gruntmaster.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
e03e380b 7use CSS::Minifier::XS;
3b69df7a 8use Encode qw/encode decode/;
f34254b8 9use File::Slurp qw/read_file/;
e03e380b 10use JavaScript::Minifier::XS;
3b69df7a 11use JSON::MaybeXS qw/encode_json/;
594d53ba
MG
12use PerlX::Maybe;
13use Scope::Upper qw/unwind SUB UP/;
3b69df7a 14use Web::Simple;
594d53ba
MG
15
16use Gruntmaster::Data;
3b69df7a
MG
17use Plack::App::Gruntmaster::HTML;
18
19use warnings NONFATAL => 'all';
20no warnings 'illegalproto';
594d53ba 21no if $] >= 5.017011, warnings => 'experimental::smartmatch';
f34254b8 22
594d53ba
MG
23##################################################
24
594d53ba
MG
25use constant USER_REGEX => qr/^\w{2,20}$/a;
26
e03e380b
MG
27use constant CONTENT_TYPES => +{
28 c => 'text/x-csrc',
29 cpp => 'text/x-c++src',
30 cs => 'text/x-csharp', # Used by GNOME. Not in mime.types.
a5e355d6
MG
31 go => 'text/plain', # ?
32 hs => 'text/x-haskell',
e03e380b
MG
33 java => 'text/x-java',
34 pas => 'text/x-pascal',
35 pl => 'text/x-perl',
36 py => 'text/x-python',
37};
594d53ba
MG
38
39use constant FORMAT_EXTENSION => {
40 C => 'c',
41 CPP => 'cpp',
a5e355d6
MG
42 GCCGO => 'go',
43 GOLANG => 'go',
44 HASKELL => 'hs',
594d53ba
MG
45 MONO => 'cs',
46 JAVA => 'java',
47 PASCAL => 'pas',
48 PERL => 'pl',
49 PYTHON => 'py',
50};
51
e03e380b 52use constant NOT_FOUND => [404, ['Content-Type' => 'text/plain'], ['Not found']];
3b69df7a 53use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']];
594d53ba 54
1bb102ef
MG
55sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' }
56
c039d63e 57my ($env, $privacy);
594d53ba
MG
58
59sub db { $env->{'gruntmaster.dbic'} }
60
61sub remote_user {
62 my $user = $env->{'gruntmaster.user'};
63 $user &&= db->user($user);
64 $user
65}
66
3b69df7a 67sub admin { remote_user && remote_user->admin }
594d53ba
MG
68sub contest { db->contest ($_{contest}) }
69sub problem { db->problem ($_{problem}) }
70sub job { db->job ($_{job}) }
71sub user { db->user ($_{user}) }
72
73sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
74sub reply { [200, ['Content-Type' => 'text/plain'], \@_] }
75sub response {
c039d63e 76 my ($template, $title, $params, $maxage) = @_;
594d53ba
MG
77 unless ($params) {
78 $params = $title;
79 $title = 'No title';
80 }
9a4806b3 81 $params->{time} = time;
c039d63e 82 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 1)}, __PACKAGE__.'::Response'
594d53ba 83}
3b69df7a 84
c039d63e
MG
85sub forbid {
86 my ($condition) = @_;
87 $privacy = 'private' if $condition;
88 return if !$condition || admin;
594d53ba
MG
89 unwind FORBIDDEN, SUB UP
90}
31d70015 91
594d53ba
MG
92sub dispatch_request{
93 $env = $_[PSGI_ENV];
c039d63e
MG
94 $privacy = 'public';
95
594d53ba
MG
96 sub (GET) {
97 sub (/css/:theme) {
98 my $theme = $_{theme};
99 return NOT_FOUND unless -e "css/themes/$theme.css";
100 my $css = read_file "css/themes/$theme.css";
101 $css .= read_file $_ for <css/*.css>;
102 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css');
1bb102ef 103 [200, \@headers, [development ? $css : CSS::Minifier::XS::minify $css]]
594d53ba
MG
104 },
105
106 sub (/js.js) {
107 my $js;
108 $js .= read_file $_ for <js/*.js>;
109 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript');
1bb102ef 110 [200, \@headers, [development ? $js : JavaScript::Minifier::XS::minify $js]]
594d53ba
MG
111 },
112
113 sub (/src/:job) {
114 return NOT_FOUND if !job;
34a258ae 115 forbid job->private || job->problem->private || job->contest && job->contest->is_running;
594d53ba
MG
116 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => CONTENT_TYPES->{job->format});
117 [200, \@headers, [job->source]]
118 },
119
120 sub (?:contest=) {
121 return NOT_FOUND if !contest;
122 forbid contest->is_pending;
123 response_filter { return shift }
124 },
125
126 sub (?:problem=) {
127 return NOT_FOUND if !problem;
128 forbid problem->is_private;
129 response_filter { return shift }
130 },
131
d3892d73 132 sub (?:format~) {
feaa8f5a 133 my $format = lc ($_{format} // '');
594d53ba
MG
134 response_filter {
135 my ($r) = @_;
136 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
c039d63e
MG
137 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
138 return [200, ['Content-Type' => 'application/json', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
3b69df7a 139 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
c039d63e 140 [200, ['Content-Type' => 'text/html', @hdrs], [encode 'UTF-8', $ret]]
594d53ba
MG
141 },
142 },
143
594d53ba
MG
144 sub (/st/:contest) {
145 response st => 'Standings', {
7468a6a7 146 st => [ contest->standings ],
ebca729d
MG
147 problems => [
148 map { [$_->id, $_->name] }
149 sort { $a->value <=> $b->value }
150 map { $_->problem } contest->contest_problems],
c039d63e 151 }, 10
594d53ba
MG
152 },
153
462db4aa
MG
154 sub (/login) {
155 forbid !remote_user;
156
4f3b70b7
MG
157 my $return = $env->{HTTP_REFERER} // '/';
158 [303, ['Set-Cookie' => "username=".remote_user->id, Location => $return], []]
462db4aa
MG
159 },
160
594d53ba
MG
161 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
162
163 sub (/us/) { response us => 'Users', {us => db->user_list} },
164 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_) },
b8a0fa71
MG
165 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~) {
166 forbid $_{private};
167 response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest}}
168 },
87ffd88b 169 sub (/pb/ + ?:owner~&:contest~&:private~) {
b8a0fa71
MG
170 forbid $_{private};
171 response pb => 'Problems', {%{db->problem_list(%_)}, maybe contest => $_{contest}}
172 },
594d53ba
MG
173
174 sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
175 sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}) },
28e89d6c
MG
176 sub (/log/:job) {
177 forbid job->private;
178 response log_entry => "Job $_{job}", db->job_entry($_{job})
179 },
84ca7535
MG
180 sub (/pb/:problem + ?contest~) {
181 my (undef, undef, $contest) = @_;
182 $_{contest} = $contest;
594d53ba
MG
183 return NOT_FOUND if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
184 forbid problem->is_private;
5b76a57d
MG
185 if (contest && contest->is_running) {
186 forbid !remote_user;
187 $privacy = 'private';
188 }
33ea2780 189 response pb_entry => problem->name, {%{db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id)}, maybe contest => $_{contest}};
594d53ba 190 },
e4d5bdf5
MG
191 sub (/sol/:problem) {
192 forbid !problem->is_in_archive;
193 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
194 },
594d53ba
MG
195
196 sub (/) { redispatch_to '/index' },
c039d63e 197 sub (/:article) { [200, ['Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=60', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
594d53ba
MG
198 },
199
200 sub (POST) {
201 sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:level=) {
202 return reply 'Parameter too long' if grep { length > 200 } values %_;
203 return reply 'Bad username. Allowed characters are letters, digits and underscores, and the username must be between 2 and 20 characters long.' unless $_{username} =~ USER_REGEX;
204 return reply 'Username already in use' if db->user($_{username});
205 return reply 'The two passwords do not match' unless $_{password} eq $_{confirm_password};
206
207 db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, level => $_{level}});
208 db->user($_{username})->set_passphrase($_{password});
209
c039d63e 210 purge '/us/';
594d53ba
MG
211 reply 'Registered successfully';
212 },
213
214 sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) {
215 forbid !remote_user;
216 return reply 'Incorrect password' unless remote_user->check_passphrase($_{password});
217 return reply 'The two passwords do not match' unless $_{new_password} eq $_{confirm_new_password};
218 remote_user->set_passphrase($_{new_password});
219 reply 'Password changed successfully';
220 },
221
08794667
MG
222 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
223 my (undef, undef, $prog) = @_;
594d53ba 224 forbid !remote_user;
3b69df7a
MG
225 return reply 'This contest has finished' if contest && contest->is_finished;
226 return reply 'This contest has not yet started' if !admin && contest && contest->is_pending;
31f5eb01 227 return reply 'This problem does not belong to this contest' if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
08794667 228 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
594d53ba
MG
229 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
230 remote_user->update({lastjob => time});
231
08794667
MG
232 my $source = $prog ? read_file $prog->path : $_{source_code};
233 unlink $prog->path if $prog;
31dc8096 234 my $newjob = db->jobs->create({
594d53ba 235 maybe contest => $_{contest},
34a258ae 236 maybe private => problem->private && !$_{contest},
594d53ba
MG
237 date => time,
238 extension => FORMAT_EXTENSION->{$_{prog_format}},
239 format => $_{prog_format},
240 problem => $_{problem},
08794667 241 source => $source,
594d53ba
MG
242 owner => remote_user->id,
243 });
244
c039d63e 245 purge '/log/';
31dc8096 246 [303, [Location => '/log/' . $newjob->id], []]
594d53ba
MG
247 }
248 }
7dc32473
MG
249}
250
594d53ba 251
7dc32473
MG
2521;
253__END__
This page took 0.037286 seconds and 4 git commands to generate.