$template_dir;
}
+has 'captcha_handler' => (is => 'lazy');
+
+sub _build_captcha_handler {
+ my ($self) = @_;
+ require App::Commentry::CaptchaHandler::Fixed;
+ App::Commentry::CaptchaHandler::Fixed->new;
+}
+
sub dispatch_request {
my ($self) = @_;
sub (/fragments/*/**) {
}
}
+sub _fragment_add {
+ my ($self, $set) = @_;
+ my $captcha = $self->captcha_handler;
+ my %params = map +($_ => ''), qw(title body);
+ sub (POST+%*) {
+ my ($self, $params) = @_;
+ @params{qw(title body)} = @{$params}{qw(title body)};
+ return unless $captcha->check_response($params);
+ $set->add($params);
+ $self->_zoom_response(fragment_add_complete => sub { });
+ },
+ sub (GET|POST) {
+ my $captcha_fields = $captcha->challenge_html;
+ $self->_zoom_response(fragment_add => sub {
+ $_->select('.captcha-fields')->replace(\$captcha_fields)
+ ->select('.new-comment-title')->set_attribute(value => $params{title})
+ ->select('.new-comment-body')->set_attribute(value => $params{body});
+ });
+ },
+}
+
sub _zoom_response {
my ($self, $template, $apply) = @_;
my $zfh = ($self->{zoom_cache}{$template} ||= do {
--- /dev/null
+package App::Commentry::CaptchaHandler::Fixed;
+
+use Moo;
+
+has challenge => (is => 'ro', default => sub {
+ q{What is mst the chainsaw wielder's weapon of choice?}
+});
+
+has field_name => (is => 'ro', default => sub { 'captcha_response' });
+
+has response_match => (is => 'ro', default => sub { qr/(?i:chainsaw)/ });
+
+sub challenge_html {
+ my ($self) = @_;
+
+ my $field_name = $self->field_name;
+
+ use HTML::Tags;
+ join '', HTML::Tags::to_html_string(
+ <p>, $self->challenge, </p>, "\n",
+ <input name="${field_name}" />, "\n",
+ );
+}
+
+sub check_response {
+ my ($self, $params) = @_;
+ my $match = $self->response_match;
+ !!($params->{$self->field_name} =~ /$match/);
+}
+
+1;