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 qw/ascii_html/;
15 use PublicInbox::Linkify;
16 use PublicInbox::MID qw/mid_clean id_compress mid2path mid_mime/;
19 use constant INDENT => ' ';
20 use constant TCHILD => '` ';
21 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
23 my $enc_utf8 = find_encoding('UTF-8');
27 my ($ctx, $mime, $footer) = @_;
28 $footer = defined($footer) ? "\n$footer" : '';
29 my $hdr = $mime->header_obj;
30 headers_to_html_header($hdr, $ctx) .
31 multipart_text_as_html($mime) .
33 html_footer($hdr, 1, $ctx, 'R/') .
35 '</pre></body></html>';
38 # /$LISTNAME/$MESSAGE_ID/R/
40 my ($ctx, $hdr, $footer) = @_;
41 my $s = $hdr->header('Subject');
42 $s = '(no subject)' if (!defined $s) || ($s eq '');
43 my $f = $hdr->header('From');
44 $f = '' unless defined $f;
45 my $mid = $hdr->header_raw('Message-ID');
46 $mid = PublicInbox::Hval->new_msgid($mid);
47 my $t = ascii_html($s);
49 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
51 my ($arg, $link) = mailto_arg_link($hdr);
52 push @$arg, '/path/to/YOUR_REPLY';
54 "<html><head><title>replying to \"$t\"</title></head><body><pre>" .
55 "replying to message:\n\n" .
56 "Subject: <b>$t</b>\n" .
57 "From: ". ascii_html($f) .
58 "\nDate: " . ascii_html($hdr->header('Date')) .
59 "\nMessage-ID: <" . $mid->as_html . ">\n\n" .
60 "There are multiple ways to reply:\n\n" .
61 "* Save the following mbox file, import it into your mail client,\n" .
62 " and reply-to-all from there: <a\nhref=../raw>mbox</a>\n\n" .
63 "* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
64 " and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
65 "\tgit send-email \\\n\t\t" .
66 join(" \\ \n\t\t", @$arg ). "\n\n" .
67 qq( <a\nhref="$se_url">$se_url</a>\n\n) .
68 "* If your mail client supports setting the <b>In-Reply-To</b>" .
69 " header\n via mailto: links, try the " .
70 qq(<a\nhref="$link">mailto: link</a>\n) .
71 "\nFor context, the original <a\nhref=../>message</a> or " .
72 qq(<a\nhref="../t/#u">thread</a>) .
73 '</pre><hr /><pre>' . $footer . '</pre></body></html>';
77 my ($class, $mime) = @_;
79 # no <head> here for <style>...
80 PublicInbox::Hval::PRE . multipart_text_as_html($mime) . '</pre>';
85 my $irt = $hdr->header_raw('In-Reply-To');
87 return mid_clean($irt) if (defined $irt);
89 my $refs = $hdr->header_raw('References');
90 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
96 # this is already inside a <pre>
98 my ($mime, $level, $state) = @_;
99 my $midx = $state->{anchor_idx}++;
100 my $ctx = $state->{ctx};
101 my $srch = $ctx->{srch};
103 my $hdr = $mime->header_obj;
104 my $enc = enc_for($hdr->header("Content-Type"));
105 my $subj = $hdr->header('Subject');
107 my $mid_raw = mid_clean(mid_mime($mime));
108 my $id = anchor_for($mid_raw);
109 my $seen = $state->{seen};
110 $seen->{$id} = "#$id"; # save the anchor for children, later
112 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
113 my $from = $hdr->header('From');
114 my @from = Email::Address->parse($from);
115 $from = $from[0]->name;
117 my $root_anchor = $state->{root_anchor} || '';
118 my $path = $root_anchor ? '../../' : '';
119 my $href = $mid->as_href;
120 my $irt = in_reply_to($hdr);
121 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
123 $from = ascii_html($from);
124 $subj = ascii_html($subj);
125 $subj = "<a\nhref=\"${path}$href/\">$subj</a>";
126 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id;
128 my $ts = _msg_date($hdr);
129 my $rv = "<pre\nid=s$midx>";
130 $rv .= "<b\nid=$id>$subj</b>\n";
131 my $txt = "${path}$href/raw";
132 my $fh = $state->{fh};
133 $fh->write($rv .= "- $from @ $ts UTC (<a\nhref=\"$txt\">raw</a>)\n\n");
135 my $mhref = "${path}$href/";
137 # scan through all parts, looking for displayable text
138 $mime->walk_parts(sub {
139 index_walk($fh, $_[0], $enc, \$part_nr);
142 $rv = "\n" . html_footer($hdr, 0, $ctx, "$path$href/R");
145 unless (defined $parent_anchor) {
146 my $v = PublicInbox::Hval->new_msgid($irt, 1);
148 $parent_anchor = "${path}$v/";
150 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
152 if (my $pct = $state->{pct}) { # used by SearchView.pm
153 $rv .= " [relevance $pct->{$mid_raw}%]";
155 my $threaded = 'threaded';
159 $flat = "<b>$flat</b>";
160 $end = "\n"; # for lynx
162 $threaded = "<b>$threaded</b>";
164 $rv .= " [<a\nhref=\"${path}$href/t/#u\">$threaded</a>";
165 $rv .= "|<a\nhref=\"${path}$href/T/#u\">$flat</a>]$end";
167 $fh->write($rv .= '</pre>');
171 my ($ctx, $foot, $srch) = @_;
172 # $_[0] in sub is the Plack callback
173 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
176 # only private functions below.
178 sub emit_thread_html {
179 my ($res, $ctx, $foot, $srch) = @_;
180 my $mid = $ctx->{mid};
181 my $flat = $ctx->{flat};
182 my $msgs = load_results($srch->get_thread($mid, { asc => $flat }));
183 my $nr = scalar @$msgs;
184 return missing_thread($res, $ctx) if $nr == 0;
190 root_anchor => anchor_for($mid),
195 require PublicInbox::Git;
196 $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
198 pre_anchor_entry($seen, $_) for (@$msgs);
199 __thread_entry($state, $_, 0) for (@$msgs);
201 my $th = thread_results($msgs);
202 thread_entry($state, $_, 0) for $th->rootset;
203 if (my $max = $state->{cur_level}) {
205 ('</ul></li>' x ($max - 1)) . '</ul>');
208 Email::Address->purge_cache;
210 # there could be a race due to a message being deleted in git
211 # but still being in the Xapian index:
212 my $fh = delete $state->{fh} or return missing_thread($res, $ctx);
214 my $final_anchor = $state->{anchor_idx};
215 my $next = "<a\nid=s$final_anchor>";
216 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
217 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
218 $next .= "\ndownload thread: ";
219 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
220 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
221 $fh->write('<hr /><pre>' . $next . "\n\n".
222 $foot . '</pre></body></html>');
227 my ($fh, $part, $enc, $part_nr) = @_;
228 my $s = add_text_body($enc, $part, $part_nr);
232 $s .= "\n"; # ensure there's a trailing newline
238 my ($ct, $default) = @_;
239 $default ||= $enc_utf8;
240 defined $ct or return $default;
241 my $ct_parsed = parse_content_type($ct);
243 if (my $charset = $ct_parsed->{attributes}->{charset}) {
244 my $enc = find_encoding($charset);
251 sub multipart_text_as_html {
255 my $enc = enc_for($mime->header("Content-Type"));
257 # scan through all parts, looking for displayable text
258 $mime->walk_parts(sub {
260 $part = add_text_body($enc, $part, \$part_nr);
262 $rv .= "\n" if $part ne '';
268 sub add_filename_line {
272 $fn = $enc->decode($fn);
274 $pad x= ($len/2) if ($len > 0);
275 "$pad " . ascii_html($fn) . " $pad\n";
279 my ($s, $l, $quot, $part_nr) = @_;
281 # show everything in the full version with anchor from
282 # short version (see above)
283 my $rv = $l->linkify_1(join('', @$quot));
286 # we use a <div> here to allow users to specify their own
287 # color for quoted text
288 $rv = $l->linkify_2(ascii_html($rv));
289 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
293 my ($enc_msg, $part, $part_nr) = @_;
294 return '' if $part->subparts;
296 my $ct = $part->content_type;
297 # account for filter bugs...
298 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
302 my $enc = enc_for($ct, $enc_msg);
305 $s = $enc->decode($s);
306 my @lines = split(/^/m, $s);
310 my $fn = $part->filename;
311 defined($fn) or $fn = "part #" . ($$part_nr + 1);
312 $s .= add_filename_line($enc, $fn);
316 my $l = PublicInbox::Linkify->new;
317 while (defined(my $cur = shift @lines)) {
319 # show the previously buffered quote inline
320 flush_quote(\$s, $l, \@quot, $$part_nr) if @quot;
323 $cur = $l->linkify_1($cur);
324 $cur = ascii_html($cur);
325 $s .= $l->linkify_2($cur);
331 flush_quote(\$s, $l, \@quot, $$part_nr) if @quot;
334 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
335 $s =~ s/\A\n+//s; # kill leading blank lines
336 $s =~ s/\s+\z//s; # kill all trailing spaces (final "\n" added if ne '')
340 sub headers_to_html_header {
341 my ($hdr, $ctx) = @_;
342 my $srch = $ctx->{srch} if $ctx;
348 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
349 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
353 my $mid = $hdr->header_raw('Message-ID');
354 $mid = PublicInbox::Hval->new_msgid($mid);
355 foreach my $h (qw(From To Cc Subject Date)) {
356 my $v = $hdr->header($h);
357 defined($v) && ($v ne '') or next;
358 $v = PublicInbox::Hval->new($v);
361 my @from = Email::Address->parse($v->raw);
362 $title[1] = ascii_html($from[0]->name);
363 } elsif ($h eq 'Subject') {
364 $title[0] = $v->as_html;
366 $rv .= qq($h: <a\nhref="#r"\nid=t>);
367 $rv .= $v->as_html . "</a>\n";
371 $rv .= "$h: " . $v->as_html . "\n";
374 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
375 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
376 $rv .= _parent_headers($hdr, $srch);
379 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
380 PublicInbox::Hval::STYLE .
381 "</head><body><pre\nid=b>" . # anchor for body start
386 my ($dst, $ctx, $hdr, $tpfx) = @_;
387 my $srch = $ctx->{srch};
388 my $mid = mid_clean($hdr->header_raw('Message-ID'));
389 my $sres = $srch->get_thread($mid);
390 my $nr = $sres->{total};
391 my $expand = qq(<a\nhref="${tpfx}t/#u">expand</a> ) .
392 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
393 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
395 my $parent = in_reply_to($hdr);
397 if (defined $parent) {
398 $$dst .= "($expand)\n ";
399 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
401 $$dst .= "[no followups, yet] ($expand)\n";
403 $ctx->{next_msg} = undef;
404 $ctx->{parent_msg} = $parent;
408 $$dst .= "$nr+ messages in thread ($expand";
409 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
411 my $subj = $srch->subject_path($hdr->header('Subject'));
413 seen => { $subj => 1 },
419 for (thread_results(load_results($sres))->rootset) {
420 skel_dump($dst, $state, $tpfx, $_, 0);
422 $ctx->{next_msg} = $state->{next_msg};
423 $ctx->{parent_msg} = $parent;
426 sub _parent_headers {
427 my ($hdr, $srch) = @_;
430 my $irt = in_reply_to($hdr);
432 my $v = PublicInbox::Hval->new_msgid($irt, 1);
433 my $html = $v->as_html;
434 my $href = $v->as_href;
435 $rv .= "In-Reply-To: <";
436 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
439 # do not display References: if search is present,
440 # we show the thread skeleton at the bottom, instead.
443 my $refs = $hdr->header_raw('References');
445 # avoid redundant URLs wasting bandwidth
447 $seen{$irt} = 1 if defined $irt;
449 my @raw_refs = ($refs =~ /<([^>]+)>/g);
450 foreach my $ref (@raw_refs) {
453 push @refs, linkify_ref_nosrch($ref);
457 $rv .= 'References: '. join(' ', @refs) . "\n";
463 sub mailto_arg_link {
465 my %cc; # everyone else
466 my $to; # this is the From address
468 foreach my $h (qw(From To Cc)) {
469 my $v = $hdr->header($h);
470 defined($v) && ($v ne '') or next;
471 my @addrs = Email::Address->parse($v);
472 foreach my $recip (@addrs) {
473 my $address = $recip->address;
474 my $dst = lc($address);
475 $cc{$dst} ||= $address;
479 Email::Address->purge_cache;
482 my $subj = $hdr->header('Subject') || '';
483 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
484 my $mid = $hdr->header_raw('Message-ID');
485 push @arg, "--in-reply-to='" . ascii_html($mid) . "'";
486 my $irt = uri_escape_utf8($mid);
488 push @arg, '--to=' . ascii_html($to);
489 $to = uri_escape_utf8($to);
490 $subj = uri_escape_utf8($subj);
491 my $cc = join(',', sort values %cc);
492 push @arg, '--cc=' . ascii_html($cc);
493 $cc = uri_escape_utf8($cc);
494 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
501 my ($hdr, $standalone, $ctx, $rhref) = @_;
503 my $srch = $ctx->{srch} if $ctx;
506 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
510 thread_skel(\$idx, $ctx, $hdr, $tpfx);
511 my $p = $ctx->{parent_msg};
512 my $next = $ctx->{next_msg};
514 $p = PublicInbox::Hval->new_msgid($p);
516 $irt = "<a\nhref=\"$upfx$p/\"rel=prev>parent</a> ";
518 $irt = ' ' x length('parent ');
521 $irt .= "<a\nhref=\"$upfx$next/\"\nrel=next>next</a> ";
523 $irt .= ' ' x length('next ');
529 $irt . qq(<a\nhref="$rhref">reply</a>) . $idx;
532 sub linkify_ref_nosrch {
533 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
534 my $html = $v->as_html;
535 my $href = $v->as_href;
536 "<<a\nhref=\"../$href/\">$html</a>>";
542 if ($id !~ /\A[a-f0-9]{40}\z/) {
543 $id = id_compress(mid_clean($id), 1);
548 sub thread_html_head {
549 my ($hdr, $state) = @_;
550 my $res = delete $state->{res} or die "BUG: no Plack callback in {res}";
551 my $fh = $res->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
554 my $s = ascii_html($hdr->header('Subject'));
555 $fh->write("<html><head><title>$s</title>".
556 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
557 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
558 PublicInbox::Hval::STYLE .
562 sub pre_anchor_entry {
563 my ($seen, $mime) = @_;
564 my $id = anchor_for(mid_mime($mime));
565 $seen->{$id} = "#$id"; # save the anchor for children, later
569 my ($upfx, $mid) = @_;
570 # 'subject dummy' is used internally by Mail::Thread
571 return '[no common parent]' if ($mid eq 'subject dummy');
573 $mid = PublicInbox::Hval->new_msgid($mid);
574 my $href = $mid->as_href;
575 my $html = $mid->as_html;
576 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
579 sub thread_adj_level {
580 my ($state, $level) = @_;
582 my $max = $state->{cur_level};
584 return '' if $max == 0; # flat output
586 # reset existing lists
587 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
588 $state->{fh}->write($x . '</ul>');
589 $state->{cur_level} = 0;
592 if ($level == $max) { # continue existing list
593 $state->{fh}->write('<li>');
594 } elsif ($level < $max) {
595 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
596 $state->{fh}->write($x .= '<li>');
597 $state->{cur_level} = $level;
598 } else { # ($level > $max) # start a new level
599 $state->{cur_level} = $level;
600 $state->{fh}->write(($max ? '<li>' : '') . '<ul><li>');
606 my ($state, $upfx, $mid, $level) = @_;
607 my $end = '<pre>'. ghost_parent($upfx, $mid) . '</pre>';
608 $state->{fh}->write($end .= thread_adj_level($state, $level));
612 my ($state, $mime, $level) = @_;
614 # lazy load the full message from mini_mime:
616 my $path = mid2path(mid_clean(mid_mime($mime)));
617 Email::MIME->new($state->{ctx}->{git}->cat_file('HEAD:'.$path));
620 thread_html_head($mime, $state) if $state->{anchor_idx} == 0;
621 if (my $ghost = delete $state->{ghost}) {
622 # n.b. ghost messages may only be parents, not children
623 foreach my $g (@$ghost) {
624 ghost_flush($state, '../../', @$g);
627 my $end = thread_adj_level($state, $level);
628 index_entry($mime, $level, $state);
629 $state->{fh}->write($end) if $end;
636 INDENT x ($level - 1);
639 sub __ghost_prepare {
640 my ($state, $node, $level) = @_;
641 my $ghost = $state->{ghost} ||= [];
642 push @$ghost, [ $node->messageid, $level ];
646 my ($state, $node, $level) = @_;
648 if (my $mime = $node->message) {
649 unless (__thread_entry($state, $mime, $level)) {
650 __ghost_prepare($state, $node, $level);
653 __ghost_prepare($state, $node, $level);
656 thread_entry($state, $node->child, $level + 1);
657 thread_entry($state, $node->next, $level);
663 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
668 my $ts = eval { str2time($hdr->header('Date')) };
669 defined($ts) ? $ts : 0;
673 my ($msgs, $nosubject, $nosort) = @_;
674 require PublicInbox::Thread;
675 my $th = PublicInbox::Thread->new(@$msgs);
677 # WARNING! both these Mail::Thread knobs were found by inspecting
678 # the Mail::Thread 2.55 source code, and we have some monkey patches
679 # in PublicInbox::Thread to fix memory leaks. Since Mail::Thread
680 # appears unmaintained, I suppose it's safe to depend on these
683 $Mail::Thread::nosubject = $nosubject;
684 # Keep ghosts with only a single direct child:
685 $Mail::Thread::noprune = 1;
687 $th->order(*sort_ts) unless $nosort;
692 my ($res, $ctx) = @_;
693 require PublicInbox::ExtMsg;
695 $res->(PublicInbox::ExtMsg::ext_msg($ctx))
700 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
704 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
707 my ($dst, $state, $upfx, $hdr, $level) = @_;
709 my $cur = $state->{cur};
710 my $mid = mid_clean($hdr->header_raw('Message-ID'));
711 my $f = ascii_html($hdr->header('X-PI-From'));
712 my $d = _msg_date($hdr);
713 my $pfx = "$d " . indent_for($level) . th_pfx($level);
715 $state->{first_level} ||= $level;
717 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
718 $state->{prev_attr} = $attr;
722 $state->{prev_level} = $level;
726 delete $state->{cur};
727 $$dst .= "$pfx<b><a\nid=r\nhref=\"#t\">".
728 "$attr [this message]</a></b>\n";
733 $state->{next_msg} ||= $mid;
736 # Subject is never undef, this mail was loaded from
737 # our Xapian which would've resulted in '' if it were
738 # really missing (and Filter rejects empty subjects)
739 my $s = $hdr->header('Subject');
740 my $h = $state->{srch}->subject_path($s);
741 if ($state->{seen}->{$h}) {
744 $state->{seen}->{$h} = 1;
745 $s = PublicInbox::Hval->new($s);
748 my $m = PublicInbox::Hval->new_msgid($mid);
749 $m = $upfx . '../' . $m->as_href . '/';
750 $$dst .= "$pfx<a\nhref=\"$m\">";
751 $$dst .= defined($s) ? "$s</a> $f\n" : "$f</a>\n";
755 my ($dst, $state, $upfx, $node, $level) = @_;
757 if (my $mime = $node->message) {
758 my $hdr = $mime->header_obj;
759 my $mid = mid_clean($hdr->header_raw('Message-ID'));
760 _skel_header($dst, $state, $upfx, $hdr, $level);
762 my $mid = $node->messageid;
763 if ($mid eq 'subject dummy') {
764 $$dst .= "\t[no common parent]\n";
766 $$dst .= ' [not found] ';
767 $$dst .= indent_for($level) . th_pfx($level);
768 $mid = PublicInbox::Hval->new_msgid($mid);
769 my $href = "$upfx../" . $mid->as_href . '/';
770 my $html = $mid->as_html;
771 $$dst .= qq{<<a\nhref="$href">$html</a>>\n};
774 skel_dump($dst, $state, $upfx, $node->child, $level+1);
775 skel_dump($dst, $state, $upfx, $node->next, $level);
780 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
781 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
785 # accumulate recent topics if search is supported
786 # returns 1 if done, undef if not
788 my ($state, $node, $level) = @_;
790 my $child_adjust = 1;
792 if (my $x = $node->message) {
796 $subj = $x->header('Subject');
797 $subj = $state->{srch}->subject_normalized($subj);
799 if (++$state->{subjs}->{$subj} == 1) {
800 push @{$state->{order}}, [ $level, $subj ];
803 my $mid = mid_clean($x->header_raw('Message-ID'));
805 my $ts = $x->header('X-PI-TS');
806 my $exist = $state->{latest}->{$subj};
807 if (!$exist || $exist->[1] < $ts) {
808 $state->{latest}->{$subj} = [ $mid, $ts ];
811 # ghost message, do not bump level
815 add_topic($state, $node->child, $level + $child_adjust);
816 add_topic($state, $node->next, $level);
821 my $order = $state->{order};
822 my $subjs = $state->{subjs};
823 my $latest = $state->{latest};
824 my $fh = $state->{fh};
825 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
831 while (defined(my $info = shift @$order)) {
832 my ($level, $subj) = @$info;
833 my $n = delete $subjs->{$subj};
834 my ($mid, $ts) = @{delete $latest->{$subj}};
835 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
836 $subj = PublicInbox::Hval->new($subj)->as_html;
837 $pfx = indent_for($level);
838 my $nl = $level == $prev ? "\n" : '';
843 $cur ||= [ $ts, '' ];
844 $cur->[0] = $ts if $ts > $cur->[0];
845 $cur->[1] .= $nl . $pfx . th_pfx($level) .
846 "<a\nhref=\"$mid/t/#u\"><b>" .
847 $subj . "</b></a>\n";
850 my $attr = " $ts UTC";
852 # $n isn't the total number of posts on the topic,
853 # just the number of posts in the current results window
854 $n = $n == 1 ? '' : " ($n+ messages)";
856 if ($level == 0 || $attr ne $prev_attr) {
857 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
858 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
859 $pfx .= INDENT if $level > 0;
860 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
864 push @recent, $cur if $cur;
865 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
866 $fh->write(join('', @recent) . '</pre>');
869 sub emit_index_topics {
871 my $off = $state->{ctx}->{cgi}->param('o');
872 $off = 0 unless defined $off;
873 $state->{order} = [];
874 $state->{subjs} = {};
875 $state->{latest} = {};
877 my %opts = ( offset => int $off, limit => $max * 4 );
878 while (scalar @{$state->{order}} < $max) {
879 my $sres = $state->{srch}->query('', \%opts);
880 my $nr = scalar @{$sres->{msgs}} or last;
882 for (thread_results(load_results($sres), 1)->rootset) {
883 add_topic($state, $_, 0);
885 $opts{offset} += $nr;