From eb2ecbd7f30236acf7a78215f24f91e54eb08917 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Wed, 23 Aug 2017 11:22:54 +1000 Subject: [PATCH] + CMTD::Helpers->client_reply --- lib/CMTD/Helpers.pm | 7 +++++++ lib/CMTD/Main.pm | 33 +++++++++++---------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/lib/CMTD/Helpers.pm b/lib/CMTD/Helpers.pm index abbcd14..fa515fb 100644 --- a/lib/CMTD/Helpers.pm +++ b/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; diff --git a/lib/CMTD/Main.pm b/lib/CMTD/Main.pm index 09488de..926ec93 100644 --- a/lib/CMTD/Main.pm +++ b/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"); }; }