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   1105 use Mojo::Base 'Mojolicious::Controller';
  2         6  
  2         12  
3 2     2   353 use Whim::Mention;
  2         4  
  2         46  
4 2     2   10 use Try::Tiny;
  2         5  
  2         111  
5              
6 2     2   13 use Readonly;
  2         4  
  2         993  
7             Readonly my $OK => 200;
8             Readonly my $ACCEPTED => 202;
9             Readonly my $BAD_REQUEST => 400;
10              
11             sub default {
12 1     1 0 18024 my $self = shift;
13              
14 1         8 $self->render(
15             status => $OK,
16             text => 'OK (listening for webmentions)'
17             );
18             }
19              
20             sub receive {
21 7     7 0 96800 my $self = shift;
22              
23 7         15 my $webmention;
24             try {
25 7     7   337 $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         44 };
34 7 50       19485 return unless $webmention;
35              
36             # Pull out the source and target params, mostly for logging
37 7         29 my $source = $self->param('source');
38 7         521 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         392 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         15 my $success_text =
55             "Webmention accepted, and queued for verification and "
56             . "processing. Thank you!";
57              
58 7         12 my $return_link_text = 'Return to previous page.';
59 7         35 $success_text .= qq{ <a href="$target">$return_link_text</a>};
60              
61 7         55 $self->render( status => $ACCEPTED, text => $success_text );
62              
63 7         2643 $self->log->info("Accepted: $source -> $target");
64              
65 7         209 $self->whim->receive_webmention($webmention);
66             }
67              
68             1;