diff --git a/lib/CMTD/Main.pm b/lib/CMTD/Main.pm index 32e9ff9..5931e35 100644 --- a/lib/CMTD/Main.pm +++ b/lib/CMTD/Main.pm @@ -6,7 +6,8 @@ use utf8; use Mojo::Base 'Mojolicious::Controller'; -use Mojo::Util qw(trim); +use Mojo::Util qw(trim b64_decode); +use POSIX qw(strftime); sub index { my ($self) = @_; @@ -18,6 +19,7 @@ sub captcha { my ($self) = @_; eval { + $self->allow_cors; my ($cid, $data) = $self->app->get_captcha; $self->app->log->debug("showing captcha #$cid"); $self->render(json => {cid => $cid, data => $data}); 1; @@ -28,6 +30,13 @@ sub captcha { }; } +sub c_form { + my ($self) = @_; + + $self->allow_cors; + $self->respond_to(json => {json => {}}, any => {template => 'main/form'}); +} + sub c_list { my ($self) = @_; @@ -38,13 +47,18 @@ sub c_list { } eval { + $self->allow_cors; my $site = $self->app->site_by_name($ref->{site}); if ($site and ref($site) eq 'HASH') { my @cm = (); if (my $pid = $self->app->pid_by_hash($site->{id}, $ref->{hash})) { $ref->{pid} = $pid; my @tm = $self->app->comments_by_pid($pid); - # TODO: processing + foreach my $c (@tm) { + $c->{date} = strftime("%Y-%m-%d %H:%M", localtime($c->{date})); + $c->{text} = b64_decode($c->{text}); + # TODO: processing + } @cm = @tm; } $self->stash(comments => \@cm); @@ -71,6 +85,7 @@ sub c_add { } eval { + $self->allow_cors; my (%msg, %cap); $msg{addr} = $self->tx->remote_address; foreach my $param (qw(name email text reply)) { @@ -98,7 +113,7 @@ sub c_add { $self->client_reply(400, "missing captcha code"); last; } - if (my $error = $self->app->captcha_solve($cap{id}, $cap{code})) { + if (my $error = $self->app->captcha_solve($cap{cid}, $cap{code})) { $self->client_reply(400, $error); last; } @@ -109,6 +124,7 @@ sub c_add { $pid = $self->app->pid_by_hash($site->{id}, $ref->{hash}); } $self->add_comment($pid, \%msg); + $self->client_reply(200, "OK"); }} while (0); 1; } or do { chomp $@;