File Coverage

blib/lib/Whim/Controller/Listen.pm
Criterion Covered Total %
statement 28 30 93.3
branch 1 2 50.0
condition n/a
subroutine 7 8 87.5
pod 0 2 0.0
total 36 42 85.7


line stmt bran cond sub pod time code
1             package Whim::Controller::Listen;
2 2     2   1227 use Mojo::Base 'Mojolicious::Controller';
  2         4  
  2         12  
3 2     2   354 use Whim::Mention;
  2         7  
  2         40  
4 2     2   10 use Try::Tiny;
  2         5  
  2         108  
5              
6 2     2   11 use Readonly;
  2         5  
  2         1037  
7             Readonly my $OK => 200;
8             Readonly my $ACCEPTED => 202;
9             Readonly my $BAD_REQUEST => 400;
10              
11             sub default {
12 1     1 0 18975 my $self = shift;
13              
14 1         82 $self->render(
15             status => $OK,
16             text => 'OK (listening for webmentions)'
17             );
18             }
19              
20             sub receive {
21 7     7 0 97308 my $self = shift;
22              
23 7         13 my $webmention;
24             try {
25 7     7   334 $webmention = Whim::Mention->new_from_request($self);
26             }
27             catch {
28 0     0   0 $self->render(
29             status => $BAD_REQUEST,
30             text => "Malformed webmention: $_"
31             );
32 0         0 $self->log->info('Rejected a malformed webmention.');
33 7         52 };
34 7 50       19776 return unless $webmention;
35              
36             # Pull out the source and target params, mostly for logging
37 7         33 my $source = $self->param('source');
38 7         526 my $target = $self->param('target');
39              
40             # XXX For the present, naively accept all webmentions.
41             # This is technically legal under section 3.2.1 of the spec.
42             # But it SHOULD check against some stored config about whether
43             # it cares about the target URL at all.
44 7         385 unless (1) {
45             my $error_text = "Unrecognized target URL: $target";
46             $self->render(
47             status => $BAD_REQUEST,
48             text => $error_text
49             );
50             $self->log->info($error_text);
51             return;
52             }
53              
54 7         14 my $success_text =
55             "Webmention accepted, and queued for verification and "
56             . "processing. Thank you!";
57              
58 7         11 my $return_link_text = 'Return to previous page.';
59 7         30 $success_text .= qq{ <a href="$target">$return_link_text</a>};
60              
61 7         67 $self->render( status => $ACCEPTED, text => $success_text );
62              
63 7         2727 $self->log->info("Accepted: $source -> $target");
64              
65 7         216 $self->whim->receive_webmention($webmention);
66             }
67              
68             1;