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::MIME::Header;
13 use PublicInbox::Hval qw/ascii_html/;
14 use PublicInbox::Linkify;
15 use PublicInbox::MID qw/mid_clean id_compress mid_mime/;
16 use PublicInbox::MsgIter;
17 use PublicInbox::Address;
18 use PublicInbox::WwwStream;
21 use constant INDENT => ' ';
22 use constant TCHILD => '` ';
23 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
25 # public functions: (unstable)
27 my ($ctx, $mime, $footer) = @_;
28 my $hdr = $mime->header_obj;
29 my $tip = _msg_html_prepare($hdr, $ctx);
30 PublicInbox::WwwStream->new($ctx, sub {
33 $tip . multipart_text_as_html($mime, '') .
36 # fake an EOF if generating the footer fails;
37 # we want to at least show the message if something
40 '<pre>' . html_footer($hdr, 1, $ctx) .
41 '</pre>' . msg_reply($ctx, $hdr)
49 # /$INBOX/$MESSAGE_ID/#R
52 my $se_url = 'https://git-htmldocs.bogomips.org/git-send-email.html';
54 my ($arg, $link) = mailto_arg_link($hdr);
55 push @$arg, '/path/to/YOUR_REPLY';
58 "You may reply publically to <a\nhref=#t>this message</a> via\n".
59 "plain-text email using any one of the following methods:\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 " git send-email \\\n " .
65 join(" \\\n ", @$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) .
75 my $irt = $hdr->header_raw('In-Reply-To');
77 return mid_clean($irt) if (defined $irt);
79 my $refs = $hdr->header_raw('References');
80 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
87 my ($hdr, $field) = @_;
88 my $val = $hdr->header($field) or return '';
89 ascii_html(join(', ', PublicInbox::Address::names($val)));
93 my ($nr, $singular, $plural) = @_;
94 return "0 $plural" if $nr == 0;
95 $nr == 1 ? "$nr $singular" : "$nr $plural";
98 # this is already inside a <pre>
100 my ($mime, $ctx, $more) = @_;
101 my $srch = $ctx->{srch};
102 my $hdr = $mime->header_obj;
103 my $subj = $hdr->header('Subject');
105 my $mid_raw = mid_clean(mid_mime($mime));
106 my $id = id_compress($mid_raw);
108 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
110 my $root_anchor = $ctx->{root_anchor} || '';
111 my $irt = in_reply_to($hdr);
113 my $rv = '<b>'.ascii_html($subj).'</b>';
114 $rv = "<u\nid=u>$rv</u>" if $root_anchor eq $id_m;
116 $rv .= _th_index_lite($mid_raw, $irt, $id, $ctx);
118 foreach my $f (qw(To Cc)) {
119 my $dst = _hdr_names($hdr, $f);
120 push @tocc, "$f: $dst" if $dst ne '';
122 my $mapping = $ctx->{mapping};
123 $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
124 my $upfx = $ctx->{-upfx};
125 $rv .= qq{ (<a\nhref="$upfx$mid_raw/">permalink</a> / };
126 $rv .= qq{<a\nhref="$upfx$mid_raw/raw">raw</a>)\n};
127 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
128 if (!$mapping && $irt) {
129 $rv .= qq(In-Reply-To: <<a\nhref="$upfx$irt/">$irt</a>>\n)
133 # scan through all parts, looking for displayable text
134 my $href = $mid->as_href;
135 my $mhref = $ctx->{-upfx}.$href.'/';
136 msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
139 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
140 "<a\nhref=\"$mhref\">permalink</a>" .
141 " / <a\nhref=\"${mhref}raw\">raw</a>" .
142 " / <a\nhref=\"${mhref}#R\">reply</a>";
143 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
144 $rv .= " [relevance $pct->{$mid_raw}%]";
146 $rv .= $more ? "\n\n" : "\n";
149 sub pad_link ($$;$) {
150 my ($mid, $level, $s) = @_;
152 my $id = id_compress($mid, 1);
153 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)<a>\n";
157 my ($mid_raw, $irt, $id, $ctx) = @_;
159 my $mapping = $ctx->{mapping} or return $rv;
161 # map = [children, attr, node, idx, level]
162 my $map = $mapping->{$mid_raw};
163 my $nr_c = scalar @{$map->[0]};
165 my $level = $map->[4];
168 my $irt_map = $mapping->{$irt};
169 my $siblings = $irt_map->[0];
170 $nr_s = scalar(@$siblings) - 1;
171 $rv .= $pad . $irt_map->[1];
173 my $prev = $siblings->[$idx - 1];
174 my $pmid = $prev->messageid;
176 my $s = ($idx - 1). ' preceding siblings ...';
177 $rv .= pad_link($pmid, $level, $s);
178 } elsif ($idx == 2) {
179 my $ppmid = $siblings->[0]->messageid;
180 $rv .= $pad . $mapping->{$ppmid}->[1];
182 $rv .= $pad . $mapping->{$pmid}->[1];
185 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
186 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
187 my $this = $map->[1];
188 $this =~ s!\n\z!</b>\n!s;
189 $this =~ s!<a\nhref.*a> !!s; # no point in duplicating subject
191 my $node = $map->[2];
192 if (my $child = $node->child) {
193 my $cmid = $child->messageid;
194 $rv .= $pad . $mapping->{$cmid}->[1];
196 my $s = ($nr_c - 1). ' more replies';
197 $rv .= pad_link($cmid, $level + 1, $s);
198 } elsif (my $cn = $child->next) {
199 $rv .= $pad . $mapping->{$cn->messageid}->[1];
202 if (my $next = $node->next) {
203 my $nmid = $next->messageid;
204 $rv .= $pad . $mapping->{$nmid}->[1];
205 my $nnext = $nr_s - $idx;
207 my $s = ($nnext - 1).' subsequent siblings';
208 $rv .= pad_link($nmid, $level, $s);
209 } elsif (my $nn = $next->next) {
210 $rv .= $pad . $mapping->{$nn->messageid}->[1];
213 $rv .= "<a\nhref=#e$id\nid=m$id>_<a> <a\nhref=#r$id\n>$s_s, $s_c</a>\n";
217 my ($th, $ctx, $cb) = @_;
218 my @q = map { (0, $_) } $th->rootset;
220 my $level = shift @q;
221 my $node = shift @q or next;
222 $cb->($ctx, $level, $node);
223 unshift @q, $level+1, $node->child, $level, $node->next;
228 my ($ctx, $level, $node) = @_;
229 my $mapping = $ctx->{mapping};
231 if (my $parent = $node->parent) {
232 my $m = $mapping->{$parent->messageid}->[0];
236 $mapping->{$node->messageid} = [ [], '', $node, $idx, $level ];
237 skel_dump($ctx, $level, $node);
242 my $mid = $ctx->{mid};
243 my $sres = $ctx->{srch}->get_thread($mid, { asc => 1 });
244 my $msgs = load_results($sres);
245 my $nr = $sres->{total};
246 return missing_thread($ctx) if $nr == 0;
247 my $skel = '</pre><hr /><pre>';
248 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
249 $skel .= ", back to <a\nhref=\"../../\">index</a>";
250 $skel .= "\n<a\nid=t>$nr+ messages in thread:</a> (download: ";
251 $skel .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
252 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
253 $ctx->{cur_level} = 0;
254 $ctx->{dst} = \$skel;
255 $ctx->{mapping} = {}; # mid -> [ reply count, from@date, node ];
256 $ctx->{prev_attr} = '';
257 $ctx->{prev_level} = 0;
258 $ctx->{root_anchor} = anchor_for($mid);
261 walk_thread(thread_results($msgs), $ctx, *pre_thread);
263 # lazy load the full message from mini_mime:
264 my $inbox = $ctx->{-inbox};
266 while ($mime = shift @$msgs) {
267 $mime = $inbox->msg_by_mid(mid_clean(mid_mime($mime))) and last;
269 $mime = Email::MIME->new($mime);
270 $ctx->{-upfx} = '../../';
271 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
272 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
274 my $body = PublicInbox::WwwStream->new($ctx, sub {
276 while ($mime = shift @$msgs) {
277 $mid = mid_clean(mid_mime($mime));
278 $mime = $inbox->msg_by_mid($mid) and last;
281 $mime = Email::MIME->new($mime);
282 return index_entry($mime, $ctx, scalar @$msgs);
287 [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
290 sub multipart_text_as_html {
291 my ($mime, $upfx) = @_;
294 # scan through all parts, looking for displayable text
295 msg_iter($mime, sub {
297 $rv .= add_text_body($upfx, $p);
303 my ($s, $l, $quot) = @_;
305 # show everything in the full version with anchor from
306 # short version (see above)
307 my $rv = $l->linkify_1(join('', @$quot));
310 # we use a <span> here to allow users to specify their own
311 # color for quoted text
312 $rv = $l->linkify_2(ascii_html($rv));
313 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
316 sub attach_link ($$$$) {
317 my ($upfx, $ct, $p, $fn) = @_;
318 my ($part, $depth, @idx) = @$p;
319 my $nl = $idx[-1] > 1 ? "\n" : '';
320 my $idx = join('.', @idx);
321 my $size = bytes::length($part->body);
322 $ct ||= 'text/plain';
323 $ct =~ s/;.*//; # no attributes
324 $ct = ascii_html($ct);
325 my $desc = $part->header('Content-Description');
326 $desc = $fn unless defined $desc;
327 $desc = '' unless defined $desc;
329 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
331 } elsif ($ct eq 'text/plain') {
336 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
337 my $ts = "Type: $ct, Size: $size bytes";
338 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
339 join('', @ret, "</a>\n");
343 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
344 my ($part, $depth, @idx) = @$p;
345 my $ct = $part->content_type;
346 my $fn = $part->filename;
348 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
349 return attach_link($upfx, $ct, $p, $fn);
352 my $s = eval { $part->body_str };
354 # badly-encoded message? tell the world about it!
355 return attach_link($upfx, $ct, $p, $fn) if $@;
357 my @lines = split(/^/m, $s);
359 if (defined($fn) || $depth > 0) {
360 $s .= attach_link($upfx, $ct, $p, $fn);
364 my $l = PublicInbox::Linkify->new;
365 while (defined(my $cur = shift @lines)) {
367 # show the previously buffered quote inline
368 flush_quote(\$s, $l, \@quot) if @quot;
371 $cur = $l->linkify_1($cur);
372 $cur = ascii_html($cur);
373 $s .= $l->linkify_2($cur);
382 flush_quote(\$s, $l, \@quot);
384 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
385 $s =~ s/\A\n+//s; # kill leading blank lines
386 $s =~ s/\s+\z//s; # kill all trailing spaces
390 sub _msg_html_prepare {
391 my ($hdr, $ctx) = @_;
392 my $srch = $ctx->{srch} if $ctx;
394 my $rv = "<pre\nid=b>"; # anchor for body start
397 $ctx->{-upfx} = '../';
400 my $mid = $hdr->header_raw('Message-ID');
401 $mid = PublicInbox::Hval->new_msgid($mid);
402 foreach my $h (qw(From To Cc Subject Date)) {
403 my $v = $hdr->header($h);
404 defined($v) && ($v ne '') or next;
405 $v = PublicInbox::Hval->new($v);
408 my @n = PublicInbox::Address::names($v->raw);
409 $title[1] = ascii_html(join(', ', @n));
410 } elsif ($h eq 'Subject') {
411 $title[0] = $v->as_html;
413 $rv .= qq($h: <a\nhref="#r"\nid=t>);
414 $rv .= $v->as_html . "</a>\n";
419 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
423 $title[0] ||= '(no subject)';
424 $ctx->{-title_html} = join(' - ', @title);
425 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
426 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
427 $rv .= _parent_headers($hdr, $srch);
432 my ($dst, $ctx, $hdr, $tpfx) = @_;
433 my $srch = $ctx->{srch};
434 my $mid = mid_clean($hdr->header_raw('Message-ID'));
435 my $sres = $srch->get_thread($mid);
436 my $nr = $sres->{total};
437 my $expand = qq(<a\nhref="${tpfx}t/#u">expand</a> ) .
438 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
439 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
441 my $parent = in_reply_to($hdr);
443 if (defined $parent) {
444 $$dst .= "($expand)\n ";
445 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
447 $$dst .= "[no followups, yet] ($expand)\n";
449 $ctx->{next_msg} = undef;
450 $ctx->{parent_msg} = $parent;
454 $$dst .= "$nr+ messages in thread ($expand";
455 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
457 my $subj = $srch->subject_path($hdr->header('Subject'));
458 $ctx->{seen} = { $subj => 1 };
460 $ctx->{prev_attr} = '';
461 $ctx->{prev_level} = 0;
463 walk_thread(thread_results(load_results($sres)), $ctx, *skel_dump);
464 $ctx->{parent_msg} = $parent;
467 sub _parent_headers {
468 my ($hdr, $srch) = @_;
471 my $irt = in_reply_to($hdr);
473 my $v = PublicInbox::Hval->new_msgid($irt, 1);
474 my $html = $v->as_html;
475 my $href = $v->as_href;
476 $rv .= "In-Reply-To: <";
477 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
480 # do not display References: if search is present,
481 # we show the thread skeleton at the bottom, instead.
484 my $refs = $hdr->header_raw('References');
486 # avoid redundant URLs wasting bandwidth
488 $seen{$irt} = 1 if defined $irt;
490 my @raw_refs = ($refs =~ /<([^>]+)>/g);
491 foreach my $ref (@raw_refs) {
494 push @refs, linkify_ref_nosrch($ref);
498 $rv .= 'References: '. join("\n\t", @refs) . "\n";
504 sub squote_maybe ($) {
506 if ($val =~ m{([^\w@\./,\%\+\-])}) {
507 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
513 sub mailto_arg_link {
515 my %cc; # everyone else
516 my $to; # this is the From address
518 foreach my $h (qw(From To Cc)) {
519 my $v = $hdr->header($h);
520 defined($v) && ($v ne '') or next;
521 my @addrs = PublicInbox::Address::emails($v);
522 foreach my $address (@addrs) {
523 my $dst = lc($address);
524 $cc{$dst} ||= $address;
530 my $subj = $hdr->header('Subject') || '';
531 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
532 my $mid = $hdr->header_raw('Message-ID');
533 push @arg, '--in-reply-to='.ascii_html(squote_maybe(mid_clean($mid)));
534 my $irt = uri_escape_utf8($mid);
536 push @arg, '--to=' . ascii_html($to);
537 $to = uri_escape_utf8($to);
538 $subj = uri_escape_utf8($subj);
539 my $cc = join(',', sort values %cc);
540 push @arg, '--cc=' . ascii_html($cc);
541 $cc = uri_escape_utf8($cc);
542 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
545 (\@arg, ascii_html($href));
549 my ($hdr, $standalone, $ctx, $rhref) = @_;
551 my $srch = $ctx->{srch} if $ctx;
554 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
558 thread_skel(\$idx, $ctx, $hdr, $tpfx);
559 my $p = $ctx->{parent_msg};
560 my $next = $ctx->{next_msg};
562 $p = PublicInbox::Hval->new_msgid($p);
564 $irt = "<a\nhref=\"$upfx$p/\"\nrel=prev>parent</a> ";
566 $irt = ' ' x length('parent ');
569 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
570 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
572 $irt .= ' ' x length('next ');
578 $irt .= qq(<a\nhref="$rhref">reply</a>);
582 sub linkify_ref_nosrch {
583 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
584 my $html = $v->as_html;
585 my $href = $v->as_href;
586 "<<a\nhref=\"../$href/\">$html</a>>";
591 'm' . id_compress($msgid, 1);
595 my ($upfx, $mid) = @_;
596 # 'subject dummy' is used internally by Mail::Thread
597 return '[no common parent]' if ($mid eq 'subject dummy');
599 $mid = PublicInbox::Hval->new_msgid($mid);
600 my $href = $mid->as_href;
601 my $html = $mid->as_html;
602 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
607 INDENT x ($level - 1);
613 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
618 my $ts = eval { str2time($hdr->header('Date')) };
619 defined($ts) ? $ts : 0;
624 require PublicInbox::Thread;
625 my $th = PublicInbox::Thread->new(@$msgs);
627 $th->order(*sort_ts);
633 require PublicInbox::ExtMsg;
634 PublicInbox::ExtMsg::ext_msg($ctx);
639 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
643 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
646 my ($ctx, $hdr, $level) = @_;
648 my $dst = $ctx->{dst};
649 my $cur = $ctx->{cur};
650 my $mid = mid_clean($hdr->header_raw('Message-ID'));
651 my $f = ascii_html($hdr->header('X-PI-From'));
652 my $d = _msg_date($hdr) . ' ' . indent_for($level) . th_pfx($level);
654 $ctx->{first_level} ||= $level;
656 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
657 $ctx->{prev_attr} = $attr;
661 $ctx->{prev_level} = $level;
667 $$dst .= "<b><a\nid=r\nhref=\"#t\">".
668 "$attr [this message]</a></b>\n";
672 $ctx->{next_msg} ||= $mid;
675 # Subject is never undef, this mail was loaded from
676 # our Xapian which would've resulted in '' if it were
677 # really missing (and Filter rejects empty subjects)
678 my $s = $hdr->header('Subject');
679 my $h = $ctx->{srch}->subject_path($s);
680 if ($ctx->{seen}->{$h}) {
683 $ctx->{seen}->{$h} = 1;
684 $s = PublicInbox::Hval->new($s);
687 my $m = PublicInbox::Hval->new_msgid($mid);
689 my $mapping = $ctx->{mapping};
690 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
692 my $map = $mapping->{$mid};
693 $id = id_compress($mid, 1);
695 $map->[1] = "$d<a\nhref=\"$m\">$end";
698 $m = $ctx->{-upfx}.$m->as_href.'/';
700 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
704 my ($ctx, $level, $node) = @_;
705 if (my $mime = $node->message) {
706 _skel_header($ctx, $mime->header_obj, $level);
708 my $mid = $node->messageid;
709 my $dst = $ctx->{dst};
710 my $mapping = $ctx->{mapping};
711 my $map = $mapping->{$mid} if $mapping;
712 if ($mid eq 'subject dummy') {
713 my $ncp = "\t[no common parent]\n";
714 $map->[1] = $ncp if $map;
718 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
720 $d .= indent_for($level) . th_pfx($level);
721 my $upfx = $ctx->{-upfx};
722 my $m = PublicInbox::Hval->new_msgid($mid);
723 my $href = $upfx . $m->as_href . '/';
724 my $html = $m->as_html;
727 my $id = id_compress($mid, 1);
728 $map->[1] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
729 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
731 $d .= qq{<<a\nhref="$href">$html</a>>\n};
739 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
740 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
744 sub _tryload_ghost ($$) {
745 my ($srch, $mid) = @_;
746 my $smsg = $srch->lookup_mail($mid) or return;
750 # accumulate recent topics if search is supported
751 # returns 1 if done, undef if not
753 my ($ctx, $level, $node) = @_;
754 my $srch = $ctx->{srch};
755 my $mid = $node->messageid;
756 my $x = $node->message || _tryload_ghost($srch, $mid);
760 $subj = $x->header('Subject');
761 $subj = $srch->subject_normalized($subj);
762 $ts = $x->header('X-PI-TS');
763 } else { # ghost message, do not bump level
767 if (++$ctx->{subjs}->{$subj} == 1) {
768 push @{$ctx->{order}}, [ $level, $subj ];
770 my $exist = $ctx->{latest}->{$subj};
771 if (!$exist || $exist->[1] < $ts) {
772 $ctx->{latest}->{$subj} = [ $mid, $ts ];
778 my $order = $ctx->{order};
779 my $subjs = $ctx->{subjs};
780 my $latest = $ctx->{latest};
782 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
788 while (defined(my $info = shift @$order)) {
789 my ($level, $subj) = @$info;
790 my $n = delete $subjs->{$subj};
791 my ($mid, $ts) = @{delete $latest->{$subj}};
792 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
793 $pfx = indent_for($level);
794 my $nl = $level == $prev ? "\n" : '';
799 $cur ||= [ $ts, '' ];
800 $cur->[0] = $ts if $ts > $cur->[0];
801 $cur->[1] .= $nl . $pfx . th_pfx($level);
802 if ($ts == -666) { # ghost
803 $cur->[1] .= ghost_parent('', $mid) . "\n";
804 next; # child will have mbox / atom link
807 $subj = PublicInbox::Hval->new($subj)->as_html;
808 $cur->[1] .= "<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
810 my $attr = " $ts UTC";
812 # $n isn't the total number of posts on the topic,
813 # just the number of posts in the current results window
814 $n = $n == 1 ? '' : " ($n+ messages)";
816 if ($level == 0 || $attr ne $prev_attr) {
817 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
818 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
819 $pfx .= INDENT if $level > 0;
820 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
824 push @recent, $cur if $cur;
825 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
826 $fh->write(join('', @recent) . '</pre>');
829 sub emit_index_topics {
831 my ($off) = (($ctx->{cgi}->param('o') || '0') =~ /(\d+)/);
836 my %opts = ( offset => $off, limit => $max * 4 );
837 while (scalar @{$ctx->{order}} < $max) {
838 my $sres = $ctx->{srch}->query('', \%opts);
839 my $nr = scalar @{$sres->{msgs}} or last;
840 $sres = load_results($sres);
841 walk_thread(thread_results($sres), $ctx, *add_topic);
842 $opts{offset} += $nr;