You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.5 KiB

7 years ago
package CMTD::Main;
use strict;
use warnings;
use utf8;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Util qw(trim b64_decode);
use POSIX qw(strftime);
7 years ago
sub index {
7 years ago
my ($self) = @_;
7 years ago
$self->render(text => "Go away!\n");
7 years ago
}
sub captcha {
7 years ago
my ($self) = @_;
eval {
$self->allow_cors;
my ($cid, $data) = $self->app->get_captcha;
7 years ago
$self->app->log->debug("showing captcha #$cid");
$self->render(json => {cid => $cid, data => $data}); 1;
7 years ago
} or do {
chomp $@;
$self->app->log->error("error when showing captcha: $@");
$self->client_reply(500, "internal error");
7 years ago
};
7 years ago
}
sub c_form {
my ($self) = @_;
$self->allow_cors;
$self->respond_to(json => {json => {}}, any => {template => 'main/form'});
}
7 years ago
sub c_list {
7 years ago
my ($self) = @_;
my $ref = $self->referrer;
unless ($ref and ref($ref) eq 'HASH') {
$self->client_reply(400, "can't detect referred page");
7 years ago
return;
}
eval {
$self->allow_cors;
my $site = $self->app->site_by_name($ref->{site});
if ($site and ref($site) eq 'HASH') {
7 years ago
my @cm = ();
if (my $pid = $self->app->pid_by_hash($site->{id}, $ref->{hash})) {
$ref->{pid} = $pid;
7 years ago
my @tm = $self->app->comments_by_pid($pid);
foreach my $c (@tm) {
$c->{date} = strftime("%Y-%m-%d %H:%M", localtime($c->{date}));
$c->{text} = b64_decode($c->{text});
# TODO: processing
}
7 years ago
@cm = @tm;
}
$self->stash(comments => \@cm);
$self->respond_to(json => {json => \@cm}, any => {template => 'main/list'});
7 years ago
} else {
$self->client_reply(400, "no such site");
7 years ago
} 1;
7 years ago
} or do {
chomp $@;
my $msg = sprintf "Error when listing comments for %s/%s: %s",
$ref->{site}, $ref->{pid}, $@;
$self->app->log->error($msg);
$self->client_reply(500, "internal error");
7 years ago
};
7 years ago
}
sub c_add {
my ($self) = @_;
7 years ago
my $ref = $self->referrer;
unless ($ref and ref($ref) eq 'HASH') {
$self->client_reply(400, "can't detect referred page");
7 years ago
return;
}
eval {
$self->allow_cors;
my (%msg, %cap);
$msg{addr} = $self->tx->remote_address;
foreach my $param (qw(name email text reply)) {
$msg{$param} = trim($self->req->param($param) || '');
}
foreach my $param (qw(cid code)) {
$cap{$param} = trim($self->req->param($param) || '');
}
do {{
unless ($msg{text}) {
$self->client_reply(400, "empty message");
last;
}
my $site = $self->app->site_by_name($ref->{site});
unless ($site and ref($site) eq 'HASH') {
$self->client_reply(400, "no such site");
last;
}
if ($site->{captcha}) {
unless ($cap{cid}) {
$self->client_reply(400, "missing captcha id");
last;
}
unless ($cap{code}) {
$self->client_reply(400, "missing captcha code");
last;
}
if (my $error = $self->app->captcha_solve($cap{cid}, $cap{code})) {
$self->client_reply(400, $error);
last;
}
}
my $pid = $self->app->pid_by_hash($site->{id}, $ref->{hash});
unless ($pid) {
$self->app->add_page($site->{id}, $ref);
$pid = $self->app->pid_by_hash($site->{id}, $ref->{hash});
}
$self->add_comment($pid, \%msg);
$self->client_reply(200, "OK");
}} while (0); 1;
7 years ago
} or do {
chomp $@;
my $msg = sprintf "Error when listing comments for %s/%s: %s",
$ref->{site}, $ref->{pid}, $@;
$self->app->log->error($msg);
$self->client_reply(500, "internal error");
7 years ago
};
}
1;