Browse Source

+ CMTD::Helpers->client_reply

master
Alex 'AdUser' Z 7 years ago
parent
commit
eb2ecbd7f3
  1. 7
      lib/CMTD/Helpers.pm
  2. 33
      lib/CMTD/Main.pm

7
lib/CMTD/Helpers.pm

@ -59,6 +59,13 @@ sub register {
};
});
$app->helper(client_reply => sub {
my ($c, $code, $msg) = @_;
$c->res->code($code);
$c->render(text => "$msg\n");
return;
});
$app->helper(sid_by_name => sub {
my ($c, $site) = @_;
return $self->app->db->select('sites', 'id', {site => $site})->list;

33
lib/CMTD/Main.pm

@ -29,8 +29,7 @@ sub captcha {
} or do {
chomp $@;
$self->app->log->error("error when showing captcha: $@");
$self->res->code(500);
$self->render(text => "internal error\n");
$self->app->client_reply(500, "internal error");
};
}
@ -39,8 +38,7 @@ sub c_list {
my $ref = $self->referrer;
unless ($ref and ref($ref) eq 'HASH') {
$self->res->code(400);
$self->render(text => "can't detect referred page\n");
$self->app->client_reply(400, "can't detect referred page");
return;
}
@ -54,16 +52,14 @@ sub c_list {
}
$self->render(json => \@cm);
} else {
$self->res->code(400);
$self->render(text => "no such site\n");
$self->app->client_reply(400, "no such site");
} 1;
} 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");
$self->app->client_reply(500, "internal error");
};
}
@ -72,8 +68,7 @@ sub c_add {
my $ref = $self->referrer;
unless ($ref and ref($ref) eq 'HASH') {
$self->res->code(400);
$self->render(text => "can't detect referred page\n");
$self->app->client_reply(400, "can't detect referred page");
return;
}
@ -88,31 +83,26 @@ sub c_add {
}
do {{
unless ($msg{text}) {
$self->res->code(400);
$self->render(text => "empty message\n");
$self->app->client_reply(400, "empty message");
last;
}
my $sid = $self->app->sid_by_name($ref->{site});
unless ($sid) {
$self->res->code(400);
$self->render(text => "no such site\n");
$self->app->client_reply(400, "no such site");
last;
}
if ($cap{cid}) {
unless ($cap{code}) {
$self->res->code(400);
$self->render(text => "missing captcha code\n");
$self->app->client_reply(400, "missing captcha code");
last;
}
my $cap = $self->captcha_by_id($cap{cid});
unless ($cap and $cap eq 'HASH') {
$self->res->code(400);
$self->render(text => "no captcha with this id\n");
$self->app->client_reply(400, "no captcha with this id");
last;
}
unless ($cap->{code} eq $cap{code}) {
$self->res->code(400);
$self->render(text => "captcha code mismatch\n");
$self->app->client_reply(400, "captcha code mismatch");
last;
}
}
@ -128,8 +118,7 @@ sub c_add {
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");
$self->app->client_reply(500, "internal error");
};
}

Loading…
Cancel
Save