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.

98 lines
2.2 KiB

7 years ago
package CMTD::Main;
use strict;
use warnings;
use utf8;
use Mojo::Base 'Mojolicious::Controller';
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 {
my $table = $c->app->config->{captcha}->{table};
my @sort = qw(shown tries);
$self->app->db->begin;
my ($cid, $data) = $self->app->db->select($table, ['id', 'data'], undef, \@sort)->list;
$self->app->db->update($table, {shown => time()}, {id => $cid});
$self->app->db->commit;
$self->app->log->debug("showing captcha #$cid");
$self->render(json => {id => $cid, data => $data}); 1;
} or do {
chomp $@;
$self->app->log->error("error when showing captcha: $@");
$self->res->code(500);
7 years ago
$self->render(text => "internal error\n");
7 years ago
};
7 years ago
}
sub c_list {
7 years ago
my ($self) = @_;
my $ref = $self->referrer;
unless ($ref and ref($ref) eq 'HASH') {
$self->res->code(400);
$self->render(text => "can't detect referred page\n");
return;
}
eval {
if (my $sid = $self->app->sid_by_name($ref->{site})) {
my @cm = ();
if (my $pid = $self->app->pid_by_hash($sid, $hash)) {
my @tm = $self->app->comments_by_pid($pid);
# TODO: processing
@cm = @tm;
}
$self->render(json => \@cm);
} else {
$self->res->code(400);
$self->render(text => "no such site\n");
}
} or do {
chomp $@;
my $msg = sprintf "Error when listing comments for %s/%s: %s",
$ref->{site}, $ref->{pid}, $@;
$self->app->log->error($msg);
$self->res->code(500);
$self->render(text => "internal error\n");
};
7 years ago
}
sub c_add {
my ($self) = @_;
7 years ago
my $ref = $self->referrer;
unless ($ref and ref($ref) eq 'HASH') {
7 years ago
$self->res->code(400);
$self->render(text => "can't detect referred page\n");
7 years ago
return;
}
7 years ago
my $site = $self->app->sites->{ $ref->{site} };
unless ($site and ref($site) eq 'HASH') {
7 years ago
$self->res->code(400);
$self->render(text => "no such site\n");
7 years ago
return;
}
eval {
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->res->code(500);
$self->render(text => "internal error\n");
7 years ago
};
}
1;