lib/App/teai/Dash/Mail.pm

filter: Statementstmt 16.2% 21 / 129 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 21.8% 7 / 32 Podpod 100% 2 / 2 totaltotal 10.3% 30 / 291 CC 56 SCAR 46.3
CC
56
Cov
9%
_send
380.0
__ANON__
72.0
_message
56.0
CRAP 102.1
« lib/App/teai/Session.pm ↑ Summary lib/App/teai/Bus.pm »
1 package App::teai::Dash::Mail;
2 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use App::teai::Err;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use App::teai::Address ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use App::teai::Bus ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use App::teai::Dash ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use App::teai::Wire ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.08';
10  
11 1 ▶BEGIN { *esc = \&App::teai::Dash::esc }
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my $SEQ = qr/\A[1-9][0-9]{0,15}\z/;
14  
15 0 ▶sub _err ($code, $msg) { eval { App::teai::Err::fail($code, $msg) }; $@ }
Subroutine _err: not called CC=1 SCAR=6.9
16  
17 0 ▶sub nav ($class) { ([ 'Compose', '/compose' ], [ 'Inbox', '/inbox' ]) }
Subroutine nav: not called CC=1 SCAR=6.9
Pod documented
18  
19 0 ▶sub routes ($class) {
Subroutine routes: not called CC=1 SCAR=6.9
Pod documented
20 return (
21 0 [ GET => qr{\A/compose\z}, \&_compose_form ],
22 [ POST => qr{\A/compose\z}, \&_compose ],
23 [ GET => qr{\A/inbox\z}, \&_inbox ],
24 [ POST => qr{\A/inbox/read\z}, \&_read ],
25 );
26 }
27  
28 0 ▶sub _message ($m, $reply = 0) {
Subroutine _message: not called CC=7 SCAR=40.3
29 my @head = ("#$m->{seq}", $m->{time} // (), $m->{from}, '->', $m->{to}, $m->{kind},
30 0 ▶ (map { defined $m->{$_} ? "$_=$m->{$_}" : () } qw(re thread via)));
Branch: 1 branch
BranchTrueFalseTotal
defined $m->{$_} ? : 0 0 0
Condition0%0/3
$m->{"time"} // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$m->{"time"} // ()
$m->{"time"} ()
Truth table0%0/3
A // B
A= $m->{"time"}B= ()
ABresult
1X1
011
000
31 return '<article id="m' . esc($m->{seq}) . '"><p class="head">' . esc("@head")
32 . ($reply && ($m->{kind} eq 'ask' || $m->{kind} eq 'msg')
33 ? ' <a href="/compose?re=' . esc($m->{seq}) . '">reply</a>' : '')
34 0 ▶ . '</p><pre>' . esc($m->{body}) . "</pre></article>\n";
Branch: 1 branch
BranchTrueFalseTotal
$reply && ($m->{'kind'} eq 'ask' || $m->{'kind'} eq 'msg') ? : 0 0 0
Condition0%0/6
$reply and $m->{"kind"} eq "ask" || $m->{"kind"} eq "msg"
!ll&&!rl&&r
000
$m->{"kind"} eq "ask" or $m->{"kind"} eq "msg"
l!l&&r!l&&!r
000
MC/DC0%0/3
$reply and $m->{"kind"} eq "ask" || $m->{"kind"} eq "msg"
$reply $m->{"kind"} eq "ask" $m->{"kind"} eq "msg"
Truth table0%0/4
A and B or C
A= $replyB= $m->{"kind"} eq "ask"C= $m->{"kind"} eq "msg"
ABCresult
0XX0
1000
11X1
1011
35 }
36  
37 0 ▶sub _compose_form ($c) {
Subroutine _compose_form: not called CC=4 SCAR=30.0
38 0 my $re = $c->param('re');
39 0 ▶ if (defined $re && length $re) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $re and length $re) 0 0 0
Condition0%0/3
defined $re and length $re
!ll&&!rl&&r
000
MC/DC0%0/2
defined $re and length $re
defined $re length $re
Truth table0%0/3
A and B
A= defined $reB= length $re
ABresult
0X0
100
111
40 0 ▶ return $c->fail(_err(2, "invalid seq '$re'")) unless $re =~ $SEQ;
Branch: 1 branch
BranchTrueFalseTotal
unless $re =~ /$SEQ/u 0 0 0
41 0 ▶ $c->bus->message($re + 0, $c->cb(sub ($m, $e = undef) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
42 0 ▶ return $c->fail($e) if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
43 0 $c->page(200, "Reply to #$re", _message($m)
44 . $c->form('/compose', { re => $re + 0 }, '<label>Reply <textarea name="body"></textarea></label>', 'send the reply'));
45 0 }));
46 0 return;
47 }
48 0 my $inner = '<label>To <input type="text" name="to" value="' . esc($c->param('to')) . '"'
49 . ' placeholder="session:ID, live:P, role:P/R or project:P"></label>'
50 . '<label>Kind <select name="kind"><option value="msg">msg</option>'
51 . '<option value="ask">ask: its reply comes to the inbox</option></select></label>'
52 . '<label>Thread <input type="text" name="thread" value="' . esc($c->param('thread')) . '"></label>'
53 . '<label>Body <textarea name="body"></textarea></label>';
54 0 $c->page(200, 'Compose', $c->form('/compose', {}, $inner, 'send'));
55 }
56  
57 0 ▶sub _sent ($c, $html) { $c->result(200, 'Sent', $html . qq{<p><a href="/inbox">inbox</a> <a href="/compose">compose</a></p>}) }
Subroutine _sent: not called CC=1 SCAR=6.9
58  
59 0 ▶sub _compose ($c) { $c->own_inbox(sub { _send($c) }) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _compose: not called CC=1 SCAR=6.9
60  
61 0 ▶sub _send ($c) {
Subroutine _send: not called CC=19 SCAR=59.4
62 0 my $bus = $c->bus;
63 0 ▶ my $body = ($c->param('body') // '') =~ s/\r\n?/\n/gr;
Condition0%0/2
$c->param("body") // ""
l!l
00
MC/DC0%0/1
$c->param("body") // ""
$c->param("body")
Truth table0%0/2
A
A= $c->param("body")
Aresult
11
00
64 0 my %base = (body => $body, project => $c->project, agent => 'shell', role => 'operator');
65 0 my $re = $c->param('re');
66 0 ▶ if (defined $re && length $re) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $re and length $re) 0 0 0
Condition0%0/3
defined $re and length $re
!ll&&!rl&&r
000
MC/DC0%0/2
defined $re and length $re
defined $re length $re
Truth table0%0/3
A and B
A= defined $reB= length $re
ABresult
0X0
100
111
67 0 ▶ return $c->fail(_err(2, "invalid seq '$re'")) unless $re =~ $SEQ;
Branch: 1 branch
BranchTrueFalseTotal
unless $re =~ /$SEQ/u 0 0 0
68 0 my $payload = { %base, kind => 'reply', re => $re + 0 };
69 0 ▶ eval { App::teai::Wire::encode_payload($payload); 1 } or return $c->fail($@);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { App::teai::Wire::encode_payload($payload); 1 } } or return $c->fail($@) 0 0 0
70 0 ▶ App::teai::Address::reply($bus, $re + 0, $payload, $c->operator, $c->cb(sub ($seq, $e = undef) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
71 0 ▶ return $c->fail_after($e) if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
72 0 _sent($c, '<p>' . esc("#$seq: the reply to #$re") . "</p>\n");
73 0 }));
74 0 return;
75 }
76 0 ▶ my $addr = eval { App::teai::Address::parse($c->param('to') // '') } or return $c->fail($@);
Branch: 1 branch
BranchTrueFalseTotal
my $addr = eval { do { App::teai::Address::parse($c->param("to") // "") } } or return $c->fail($@) 0 0 0
Condition0%0/2
$c->param("to") // ""
l!l
00
MC/DC0%0/1
$c->param("to") // ""
$c->param("to")
Truth table0%0/2
A
A= $c->param("to")
Aresult
11
00
77 0 ▶ my $kind = $c->param('kind') // 'msg';
Condition0%0/2
$c->param("kind") // "msg"
l!l
00
MC/DC0%0/1
$c->param("kind") // "msg"
$c->param("kind")
Truth table0%0/2
A
A= $c->param("kind")
Aresult
11
00
78 0 ▶ return $c->fail(_err(2, "invalid kind '$kind'")) unless $kind eq 'msg' || $kind eq 'ask';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "msg" or $kind eq "ask" 0 0 0
Condition0%0/3
$kind eq "msg" or $kind eq "ask"
l!l&&r!l&&!r
000
MC/DC0%0/2
$kind eq "msg" or $kind eq "ask"
$kind eq "msg" $kind eq "ask"
Truth table0%0/3
A or B
A= $kind eq "msg"B= $kind eq "ask"
ABresult
1X1
011
000
79 0 my $thread = $c->param('thread');
80 0 ▶ $thread = undef if defined $thread && !length $thread;
Branch: 1 branch
BranchTrueFalseTotal
if defined $thread and not length $thread 0 0 0
Condition0%0/3
defined $thread and not length $thread
!ll&&!rl&&r
000
MC/DC0%0/2
defined $thread and not length $thread
defined $thread not length $thread
Truth table0%0/3
A and not(B)
A= defined $threadB= not length $thread
ABresult
0X0
100
111
81 0 ▶ return $c->fail(_err(2, "invalid thread '$thread'")) if defined $thread && !App::teai::Wire::valid_id($thread);
Branch: 1 branch
BranchTrueFalseTotal
if defined $thread and not App::teai::Wire::valid_id($thread) 0 0 0
Condition0%0/3
defined $thread and not App::teai::Wire::valid_id($thread)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $thread and not App::teai::Wire::valid_id($thread)
defined $thread not App::teai::Wire::valid_id($thread)
Truth table0%0/3
A and not(B)
A= defined $threadB= not App::teai::Wire::valid_id($thread)
ABresult
0X0
100
111
82 0 ▶ my $fan = $addr->{type} eq 'live' || $addr->{type} eq 'role';
Condition0%0/3
$addr->{'type'} eq 'live' || $addr->{'type'} eq 'role'
l!l&&r!l&&!r
000
MC/DC0%0/2
$addr->{'type'} eq 'live' || $addr->{'type'} eq 'role'
$addr->{'type'} eq 'live' $addr->{'type'} eq 'role'
Truth table0%0/3
A || B
A= $addr->{'type'} eq 'live'B= $addr->{'type'} eq 'role'
ABresult
1X1
011
000
83 0 ▶ my $payload = { %base, kind => $kind, thread => $thread,
Branch: 1 branch
BranchTrueFalseTotal
$fan ? : 0 0 0
84 $fan ? (via => App::teai::Wire::address_text($addr)) : () };
85 0 ▶ eval { App::teai::Wire::encode_payload($payload); 1 } or return $c->fail($@);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { App::teai::Wire::encode_payload($payload); 1 } } or return $c->fail($@) 0 0 0
86 0 ▶ my $send = sub ($warn = undef) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
87 0 ▶ App::teai::Address::deliver($bus, $addr, $payload, $c->operator, $c->cb(sub ($copies, $e = undef) {
Subroutine __ANON__: not called CC=6 SCAR=37.4
88 0 ▶ return $c->fail_after($e) if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
89 0 ▶ my @lines = map { defined $_->{seq} ? "#$_->{seq} to $_->{to}" : "not sent to $_->{to}: $_->{error}" } @$copies;
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'seq'} ? : 0 0 0
90 0 my @notes = grep { defined } $warn, map { App::teai::Address::note($_) } @$copies;
91 0 ▶ push @notes, 'the reply comes to the inbox of ' . $c->operator if $kind eq 'ask';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "ask" 0 0 0
92 0 my $html = '<ul>' . join('', map { '<li>' . esc($_) . '</li>' } @lines) . "</ul>\n"
93 0 . join('', map { '<p class="note">' . esc($_) . "</p>\n" } @notes);
94 0 ▶ return _sent($c, $html) if grep { defined $_->{seq} } @$copies;
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_->{"seq"};} @$copies 0 0 0
95 0 $c->result(502, 'Not sent', $html);
96 0 }));
97 0 };
98 0 ▶ return $send->() unless $addr->{type} eq 'session';
Branch: 1 branch
BranchTrueFalseTotal
unless $addr->{"type"} eq "session" 0 0 0
99 0 App::teai::Address::inbox_note($bus, $addr->{id}, $c->cb($send));
100 }
101  
102 0 ▶sub _inbox ($c) { $c->own_inbox(sub { _list($c) }) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _inbox: not called CC=1 SCAR=6.9
103  
104 0 ▶sub _list ($c) {
Subroutine _list: not called CC=1 SCAR=6.9
105 0 my ($bus, $op) = ($c->bus, $c->operator);
106 0 ▶ $bus->consumer_info($op, $c->cb(sub ($info, $e = undef) {
Subroutine __ANON__: not called CC=8 SCAR=42.8
107 0 ▶ return $c->fail($e) if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
108 0 ▶ return $c->fail(_err(1, "no session $op")) unless $info;
Branch: 1 branch
BranchTrueFalseTotal
unless $info 0 0 0
109 0 ▶ my $floor = ref $info->{ack_floor} eq 'HASH' ? $info->{ack_floor}{stream_seq} // 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $info->{'ack_floor'} eq 'HASH' ? : 0 0 0
Condition0%0/2
$info->{"ack_floor"}{"stream_seq"} // 0
l!l
00
MC/DC0%0/1
$info->{"ack_floor"}{"stream_seq"} // 0
$info->{"ack_floor"}{"stream_seq"}
Truth table0%0/2
A
A= $info->{"ack_floor"}{"stream_seq"}
Aresult
11
00
110 0 ▶ my $pending = ($info->{num_pending} // 0) + ($info->{num_ack_pending} // 0);
Condition0%0/4
$info->{"num_pending"} // 0
l!l
00
$info->{"num_ack_pending"} // 0
l!l
00
MC/DC0%0/1
$info->{"num_pending"} // 0
$info->{"num_pending"}
MC/DC0%0/1
$info->{"num_ack_pending"} // 0
$info->{"num_ack_pending"}
Truth table0%0/2
A
A= $info->{"num_pending"}
Aresult
11
00
Truth table0%0/2
A
A= $info->{"num_ack_pending"}
Aresult
11
00
111 0 my @mail;
112 0 ▶ $c->hold(sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 $bus->scan(filter => "teai.s.$op.>", from => $floor + 1, label => 'dash inbox',
114 0 ▶ record => sub ($r) { push @mail, $r; 0 }, done => $done);
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 ▶ }, sub ($n, $err = undef) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
116 0 ▶ return $c->fail($err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 0 0
117 0 my $html = '<p class="pending">' . esc("pending $pending") . "</p>\n";
118 0 $html .= @mail ? join('', map { _message($_, 1) } @mail)
119 0 ▶ . $c->form('/inbox/read', { upto => $mail[-1]{seq} }, '', "mark these read") : "<p>no unread mail</p>\n";
Branch: 1 branch
BranchTrueFalseTotal
@mail ? : 0 0 0
120 0 $c->page(200, 'Inbox', $html, watch => "to:$op");
121 0 });
122 0 }));
123 }
124  
125 0 ▶sub _read ($c) {
Subroutine _read: not called CC=3 SCAR=24.8
126 0 my ($bus, $op) = ($c->bus, $c->operator);
127 0 ▶ my $upto = $c->param('upto') // '';
Condition0%0/2
$c->param("upto") // ""
l!l
00
MC/DC0%0/1
$c->param("upto") // ""
$c->param("upto")
Truth table0%0/2
A
A= $c->param("upto")
Aresult
11
00
128 0 ▶ return $c->fail(_err(2, "invalid seq '$upto'")) unless $upto =~ $SEQ;
Branch: 1 branch
BranchTrueFalseTotal
unless $upto =~ /$SEQ/u 0 0 0
129 0 my $lease;
130 $lease = $bus->collect(
131 session => $op, deadline => App::teai::Bus::now(), drain => 1,
132 0 ▶ keep => sub ($r) { $r->{seq} <= $upto },
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 ▶ $c->cb(sub ($res) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
134 0 ▶ if (!$res->{settle}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"settle"}) 0 0 0
135 0 undef $lease;
136 return $c->fail($res->{busy} ? _err(4, "session $op is being read by another process")
137 0 ▶ : _err(3, $res->{error} // 'cancelled'));
Branch: 1 branch
BranchTrueFalseTotal
$res->{'busy'} ? : 0 0 0
Condition0%0/2
$res->{"error"} // "cancelled"
l!l
00
MC/DC0%0/1
$res->{"error"} // "cancelled"
$res->{"error"}
Truth table0%0/2
A
A= $res->{"error"}
Aresult
11
00
138 }
139 0 my $n = @{ $res->{records} };
140 0 ▶ $res->{settle}->($res->{records}, $c->cb(sub ($e = undef) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
141 0 undef $lease;
142 0 ▶ return $c->fail_result(_err(3, App::teai::Bus::_text($e))) if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
143 0 $c->result(200, 'Inbox', '<p>' . esc("marked $n read") . qq{</p>\n<p><a href="/inbox">inbox</a></p>});
144 0 }));
145 0 }),
146 );
147 0 ▶ $c->on_close(sub { my $l = $lease; undef $lease; $l->cancel if $l });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $l 0 0 0
148 }
149  
150 1 1;
151 __END__
« lib/App/teai/Session.pm ↑ Summary lib/App/teai/Bus.pm »