1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # Used for displaying the HTML web interface.
5 # See Documentation/design_www.txt for this.
6 package PublicInbox::View;
9 use URI::Escape qw/uri_escape_utf8/;
10 use Date::Parse qw/str2time/;
11 use Encode qw/find_encoding/;
12 use Encode::MIME::Header;
13 use Email::MIME::ContentType qw/parse_content_type/;
14 use PublicInbox::Hval;
15 use PublicInbox::MID qw/mid_clean id_compress mid2path/;
16 use Digest::SHA qw/sha1_hex/;
20 # TODO: make these constants tunable
21 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
22 use constant MAX_TRUNC_LEN => 72;
23 use constant T_ANCHOR => '#u';
24 use constant INDENT => ' ';
26 *ascii_html = *PublicInbox::Hval::ascii_html;
28 my $enc_utf8 = find_encoding('UTF-8');
32 my ($ctx, $mime, $full_pfx, $footer) = @_;
33 if (defined $footer) {
34 $footer = "\n" . $footer;
38 headers_to_html_header($mime, $full_pfx, $ctx) .
39 multipart_text_as_html($mime, $full_pfx) .
41 html_footer($mime, 1, $full_pfx, $ctx) .
43 '</pre></body></html>';
47 my ($class, $mime, $full_pfx) = @_;
49 # no <head> here for <style>...
50 PublicInbox::Hval::PRE .
51 multipart_text_as_html($mime, $full_pfx) . '</pre>';
55 my ($header_obj) = @_;
56 my $irt = $header_obj->header('In-Reply-To');
58 return mid_clean($irt) if (defined $irt);
60 my $refs = $header_obj->header('References');
61 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
67 # this is already inside a <pre>
69 my ($fh, $mime, $level, $state) = @_;
70 my $midx = $state->{anchor_idx}++;
71 my $ctx = $state->{ctx};
72 my $srch = $ctx->{srch};
73 my ($prev, $next) = ($midx - 1, $midx + 1);
75 my $enc = enc_for($mime->header("Content-Type"));
76 my $subj = $mime->header('Subject');
77 my $header_obj = $mime->header_obj;
79 my $mid_raw = mid_clean($header_obj->header('Message-ID'));
80 my $id = anchor_for($mid_raw);
81 my $seen = $state->{seen};
82 $seen->{$id} = "#$id"; # save the anchor for children, later
84 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
85 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
86 my @from = Email::Address->parse($from);
87 $from = $from[0]->name;
89 $from = PublicInbox::Hval->new_oneline($from)->as_html;
90 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
91 my $root_anchor = $state->{root_anchor} || '';
92 my $path = $root_anchor ? '../../' : '';
93 my $href = $mid->as_href;
94 my $irt = in_reply_to($header_obj);
95 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
98 my $t = $ctx->{flat} ? 'T' : 't';
99 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
101 if ($root_anchor eq $id) {
102 $subj = "<u\nid=u>$subj</u>";
105 my $ts = _msg_date($mime);
106 my $rv = "<pre\nid=s$midx>";
107 $rv .= "<b\nid=$id>$subj</b>\n";
108 $rv .= "- $from @ $ts UTC - ";
109 $rv .= "<a\nhref=\"#s$next\">next</a>";
111 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
113 $fh->write($rv .= "\n\n");
115 my ($fhref, $more_ref);
116 my $mhref = "${path}$href/";
117 my $more = 'permalink';
119 # show full message if it's our root message
120 my $neq = $root_anchor ne $id;
121 if ($neq || ($neq && $level != 0 && !$ctx->{flat})) {
122 $fhref = "${path}$href/f/";
125 # scan through all parts, looking for displayable text
126 $mime->walk_parts(sub {
127 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
131 my $txt = "${path}$href/raw";
132 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
133 $rv .= html_footer($mime, 0, undef, $ctx);
136 unless (defined $parent_anchor) {
137 my $v = PublicInbox::Hval->new_msgid($irt, 1);
139 $parent_anchor = "${path}$v/";
141 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
143 if (my $pct = $state->{pct}) { # used by SearchView.pm
144 $rv .= " [relevance $pct->{$mid_raw}%]";
147 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
150 $rv .= " [<b>threaded</b>|" .
151 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
154 $fh->write($rv .= '</pre>');
158 my ($ctx, $foot, $srch) = @_;
159 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
162 # only private functions below.
164 sub emit_thread_html {
165 my ($cb, $ctx, $foot, $srch) = @_;
166 my $mid = $ctx->{mid};
167 my $res = $srch->get_thread($mid);
168 my $msgs = load_results($res);
169 my $nr = scalar @$msgs;
170 return missing_thread($cb, $ctx) if $nr == 0;
171 my $flat = $ctx->{flat};
177 root_anchor => anchor_for($mid),
182 require PublicInbox::Git;
183 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
185 pre_anchor_entry($seen, $_) for (@$msgs);
186 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
188 my $th = thread_results($msgs);
189 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
190 if (my $max = $state->{cur_level}) {
191 $cb->write(('</ul></li>' x ($max - 1)) . '</ul>');
195 Email::Address->purge_cache;
197 # there could be a race due to a message being deleted in git
198 # but still being in the Xapian index:
199 return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
201 my $final_anchor = $state->{anchor_idx};
202 my $next = "<a\nid=s$final_anchor>";
203 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
204 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
205 $next .= "\ndownload thread: ";
206 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
207 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
208 $cb->write('<hr /><pre>' . $next . "\n\n".
209 $foot . '</pre></body></html>');
214 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
215 my $s = add_text_body($enc, $part, $part_nr, $fhref);
219 # drop the remainder of git patches, they're usually better
220 # to review when the full message is viewed
221 $s =~ s!^---+\n.*\z!!ms and $m = 1;
224 $s =~ s/^-- \n.*\z//ms and $m = 1;
225 $$more = "<b>More...</b>\n\n$$more" if $m;
228 # kill any leading or trailing whitespace lines
233 # kill per-line trailing whitespace
234 $s =~ s/[ \t]+$//sgm;
235 $s .= "\n" unless $s =~ /\n\z/s;
241 my ($ct, $default) = @_;
242 $default ||= $enc_utf8;
243 defined $ct or return $default;
244 my $ct_parsed = parse_content_type($ct);
246 if (my $charset = $ct_parsed->{attributes}->{charset}) {
247 my $enc = find_encoding($charset);
254 sub multipart_text_as_html {
255 my ($mime, $full_pfx, $srch) = @_;
258 my $enc = enc_for($mime->header("Content-Type"));
260 # scan through all parts, looking for displayable text
261 $mime->walk_parts(sub {
263 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
269 sub add_filename_line {
273 $fn = $enc->decode($fn);
275 $pad x= ($len/2) if ($len > 0);
276 "$pad " . ascii_html($fn) . " $pad\n";
279 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
281 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
284 my ($link_map, $s) = @_;
287 # salt this, as this could be exploited to show
288 # links in the HTML which don't show up in the raw mail.
289 my $key = sha1_hex($url . $SALT);
290 $link_map->{$key} = $url;
297 my ($link_map, $s) = @_;
299 # Added "PI-LINK-" prefix to avoid false-positives on git commits
300 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
302 my $url = $link_map->{$key};
304 $url = ascii_html($url);
305 "<a\nhref=\"$url\">$url</a>";
307 # false positive or somebody tried to mess with us
315 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
317 # n.b.: do not use <blockquote> since it screws up alignment
318 # w.r.t. unquoted text. Repliers may rely on pre-formatted
319 # alignment to point out a certain word in quoted text.
321 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
324 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
326 $rv = ascii_html($rv);
327 return linkify_2(\%l, $rv);
330 # show a short snippet of quoted text and link to full version:
331 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
332 my $cur = join(' ', @$quot);
333 @$quot = split(/\s+/, $cur);
336 my $tmp = shift(@$quot);
337 my $len = length($tmp) + length($cur);
338 if ($len > MAX_TRUNC_LEN) {
343 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
345 $cur =~ s/ \z/ .../s;
346 $cur = ascii_html($cur);
348 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
350 # show everything in the full version with anchor from
351 # short version (see above)
353 my $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
355 $rv = ascii_html($rv);
356 return linkify_2(\%l, $rv) unless $do_anchor;
358 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
363 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
364 return '' if $part->subparts;
366 my $ct = $part->content_type;
367 # account for filter bugs...
368 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
372 my $enc = enc_for($ct, $enc_msg);
377 $s = $enc->decode($s);
378 my @lines = split(/^/m, $s);
382 my $fn = $part->filename;
383 defined($fn) or $fn = "part #" . ($$part_nr + 1);
384 $s .= add_filename_line($enc, $fn);
388 while (defined(my $cur = shift @lines)) {
390 # show the previously buffered quote inline
392 $s .= flush_quote(\@quot, \$n, $$part_nr,
393 $full_pfx, 0, $do_anchor);
398 $cur = linkify_1(\%l, $cur);
399 $cur = ascii_html($cur);
400 $s .= linkify_2(\%l, $cur);
406 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
409 $s .= "\n" unless $s =~ /\n\z/s;
414 sub headers_to_html_header {
415 my ($mime, $full_pfx, $ctx) = @_;
416 my $srch = $ctx->{srch} if $ctx;
419 my $header_obj = $mime->header_obj;
420 my $mid = $header_obj->header('Message-ID');
421 $mid = PublicInbox::Hval->new_msgid($mid);
422 foreach my $h (qw(From To Cc Subject Date)) {
423 my $v = $header_obj->header($h);
424 defined($v) && ($v ne '') or next;
425 $v = PublicInbox::Hval->new_oneline($v);
428 my @from = Email::Address->parse($v->raw);
429 $title[1] = ascii_html($from[0]->name);
430 } elsif ($h eq 'Subject') {
431 $title[0] = $v->as_html;
433 $rv .= "$h: <b\nid=t>";
434 $rv .= $v->as_html . "</b>\n";
438 $rv .= "$h: " . $v->as_html . "\n";
441 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
442 my $upfx = $full_pfx ? '' : '../';
443 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
446 thread_inline(\$rv, $ctx, $mime, $upfx);
448 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
449 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
451 $rv .= _parent_headers_nosrch($header_obj);
456 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
457 PublicInbox::Hval::STYLE . "</head><body><pre>" . $rv);
461 my ($dst, $ctx, $cur, $upfx) = @_;
462 my $srch = $ctx->{srch};
463 my $mid = mid_clean($cur->header('Message-ID'));
464 my $res = $srch->get_thread($mid);
465 my $nr = $res->{total};
466 my $expand = "<a\nhref=\"${upfx}t/#u\">expand</a> " .
467 "/ <a\nhref=\"${upfx}t.mbox.gz\">mbox.gz</a>";
470 my $parent = in_reply_to($cur);
472 $$dst .= "[no followups, yet] ($expand)\n";
473 $ctx->{next_msg} = undef;
474 $ctx->{parent_msg} = $parent;
478 $$dst .= "~$nr messages ($expand";
479 if ($nr > MAX_INLINE_QUOTED) {
480 $$dst .= qq! / <a\nhref="#b">[scroll down]</a>!;
484 my $subj = $srch->subject_path($cur->header('Subject'));
486 seen => { $subj => 1 },
489 parent_cmp => defined $parent ? $parent : '',
494 for (thread_results(load_results($res))->rootset) {
495 inline_dump($dst, $state, $upfx, $_, 0);
497 $$dst .= "<a\nid=b></a>"; # anchor for body start
498 $ctx->{next_msg} = $state->{next_msg};
499 $ctx->{parent_msg} = $state->{parent};
502 sub _parent_headers_nosrch {
503 my ($header_obj) = @_;
506 my $irt = in_reply_to($header_obj);
508 my $v = PublicInbox::Hval->new_msgid($irt, 1);
509 my $html = $v->as_html;
510 my $href = $v->as_href;
511 $rv .= "In-Reply-To: <";
512 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
515 my $refs = $header_obj->header('References');
517 # avoid redundant URLs wasting bandwidth
519 $seen{$irt} = 1 if defined $irt;
521 my @raw_refs = ($refs =~ /<([^>]+)>/g);
522 foreach my $ref (@raw_refs) {
525 push @refs, linkify_ref_nosrch($ref);
529 $rv .= 'References: '. join(' ', @refs) . "\n";
536 my ($mime, $standalone, $full_pfx, $ctx) = @_;
537 my %cc; # everyone else
538 my $to; # this is the From address
540 foreach my $h (qw(From To Cc)) {
541 my $v = $mime->header($h);
542 defined($v) && ($v ne '') or next;
543 my @addrs = Email::Address->parse($v);
544 foreach my $recip (@addrs) {
545 my $address = $recip->address;
546 my $dst = lc($address);
547 $cc{$dst} ||= $address;
551 Email::Address->purge_cache if $standalone;
553 my $subj = $mime->header('Subject') || '';
554 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
555 my $mid = $mime->header('Message-ID');
556 my $irt = uri_escape_utf8($mid);
558 $to = uri_escape_utf8($to);
559 $subj = uri_escape_utf8($subj);
561 my $cc = uri_escape_utf8(join(',', sort values %cc));
562 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
565 my $srch = $ctx->{srch} if $ctx;
566 my $upfx = $full_pfx ? '../' : '../../';
567 my $tpfx = $full_pfx ? '' : '../';
568 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
570 if ($srch && $standalone) {
571 $idx .= qq{ / follow: <a\nhref="${tpfx}t.atom">Atom feed</a>\n};
574 my $p = $ctx->{parent_msg};
575 my $next = $ctx->{next_msg};
577 $p = PublicInbox::Hval->new_oneline($p);
579 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
581 $irt = ' ' x length('parent ');
584 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
586 $irt .= ' ' x length('next ');
589 $irt .= "<a\nhref=\"${tpfx}t/#u\">thread</a> ";
591 $irt .= ' ' x length('thread ');
597 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
600 sub linkify_ref_nosrch {
601 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
602 my $html = $v->as_html;
603 my $href = $v->as_href;
604 "<<a\nhref=\"../$href/\">$html</a>>";
610 if ($id !~ /\A[a-f0-9]{40}\z/) {
611 $id = id_compress(mid_clean($id), 1);
616 sub thread_html_head {
617 my ($cb, $header, $state) = @_;
618 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
620 my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
622 $$cb->write("<html><head><title>$s</title>".
623 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
624 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
625 PublicInbox::Hval::STYLE .
629 sub pre_anchor_entry {
630 my ($seen, $mime) = @_;
631 my $id = anchor_for($mime->header('Message-ID'));
632 $seen->{$id} = "#$id"; # save the anchor for children, later
636 my ($upfx, $mid) = @_;
637 # 'subject dummy' is used internally by Mail::Thread
638 return '[no common parent]' if ($mid eq 'subject dummy');
640 $mid = PublicInbox::Hval->new_msgid($mid);
641 my $href = $mid->as_href;
642 my $html = $mid->as_html;
643 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
646 sub thread_adj_level {
647 my ($fh, $state, $level) = @_;
649 my $max = $state->{cur_level};
651 return '' if $max == 0; # flat output
653 # reset existing lists
654 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
655 $fh->write($x . '</ul>');
656 $state->{cur_level} = 0;
659 if ($level == $max) { # continue existing list
661 } elsif ($level < $max) {
662 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
663 $fh->write($x .= '<li>');
664 $state->{cur_level} = $level;
665 } else { # ($level > $max) # start a new level
666 $state->{cur_level} = $level;
667 $fh->write(($max ? '<li>' : '') . '<ul><li>');
673 my ($fh, $state, $upfx, $mid, $level) = @_;
675 my $end = thread_adj_level($fh, $state, $level);
676 $fh->write('<pre>'. ghost_parent($upfx, $mid) . '</pre>' . $end);
680 my ($cb, $git, $state, $mime, $level) = @_;
682 # lazy load the full message from mini_mime:
684 my $path = mid2path(mid_clean($mime->header('Message-ID')));
685 Email::MIME->new($git->cat_file('HEAD:'.$path));
688 if ($state->{anchor_idx} == 0) {
689 thread_html_head($cb, $mime, $state, $level);
692 if (my $ghost = delete $state->{ghost}) {
693 # n.b. ghost messages may only be parents, not children
694 foreach my $g (@$ghost) {
695 ghost_flush($fh, $state, '../../', @$g);
698 my $end = thread_adj_level($fh, $state, $level);
699 index_entry($fh, $mime, $level, $state);
700 $fh->write($end) if $end;
707 INDENT x ($level - 1);
710 sub __ghost_prepare {
711 my ($state, $node, $level) = @_;
712 my $ghost = $state->{ghost} ||= [];
713 push @$ghost, [ $node->messageid, $level ];
717 my ($cb, $git, $state, $node, $level) = @_;
719 if (my $mime = $node->message) {
720 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
721 __ghost_prepare($state, $node, $level);
724 __ghost_prepare($state, $node, $level);
727 thread_entry($cb, $git, $state, $node->child, $level + 1);
728 thread_entry($cb, $git, $state, $node->next, $level);
734 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
739 my $ts = eval { str2time($mime->header('Date')) };
740 defined($ts) ? $ts : 0;
744 my ($msgs, $nosubject) = @_;
745 require PublicInbox::Thread;
746 my $th = PublicInbox::Thread->new(@$msgs);
748 $Mail::Thread::nosubject = $nosubject;
750 $th->order(*sort_ts);
756 require PublicInbox::ExtMsg;
758 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
763 my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
767 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
770 my ($dst, $state, $upfx, $mime, $level) = @_;
771 my $dot = $level == 0 ? '' : '` ';
773 my $cur = $state->{cur};
774 my $mid = mid_clean($mime->header('Message-ID'));
775 my $f = $mime->header('X-PI-From');
776 my $d = _msg_date($mime);
777 $f = PublicInbox::Hval->new_oneline($f)->as_html;
778 my $pfx = ' ' . $d . ' ' . indent_for($level);
780 $state->{first_level} ||= $level;
782 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
783 $state->{prev_attr} = $attr;
787 $state->{prev_level} = $level;
791 delete $state->{cur};
792 $$dst .= "$pfx$dot<b><a\nid=r\nhref=\"#b\">".
793 "$attr [this message]</a></b>\n";
798 $state->{next_msg} ||= $mid;
801 # Subject is never undef, this mail was loaded from
802 # our Xapian which would've resulted in '' if it were
803 # really missing (and Filter rejects empty subjects)
804 my $s = $mime->header('Subject');
805 my $h = $state->{srch}->subject_path($s);
806 if ($state->{seen}->{$h}) {
809 $state->{seen}->{$h} = 1;
810 $s = PublicInbox::Hval->new($s);
813 my $m = PublicInbox::Hval->new_msgid($mid);
814 $m = $upfx . '../' . $m->as_href . '/';
816 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a> $attr\n";
818 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f</a>\n";
823 my ($dst, $state, $upfx, $node, $level) = @_;
825 if (my $mime = $node->message) {
826 my $mid = mid_clean($mime->header('Message-ID'));
827 if ($mid eq $state->{parent_cmp}) {
828 $state->{parent} = $mid;
830 _inline_header($dst, $state, $upfx, $mime, $level);
832 my $dot = $level == 0 ? '' : '` ';
833 my $pfx = (' ' x length(' 1970-01-01 13:37 ')).
834 indent_for($level) . $dot;
836 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
838 inline_dump($dst, $state, $upfx, $node->child, $level+1);
839 inline_dump($dst, $state, $upfx, $node->next, $level);
844 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
845 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
851 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
852 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
856 # accumulate recent topics if search is supported
857 # returns 1 if done, undef if not
859 my ($state, $node, $level) = @_;
861 my $child_adjust = 1;
863 if (my $x = $node->message) {
867 $subj = $x->header('Subject');
868 $subj = $state->{srch}->subject_normalized($subj);
871 # kill "[PATCH v2]" etc. for summarization
872 unless ($level == 0) {
873 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
876 if (++$state->{subjs}->{$topic} == 1) {
877 push @{$state->{order}}, [ $level, $subj, $topic ];
880 my $mid = mid_clean($x->header('Message-ID'));
882 my $ts = $x->header('X-PI-TS');
883 my $exist = $state->{latest}->{$topic};
884 if (!$exist || $exist->[1] < $ts) {
885 $state->{latest}->{$topic} = [ $mid, $ts ];
888 # ghost message, do not bump level
892 add_topic($state, $node->child, $level + $child_adjust);
893 add_topic($state, $node->next, $level);
898 my $order = $state->{order};
899 my $subjs = $state->{subjs};
900 my $latest = $state->{latest};
901 return "\n[No topics in range]</pre>" unless (scalar @$order);
906 while (defined(my $info = shift @$order)) {
907 my ($level, $subj, $topic) = @$info;
908 my $n = delete $subjs->{$topic};
909 my ($mid, $ts) = @{delete $latest->{$topic}};
910 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
911 $subj = PublicInbox::Hval->new($subj)->as_html;
912 $pfx = indent_for($level);
913 my $nl = $level == $prev ? "\n" : '';
914 my $dot = $level == 0 ? '' : '` ';
915 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
921 # $n isn't the total number of posts on the topic,
922 # just the number of posts in the current results
923 # window, so leave it unlabeled
924 $n = $n == 1 ? '' : " ($n+ messages)";
926 if ($level == 0 || $attr ne $prev_attr) {
927 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
928 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
929 $pfx .= INDENT if $level > 0;
930 $dst .= $pfx . $attr . $n . " - $mbox / $atom\n";
937 sub emit_index_topics {
938 my ($state, $fh) = @_;
939 my $off = $state->{ctx}->{cgi}->param('o');
940 $off = 0 unless defined $off;
941 $state->{order} = [];
942 $state->{subjs} = {};
943 $state->{latest} = {};
945 my %opts = ( offset => int $off, limit => $max * 4 );
946 while (scalar @{$state->{order}} < $max) {
947 my $res = $state->{srch}->query('', \%opts);
948 my $nr = scalar @{$res->{msgs}} or last;
950 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
951 add_topic($state, $_, 0);
953 $opts{offset} += $nr;
956 $fh->write(dump_topics($state));