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/;
18 my $MBOX_TITLE = 'title="download thread as gzipped mbox"';
21 # TODO: make these constants tunable
22 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
23 use constant MAX_TRUNC_LEN => 72;
24 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
25 use constant T_ANCHOR => '#u';
26 use constant INDENT => ' ';
28 *ascii_html = *PublicInbox::Hval::ascii_html;
30 my $enc_utf8 = find_encoding('UTF-8');
34 my ($ctx, $mime, $full_pfx, $footer) = @_;
35 if (defined $footer) {
36 $footer = "\n" . $footer;
40 headers_to_html_header($mime, $full_pfx, $ctx) .
41 multipart_text_as_html($mime, $full_pfx) .
42 '</pre><hr />' . PRE_WRAP .
43 html_footer($mime, 1, $full_pfx, $ctx) .
45 '</pre></body></html>';
49 my ($class, $mime, $full_pfx) = @_;
51 PRE_WRAP . 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 $more = 'permalink';
92 my $root_anchor = $state->{root_anchor} || '';
93 my $path = $root_anchor ? '../../' : '';
94 my $href = $mid->as_href;
95 my $irt = in_reply_to($header_obj);
96 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
99 my $t = $ctx->{flat} ? 'T' : 't';
100 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
102 if ($root_anchor eq $id) {
103 $subj = "<u\nid=\"u\">$subj</u>";
106 my $ts = _msg_date($mime);
107 my $rv = "<table\nsummary=l$level><tr>";
109 $rv .= '<td><pre>' . (INDENT x $level) . '</pre></td>';
111 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
112 $rv .= "<b\nid=\"$id\">$subj</b>\n";
113 $rv .= "- $from @ $ts UTC - ";
114 $rv .= "<a\nhref=\"#s$next\">next</a>";
116 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
118 $fh->write($rv .= "\n\n");
120 my ($fhref, $more_ref);
121 my $mhref = "${path}$href/";
123 # show full message if it's our root message
124 if ($root_anchor ne $id || ($level != 0 && !$ctx->{flat})) {
125 $fhref = "${path}$href/f/";
128 # scan through all parts, looking for displayable text
129 $mime->walk_parts(sub {
130 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
134 my $txt = "${path}$href/raw";
135 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
136 $rv .= html_footer($mime, 0, undef, $ctx);
139 unless (defined $parent_anchor) {
140 my $v = PublicInbox::Hval->new_msgid($irt, 1);
142 $parent_anchor = "${path}$v/";
144 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
146 if (my $pct = $state->{pct}) {
147 $rv .= " [$pct->{$mid_raw}%]";
150 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
153 $rv .= " [<b>threaded</b>|" .
154 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
158 $fh->write($rv .= '</pre></td></tr></table>');
162 my ($ctx, $foot, $srch) = @_;
163 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
166 # only private functions below.
168 sub emit_thread_html {
169 my ($cb, $ctx, $foot, $srch) = @_;
170 my $mid = $ctx->{mid};
171 my $res = $srch->get_thread($mid);
172 my $msgs = load_results($res);
173 my $nr = scalar @$msgs;
174 return missing_thread($cb, $ctx) if $nr == 0;
175 my $flat = $ctx->{flat};
181 root_anchor => anchor_for($mid),
185 require PublicInbox::GitCatFile;
186 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
188 pre_anchor_entry($seen, $_) for (@$msgs);
189 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
191 my $th = thread_results($msgs);
192 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
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\n$MBOX_TITLE\nhref=\"../t.mbox.gz\">mbox.gz</a>";
207 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
208 $cb->write("<hr />" . PRE_WRAP . $next . "\n\n". $foot .
209 "</pre></body></html>");
214 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
215 my $s = add_text_body($enc, $part, $part_nr, $fhref);
218 # drop the remainder of git patches, they're usually better
219 # to review when the full message is viewed
220 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
223 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
226 # kill any leading or trailing whitespace lines
231 # kill per-line trailing whitespace
232 $s =~ s/[ \t]+$//sgm;
233 $s .= "\n" unless $s =~ /\n\z/s;
239 my ($ct, $default) = @_;
240 $default ||= $enc_utf8;
241 defined $ct or return $default;
242 my $ct_parsed = parse_content_type($ct);
244 if (my $charset = $ct_parsed->{attributes}->{charset}) {
245 my $enc = find_encoding($charset);
252 sub multipart_text_as_html {
253 my ($mime, $full_pfx, $srch) = @_;
256 my $enc = enc_for($mime->header("Content-Type"));
258 # scan through all parts, looking for displayable text
259 $mime->walk_parts(sub {
261 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
267 sub add_filename_line {
271 $fn = $enc->decode($fn);
273 $pad x= ($len/2) if ($len > 0);
274 "$pad " . ascii_html($fn) . " $pad\n";
277 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
279 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
282 my ($link_map, $s) = @_;
285 # salt this, as this could be exploited to show
286 # links in the HTML which don't show up in the raw mail.
287 my $key = sha1_hex($url . $SALT);
288 $link_map->{$key} = $url;
295 my ($link_map, $s) = @_;
297 # Added "PI-LINK-" prefix to avoid false-positives on git commits
298 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
300 my $url = $link_map->{$key};
302 $url = ascii_html($url);
303 "<a\nhref=\"$url\">$url</a>";
305 # false positive or somebody tried to mess with us
313 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
316 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
319 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
321 $rv = ascii_html($rv);
322 return linkify_2(\%l, $rv);
325 # show a short snippet of quoted text and link to full version:
326 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
327 my $cur = join(' ', @$quot);
328 @$quot = split(/\s+/, $cur);
331 my $tmp = shift(@$quot);
332 my $len = length($tmp) + length($cur);
333 if ($len > MAX_TRUNC_LEN) {
338 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
340 $cur =~ s/ \z/ .../s;
341 $cur = ascii_html($cur);
343 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
345 # show everything in the full version with anchor from
346 # short version (see above)
348 my $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
350 $rv = ascii_html($rv);
351 return linkify_2(\%l, $rv) unless $do_anchor;
353 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
358 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
359 return '' if $part->subparts;
361 my $ct = $part->content_type;
362 # account for filter bugs...
363 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
367 my $enc = enc_for($ct, $enc_msg);
372 $s = $enc->decode($s);
373 my @lines = split(/^/m, $s);
377 my $fn = $part->filename;
378 defined($fn) or $fn = "part #" . ($$part_nr + 1);
379 $s .= add_filename_line($enc, $fn);
383 while (defined(my $cur = shift @lines)) {
385 # show the previously buffered quote inline
387 $s .= flush_quote(\@quot, \$n, $$part_nr,
388 $full_pfx, 0, $do_anchor);
393 $cur = linkify_1(\%l, $cur);
394 $cur = ascii_html($cur);
395 $s .= linkify_2(\%l, $cur);
401 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
404 $s .= "\n" unless $s =~ /\n\z/s;
409 sub headers_to_html_header {
410 my ($mime, $full_pfx, $ctx) = @_;
411 my $srch = $ctx->{srch} if $ctx;
414 my $header_obj = $mime->header_obj;
415 my $mid = $header_obj->header('Message-ID');
416 $mid = PublicInbox::Hval->new_msgid($mid);
417 foreach my $h (qw(From To Cc Subject Date)) {
418 my $v = $header_obj->header($h);
419 defined($v) && ($v ne '') or next;
420 $v = PublicInbox::Hval->new_oneline($v);
423 my @from = Email::Address->parse($v->raw);
424 $title[1] = ascii_html($from[0]->name);
425 } elsif ($h eq 'Subject') {
426 $title[0] = $v->as_html;
428 my $p = $full_pfx ? '' : '../';
429 $rv .= "$h: <a\nid=\"t\"\nhref=\"${p}t/#u\">";
430 $rv .= $v->as_html . "</a>\n";
434 $rv .= "$h: " . $v->as_html . "\n";
437 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
438 my $upfx = $full_pfx ? '' : '../';
439 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
442 if ($header_obj->header('In-Reply-To') ||
443 $header_obj->header('References')) {
444 $rv .= "<a\nhref=\"${upfx}t/#u\">" .
445 "References: [expand]</a>\n";
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) .
457 "</title>$atom</head><body>" . PRE_WRAP . $rv);
461 my ($dst, $ctx, $cur, $full_pfx) = @_;
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};
468 $$dst .= "\n[no followups, yet]\n";
469 return (undef, in_reply_to($cur));
471 my $upfx = $full_pfx ? '' : '../';
473 $$dst .= "\n\n~$nr messages in thread: ".
474 "(<a\nhref=\"${upfx}t/#u\">expand</a>)\n";
475 my $subj = $srch->subject_path($cur->header('Subject'));
476 my $parent = in_reply_to($cur);
478 seen => { $subj => 1 },
481 parent_cmp => defined $parent ? $parent : '',
486 for (thread_results(load_results($res))->rootset) {
487 inline_dump($dst, $state, $upfx, $_, 0);
489 ($state->{next_msg}, $state->{parent});
492 sub _parent_headers_nosrch {
493 my ($header_obj) = @_;
496 my $irt = in_reply_to($header_obj);
498 my $v = PublicInbox::Hval->new_msgid($irt, 1);
499 my $html = $v->as_html;
500 my $href = $v->as_href;
501 $rv .= "In-Reply-To: <";
502 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
505 my $refs = $header_obj->header('References');
507 # avoid redundant URLs wasting bandwidth
509 $seen{$irt} = 1 if defined $irt;
511 my @raw_refs = ($refs =~ /<([^>]+)>/g);
512 foreach my $ref (@raw_refs) {
515 push @refs, linkify_ref_nosrch($ref);
519 $rv .= 'References: '. join(' ', @refs) . "\n";
526 my ($mime, $standalone, $full_pfx, $ctx) = @_;
527 my %cc; # everyone else
528 my $to; # this is the From address
530 foreach my $h (qw(From To Cc)) {
531 my $v = $mime->header($h);
532 defined($v) && ($v ne '') or next;
533 my @addrs = Email::Address->parse($v);
534 foreach my $recip (@addrs) {
535 my $address = $recip->address;
536 my $dst = lc($address);
537 $cc{$dst} ||= $address;
541 Email::Address->purge_cache if $standalone;
543 my $subj = $mime->header('Subject') || '';
544 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
545 my $mid = $mime->header('Message-ID');
546 my $irt = uri_escape_utf8($mid);
548 $to = uri_escape_utf8($to);
549 $subj = uri_escape_utf8($subj);
551 my $cc = uri_escape_utf8(join(',', sort values %cc));
552 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
555 my $srch = $ctx->{srch} if $ctx;
556 my $upfx = $full_pfx ? '../' : '../../';
557 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
559 if ($srch && $standalone) {
560 $idx .= qq{ / follow: <a\nhref="t.atom">Atom feed</a>};
563 my ($next, $p) = thread_inline(\$idx, $ctx, $mime, $full_pfx);
565 $p = PublicInbox::Hval->new_oneline($p);
567 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
569 $irt = ' ' x length('parent ');
572 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
580 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
583 sub linkify_ref_nosrch {
584 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
585 my $html = $v->as_html;
586 my $href = $v->as_href;
587 "<<a\nhref=\"../$href/\">$html</a>>";
593 if ($id !~ /\A[a-f0-9]{40}\z/) {
594 $id = id_compress(mid_clean($id), 1);
599 sub thread_html_head {
600 my ($cb, $header, $state) = @_;
601 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
603 my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
605 $$cb->write("<html><head><title>$s</title>".
606 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
607 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
611 sub pre_anchor_entry {
612 my ($seen, $mime) = @_;
613 my $id = anchor_for($mime->header('Message-ID'));
614 $seen->{$id} = "#$id"; # save the anchor for children, later
618 my ($upfx, $mid) = @_;
619 # 'subject dummy' is used internally by Mail::Thread
620 return '[no common parent]' if ($mid eq 'subject dummy');
622 $mid = PublicInbox::Hval->new_msgid($mid);
623 my $href = $mid->as_href;
624 my $html = $mid->as_html;
625 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
629 my ($upfx, $mid, $level) = @_;
630 "<table\nsummary=ghost><tr><td>" .
631 (INDENT x $level) . "</td><td>" .
632 PRE_WRAP . ghost_parent($upfx, $mid) .
633 '</pre></td></table>';
637 my ($cb, $git, $state, $mime, $level) = @_;
639 # lazy load the full message from mini_mime:
641 my $path = mid2path(mid_clean($mime->header('Message-ID')));
642 Email::MIME->new($git->cat_file('HEAD:'.$path));
645 if ($state->{anchor_idx} == 0) {
646 thread_html_head($cb, $mime, $state);
649 if (my $ghost = delete $state->{ghost}) {
650 # n.b. ghost messages may only be parents, not children
651 foreach my $g (@$ghost) {
652 $$cb->write(ghost_table('../../', @$g));
655 index_entry($$cb, $mime, $level, $state);
660 my ($state, $node, $level) = @_;
661 my $ghost = $state->{ghost} ||= [];
662 push @$ghost, [ $node->messageid, $level ];
666 my ($cb, $git, $state, $node, $level) = @_;
668 if (my $mime = $node->message) {
669 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
670 __ghost_entry($state, $node, $level);
673 __ghost_entry($state, $node, $level);
676 thread_entry($cb, $git, $state, $node->child, $level + 1);
677 thread_entry($cb, $git, $state, $node->next, $level);
683 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
688 my $ts = eval { str2time($mime->header('Date')) };
689 defined($ts) ? $ts : 0;
693 my ($msgs, $nosubject) = @_;
694 require PublicInbox::Thread;
695 my $th = PublicInbox::Thread->new(@$msgs);
697 $Mail::Thread::nosubject = $nosubject;
699 $th->order(*sort_ts);
705 require PublicInbox::ExtMsg;
707 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
712 my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
713 POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
717 my ($dst, $state, $upfx, $mime, $level) = @_;
718 my $pfx = INDENT x ($level - 1);
719 my $dot = $level == 0 ? '' : '` ';
721 my $cur = $state->{cur};
722 my $mid = mid_clean($mime->header('Message-ID'));
723 my $f = $mime->header('X-PI-From');
724 my $d = _msg_date($mime);
725 $f = PublicInbox::Hval->new($f)->as_html;
726 $d = PublicInbox::Hval->new($d)->as_html;
727 my $attr = "$f @ $d";
728 $state->{first_level} ||= $level;
729 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
730 $state->{prev_attr} = $attr;
731 $attr = ' - ' . $attr;
732 $attr .= ' UTC' if $level >= $state->{first_level};
736 $state->{prev_level} = $level;
740 delete $state->{cur};
741 $$dst .= "$pfx$dot<b><a\nid=\"r\"\nhref=\"#t\">".
742 "[this message]</a></b>$attr\n";
747 $state->{next_msg} ||= $mid;
750 # Subject is never undef, this mail was loaded from
751 # our Xapian which would've resulted in '' if it were
752 # really missing (and Filter rejects empty subjects)
753 my $s = $mime->header('Subject');
754 my $h = $state->{srch}->subject_path($s);
755 if ($state->{seen}->{$h}) {
758 $state->{seen}->{$h} = 1;
759 $s = PublicInbox::Hval->new($s);
762 my $m = PublicInbox::Hval->new_msgid($mid);
763 $m = $upfx . '../' . $m->as_href . '/';
765 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a>$attr\n";
767 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f @ $d</a>\n";
772 my ($dst, $state, $upfx, $node, $level) = @_;
774 if (my $mime = $node->message) {
775 my $mid = mid_clean($mime->header('Message-ID'));
776 if ($mid eq $state->{parent_cmp}) {
777 $state->{parent} = $mid;
779 _inline_header($dst, $state, $upfx, $mime, $level);
781 my $dot = $level == 0 ? '' : '` ';
782 my $pfx = (INDENT x $level) . $dot;
784 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
786 inline_dump($dst, $state, $upfx, $node->child, $level+1);
787 inline_dump($dst, $state, $upfx, $node->next, $level);
792 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
793 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
799 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
800 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
804 # accumulate recent topics if search is supported
805 # returns 1 if done, undef if not
807 my ($state, $node, $level) = @_;
809 my $child_adjust = 1;
811 if (my $x = $node->message) {
815 $subj = $x->header('Subject');
816 $subj = $state->{srch}->subject_normalized($subj);
819 # kill "[PATCH v2]" etc. for summarization
820 unless ($level == 0) {
821 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
823 $topic = substr($topic, 0, 30);
825 if (++$state->{subjs}->{$topic} == 1) {
826 push @{$state->{order}}, [ $level, $subj, $topic ];
829 my $mid = mid_clean($x->header('Message-ID'));
831 my $ts = $x->header('X-PI-TS');
832 my $exist = $state->{latest}->{$topic};
833 if (!$exist || $exist->[1] < $ts) {
834 $state->{latest}->{$topic} = [ $mid, $ts ];
837 # ghost message, do not bump level
841 add_topic($state, $node->child, $level + $child_adjust);
842 add_topic($state, $node->next, $level);
847 my $order = $state->{order};
848 my $subjs = $state->{subjs};
849 my $latest = $state->{latest};
850 return "\n[No recent topics]</pre>" unless (scalar @$order);
855 while (defined(my $info = shift @$order)) {
856 my ($level, $subj, $topic) = @$info;
857 my $n = delete $subjs->{$topic};
858 my ($mid, $ts) = @{delete $latest->{$topic}};
859 $mid = PublicInbox::Hval->new($mid)->as_href;
860 $subj = PublicInbox::Hval->new($subj)->as_html;
861 $pfx = INDENT x ($level - 1);
862 my $nl = $level == $prev ? "\n" : '';
863 my $dot = $level == 0 ? '' : '` ';
864 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
867 $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
872 # $n isn't the total number of posts on the topic,
873 # just the number of posts in the current results
874 # window, so leave it unlabeled
878 if ($level == 0 || $attr ne $prev_attr) {
879 my $mbox = qq(<a\n$MBOX_TITLE\n) .
880 qq(href="$mid/t.mbox.gz">mbox.gz</a>);
881 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
882 $pfx .= INDENT if $level > 0;
883 $dst .= $pfx . $attr . $n . " - $mbox / $atom\n";
890 sub emit_index_topics {
891 my ($state, $fh) = @_;
892 my $off = $state->{ctx}->{cgi}->param('o');
893 $off = 0 unless defined $off;
894 $state->{order} = [];
895 $state->{subjs} = {};
896 $state->{latest} = {};
898 my %opts = ( offset => int $off, limit => $max * 4 );
899 while (scalar @{$state->{order}} < $max) {
900 my $res = $state->{srch}->query('', \%opts);
901 my $nr = scalar @{$res->{msgs}} or last;
903 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
904 add_topic($state, $_, 0);
906 $opts{offset} += $nr;
909 $fh->write(dump_topics($state));