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 PublicInbox::Hval qw/ascii_html/;
14 use PublicInbox::Linkify;
15 use PublicInbox::MID qw/mid_clean id_compress mid2path mid_mime/;
16 use PublicInbox::MsgIter;
19 use constant INDENT => ' ';
20 use constant TCHILD => '` ';
21 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
25 my ($ctx, $mime, $footer) = @_;
26 $footer = defined($footer) ? "\n$footer" : '';
27 my $hdr = $mime->header_obj;
28 headers_to_html_header($hdr, $ctx) .
29 multipart_text_as_html($mime, '') .
31 html_footer($hdr, 1, $ctx, 'R/') .
33 '</pre></body></html>';
36 # /$INBOX/$MESSAGE_ID/R/
38 my ($ctx, $hdr, $footer) = @_;
39 my $s = $hdr->header('Subject');
40 $s = '(no subject)' if (!defined $s) || ($s eq '');
41 my $f = $hdr->header('From');
42 $f = '' unless defined $f;
43 my $mid = $hdr->header_raw('Message-ID');
44 $mid = PublicInbox::Hval->new_msgid($mid);
45 my $t = ascii_html($s);
47 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
49 my ($arg, $link) = mailto_arg_link($hdr);
50 push @$arg, '/path/to/YOUR_REPLY';
52 "<html><head><title>replying to \"$t\"</title></head><body><pre>" .
53 "replying to message:\n\n" .
54 "Subject: <b>$t</b>\n" .
55 "From: ". ascii_html($f) .
56 "\nDate: " . ascii_html($hdr->header('Date')) .
57 "\nMessage-ID: <" . $mid->as_html . ">\n\n" .
58 "There are multiple ways to reply:\n\n" .
59 "* Save the following mbox file, import it into your mail client,\n" .
60 " and reply-to-all from there: <a\nhref=../raw>mbox</a>\n\n" .
61 "* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
62 " and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
63 "\tgit send-email \\\n\t\t" .
64 join(" \\ \n\t\t", @$arg ). "\n\n" .
65 qq( <a\nhref="$se_url">$se_url</a>\n\n) .
66 "* If your mail client supports setting the <b>In-Reply-To</b>" .
67 " header\n via mailto: links, try the " .
68 qq(<a\nhref="$link">mailto: link</a>\n) .
69 "\nFor context, the original <a\nhref=../>message</a> or " .
70 qq(<a\nhref="../t/#u">thread</a>) .
71 '</pre><hr /><pre>' . $footer . '</pre></body></html>';
76 my $irt = $hdr->header_raw('In-Reply-To');
78 return mid_clean($irt) if (defined $irt);
80 my $refs = $hdr->header_raw('References');
81 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
87 # this is already inside a <pre>
89 my ($mime, $level, $state) = @_;
90 my $midx = $state->{anchor_idx}++;
91 my $ctx = $state->{ctx};
92 my $srch = $ctx->{srch};
93 my $hdr = $mime->header_obj;
94 my $subj = $hdr->header('Subject');
96 my $mid_raw = mid_clean(mid_mime($mime));
97 my $id = anchor_for($mid_raw);
98 my $seen = $state->{seen};
99 $seen->{$id} = "#$id"; # save the anchor for children, later
101 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
102 my $from = $hdr->header('From');
103 my @from = Email::Address->parse($from);
104 $from = $from[0]->name;
106 my $root_anchor = $state->{root_anchor} || '';
107 my $path = $root_anchor ? '../../' : '';
108 my $href = $mid->as_href;
109 my $irt = in_reply_to($hdr);
110 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
112 $from = ascii_html($from);
113 $subj = ascii_html($subj);
114 $subj = "<a\nhref=\"${path}$href/\">$subj</a>";
115 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id;
117 my $ts = _msg_date($hdr);
118 my $rv = "<pre\nid=s$midx>";
119 $rv .= "<b\nid=$id>$subj</b>\n";
120 my $txt = "${path}$href/raw";
121 my $fh = $state->{fh};
122 $fh->write($rv .= "- $from @ $ts UTC (<a\nhref=\"$txt\">raw</a>)\n\n");
124 my $mhref = "${path}$href/";
126 # scan through all parts, looking for displayable text
127 msg_iter($mime, sub { index_walk($fh, $mhref, $_[0]) });
128 $rv = "\n" . html_footer($hdr, 0, $ctx, "$path$href/R/");
131 unless (defined $parent_anchor) {
132 my $v = PublicInbox::Hval->new_msgid($irt, 1);
134 $parent_anchor = "${path}$v/";
136 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
138 if (my $pct = $state->{pct}) { # used by SearchView.pm
139 $rv .= " [relevance $pct->{$mid_raw}%]";
141 my $threaded = 'threaded';
145 $flat = "<b>$flat</b>";
146 $end = "\n"; # for lynx
148 $threaded = "<b>$threaded</b>";
150 $rv .= " [<a\nhref=\"${path}$href/t/#u\">$threaded</a>";
151 $rv .= "|<a\nhref=\"${path}$href/T/#u\">$flat</a>]$end";
153 $fh->write($rv .= '</pre>');
157 my ($ctx, $foot, $srch) = @_;
158 # $_[0] in sub is the Plack callback
159 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
162 # only private functions below.
164 sub emit_thread_html {
165 my ($res, $ctx, $foot, $srch) = @_;
166 my $mid = $ctx->{mid};
167 my $flat = $ctx->{flat};
168 my $msgs = load_results($srch->get_thread($mid, { asc => $flat }));
169 my $nr = scalar @$msgs;
170 return missing_thread($res, $ctx) if $nr == 0;
176 root_anchor => anchor_for($mid),
181 require PublicInbox::Git;
182 $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
184 pre_anchor_entry($seen, $_) for (@$msgs);
185 __thread_entry($state, $_, 0) for (@$msgs);
187 my $th = thread_results($msgs);
188 thread_entry($state, $_, 0) for $th->rootset;
189 if (my $max = $state->{cur_level}) {
191 ('</ul></li>' x ($max - 1)) . '</ul>');
194 Email::Address->purge_cache;
196 # there could be a race due to a message being deleted in git
197 # but still being in the Xapian index:
198 my $fh = delete $state->{fh} or return missing_thread($res, $ctx);
200 my $final_anchor = $state->{anchor_idx};
201 my $next = "<a\nid=s$final_anchor>";
202 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
203 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
204 $next .= "\ndownload thread: ";
205 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
206 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
207 $fh->write('<hr /><pre>' . $next . "\n\n".
208 $foot . '</pre></body></html>');
213 my ($fh, $upfx, $p) = @_;
214 my $s = add_text_body($upfx, $p);
218 $s .= "\n"; # ensure there's a trailing newline
223 sub multipart_text_as_html {
224 my ($mime, $upfx) = @_;
227 # scan through all parts, looking for displayable text
228 msg_iter($mime, sub {
230 $p = add_text_body($upfx, $p);
232 $rv .= "\n" if $p ne '';
238 my ($s, $l, $quot) = @_;
240 # show everything in the full version with anchor from
241 # short version (see above)
242 my $rv = $l->linkify_1(join('', @$quot));
245 # we use a <div> here to allow users to specify their own
246 # color for quoted text
247 $rv = $l->linkify_2(ascii_html($rv));
248 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
251 sub attach_link ($$$$) {
252 my ($upfx, $ct, $p, $fn) = @_;
253 my ($part, $depth, @idx) = @$p;
254 my $nl = $idx[-1] > 1 ? "\n" : '';
255 my $idx = join('.', @idx);
256 my $size = bytes::length($part->body);
257 $ct ||= 'text/plain';
258 $ct =~ s/;.*//; # no attributes
259 $ct = ascii_html($ct);
260 my $desc = $part->header('Content-Description');
261 $desc = $fn unless defined $desc;
262 $desc = '' unless defined $desc;
263 $desc = ': '.$desc if $desc;
265 if (defined $fn && $fn =~ /\A[\w-]+\.[a-z0-9]+\z/) {
267 } elsif ($ct eq 'text/plain') {
272 qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx$desc --]\n) .
273 "[-- Type: $ct, Size: $size bytes --]</a>"
277 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
278 my ($part, $depth, @idx) = @$p;
279 my $ct = $part->content_type;
280 my $fn = $part->filename;
282 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
283 return attach_link($upfx, $ct, $p, $fn);
286 my $s = eval { $part->body_str };
288 # badly-encoded message? tell the world about it!
289 return attach_link($upfx, $ct, $p, $fn) if $@;
291 my @lines = split(/^/m, $s);
293 if (defined($fn) || $depth > 0) {
294 $s .= attach_link($upfx, $ct, $p, $fn);
298 my $l = PublicInbox::Linkify->new;
299 while (defined(my $cur = shift @lines)) {
301 # show the previously buffered quote inline
302 flush_quote(\$s, $l, \@quot) if @quot;
305 $cur = $l->linkify_1($cur);
306 $cur = ascii_html($cur);
307 $s .= $l->linkify_2($cur);
313 flush_quote(\$s, $l, \@quot) if @quot;
314 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
315 $s =~ s/\A\n+//s; # kill leading blank lines
316 $s =~ s/\s+\z//s; # kill all trailing spaces (final "\n" added if ne '')
320 sub headers_to_html_header {
321 my ($hdr, $ctx) = @_;
322 my $srch = $ctx->{srch} if $ctx;
328 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
329 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
333 my $mid = $hdr->header_raw('Message-ID');
334 $mid = PublicInbox::Hval->new_msgid($mid);
335 foreach my $h (qw(From To Cc Subject Date)) {
336 my $v = $hdr->header($h);
337 defined($v) && ($v ne '') or next;
338 $v = PublicInbox::Hval->new($v);
341 my @from = Email::Address->parse($v->raw);
342 $title[1] = ascii_html($from[0]->name);
343 } elsif ($h eq 'Subject') {
344 $title[0] = $v->as_html;
346 $rv .= qq($h: <a\nhref="#r"\nid=t>);
347 $rv .= $v->as_html . "</a>\n";
351 $rv .= "$h: " . $v->as_html . "\n";
354 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
355 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
356 $rv .= _parent_headers($hdr, $srch);
359 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
360 PublicInbox::Hval::STYLE .
361 "</head><body><pre\nid=b>" . # anchor for body start
366 my ($dst, $ctx, $hdr, $tpfx) = @_;
367 my $srch = $ctx->{srch};
368 my $mid = mid_clean($hdr->header_raw('Message-ID'));
369 my $sres = $srch->get_thread($mid);
370 my $nr = $sres->{total};
371 my $expand = qq(<a\nhref="${tpfx}t/#u">expand</a> ) .
372 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
373 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
375 my $parent = in_reply_to($hdr);
377 if (defined $parent) {
378 $$dst .= "($expand)\n ";
379 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
381 $$dst .= "[no followups, yet] ($expand)\n";
383 $ctx->{next_msg} = undef;
384 $ctx->{parent_msg} = $parent;
388 $$dst .= "$nr+ messages in thread ($expand";
389 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
391 my $subj = $srch->subject_path($hdr->header('Subject'));
393 seen => { $subj => 1 },
399 for (thread_results(load_results($sres))->rootset) {
400 skel_dump($dst, $state, $tpfx, $_, 0);
402 $ctx->{next_msg} = $state->{next_msg};
403 $ctx->{parent_msg} = $parent;
406 sub _parent_headers {
407 my ($hdr, $srch) = @_;
410 my $irt = in_reply_to($hdr);
412 my $v = PublicInbox::Hval->new_msgid($irt, 1);
413 my $html = $v->as_html;
414 my $href = $v->as_href;
415 $rv .= "In-Reply-To: <";
416 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
419 # do not display References: if search is present,
420 # we show the thread skeleton at the bottom, instead.
423 my $refs = $hdr->header_raw('References');
425 # avoid redundant URLs wasting bandwidth
427 $seen{$irt} = 1 if defined $irt;
429 my @raw_refs = ($refs =~ /<([^>]+)>/g);
430 foreach my $ref (@raw_refs) {
433 push @refs, linkify_ref_nosrch($ref);
437 $rv .= 'References: '. join(' ', @refs) . "\n";
443 sub mailto_arg_link {
445 my %cc; # everyone else
446 my $to; # this is the From address
448 foreach my $h (qw(From To Cc)) {
449 my $v = $hdr->header($h);
450 defined($v) && ($v ne '') or next;
451 my @addrs = Email::Address->parse($v);
452 foreach my $recip (@addrs) {
453 my $address = $recip->address;
454 my $dst = lc($address);
455 $cc{$dst} ||= $address;
459 Email::Address->purge_cache;
462 my $subj = $hdr->header('Subject') || '';
463 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
464 my $mid = $hdr->header_raw('Message-ID');
465 push @arg, "--in-reply-to='" . ascii_html($mid) . "'";
466 my $irt = uri_escape_utf8($mid);
468 push @arg, '--to=' . ascii_html($to);
469 $to = uri_escape_utf8($to);
470 $subj = uri_escape_utf8($subj);
471 my $cc = join(',', sort values %cc);
472 push @arg, '--cc=' . ascii_html($cc);
473 $cc = uri_escape_utf8($cc);
474 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
481 my ($hdr, $standalone, $ctx, $rhref) = @_;
483 my $srch = $ctx->{srch} if $ctx;
486 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
490 thread_skel(\$idx, $ctx, $hdr, $tpfx);
491 my $p = $ctx->{parent_msg};
492 my $next = $ctx->{next_msg};
494 $p = PublicInbox::Hval->new_msgid($p);
496 $irt = "<a\nhref=\"$upfx$p/\"rel=prev>parent</a> ";
498 $irt = ' ' x length('parent ');
501 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
502 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
504 $irt .= ' ' x length('next ');
510 $irt . qq(<a\nhref="$rhref">reply</a>) . $idx;
513 sub linkify_ref_nosrch {
514 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
515 my $html = $v->as_html;
516 my $href = $v->as_href;
517 "<<a\nhref=\"../$href/\">$html</a>>";
523 if ($id !~ /\A[a-f0-9]{40}\z/) {
524 $id = id_compress(mid_clean($id), 1);
529 sub thread_html_head {
530 my ($hdr, $state) = @_;
531 my $res = delete $state->{res} or die "BUG: no Plack callback in {res}";
532 my $fh = $res->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
535 my $s = ascii_html($hdr->header('Subject'));
536 $fh->write("<html><head><title>$s</title>".
537 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
538 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
539 PublicInbox::Hval::STYLE .
543 sub pre_anchor_entry {
544 my ($seen, $mime) = @_;
545 my $id = anchor_for(mid_mime($mime));
546 $seen->{$id} = "#$id"; # save the anchor for children, later
550 my ($upfx, $mid) = @_;
551 # 'subject dummy' is used internally by Mail::Thread
552 return '[no common parent]' if ($mid eq 'subject dummy');
554 $mid = PublicInbox::Hval->new_msgid($mid);
555 my $href = $mid->as_href;
556 my $html = $mid->as_html;
557 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
560 sub thread_adj_level {
561 my ($state, $level) = @_;
563 my $max = $state->{cur_level};
565 return '' if $max == 0; # flat output
567 # reset existing lists
568 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
569 $state->{fh}->write($x . '</ul>');
570 $state->{cur_level} = 0;
573 if ($level == $max) { # continue existing list
574 $state->{fh}->write('<li>');
575 } elsif ($level < $max) {
576 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
577 $state->{fh}->write($x .= '<li>');
578 $state->{cur_level} = $level;
579 } else { # ($level > $max) # start a new level
580 $state->{cur_level} = $level;
581 $state->{fh}->write(($max ? '<li>' : '') . '<ul><li>');
587 my ($state, $upfx, $mid, $level) = @_;
588 my $end = '<pre>'. ghost_parent($upfx, $mid) . '</pre>';
589 $state->{fh}->write($end .= thread_adj_level($state, $level));
593 my ($state, $mime, $level) = @_;
595 # lazy load the full message from mini_mime:
597 my $path = mid2path(mid_clean(mid_mime($mime)));
598 Email::MIME->new($state->{ctx}->{git}->cat_file('HEAD:'.$path));
601 thread_html_head($mime, $state) if $state->{anchor_idx} == 0;
602 if (my $ghost = delete $state->{ghost}) {
603 # n.b. ghost messages may only be parents, not children
604 foreach my $g (@$ghost) {
605 ghost_flush($state, '../../', @$g);
608 my $end = thread_adj_level($state, $level);
609 index_entry($mime, $level, $state);
610 $state->{fh}->write($end) if $end;
617 INDENT x ($level - 1);
620 sub __ghost_prepare {
621 my ($state, $node, $level) = @_;
622 my $ghost = $state->{ghost} ||= [];
623 push @$ghost, [ $node->messageid, $level ];
627 my ($state, $node, $level) = @_;
629 if (my $mime = $node->message) {
630 unless (__thread_entry($state, $mime, $level)) {
631 __ghost_prepare($state, $node, $level);
634 __ghost_prepare($state, $node, $level);
637 thread_entry($state, $node->child, $level + 1);
638 thread_entry($state, $node->next, $level);
644 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
649 my $ts = eval { str2time($hdr->header('Date')) };
650 defined($ts) ? $ts : 0;
655 require PublicInbox::Thread;
656 my $th = PublicInbox::Thread->new(@$msgs);
658 $th->order(*sort_ts);
663 my ($res, $ctx) = @_;
664 require PublicInbox::ExtMsg;
666 $res->(PublicInbox::ExtMsg::ext_msg($ctx))
671 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
675 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
678 my ($dst, $state, $upfx, $hdr, $level) = @_;
680 my $cur = $state->{cur};
681 my $mid = mid_clean($hdr->header_raw('Message-ID'));
682 my $f = ascii_html($hdr->header('X-PI-From'));
683 my $d = _msg_date($hdr);
684 my $pfx = "$d " . indent_for($level) . th_pfx($level);
686 $state->{first_level} ||= $level;
688 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
689 $state->{prev_attr} = $attr;
693 $state->{prev_level} = $level;
697 delete $state->{cur};
698 $$dst .= "$pfx<b><a\nid=r\nhref=\"#t\">".
699 "$attr [this message]</a></b>\n";
704 $state->{next_msg} ||= $mid;
707 # Subject is never undef, this mail was loaded from
708 # our Xapian which would've resulted in '' if it were
709 # really missing (and Filter rejects empty subjects)
710 my $s = $hdr->header('Subject');
711 my $h = $state->{srch}->subject_path($s);
712 if ($state->{seen}->{$h}) {
715 $state->{seen}->{$h} = 1;
716 $s = PublicInbox::Hval->new($s);
719 my $m = PublicInbox::Hval->new_msgid($mid);
720 $m = $upfx . '../' . $m->as_href . '/';
721 $$dst .= "$pfx<a\nhref=\"$m\">";
722 $$dst .= defined($s) ? "$s</a> $f\n" : "$f</a>\n";
726 my ($dst, $state, $upfx, $node, $level) = @_;
728 if (my $mime = $node->message) {
729 my $hdr = $mime->header_obj;
730 my $mid = mid_clean($hdr->header_raw('Message-ID'));
731 _skel_header($dst, $state, $upfx, $hdr, $level);
733 my $mid = $node->messageid;
734 if ($mid eq 'subject dummy') {
735 $$dst .= "\t[no common parent]\n";
737 $$dst .= ' [not found] ';
738 $$dst .= indent_for($level) . th_pfx($level);
739 $mid = PublicInbox::Hval->new_msgid($mid);
740 my $href = "$upfx../" . $mid->as_href . '/';
741 my $html = $mid->as_html;
742 $$dst .= qq{<<a\nhref="$href">$html</a>>\n};
745 skel_dump($dst, $state, $upfx, $node->child, $level+1);
746 skel_dump($dst, $state, $upfx, $node->next, $level);
751 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
752 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
756 # accumulate recent topics if search is supported
757 # returns 1 if done, undef if not
759 my ($state, $node, $level) = @_;
761 my $child_adjust = 1;
763 if (my $x = $node->message) {
767 $subj = $x->header('Subject');
768 $subj = $state->{srch}->subject_normalized($subj);
770 if (++$state->{subjs}->{$subj} == 1) {
771 push @{$state->{order}}, [ $level, $subj ];
774 my $mid = mid_clean($x->header_raw('Message-ID'));
776 my $ts = $x->header('X-PI-TS');
777 my $exist = $state->{latest}->{$subj};
778 if (!$exist || $exist->[1] < $ts) {
779 $state->{latest}->{$subj} = [ $mid, $ts ];
782 # ghost message, do not bump level
786 add_topic($state, $node->child, $level + $child_adjust);
787 add_topic($state, $node->next, $level);
792 my $order = $state->{order};
793 my $subjs = $state->{subjs};
794 my $latest = $state->{latest};
795 my $fh = $state->{fh};
796 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
802 while (defined(my $info = shift @$order)) {
803 my ($level, $subj) = @$info;
804 my $n = delete $subjs->{$subj};
805 my ($mid, $ts) = @{delete $latest->{$subj}};
806 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
807 $subj = PublicInbox::Hval->new($subj)->as_html;
808 $pfx = indent_for($level);
809 my $nl = $level == $prev ? "\n" : '';
814 $cur ||= [ $ts, '' ];
815 $cur->[0] = $ts if $ts > $cur->[0];
816 $cur->[1] .= $nl . $pfx . th_pfx($level) .
817 "<a\nhref=\"$mid/t/#u\"><b>" .
818 $subj . "</b></a>\n";
821 my $attr = " $ts UTC";
823 # $n isn't the total number of posts on the topic,
824 # just the number of posts in the current results window
825 $n = $n == 1 ? '' : " ($n+ messages)";
827 if ($level == 0 || $attr ne $prev_attr) {
828 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
829 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
830 $pfx .= INDENT if $level > 0;
831 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
835 push @recent, $cur if $cur;
836 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
837 $fh->write(join('', @recent) . '</pre>');
840 sub emit_index_topics {
842 my $off = $state->{ctx}->{cgi}->param('o');
843 $off = 0 unless defined $off;
844 $state->{order} = [];
845 $state->{subjs} = {};
846 $state->{latest} = {};
848 my %opts = ( offset => int $off, limit => $max * 4 );
849 while (scalar @{$state->{order}} < $max) {
850 my $sres = $state->{srch}->query('', \%opts);
851 my $nr = scalar @{$sres->{msgs}} or last;
853 for (thread_results(load_results($sres))->rootset) {
854 add_topic($state, $_, 0);
856 $opts{offset} += $nr;