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;
17 use PublicInbox::Address;
20 use constant INDENT => ' ';
21 use constant TCHILD => '` ';
22 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
26 my ($ctx, $mime, $footer) = @_;
27 $footer = defined($footer) ? "\n$footer" : '';
28 my $hdr = $mime->header_obj;
29 headers_to_html_header($hdr, $ctx) .
30 multipart_text_as_html($mime, '') .
32 html_footer($hdr, 1, $ctx, 'R/') .
34 '</pre></body></html>';
37 # /$INBOX/$MESSAGE_ID/R/
39 my ($ctx, $hdr, $footer) = @_;
40 my $s = $hdr->header('Subject');
41 $s = '(no subject)' if (!defined $s) || ($s eq '');
42 my $f = $hdr->header('From');
43 $f = '' unless defined $f;
44 my $mid = $hdr->header_raw('Message-ID');
45 $mid = PublicInbox::Hval->new_msgid($mid);
46 my $t = ascii_html($s);
48 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
50 my ($arg, $link) = mailto_arg_link($hdr);
51 push @$arg, '/path/to/YOUR_REPLY';
53 "<html><head><title>replying to \"$t\"</title></head><body><pre>" .
54 "replying to message:\n\n" .
55 "Subject: <b>$t</b>\n" .
56 "From: ". ascii_html($f) .
57 "\nDate: " . ascii_html($hdr->header('Date')) .
58 "\nMessage-ID: <" . $mid->as_html . ">\n\n" .
59 "There are multiple ways to reply:\n\n" .
60 "* Save the following mbox file, import it into your mail client,\n" .
61 " and reply-to-all from there: <a\nhref=../raw>mbox</a>\n\n" .
62 "* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
63 " and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
64 "\tgit send-email \\\n\t\t" .
65 join(" \\ \n\t\t", @$arg ). "\n\n" .
66 qq( <a\nhref="$se_url">$se_url</a>\n\n) .
67 "* If your mail client supports setting the <b>In-Reply-To</b>" .
68 " header\n via mailto: links, try the " .
69 qq(<a\nhref="$link">mailto: link</a>\n) .
70 "\nFor context, the original <a\nhref=../>message</a> or " .
71 qq(<a\nhref="../t/#u">thread</a>) .
72 '</pre><hr /><pre>' . $footer . '</pre></body></html>';
77 my $irt = $hdr->header_raw('In-Reply-To');
79 return mid_clean($irt) if (defined $irt);
81 my $refs = $hdr->header_raw('References');
82 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
88 # this is already inside a <pre>
90 my ($mime, $level, $state) = @_;
91 my $midx = $state->{anchor_idx}++;
92 my $ctx = $state->{ctx};
93 my $srch = $ctx->{srch};
94 my $hdr = $mime->header_obj;
95 my $subj = $hdr->header('Subject');
97 my $mid_raw = mid_clean(mid_mime($mime));
98 my $id = anchor_for($mid_raw);
99 my $seen = $state->{seen};
100 $seen->{$id} = "#$id"; # save the anchor for children, later
102 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
103 my $from = PublicInbox::Address::from_name($hdr->header('From'));
105 my $root_anchor = $state->{root_anchor} || '';
106 my $path = $root_anchor ? '../../' : '';
107 my $href = $mid->as_href;
108 my $irt = in_reply_to($hdr);
109 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
111 $from = ascii_html($from);
112 $subj = ascii_html($subj);
113 $subj = "<a\nhref=\"${path}$href/\">$subj</a>";
114 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id;
116 my $ts = _msg_date($hdr);
117 my $rv = "<pre\nid=s$midx>";
118 $rv .= "<b\nid=$id>$subj</b>\n";
119 my $txt = "${path}$href/raw";
120 my $fh = $state->{fh};
121 $fh->write($rv .= "- $from @ $ts UTC (<a\nhref=\"$txt\">raw</a>)\n\n");
123 my $mhref = "${path}$href/";
125 # scan through all parts, looking for displayable text
126 msg_iter($mime, sub { index_walk($fh, $mhref, $_[0]) });
127 $rv = "\n" . html_footer($hdr, 0, $ctx, "$path$href/R/");
130 unless (defined $parent_anchor) {
131 my $v = PublicInbox::Hval->new_msgid($irt, 1);
133 $parent_anchor = "${path}$v/";
135 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
137 if (my $pct = $state->{pct}) { # used by SearchView.pm
138 $rv .= " [relevance $pct->{$mid_raw}%]";
140 my $threaded = 'threaded';
144 $flat = "<b>$flat</b>";
145 $end = "\n"; # for lynx
147 $threaded = "<b>$threaded</b>";
149 $rv .= " [<a\nhref=\"${path}$href/t/#u\">$threaded</a>";
150 $rv .= "|<a\nhref=\"${path}$href/T/#u\">$flat</a>]$end";
152 $fh->write($rv .= '</pre>');
156 my ($ctx, $foot, $srch) = @_;
157 # $_[0] in sub is the Plack callback
158 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
161 # only private functions below.
163 sub emit_thread_html {
164 my ($res, $ctx, $foot, $srch) = @_;
165 my $mid = $ctx->{mid};
166 my $flat = $ctx->{flat};
167 my $msgs = load_results($srch->get_thread($mid, { asc => $flat }));
168 my $nr = scalar @$msgs;
169 return missing_thread($res, $ctx) if $nr == 0;
175 root_anchor => anchor_for($mid),
180 require PublicInbox::Git;
181 $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
183 pre_anchor_entry($seen, $_) for (@$msgs);
184 __thread_entry($state, $_, 0) for (@$msgs);
186 my $th = thread_results($msgs);
187 thread_entry($state, $_, 0) for $th->rootset;
188 if (my $max = $state->{cur_level}) {
190 ('</ul></li>' x ($max - 1)) . '</ul>');
194 # there could be a race due to a message being deleted in git
195 # but still being in the Xapian index:
196 my $fh = delete $state->{fh} or return missing_thread($res, $ctx);
198 my $final_anchor = $state->{anchor_idx};
199 my $next = "<a\nid=s$final_anchor>";
200 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
201 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
202 $next .= "\ndownload thread: ";
203 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
204 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
205 $fh->write('<hr /><pre>' . $next . "\n\n".
206 $foot . '</pre></body></html>');
211 my ($fh, $upfx, $p) = @_;
212 my $s = add_text_body($upfx, $p);
216 $s .= "\n"; # ensure there's a trailing newline
221 sub multipart_text_as_html {
222 my ($mime, $upfx) = @_;
225 # scan through all parts, looking for displayable text
226 msg_iter($mime, sub {
228 $p = add_text_body($upfx, $p);
230 $rv .= "\n" if $p ne '';
236 my ($s, $l, $quot) = @_;
238 # show everything in the full version with anchor from
239 # short version (see above)
240 my $rv = $l->linkify_1(join('', @$quot));
243 # we use a <div> here to allow users to specify their own
244 # color for quoted text
245 $rv = $l->linkify_2(ascii_html($rv));
246 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
249 sub attach_link ($$$$) {
250 my ($upfx, $ct, $p, $fn) = @_;
251 my ($part, $depth, @idx) = @$p;
252 my $nl = $idx[-1] > 1 ? "\n" : '';
253 my $idx = join('.', @idx);
254 my $size = bytes::length($part->body);
255 $ct ||= 'text/plain';
256 $ct =~ s/;.*//; # no attributes
257 $ct = ascii_html($ct);
258 my $desc = $part->header('Content-Description');
259 $desc = $fn unless defined $desc;
260 $desc = '' unless defined $desc;
262 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
264 } elsif ($ct eq 'text/plain') {
269 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
270 my $ts = "Type: $ct, Size: $size bytes";
271 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
272 join('', @ret, '</a>');
276 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
277 my ($part, $depth, @idx) = @$p;
278 my $ct = $part->content_type;
279 my $fn = $part->filename;
281 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
282 return attach_link($upfx, $ct, $p, $fn);
285 my $s = eval { $part->body_str };
287 # badly-encoded message? tell the world about it!
288 return attach_link($upfx, $ct, $p, $fn) if $@;
290 my @lines = split(/^/m, $s);
292 if (defined($fn) || $depth > 0) {
293 $s .= attach_link($upfx, $ct, $p, $fn);
297 my $l = PublicInbox::Linkify->new;
298 while (defined(my $cur = shift @lines)) {
300 # show the previously buffered quote inline
301 flush_quote(\$s, $l, \@quot) if @quot;
304 $cur = $l->linkify_1($cur);
305 $cur = ascii_html($cur);
306 $s .= $l->linkify_2($cur);
312 flush_quote(\$s, $l, \@quot) if @quot;
313 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
314 $s =~ s/\A\n+//s; # kill leading blank lines
315 $s =~ s/\s+\z//s; # kill all trailing spaces (final "\n" added if ne '')
319 sub headers_to_html_header {
320 my ($hdr, $ctx) = @_;
321 my $srch = $ctx->{srch} if $ctx;
327 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
328 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
332 my $mid = $hdr->header_raw('Message-ID');
333 $mid = PublicInbox::Hval->new_msgid($mid);
334 foreach my $h (qw(From To Cc Subject Date)) {
335 my $v = $hdr->header($h);
336 defined($v) && ($v ne '') or next;
337 $v = PublicInbox::Hval->new($v);
340 $title[1] = PublicInbox::Address::from_name($v->raw);
341 } elsif ($h eq 'Subject') {
342 $title[0] = $v->as_html;
344 $rv .= qq($h: <a\nhref="#r"\nid=t>);
345 $rv .= $v->as_html . "</a>\n";
349 $rv .= "$h: " . $v->as_html . "\n";
352 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
353 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
354 $rv .= _parent_headers($hdr, $srch);
357 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
358 PublicInbox::Hval::STYLE .
359 "</head><body><pre\nid=b>" . # anchor for body start
364 my ($dst, $ctx, $hdr, $tpfx) = @_;
365 my $srch = $ctx->{srch};
366 my $mid = mid_clean($hdr->header_raw('Message-ID'));
367 my $sres = $srch->get_thread($mid);
368 my $nr = $sres->{total};
369 my $expand = qq(<a\nhref="${tpfx}t/#u">expand</a> ) .
370 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
371 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
373 my $parent = in_reply_to($hdr);
375 if (defined $parent) {
376 $$dst .= "($expand)\n ";
377 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
379 $$dst .= "[no followups, yet] ($expand)\n";
381 $ctx->{next_msg} = undef;
382 $ctx->{parent_msg} = $parent;
386 $$dst .= "$nr+ messages in thread ($expand";
387 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
389 my $subj = $srch->subject_path($hdr->header('Subject'));
391 seen => { $subj => 1 },
397 for (thread_results(load_results($sres))->rootset) {
398 skel_dump($dst, $state, $tpfx, $_, 0);
400 $ctx->{next_msg} = $state->{next_msg};
401 $ctx->{parent_msg} = $parent;
404 sub _parent_headers {
405 my ($hdr, $srch) = @_;
408 my $irt = in_reply_to($hdr);
410 my $v = PublicInbox::Hval->new_msgid($irt, 1);
411 my $html = $v->as_html;
412 my $href = $v->as_href;
413 $rv .= "In-Reply-To: <";
414 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
417 # do not display References: if search is present,
418 # we show the thread skeleton at the bottom, instead.
421 my $refs = $hdr->header_raw('References');
423 # avoid redundant URLs wasting bandwidth
425 $seen{$irt} = 1 if defined $irt;
427 my @raw_refs = ($refs =~ /<([^>]+)>/g);
428 foreach my $ref (@raw_refs) {
431 push @refs, linkify_ref_nosrch($ref);
435 $rv .= 'References: '. join(' ', @refs) . "\n";
441 sub mailto_arg_link {
443 my %cc; # everyone else
444 my $to; # this is the From address
446 foreach my $h (qw(From To Cc)) {
447 my $v = $hdr->header($h);
448 defined($v) && ($v ne '') or next;
449 my @addrs = PublicInbox::Address::emails($v);
450 foreach my $address (@addrs) {
451 my $dst = lc($address);
452 $cc{$dst} ||= $address;
458 my $subj = $hdr->header('Subject') || '';
459 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
460 my $mid = $hdr->header_raw('Message-ID');
461 push @arg, "--in-reply-to='" . ascii_html($mid) . "'";
462 my $irt = uri_escape_utf8($mid);
464 push @arg, '--to=' . ascii_html($to);
465 $to = uri_escape_utf8($to);
466 $subj = uri_escape_utf8($subj);
467 my $cc = join(',', sort values %cc);
468 push @arg, '--cc=' . ascii_html($cc);
469 $cc = uri_escape_utf8($cc);
470 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
477 my ($hdr, $standalone, $ctx, $rhref) = @_;
479 my $srch = $ctx->{srch} if $ctx;
482 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
486 thread_skel(\$idx, $ctx, $hdr, $tpfx);
487 my $p = $ctx->{parent_msg};
488 my $next = $ctx->{next_msg};
490 $p = PublicInbox::Hval->new_msgid($p);
492 $irt = "<a\nhref=\"$upfx$p/\"rel=prev>parent</a> ";
494 $irt = ' ' x length('parent ');
497 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
498 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
500 $irt .= ' ' x length('next ');
506 $irt . qq(<a\nhref="$rhref">reply</a>) . $idx;
509 sub linkify_ref_nosrch {
510 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
511 my $html = $v->as_html;
512 my $href = $v->as_href;
513 "<<a\nhref=\"../$href/\">$html</a>>";
519 if ($id !~ /\A[a-f0-9]{40}\z/) {
520 $id = id_compress(mid_clean($id), 1);
525 sub thread_html_head {
526 my ($hdr, $state) = @_;
527 my $res = delete $state->{res} or die "BUG: no Plack callback in {res}";
528 my $fh = $res->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
531 my $s = ascii_html($hdr->header('Subject'));
532 $fh->write("<html><head><title>$s</title>".
533 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
534 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
535 PublicInbox::Hval::STYLE .
539 sub pre_anchor_entry {
540 my ($seen, $mime) = @_;
541 my $id = anchor_for(mid_mime($mime));
542 $seen->{$id} = "#$id"; # save the anchor for children, later
546 my ($upfx, $mid) = @_;
547 # 'subject dummy' is used internally by Mail::Thread
548 return '[no common parent]' if ($mid eq 'subject dummy');
550 $mid = PublicInbox::Hval->new_msgid($mid);
551 my $href = $mid->as_href;
552 my $html = $mid->as_html;
553 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
556 sub thread_adj_level {
557 my ($state, $level) = @_;
559 my $max = $state->{cur_level};
561 return '' if $max == 0; # flat output
563 # reset existing lists
564 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
565 $state->{fh}->write($x . '</ul>');
566 $state->{cur_level} = 0;
569 if ($level == $max) { # continue existing list
570 $state->{fh}->write('<li>');
571 } elsif ($level < $max) {
572 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
573 $state->{fh}->write($x .= '<li>');
574 $state->{cur_level} = $level;
575 } else { # ($level > $max) # start a new level
576 $state->{cur_level} = $level;
577 $state->{fh}->write(($max ? '<li>' : '') . '<ul><li>');
583 my ($state, $upfx, $mid, $level) = @_;
584 my $end = '<pre>'. ghost_parent($upfx, $mid) . '</pre>';
585 $state->{fh}->write($end .= thread_adj_level($state, $level));
589 my ($state, $mime, $level) = @_;
591 # lazy load the full message from mini_mime:
593 my $path = mid2path(mid_clean(mid_mime($mime)));
594 Email::MIME->new($state->{ctx}->{git}->cat_file('HEAD:'.$path));
597 thread_html_head($mime, $state) if $state->{anchor_idx} == 0;
598 if (my $ghost = delete $state->{ghost}) {
599 # n.b. ghost messages may only be parents, not children
600 foreach my $g (@$ghost) {
601 ghost_flush($state, '../../', @$g);
604 my $end = thread_adj_level($state, $level);
605 index_entry($mime, $level, $state);
606 $state->{fh}->write($end) if $end;
613 INDENT x ($level - 1);
616 sub __ghost_prepare {
617 my ($state, $node, $level) = @_;
618 my $ghost = $state->{ghost} ||= [];
619 push @$ghost, [ $node->messageid, $level ];
623 my ($state, $node, $level) = @_;
625 if (my $mime = $node->message) {
626 unless (__thread_entry($state, $mime, $level)) {
627 __ghost_prepare($state, $node, $level);
630 __ghost_prepare($state, $node, $level);
633 thread_entry($state, $node->child, $level + 1);
634 thread_entry($state, $node->next, $level);
640 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
645 my $ts = eval { str2time($hdr->header('Date')) };
646 defined($ts) ? $ts : 0;
651 require PublicInbox::Thread;
652 my $th = PublicInbox::Thread->new(@$msgs);
654 $th->order(*sort_ts);
659 my ($res, $ctx) = @_;
660 require PublicInbox::ExtMsg;
662 $res->(PublicInbox::ExtMsg::ext_msg($ctx))
667 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
671 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
674 my ($dst, $state, $upfx, $hdr, $level) = @_;
676 my $cur = $state->{cur};
677 my $mid = mid_clean($hdr->header_raw('Message-ID'));
678 my $f = ascii_html($hdr->header('X-PI-From'));
679 my $d = _msg_date($hdr);
680 my $pfx = "$d " . indent_for($level) . th_pfx($level);
682 $state->{first_level} ||= $level;
684 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
685 $state->{prev_attr} = $attr;
689 $state->{prev_level} = $level;
693 delete $state->{cur};
694 $$dst .= "$pfx<b><a\nid=r\nhref=\"#t\">".
695 "$attr [this message]</a></b>\n";
700 $state->{next_msg} ||= $mid;
703 # Subject is never undef, this mail was loaded from
704 # our Xapian which would've resulted in '' if it were
705 # really missing (and Filter rejects empty subjects)
706 my $s = $hdr->header('Subject');
707 my $h = $state->{srch}->subject_path($s);
708 if ($state->{seen}->{$h}) {
711 $state->{seen}->{$h} = 1;
712 $s = PublicInbox::Hval->new($s);
715 my $m = PublicInbox::Hval->new_msgid($mid);
716 $m = $upfx . '../' . $m->as_href . '/';
717 $$dst .= "$pfx<a\nhref=\"$m\">";
718 $$dst .= defined($s) ? "$s</a> $f\n" : "$f</a>\n";
722 my ($dst, $state, $upfx, $node, $level) = @_;
724 if (my $mime = $node->message) {
725 my $hdr = $mime->header_obj;
726 my $mid = mid_clean($hdr->header_raw('Message-ID'));
727 _skel_header($dst, $state, $upfx, $hdr, $level);
729 my $mid = $node->messageid;
730 if ($mid eq 'subject dummy') {
731 $$dst .= "\t[no common parent]\n";
733 $$dst .= ' [not found] ';
734 $$dst .= indent_for($level) . th_pfx($level);
735 $mid = PublicInbox::Hval->new_msgid($mid);
736 my $href = "$upfx../" . $mid->as_href . '/';
737 my $html = $mid->as_html;
738 $$dst .= qq{<<a\nhref="$href">$html</a>>\n};
741 skel_dump($dst, $state, $upfx, $node->child, $level+1);
742 skel_dump($dst, $state, $upfx, $node->next, $level);
747 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
748 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
752 # accumulate recent topics if search is supported
753 # returns 1 if done, undef if not
755 my ($state, $node, $level) = @_;
757 my $child_adjust = 1;
759 if (my $x = $node->message) {
763 $subj = $x->header('Subject');
764 $subj = $state->{srch}->subject_normalized($subj);
766 if (++$state->{subjs}->{$subj} == 1) {
767 push @{$state->{order}}, [ $level, $subj ];
770 my $mid = mid_clean($x->header_raw('Message-ID'));
772 my $ts = $x->header('X-PI-TS');
773 my $exist = $state->{latest}->{$subj};
774 if (!$exist || $exist->[1] < $ts) {
775 $state->{latest}->{$subj} = [ $mid, $ts ];
778 # ghost message, do not bump level
782 add_topic($state, $node->child, $level + $child_adjust);
783 add_topic($state, $node->next, $level);
788 my $order = $state->{order};
789 my $subjs = $state->{subjs};
790 my $latest = $state->{latest};
791 my $fh = $state->{fh};
792 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
798 while (defined(my $info = shift @$order)) {
799 my ($level, $subj) = @$info;
800 my $n = delete $subjs->{$subj};
801 my ($mid, $ts) = @{delete $latest->{$subj}};
802 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
803 $subj = PublicInbox::Hval->new($subj)->as_html;
804 $pfx = indent_for($level);
805 my $nl = $level == $prev ? "\n" : '';
810 $cur ||= [ $ts, '' ];
811 $cur->[0] = $ts if $ts > $cur->[0];
812 $cur->[1] .= $nl . $pfx . th_pfx($level) .
813 "<a\nhref=\"$mid/t/#u\"><b>" .
814 $subj . "</b></a>\n";
817 my $attr = " $ts UTC";
819 # $n isn't the total number of posts on the topic,
820 # just the number of posts in the current results window
821 $n = $n == 1 ? '' : " ($n+ messages)";
823 if ($level == 0 || $attr ne $prev_attr) {
824 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
825 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
826 $pfx .= INDENT if $level > 0;
827 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
831 push @recent, $cur if $cur;
832 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
833 $fh->write(join('', @recent) . '</pre>');
836 sub emit_index_topics {
838 my ($off) = (($state->{ctx}->{cgi}->param('o') || '0') =~ /(\d+)/);
839 $state->{order} = [];
840 $state->{subjs} = {};
841 $state->{latest} = {};
843 my %opts = ( offset => $off, limit => $max * 4 );
844 while (scalar @{$state->{order}} < $max) {
845 my $sres = $state->{srch}->query('', \%opts);
846 my $nr = scalar @{$sres->{msgs}} or last;
848 for (thread_results(load_results($sres))->rootset) {
849 add_topic($state, $_, 0);
851 $opts{offset} += $nr;