| 1 | package App::teai::Dash::Threads; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use App::teai::Err; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use App::teai::Dash (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use App::teai::Wire (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | BEGIN { *esc = \&App::teai::Dash::esc } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | my $DAY = 86400; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | my $CUT = 65536; | |||||||||||||||||||||||||||||||||||||||||
| 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) { ([ 'Threads', '/threads' ]) } | ||||||||||||||||||||||||||||||||||||||||
|
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/threads\z}, \&_list ], | |||||||||||||||||||||||||||||||||||||||||
| 22 | [ GET => qr{\A/thread/([^/]+)\z}, \&_thread ], | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub _plural ($n, $word) { "$n $word" . ($n == 1 ? '' : 's') } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_plural: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub _body ($m) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_body: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my $text = $m->{body} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$m->{"body"} // ""
MC/DC0%0/1
$m->{"body"} // ""
$m->{"body"}
Truth table0%0/2
A
A= $m->{"body"}
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $octets = Encode::encode('UTF-8', $text); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | return '<pre>' . esc($text) . '</pre>' if length $octets <= $CUT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my $head = substr $octets, 0, $CUT; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $kept = Encode::decode('UTF-8', $head, Encode::FB_QUIET); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | my $where = defined $m->{thread} ? "; teai log --thread $m->{thread} shows it all" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | return '<pre>' . esc($kept) . '</pre><p class="note">' . esc('cut at 64 KiB of ' . length($octets) . " bytes$where") . '</p>'; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub article ($m, %o) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
article: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $page = $o{page} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$o{"page"} // ""
MC/DC0%0/1
$o{"page"} // ""
$o{"page"}
Truth table0%0/2
A
A= $o{"page"}
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | my @head = ( | ||||||||||||||||||||||||||||||||||||||||||
| 41 | esc("#$m->{seq}"), (defined $o{label} ? esc("$o{label}:") : ()), (defined $m->{time} ? esc($m->{time}) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 42 | esc($m->{from}), '->', esc($m->{to}), esc($m->{kind}), | ||||||||||||||||||||||||||||||||||||||||||
| 43 | (defined $m->{re} ? '<a href="' . esc("$page#m$m->{re}") . '">' . esc("re=$m->{re}") . '</a>' : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | (map { defined $m->{$_} ? esc("$_=$m->{$_}") : () } qw(thread via)), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my $html = '<article id="m' . esc($m->{seq}) . '"><p class="head">' . join(' ', @head) . '</p>' . _body($m); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if (my $replies = $o{replies}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | $html .= @$replies | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | ? '<p class="replies">replies: ' . join(', ', map { '<a href="#m' . esc($_) . '">' . esc("#$_") . '</a>' } @$replies) . '</p>' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | : $m->{kind} eq 'ask' ? '<p class="replies">no reply yet</p>' : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return "$html</article>\n"; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub _list ($c) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_list: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my (%t, %ask_of); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $record = sub ($r) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ($r->{kind} eq 'reply' && defined $r->{re} && defined(my $of = delete $ask_of{ $r->{re} })) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$r->{"kind"} eq "reply" and defined $r->{"re"} and defined(my $of = delete $ask_of{$r->{"re"}})
$r->{"kind"} eq "reply" and defined $r->{"re"}
MC/DC0%0/3
$r->{"kind"} eq "reply" and defined $r->{"re"} and defined(my $of = delete $ask_of{$r->{"re"}})
$r->{"kind"} eq "reply"
defined $r->{"re"}
defined(my $of = delete $ask_of{$r->{"re"}})
Truth table0%0/4
A and B and C
A= $r->{"kind"} eq "reply"B= defined $r->{"re"}C= defined(my $of = delete $ask_of{$r->{"re"}})
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $t{$of}{open}--; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $thread = $r->{thread} // return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$r->{"thread"} // (return 0)
MC/DC0%0/1
$r->{"thread"} // (return 0)
$r->{"thread"}
Truth table0%0/2
A
A= $r->{"thread"}
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my $e = $t{$thread} //= { n => 0, open => 0 }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$t{$thread} //= {"n", 0, "open", 0}
MC/DC0%0/1
$t{$thread} //= {"n", 0, "open", 0}
$t{$thread}
Truth table0%0/2
A
A= $t{$thread}
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $e->{n}++; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | @$e{qw(seq time)} = @$r{qw(seq time)}; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($r->{kind} eq 'ask') { $e->{open}++; $ask_of{ $r->{seq} } = $thread } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $from = { time => App::teai::Wire::iso_time((time - $DAY) . '000000000') }; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $c->hold(sub ($done) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $c->bus->scan(filter => 'teai.>', label => 'dash threads', from => $from, record => $record, done => $done); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | }, sub ($n, $e = undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return $c->fail($e) if defined $e; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my @names = sort { $t{$b}{seq} <=> $t{$a}{seq} } keys %t; | |||||||||||||||||||||||||||||||||||||||||
| 74 | my $rows = join '', map { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | '<tr id="thread-' . esc($_) . '"><td><a href="/thread/' . esc($_) . '">' . esc($_) . '</a></td><td>' | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | . esc(_plural($t{$_}{n}, 'message') . ", $t{$_}{open} open") . '</td><td>' . esc($t{$_}{time}) . "</td></tr>\n"; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } @names; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $html = '<p class="counts">' . esc(_plural(scalar @names, 'thread') . ' with mail in the last 24 hours') . "</p>\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | . ($rows ? qq{<div class="scroll"><table><thead><tr><th>thread</th><th>mail</th><th>last</th></tr></thead>\n} | ||||||||||||||||||||||||||||||||||||||||||
| 80 | . "<tbody>\n$rows</tbody></table></div>\n" : ''); | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $c->page(200, 'Threads', $html, watch => 'any'); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub _thread ($c, $thread) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_thread: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return $c->fail(_err(2, "invalid thread '$thread'")) unless App::teai::Wire::valid_id($thread); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $c->hold(sub ($done) { $c->bus->thread_log($thread, $done) }, sub ($mail, $e = undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | return $c->fail($e) if defined $e; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my %replies; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | push @{ $replies{ $_->{re} } }, $_->{seq} for grep { $_->{kind} eq 'reply' && defined $_->{re} } @$mail; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$_->{'kind'} eq 'reply' && defined $_->{'re'}
MC/DC0%0/2
$_->{'kind'} eq 'reply' && defined $_->{'re'}
$_->{'kind'} eq 'reply'
defined $_->{'re'}
Truth table0%0/3
A && B
A= $_->{'kind'} eq 'reply'B= defined $_->{'re'}
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my $html = @$mail ? join('', map { article($_, replies => $replies{ $_->{seq} } // []) } @$mail) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$replies{$_->{"seq"}} // []
MC/DC0%0/1
$replies{$_->{"seq"}} // []
$replies{$_->{"seq"}}
Truth table0%0/2
A
A= $replies{$_->{"seq"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | : "<p>no mail on this thread</p>\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $c->page(200, "Thread $thread", $html, watch => "thread:$thread"); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||||||||||||||||||||||