| 1 | package BusyBird::Main::PSGI; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use v5.8.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use BusyBird::Util qw(set_param future_of); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use BusyBird::Main::PSGI::View; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Router::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Plack::Builder (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Plack::App::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use JSON qw(decode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use List::Util qw(min); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use Exporter 5.57 qw(import); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use URI::Escape qw(uri_unescape); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Encode qw(decode_utf8); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use Future::Q; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use POSIX qw(ceil); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | our @EXPORT_OK = qw(create_psgi_app); | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 45 | ▶ | sub create_psgi_app { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_psgi_app: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 45 | my ($main_obj) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 45 | my $self = __PACKAGE__->_new(main_obj => $main_obj); | |||||||||||||||||||||||||||||||||||||||||
| 28 | 45 | return $self->_to_app; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 45 | ▶ | sub _new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 45 | my ($class, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 45 | my $self = bless { | |||||||||||||||||||||||||||||||||||||||||
| 34 | router => Router::Simple->new, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | view => undef, ## lazy build | ||||||||||||||||||||||||||||||||||||||||||
| 36 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 45 | $self->set_param(\%params, "main_obj", undef, 1); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 45 | $self->_build_routes(); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 45 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 45 | ▶ | sub _to_app { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_to_app: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 45 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 45 | my $sharedir = $self->{main_obj}->get_config("sharedir_path"); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 45 | $sharedir =~ s{/+$}{}; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 45 | ▶ | return Plack::Builder::builder { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 45 | Plack::Builder::enable 'ContentLength'; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 45 | Plack::Builder::mount '/static' => Plack::App::File->new( | |||||||||||||||||||||||||||||||||||||||||
| 49 | root => File::Spec->catdir($sharedir, 'www', 'static') | ||||||||||||||||||||||||||||||||||||||||||
| 50 | )->to_app; | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 45 | Plack::Builder::mount '/' => $self->_my_app; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 45 | }; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 45 | ▶ | sub _my_app { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_my_app: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 45 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 185 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 185 | my ($env) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 185 | ▶ | $self->{view} ||= BusyBird::Main::PSGI::View->new(main_obj => $self->{main_obj}, script_name => $env->{SCRIPT_NAME}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$self{"view"} ||= "BusyBird::Main::PSGI::View"->new("main_obj", $$self{"main_obj"}, "script_name", $$env{"SCRIPT_NAME"})
MC/DC0%0/2
$$self{"view"} ||= "BusyBird::Main::PSGI::View"->new("main_obj", $$self{"main_obj"}, "script_name", $$env{"SCRIPT_NAME"})
$$self{"view"}
"BusyBird::Main::PSGI::View"->new("main_obj", $$self{"main_obj"}, "script_name", $$env{"SCRIPT_NAME"})
Truth table66%2/3
A ||= B
A= $$self{"view"}B= "BusyBird::Main::PSGI::View"->new("main_obj", $$self{"main_obj"}, "script_name", $$env{"SCRIPT_NAME"})
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 185 | ▶ | if(my $dest = $self->{router}->match($env)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 179 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 179 | my $code = $dest->{code}; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 179 | my $method = $dest->{method}; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 179 | ▶ | return defined($code) ? $code->($self, $req, $dest) : $self->$method($req, $dest); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | }else { | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 6 | return $self->{view}->response_notfound(); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 45 | }; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 45 | ▶ | sub _build_routes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_routes: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 45 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 73 | my $tl_mapper = $self->{router}->submapper( | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 45 | '/timelines/{timeline}', {} | |||||||||||||||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 45 | $tl_mapper->connect('/statuses.{format}', | |||||||||||||||||||||||||||||||||||||||||
| 77 | {method => '_handle_tl_get_statuses'}, {method => 'GET'}); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 45 | $tl_mapper->connect('/statuses.json', | |||||||||||||||||||||||||||||||||||||||||
| 79 | {method => '_handle_tl_post_statuses'}, {method => 'POST'}); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 45 | $tl_mapper->connect('/ack.json', | |||||||||||||||||||||||||||||||||||||||||
| 81 | {method => '_handle_tl_ack'}, {method => 'POST'}); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 45 | $tl_mapper->connect('/updates/unacked_counts.json', | |||||||||||||||||||||||||||||||||||||||||
| 83 | {method => '_handle_tl_get_unacked_counts'}, {method => 'GET'}); | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 45 | $tl_mapper->connect($_, {method => '_handle_tl_index'}) foreach "", qw(/ /index.html /index.htm); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 45 | $self->{router}->connect('/updates/unacked_counts.json', | |||||||||||||||||||||||||||||||||||||||||
| 86 | {method => '_handle_get_unacked_counts'}, {method => 'GET'}); | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 45 | foreach my $path ("/", "/index.html") { | |||||||||||||||||||||||||||||||||||||||||
| 88 | 90 | $self->{router}->connect($path, {method => '_handle_get_timeline_list'}, {method => 'GET'}); | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 152 | ▶ | sub _get_timeline_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_timeline_name: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 152 | my ($dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 152 | my $name = $dest->{timeline}; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 152 | ▶ | $name = "" if not defined($name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 152 | $name =~ s/\+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 152 | return decode_utf8(uri_unescape($name)); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 135 | ▶ | sub _get_timeline { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_timeline: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 135 | my ($self, $dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 135 | my $name = _get_timeline_name($dest); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 135 | my $timeline = $self->{main_obj}->get_timeline($name); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 135 | ▶ | if(!defined($timeline)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | die qq{No timeline named $name}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 131 | return $timeline; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 70 | ▶ | sub _handle_tl_get_statuses { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_tl_get_statuses: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 70 | my ($self, $req, $dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 70 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 70 | my $responder = shift; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 70 | my $timeline; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 70 | my $only_statuses = !!($req->query_parameters->{only_statuses}); | |||||||||||||||||||||||||||||||||||||||||
| 116 | my $format = !defined($dest->{format}) ? "" | ||||||||||||||||||||||||||||||||||||||||||
| 117 | : (lc($dest->{format}) eq "json" && $only_statuses) ? "json_only_statuses" | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 70 | ▶ | : $dest->{format}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
lc $$dest{"format"} eq "json" and $only_statuses
MC/DC100%2/2
lc $$dest{"format"} eq "json" and $only_statuses
lc $$dest{"format"} eq "json"
$only_statuses
Truth table100%3/3
A and B
A= lc $$dest{"format"} eq "json"B= $only_statuses
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 70 | ▶ | Future::Q->try(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.8
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 70 | $timeline = $self->_get_timeline($dest); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 69 | ▶ | my $count = $req->query_parameters->{count} || 20; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$req->query_parameters->{'count'} || 20
MC/DC100%1/1
$req->query_parameters->{'count'} || 20
$req->query_parameters->{'count'}
Truth table100%2/2
A
A= $req->query_parameters->{'count'}
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 69 | ▶ | if(!looks_like_number($count) || int($count) != $count) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not &looks_like_number($count) or int $count != $count
MC/DC0%0/2
not &looks_like_number($count) or int $count != $count
not &looks_like_number($count)
int $count != $count
Truth table33%1/3
not(A) or B
A= not &looks_like_number($count)B= int $count != $count
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | die "count parameter must be an integer\n"; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 69 | ▶ | my $ack_state = $req->query_parameters->{ack_state} || 'any'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$req->query_parameters->{'ack_state'} || 'any'
MC/DC100%1/1
$req->query_parameters->{'ack_state'} || 'any'
$req->query_parameters->{'ack_state'}
Truth table100%2/2
A
A= $req->query_parameters->{'ack_state'}
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 69 | my $max_id = decode_utf8($req->query_parameters->{max_id}); | |||||||||||||||||||||||||||||||||||||||||
| 127 | 69 | return future_of($timeline, "get_statuses", | |||||||||||||||||||||||||||||||||||||||||
| 128 | count => $count, ack_state => $ack_state, max_id => $max_id); | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 65 | ▶ | })->then(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 65 | my $statuses = shift; | |||||||||||||||||||||||||||||||||||||||||
| 131 | $responder->($self->{view}->response_statuses( | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 65 | statuses => $statuses, http_code => 200, format => $format, | |||||||||||||||||||||||||||||||||||||||||
| 133 | timeline_name => $timeline->name | ||||||||||||||||||||||||||||||||||||||||||
| 134 | )); | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 5 | ▶ | })->catch(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 5 | my ($error, $is_normal_error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 137 | $responder->($self->{view}->response_statuses( | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 5 | ▶ | error => "$error", http_code => ($is_normal_error ? 500 : 400), format => $format, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | ($timeline ? (timeline_name => $timeline->name) : ()) | ||||||||||||||||||||||||||||||||||||||||||
| 140 | )); | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 70 | }); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 70 | }; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 31 | ▶ | sub _handle_tl_post_statuses { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_tl_post_statuses: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 31 | my ($self, $req, $dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 31 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 31 | my $responder = shift; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 31 | ▶ | Future::Q->try(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 31 | my $timeline = $self->_get_timeline($dest); | |||||||||||||||||||||||||||||||||||||||||
| 151 | 30 | my $posted_obj = decode_json($req->content); | |||||||||||||||||||||||||||||||||||||||||
| 152 | 29 | ▶ | if(ref($posted_obj) ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 5 | $posted_obj = [$posted_obj]; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 29 | return future_of($timeline, "add_statuses", statuses => $posted_obj); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 27 | ▶ | })->then(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 27 | my $added_num = shift; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 27 | $responder->($self->{view}->response_json(200, {count => $added_num + 0})); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 4 | ▶ | })->catch(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 4 | my ($e, $is_normal_error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 4 | ▶ | $responder->($self->{view}->response_json(($is_normal_error ? 500 : 400), {error => "$e"})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 31 | }); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 31 | }; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 28 | ▶ | sub _handle_tl_ack { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_tl_ack: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 28 | my ($self, $req, $dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 28 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 28 | my $responder = shift; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 28 | ▶ | Future::Q->try(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 28 | my $timeline = $self->_get_timeline($dest); | |||||||||||||||||||||||||||||||||||||||||
| 172 | 27 | my $max_id = undef; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 27 | my $ids = undef; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 27 | ▶ | if($req->content) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 23 | my $body_obj = decode_json($req->content); | |||||||||||||||||||||||||||||||||||||||||
| 176 | 23 | ▶ | if(ref($body_obj) ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | die "Response body must be an object.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 23 | $max_id = $body_obj->{max_id}; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 23 | $ids = $body_obj->{ids}; | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 27 | return future_of($timeline, "ack_statuses", max_id => $max_id, ids => $ids); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 25 | ▶ | })->then(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 25 | my $acked_num = shift; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 25 | $responder->($self->{view}->response_json(200, {count => $acked_num + 0})); | |||||||||||||||||||||||||||||||||||||||||
| 186 | 3 | ▶ | })->catch(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 3 | my ($e, $is_normal_error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 3 | ▶ | $responder->($self->{view}->response_json(($is_normal_error ? 500 : 400), {error => "$e"})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 28 | }); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 28 | }; | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 6 | ▶ | sub _handle_tl_get_unacked_counts { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_tl_get_unacked_counts: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 6 | my ($self, $req, $dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 6 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 6 | my $responder = shift; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 6 | ▶ | Future::Q->try(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 6 | my $timeline = $self->_get_timeline($dest); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 5 | my $query_params = $req->query_parameters; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 5 | my %assumed = (); | |||||||||||||||||||||||||||||||||||||||||
| 201 | 5 | ▶ | if(defined $query_params->{total}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 3 | $assumed{total} = delete $query_params->{total}; | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 5 | foreach my $query_key (keys %$query_params) { | |||||||||||||||||||||||||||||||||||||||||
| 205 | 5 | ▶ | next if !looks_like_number($query_key); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 5 | ▶ | next if int($query_key) != $query_key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 5 | $assumed{$query_key} = $query_params->{$query_key}; | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 5 | my $ret_future = Future::Q->new; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 5 | ▶ | $timeline->watch_unacked_counts(assumed => \%assumed, callback => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 5 | my ($error, $w, $unacked_counts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 5 | $w->cancel(); ## immediately cancel the watcher to prevent multiple callbacks | |||||||||||||||||||||||||||||||||||||||||
| 213 | 5 | ▶ | if($error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $ret_future->reject($error, 1); | |||||||||||||||||||||||||||||||||||||||||
| 215 | }else { | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 5 | $ret_future->fulfill($unacked_counts); | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 5 | }); | |||||||||||||||||||||||||||||||||||||||||
| 219 | 5 | return $ret_future; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 5 | ▶ | })->then(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 5 | my ($unacked_counts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 5 | $responder->($self->{view}->response_json(200, {unacked_counts => $unacked_counts})); | |||||||||||||||||||||||||||||||||||||||||
| 223 | 1 | ▶ | })->catch(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | my ($e, $is_normal_error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | ▶ | $responder->($self->{view}->response_json(($is_normal_error ? 500 : 400), {error => "$e"})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 6 | }); | |||||||||||||||||||||||||||||||||||||||||
| 227 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 17 | ▶ | sub _handle_get_unacked_counts { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_get_unacked_counts: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 17 | my ($self, $req, $dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 17 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 17 | my $responder = shift; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 17 | ▶ | Future::Q->try(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.2
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 17 | my $query_params = $req->query_parameters; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 17 | my $level = $query_params->{level}; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 17 | ▶ | if(not defined($level)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 3 | $level = "total"; | |||||||||||||||||||||||||||||||||||||||||
| 239 | ▶ | }elsif($level ne 'total' && (!looks_like_number($level) || int($level) != $level)) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$level ne "total" and !&looks_like_number($level) || int $level != $level
not &looks_like_number($level) or int $level != $level
MC/DC0%0/3
$level ne "total" and !&looks_like_number($level) || int $level != $level
$level ne "total"
not &looks_like_number($level)
int $level != $level
Truth table50%2/4
A and not(B) or C
A= $level ne "total"B= not &looks_like_number($level)C= int $level != $level
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | die "level parameter must be an integer\n"; | |||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 17 | my %assumed = (); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 17 | foreach my $query_key (keys %$query_params) { | |||||||||||||||||||||||||||||||||||||||||
| 244 | 62 | ▶ | next if substr($query_key, 0, 3) ne 'tl_'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 46 | $assumed{decode_utf8(substr($query_key, 3))} = $query_params->{$query_key}; | |||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 17 | my $ret_future = Future::Q->new; | |||||||||||||||||||||||||||||||||||||||||
| 248 | $self->{main_obj}->watch_unacked_counts( | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 15 | ▶ | level => $level, assumed => \%assumed, callback => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 15 | my ($error, $w, $tl_unacked_counts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 15 | $w->cancel(); ## immediately cancel the watcher to prevent multiple callbacks | |||||||||||||||||||||||||||||||||||||||||
| 252 | 15 | ▶ | if($error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $ret_future->reject($error, 1); | |||||||||||||||||||||||||||||||||||||||||
| 254 | }else { | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 15 | $ret_future->fulfill($tl_unacked_counts); | |||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 17 | ); | |||||||||||||||||||||||||||||||||||||||||
| 259 | 15 | return $ret_future; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 15 | ▶ | })->then(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 15 | my ($tl_unacked_counts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 15 | $responder->($self->{view}->response_json(200, {unacked_counts => $tl_unacked_counts})); | |||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | ▶ | })->catch(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 2 | my ($e, $is_normal_error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | ▶ | $responder->($self->{view}->response_json(($is_normal_error ? 500 : 400), {error => "$e"})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 17 | }); | |||||||||||||||||||||||||||||||||||||||||
| 267 | 17 | }; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 17 | ▶ | sub _handle_tl_index { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_tl_index: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 17 | my ($self, $req, $dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 17 | return $self->{view}->response_timeline(_get_timeline_name($dest)); | |||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 10 | ▶ | sub _handle_get_timeline_list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_get_timeline_list: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 10 | my ($self, $req, $dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 10 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 10 | my $responder = shift; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 10 | ▶ | Future::Q->try(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 10 | my $num_per_page = $self->{main_obj}->get_config('timeline_list_per_page'); | |||||||||||||||||||||||||||||||||||||||||
| 281 | my @timelines = grep { | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 112 | !$self->{main_obj}->get_timeline_config($_->name, "hidden") | |||||||||||||||||||||||||||||||||||||||||
| 283 | 10 | } $self->{main_obj}->get_all_timelines(); | |||||||||||||||||||||||||||||||||||||||||
| 284 | 10 | ▶ | if(@timelines == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | die "No visible timeline. Probably you must configure config.psgi to create a timeline."; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 10 | my $page_num = ceil(scalar(@timelines) / $num_per_page); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 10 | my $cur_page = 0; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 10 | my $query = $req->query_parameters; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 10 | ▶ | if(defined $query->{page}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 7 | ▶ | if(!looks_like_number($query->{page}) || $query->{page} < 0 || $query->{page} >= $page_num) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not &looks_like_number($$query{"page"}) or $$query{"page"} < 0 or $$query{"page"} >= $page_num
not &looks_like_number($$query{"page"}) or $$query{"page"} < 0
MC/DC100%3/3
not &looks_like_number($$query{"page"}) or $$query{"page"} < 0 or $$query{"page"} >= $page_num
not &looks_like_number($$query{"page"})
$$query{"page"} < 0
$$query{"page"} >= $page_num
Truth table100%4/4
not(A) or B or C
A= not &looks_like_number($$query{"page"})B= $$query{"page"} < 0C= $$query{"page"} >= $page_num
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 4 | die "Invalid page parameter\n"; | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | $cur_page = $query->{page}; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 6 | my @target_timelines = @timelines[($cur_page * $num_per_page) .. min(($cur_page+1) * $num_per_page - 1, $#timelines)]; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 24 | ▶ | return Future::Q->needs_all(map { future_of($_, "get_unacked_counts") } @target_timelines)->then(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 6 | my (@unacked_counts_list) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 299 | my @timeline_unacked_counts = map { | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 6 | +{ name => $target_timelines[$_]->name, counts => $unacked_counts_list[$_] } | |||||||||||||||||||||||||||||||||||||||||
| 301 | } 0 .. $#target_timelines; | ||||||||||||||||||||||||||||||||||||||||||
| 302 | $responder->( $self->{view}->response_timeline_list( | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 6 | timeline_unacked_counts => \@timeline_unacked_counts, | |||||||||||||||||||||||||||||||||||||||||
| 304 | total_page_num => $page_num, | ||||||||||||||||||||||||||||||||||||||||||
| 305 | cur_page => $cur_page | ||||||||||||||||||||||||||||||||||||||||||
| 306 | ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 6 | }); | |||||||||||||||||||||||||||||||||||||||||
| 308 | 4 | ▶ | })->catch(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 4 | my ($error, $is_normal_error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 310 | $responder->($self->{view}->response_error_html( | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 4 | ▶ | ($is_normal_error ? 500 : 400), $error | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | )); | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 10 | }); | |||||||||||||||||||||||||||||||||||||||||
| 314 | 10 | }; | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | __END__ | ||||||||||||||||||||||||||||||||||||||||||