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 T_ANCHOR => '#u';
25 use constant INDENT => ' ';
27 *ascii_html = *PublicInbox::Hval::ascii_html;
29 my $enc_utf8 = find_encoding('UTF-8');
33 my ($ctx, $mime, $full_pfx, $footer) = @_;
34 if (defined $footer) {
35 $footer = "\n" . $footer;
39 headers_to_html_header($mime, $full_pfx, $ctx) .
40 multipart_text_as_html($mime, $full_pfx) .
42 html_footer($mime, 1, $full_pfx, $ctx) .
44 '</pre></body></html>';
48 my ($class, $mime, $full_pfx) = @_;
50 # no <head> here for <style>...
51 PublicInbox::Hval::PRE .
52 multipart_text_as_html($mime, $full_pfx) . '</pre>';
56 my ($header_obj) = @_;
57 my $irt = $header_obj->header('In-Reply-To');
59 return mid_clean($irt) if (defined $irt);
61 my $refs = $header_obj->header('References');
62 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
68 # this is already inside a <pre>
70 my ($fh, $mime, $level, $state) = @_;
71 my $midx = $state->{anchor_idx}++;
72 my $ctx = $state->{ctx};
73 my $srch = $ctx->{srch};
74 my ($prev, $next) = ($midx - 1, $midx + 1);
76 my $enc = enc_for($mime->header("Content-Type"));
77 my $subj = $mime->header('Subject');
78 my $header_obj = $mime->header_obj;
80 my $mid_raw = mid_clean($header_obj->header('Message-ID'));
81 my $id = anchor_for($mid_raw);
82 my $seen = $state->{seen};
83 $seen->{$id} = "#$id"; # save the anchor for children, later
85 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
86 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
87 my @from = Email::Address->parse($from);
88 $from = $from[0]->name;
90 $from = PublicInbox::Hval->new_oneline($from)->as_html;
91 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
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>";
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/";
122 my $more = 'permalink';
124 # show full message if it's our root message
125 my $neq = $root_anchor ne $id;
126 if ($neq || ($neq && $level != 0 && !$ctx->{flat})) {
127 $fhref = "${path}$href/f/";
130 # scan through all parts, looking for displayable text
131 $mime->walk_parts(sub {
132 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
136 my $txt = "${path}$href/raw";
137 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
138 $rv .= html_footer($mime, 0, undef, $ctx);
141 unless (defined $parent_anchor) {
142 my $v = PublicInbox::Hval->new_msgid($irt, 1);
144 $parent_anchor = "${path}$v/";
146 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
148 if (my $pct = $state->{pct}) {
149 $rv .= " [$pct->{$mid_raw}%]";
152 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
155 $rv .= " [<b>threaded</b>|" .
156 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
160 $fh->write($rv .= '</pre></td></tr></table>');
164 my ($ctx, $foot, $srch) = @_;
165 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
168 # only private functions below.
170 sub emit_thread_html {
171 my ($cb, $ctx, $foot, $srch) = @_;
172 my $mid = $ctx->{mid};
173 my $res = $srch->get_thread($mid);
174 my $msgs = load_results($res);
175 my $nr = scalar @$msgs;
176 return missing_thread($cb, $ctx) if $nr == 0;
177 my $flat = $ctx->{flat};
183 root_anchor => anchor_for($mid),
187 require PublicInbox::Git;
188 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
190 pre_anchor_entry($seen, $_) for (@$msgs);
191 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
193 my $th = thread_results($msgs);
194 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
197 Email::Address->purge_cache;
199 # there could be a race due to a message being deleted in git
200 # but still being in the Xapian index:
201 return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
203 my $final_anchor = $state->{anchor_idx};
204 my $next = "<a\nid=s$final_anchor>";
205 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
206 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
207 $next .= "\ndownload thread: ";
208 $next .= "<a\n$MBOX_TITLE\nhref=\"../t.mbox.gz\">mbox.gz</a>";
209 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
210 $cb->write('<hr /><pre>' . $next . "\n\n".
211 $foot . '</pre></body></html>');
216 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
217 my $s = add_text_body($enc, $part, $part_nr, $fhref);
221 # drop the remainder of git patches, they're usually better
222 # to review when the full message is viewed
223 $s =~ s!^---+\n.*\z!!ms and $m = 1;
226 $s =~ s/^-- \n.*\z//ms and $m = 1;
227 $$more = "<b>More...</b>\n\n$$more" if $m;
230 # kill any leading or trailing whitespace lines
235 # kill per-line trailing whitespace
236 $s =~ s/[ \t]+$//sgm;
237 $s .= "\n" unless $s =~ /\n\z/s;
243 my ($ct, $default) = @_;
244 $default ||= $enc_utf8;
245 defined $ct or return $default;
246 my $ct_parsed = parse_content_type($ct);
248 if (my $charset = $ct_parsed->{attributes}->{charset}) {
249 my $enc = find_encoding($charset);
256 sub multipart_text_as_html {
257 my ($mime, $full_pfx, $srch) = @_;
260 my $enc = enc_for($mime->header("Content-Type"));
262 # scan through all parts, looking for displayable text
263 $mime->walk_parts(sub {
265 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
271 sub add_filename_line {
275 $fn = $enc->decode($fn);
277 $pad x= ($len/2) if ($len > 0);
278 "$pad " . ascii_html($fn) . " $pad\n";
281 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
283 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
286 my ($link_map, $s) = @_;
289 # salt this, as this could be exploited to show
290 # links in the HTML which don't show up in the raw mail.
291 my $key = sha1_hex($url . $SALT);
292 $link_map->{$key} = $url;
299 my ($link_map, $s) = @_;
301 # Added "PI-LINK-" prefix to avoid false-positives on git commits
302 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
304 my $url = $link_map->{$key};
306 $url = ascii_html($url);
307 "<a\nhref=\"$url\">$url</a>";
309 # false positive or somebody tried to mess with us
317 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
320 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
323 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
325 $rv = ascii_html($rv);
326 return linkify_2(\%l, $rv);
329 # show a short snippet of quoted text and link to full version:
330 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
331 my $cur = join(' ', @$quot);
332 @$quot = split(/\s+/, $cur);
335 my $tmp = shift(@$quot);
336 my $len = length($tmp) + length($cur);
337 if ($len > MAX_TRUNC_LEN) {
342 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
344 $cur =~ s/ \z/ .../s;
345 $cur = ascii_html($cur);
347 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
349 # show everything in the full version with anchor from
350 # short version (see above)
352 my $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
354 $rv = ascii_html($rv);
355 return linkify_2(\%l, $rv) unless $do_anchor;
357 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
362 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
363 return '' if $part->subparts;
365 my $ct = $part->content_type;
366 # account for filter bugs...
367 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
371 my $enc = enc_for($ct, $enc_msg);
376 $s = $enc->decode($s);
377 my @lines = split(/^/m, $s);
381 my $fn = $part->filename;
382 defined($fn) or $fn = "part #" . ($$part_nr + 1);
383 $s .= add_filename_line($enc, $fn);
387 while (defined(my $cur = shift @lines)) {
389 # show the previously buffered quote inline
391 $s .= flush_quote(\@quot, \$n, $$part_nr,
392 $full_pfx, 0, $do_anchor);
397 $cur = linkify_1(\%l, $cur);
398 $cur = ascii_html($cur);
399 $s .= linkify_2(\%l, $cur);
405 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
408 $s .= "\n" unless $s =~ /\n\z/s;
413 sub headers_to_html_header {
414 my ($mime, $full_pfx, $ctx) = @_;
415 my $srch = $ctx->{srch} if $ctx;
418 my $header_obj = $mime->header_obj;
419 my $mid = $header_obj->header('Message-ID');
420 $mid = PublicInbox::Hval->new_msgid($mid);
421 foreach my $h (qw(From To Cc Subject Date)) {
422 my $v = $header_obj->header($h);
423 defined($v) && ($v ne '') or next;
424 $v = PublicInbox::Hval->new_oneline($v);
427 my @from = Email::Address->parse($v->raw);
428 $title[1] = ascii_html($from[0]->name);
429 } elsif ($h eq 'Subject') {
430 $title[0] = $v->as_html;
432 $rv .= "$h: <b\nid=t>";
433 $rv .= $v->as_html . "</b>\n";
437 $rv .= "$h: " . $v->as_html . "\n";
440 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
441 my $upfx = $full_pfx ? '' : '../';
442 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
445 thread_inline(\$rv, $ctx, $mime, $upfx);
447 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
448 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
450 $rv .= _parent_headers_nosrch($header_obj);
455 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
456 PublicInbox::Hval::STYLE . "</head><body><pre>" . $rv);
460 my ($dst, $ctx, $cur, $upfx) = @_;
461 my $srch = $ctx->{srch};
462 my $mid = mid_clean($cur->header('Message-ID'));
463 my $res = $srch->get_thread($mid);
464 my $nr = $res->{total};
465 my $expand = "<a\nhref=\"${upfx}t/#u\">expand</a> " .
466 "/ <a\nhref=\"${upfx}t.mbox.gz\">mbox.gz</a>";
469 my $parent = in_reply_to($cur);
471 $$dst .= "[no followups, yet] ($expand)\n";
472 $ctx->{next_msg} = undef;
473 $ctx->{parent_msg} = $parent;
477 $$dst .= "~$nr messages (<a\nhref=\"#b\">skip</a> / " .
480 my $subj = $srch->subject_path($cur->header('Subject'));
482 seen => { $subj => 1 },
485 parent_cmp => defined $parent ? $parent : '',
490 for (thread_results(load_results($res))->rootset) {
491 inline_dump($dst, $state, $upfx, $_, 0);
493 $$dst .= "<a\nid=b></a>"; # anchor for body start
494 $ctx->{next_msg} = $state->{next_msg};
495 $ctx->{parent_msg} = $state->{parent};
498 sub _parent_headers_nosrch {
499 my ($header_obj) = @_;
502 my $irt = in_reply_to($header_obj);
504 my $v = PublicInbox::Hval->new_msgid($irt, 1);
505 my $html = $v->as_html;
506 my $href = $v->as_href;
507 $rv .= "In-Reply-To: <";
508 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
511 my $refs = $header_obj->header('References');
513 # avoid redundant URLs wasting bandwidth
515 $seen{$irt} = 1 if defined $irt;
517 my @raw_refs = ($refs =~ /<([^>]+)>/g);
518 foreach my $ref (@raw_refs) {
521 push @refs, linkify_ref_nosrch($ref);
525 $rv .= 'References: '. join(' ', @refs) . "\n";
532 my ($mime, $standalone, $full_pfx, $ctx) = @_;
533 my %cc; # everyone else
534 my $to; # this is the From address
536 foreach my $h (qw(From To Cc)) {
537 my $v = $mime->header($h);
538 defined($v) && ($v ne '') or next;
539 my @addrs = Email::Address->parse($v);
540 foreach my $recip (@addrs) {
541 my $address = $recip->address;
542 my $dst = lc($address);
543 $cc{$dst} ||= $address;
547 Email::Address->purge_cache if $standalone;
549 my $subj = $mime->header('Subject') || '';
550 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
551 my $mid = $mime->header('Message-ID');
552 my $irt = uri_escape_utf8($mid);
554 $to = uri_escape_utf8($to);
555 $subj = uri_escape_utf8($subj);
557 my $cc = uri_escape_utf8(join(',', sort values %cc));
558 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
561 my $srch = $ctx->{srch} if $ctx;
562 my $upfx = $full_pfx ? '../' : '../../';
563 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
565 if ($srch && $standalone) {
566 $idx .= qq{ / follow: <a\nhref="t.atom">Atom feed</a>\n};
569 my $p = $ctx->{parent_msg};
570 my $next = $ctx->{next_msg};
572 $p = PublicInbox::Hval->new_oneline($p);
574 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
576 $irt = ' ' x length('parent ');
579 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
581 $irt .= ' ' x length('next ');
584 $irt .= "<a\nhref=\"#r\">thread</a> ";
586 $irt .= ' ' x length('thread ');
592 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
595 sub linkify_ref_nosrch {
596 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
597 my $html = $v->as_html;
598 my $href = $v->as_href;
599 "<<a\nhref=\"../$href/\">$html</a>>";
605 if ($id !~ /\A[a-f0-9]{40}\z/) {
606 $id = id_compress(mid_clean($id), 1);
611 sub thread_html_head {
612 my ($cb, $header, $state) = @_;
613 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
615 my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
617 $$cb->write("<html><head><title>$s</title>".
618 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
619 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
620 PublicInbox::Hval::STYLE .
624 sub pre_anchor_entry {
625 my ($seen, $mime) = @_;
626 my $id = anchor_for($mime->header('Message-ID'));
627 $seen->{$id} = "#$id"; # save the anchor for children, later
631 my ($upfx, $mid) = @_;
632 # 'subject dummy' is used internally by Mail::Thread
633 return '[no common parent]' if ($mid eq 'subject dummy');
635 $mid = PublicInbox::Hval->new_msgid($mid);
636 my $href = $mid->as_href;
637 my $html = $mid->as_html;
638 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
642 my ($upfx, $mid, $level) = @_;
643 "<table\nsummary=ghost><tr><td>" .
644 (INDENT x $level) . '</td><td><pre>' .
645 ghost_parent($upfx, $mid) .
646 '</pre></td></table>';
650 my ($cb, $git, $state, $mime, $level) = @_;
652 # lazy load the full message from mini_mime:
654 my $path = mid2path(mid_clean($mime->header('Message-ID')));
655 Email::MIME->new($git->cat_file('HEAD:'.$path));
658 if ($state->{anchor_idx} == 0) {
659 thread_html_head($cb, $mime, $state);
662 if (my $ghost = delete $state->{ghost}) {
663 # n.b. ghost messages may only be parents, not children
664 foreach my $g (@$ghost) {
665 $$cb->write(ghost_table('../../', @$g));
668 index_entry($$cb, $mime, $level, $state);
673 my ($state, $node, $level) = @_;
674 my $ghost = $state->{ghost} ||= [];
675 push @$ghost, [ $node->messageid, $level ];
679 my ($cb, $git, $state, $node, $level) = @_;
681 if (my $mime = $node->message) {
682 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
683 __ghost_entry($state, $node, $level);
686 __ghost_entry($state, $node, $level);
689 thread_entry($cb, $git, $state, $node->child, $level + 1);
690 thread_entry($cb, $git, $state, $node->next, $level);
696 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
701 my $ts = eval { str2time($mime->header('Date')) };
702 defined($ts) ? $ts : 0;
706 my ($msgs, $nosubject) = @_;
707 require PublicInbox::Thread;
708 my $th = PublicInbox::Thread->new(@$msgs);
710 $Mail::Thread::nosubject = $nosubject;
712 $th->order(*sort_ts);
718 require PublicInbox::ExtMsg;
720 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
725 my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
729 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
732 my ($dst, $state, $upfx, $mime, $level) = @_;
733 my $dot = $level == 0 ? '' : '` ';
735 my $cur = $state->{cur};
736 my $mid = mid_clean($mime->header('Message-ID'));
737 my $f = $mime->header('X-PI-From');
738 my $d = _msg_date($mime);
739 $f = PublicInbox::Hval->new($f)->as_html;
740 $d = PublicInbox::Hval->new($d)->as_html;
741 my $pfx = ' ' . $d . ' ' . (INDENT x $level);
743 $state->{first_level} ||= $level;
745 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
746 $state->{prev_attr} = $attr;
750 $state->{prev_level} = $level;
754 delete $state->{cur};
755 $$dst .= "$pfx$dot<b><a\nid=r\nhref=\"#b\">".
756 "$attr [this message]</a></b>\n";
761 $state->{next_msg} ||= $mid;
764 # Subject is never undef, this mail was loaded from
765 # our Xapian which would've resulted in '' if it were
766 # really missing (and Filter rejects empty subjects)
767 my $s = $mime->header('Subject');
768 my $h = $state->{srch}->subject_path($s);
769 if ($state->{seen}->{$h}) {
772 $state->{seen}->{$h} = 1;
773 $s = PublicInbox::Hval->new($s);
776 my $m = PublicInbox::Hval->new_msgid($mid);
777 $m = $upfx . '../' . $m->as_href . '/';
779 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a> $attr\n";
781 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f</a>\n";
786 my ($dst, $state, $upfx, $node, $level) = @_;
788 if (my $mime = $node->message) {
789 my $mid = mid_clean($mime->header('Message-ID'));
790 if ($mid eq $state->{parent_cmp}) {
791 $state->{parent} = $mid;
793 _inline_header($dst, $state, $upfx, $mime, $level);
795 my $dot = $level == 0 ? '' : '` ';
796 my $pfx = (' ' x length(' 1970-01-01 13:37 ')).
797 (INDENT x $level) . $dot;
799 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
801 inline_dump($dst, $state, $upfx, $node->child, $level+1);
802 inline_dump($dst, $state, $upfx, $node->next, $level);
807 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
808 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
814 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
815 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
819 # accumulate recent topics if search is supported
820 # returns 1 if done, undef if not
822 my ($state, $node, $level) = @_;
824 my $child_adjust = 1;
826 if (my $x = $node->message) {
830 $subj = $x->header('Subject');
831 $subj = $state->{srch}->subject_normalized($subj);
834 # kill "[PATCH v2]" etc. for summarization
835 unless ($level == 0) {
836 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
839 if (++$state->{subjs}->{$topic} == 1) {
840 push @{$state->{order}}, [ $level, $subj, $topic ];
843 my $mid = mid_clean($x->header('Message-ID'));
845 my $ts = $x->header('X-PI-TS');
846 my $exist = $state->{latest}->{$topic};
847 if (!$exist || $exist->[1] < $ts) {
848 $state->{latest}->{$topic} = [ $mid, $ts ];
851 # ghost message, do not bump level
855 add_topic($state, $node->child, $level + $child_adjust);
856 add_topic($state, $node->next, $level);
861 my $order = $state->{order};
862 my $subjs = $state->{subjs};
863 my $latest = $state->{latest};
864 return "\n[No topics in range]</pre>" unless (scalar @$order);
869 while (defined(my $info = shift @$order)) {
870 my ($level, $subj, $topic) = @$info;
871 my $n = delete $subjs->{$topic};
872 my ($mid, $ts) = @{delete $latest->{$topic}};
873 $mid = PublicInbox::Hval->new($mid)->as_href;
874 $subj = PublicInbox::Hval->new($subj)->as_html;
875 $pfx = INDENT x ($level - 1);
876 my $nl = $level == $prev ? "\n" : '';
877 my $dot = $level == 0 ? '' : '` ';
878 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
886 # $n isn't the total number of posts on the topic,
887 # just the number of posts in the current results
888 # window, so leave it unlabeled
892 if ($level == 0 || $attr ne $prev_attr) {
893 my $mbox = qq(<a\n$MBOX_TITLE\n) .
894 qq(href="$mid/t.mbox.gz">mbox.gz</a>);
895 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
896 $pfx .= INDENT if $level > 0;
897 $dst .= $pfx . $attr . $n . " - $mbox / $atom\n";
904 sub emit_index_topics {
905 my ($state, $fh) = @_;
906 my $off = $state->{ctx}->{cgi}->param('o');
907 $off = 0 unless defined $off;
908 $state->{order} = [];
909 $state->{subjs} = {};
910 $state->{latest} = {};
912 my %opts = ( offset => int $off, limit => $max * 4 );
913 while (scalar @{$state->{order}} < $max) {
914 my $res = $state->{srch}->query('', \%opts);
915 my $nr = scalar @{$res->{msgs}} or last;
917 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
918 add_topic($state, $_, 0);
920 $opts{offset} += $nr;
923 $fh->write(dump_topics($state));