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 => ' ';
21 my $enc_utf8 = find_encoding('UTF-8');
25 my ($ctx, $mime, $full_pfx, $footer) = @_;
26 $footer = defined($footer) ? "\n$footer" : '';
27 my $hdr = $mime->header_obj;
28 headers_to_html_header($hdr, $full_pfx, $ctx) .
29 multipart_text_as_html($mime, $full_pfx) .
31 html_footer($hdr, 1, $full_pfx, $ctx) .
33 '</pre></body></html>';
36 # /$LISTNAME/$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>';
75 my ($class, $mime, $full_pfx) = @_;
77 # no <head> here for <style>...
78 PublicInbox::Hval::PRE .
79 multipart_text_as_html($mime, $full_pfx) . '</pre>';
84 my $irt = $hdr->header_raw('In-Reply-To');
86 return mid_clean($irt) if (defined $irt);
88 my $refs = $hdr->header_raw('References');
89 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
95 # this is already inside a <pre>
97 my ($mime, $level, $state) = @_;
98 my $midx = $state->{anchor_idx}++;
99 my $ctx = $state->{ctx};
100 my $srch = $ctx->{srch};
102 my $hdr = $mime->header_obj;
103 my $enc = enc_for($hdr->header("Content-Type"));
104 my $subj = $hdr->header('Subject');
106 my $mid_raw = mid_clean(mid_mime($mime));
107 my $id = anchor_for($mid_raw);
108 my $seen = $state->{seen};
109 $seen->{$id} = "#$id"; # save the anchor for children, later
111 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
112 my $from = $hdr->header('From');
113 my @from = Email::Address->parse($from);
114 $from = $from[0]->name;
116 my $root_anchor = $state->{root_anchor} || '';
117 my $path = $root_anchor ? '../../' : '';
118 my $href = $mid->as_href;
119 my $irt = in_reply_to($hdr);
120 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
122 $from = ascii_html($from);
123 $subj = ascii_html($subj);
124 $subj = "<a\nhref=\"${path}$href/\">$subj</a>";
125 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id;
127 my $ts = _msg_date($hdr);
128 my $rv = "<pre\nid=s$midx>";
129 $rv .= "<b\nid=$id>$subj</b>\n";
130 my $txt = "${path}$href/raw";
131 my $fh = $state->{fh};
132 $fh->write($rv .= "- $from @ $ts UTC (<a\nhref=\"$txt\">raw</a>)\n\n");
134 my $mhref = "${path}$href/";
136 # scan through all parts, looking for displayable text
137 $mime->walk_parts(sub {
138 index_walk($fh, $_[0], $enc, \$part_nr);
141 $rv = "\n" . html_footer($hdr, 0, undef, $ctx, $mhref);
144 unless (defined $parent_anchor) {
145 my $v = PublicInbox::Hval->new_msgid($irt, 1);
147 $parent_anchor = "${path}$v/";
149 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
151 if (my $pct = $state->{pct}) { # used by SearchView.pm
152 $rv .= " [relevance $pct->{$mid_raw}%]";
154 my $threaded = 'threaded';
157 $flat = "<b>$flat</b>";
159 $threaded = "<b>$threaded</b>";
161 $rv .= " [<a\nhref=\"${path}$href/t/#u\">$threaded</a>";
162 $rv .= "|<a\nhref=\"${path}$href/T/#u\">$flat</a>]";
164 $fh->write($rv .= '</pre>');
168 my ($ctx, $foot, $srch) = @_;
169 # $_[0] in sub is the Plack callback
170 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
173 # only private functions below.
175 sub emit_thread_html {
176 my ($res, $ctx, $foot, $srch) = @_;
177 my $mid = $ctx->{mid};
178 my $msgs = load_results($srch->get_thread($mid));
179 my $nr = scalar @$msgs;
180 return missing_thread($res, $ctx) if $nr == 0;
181 my $flat = $ctx->{flat};
187 root_anchor => anchor_for($mid),
192 require PublicInbox::Git;
193 $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
195 pre_anchor_entry($seen, $_) for (@$msgs);
196 __thread_entry($state, $_, 0) for (@$msgs);
198 my $th = thread_results($msgs);
199 thread_entry($state, $_, 0) for $th->rootset;
200 if (my $max = $state->{cur_level}) {
202 ('</ul></li>' x ($max - 1)) . '</ul>');
205 Email::Address->purge_cache;
207 # there could be a race due to a message being deleted in git
208 # but still being in the Xapian index:
209 my $fh = delete $state->{fh} or return missing_thread($res, $ctx);
211 my $final_anchor = $state->{anchor_idx};
212 my $next = "<a\nid=s$final_anchor>";
213 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
214 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
215 $next .= "\ndownload thread: ";
216 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
217 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
218 $fh->write('<hr /><pre>' . $next . "\n\n".
219 $foot . '</pre></body></html>');
224 my ($fh, $part, $enc, $part_nr) = @_;
225 my $s = add_text_body($enc, $part, $part_nr);
229 $s .= "\n"; # ensure there's a trailing newline
235 my ($ct, $default) = @_;
236 $default ||= $enc_utf8;
237 defined $ct or return $default;
238 my $ct_parsed = parse_content_type($ct);
240 if (my $charset = $ct_parsed->{attributes}->{charset}) {
241 my $enc = find_encoding($charset);
248 sub multipart_text_as_html {
249 my ($mime, $full_pfx, $srch) = @_;
252 my $enc = enc_for($mime->header("Content-Type"));
254 # scan through all parts, looking for displayable text
255 $mime->walk_parts(sub {
257 $part = add_text_body($enc, $part, \$part_nr);
259 $rv .= "\n" if $part ne '';
265 sub add_filename_line {
269 $fn = $enc->decode($fn);
271 $pad x= ($len/2) if ($len > 0);
272 "$pad " . ascii_html($fn) . " $pad\n";
276 my ($s, $l, $quot, $part_nr) = @_;
278 # show everything in the full version with anchor from
279 # short version (see above)
280 my $rv = $l->linkify_1(join('', @$quot));
283 # we use a <div> here to allow users to specify their own
284 # color for quoted text
285 $rv = $l->linkify_2(ascii_html($rv));
286 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
290 my ($enc_msg, $part, $part_nr) = @_;
291 return '' if $part->subparts;
293 my $ct = $part->content_type;
294 # account for filter bugs...
295 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
299 my $enc = enc_for($ct, $enc_msg);
302 $s = $enc->decode($s);
303 my @lines = split(/^/m, $s);
307 my $fn = $part->filename;
308 defined($fn) or $fn = "part #" . ($$part_nr + 1);
309 $s .= add_filename_line($enc, $fn);
313 my $l = PublicInbox::Linkify->new;
314 while (defined(my $cur = shift @lines)) {
316 # show the previously buffered quote inline
317 flush_quote(\$s, $l, \@quot, $$part_nr) if @quot;
320 $cur = $l->linkify_1($cur);
321 $cur = ascii_html($cur);
322 $s .= $l->linkify_2($cur);
328 flush_quote(\$s, $l, \@quot, $$part_nr) if @quot;
331 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
332 $s =~ s/\A\n+//s; # kill leading blank lines
333 $s =~ s/\s+\z//s; # kill all trailing spaces (final "\n" added if ne '')
337 sub headers_to_html_header {
338 my ($hdr, $full_pfx, $ctx) = @_;
339 my $srch = $ctx->{srch} if $ctx;
342 my $upfx = $full_pfx ? '' : '../';
345 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
346 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
350 my $mid = $hdr->header_raw('Message-ID');
351 $mid = PublicInbox::Hval->new_msgid($mid);
352 foreach my $h (qw(From To Cc Subject Date)) {
353 my $v = $hdr->header($h);
354 defined($v) && ($v ne '') or next;
355 $v = PublicInbox::Hval->new($v);
358 my @from = Email::Address->parse($v->raw);
359 $title[1] = ascii_html($from[0]->name);
360 } elsif ($h eq 'Subject') {
361 $title[0] = $v->as_html;
363 $rv .= qq($h: <a\nhref="#r"\nid=t>);
364 $rv .= $v->as_html . "</a>\n";
368 $rv .= "$h: " . $v->as_html . "\n";
371 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
372 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
373 $rv .= _parent_headers($hdr, $srch);
376 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
377 PublicInbox::Hval::STYLE .
378 "</head><body><pre\nid=b>" . # anchor for body start
383 my ($dst, $ctx, $hdr, $upfx) = @_;
384 my $srch = $ctx->{srch};
385 my $mid = mid_clean($hdr->header_raw('Message-ID'));
386 my $sres = $srch->get_thread($mid);
387 my $nr = $sres->{total};
388 my $expand = "<a\nhref=\"${upfx}t/#u\">expand</a> " .
389 "/ <a\nhref=\"${upfx}t.mbox.gz\">mbox.gz</a>";
391 my $parent = in_reply_to($hdr);
393 if (defined $parent) {
394 $$dst .= "($expand)\n ";
395 $$dst .= ghost_parent("$upfx../", $parent) . "\n";
397 $$dst .= "[no followups, yet] ($expand)\n";
399 $ctx->{next_msg} = undef;
400 $ctx->{parent_msg} = $parent;
404 $$dst .= "$nr+ messages in thread ($expand";
405 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
407 my $subj = $srch->subject_path($hdr->header('Subject'));
409 seen => { $subj => 1 },
415 for (thread_results(load_results($sres))->rootset) {
416 skel_dump($dst, $state, $upfx, $_, 0);
418 $ctx->{next_msg} = $state->{next_msg};
419 $ctx->{parent_msg} = $parent;
422 sub _parent_headers {
423 my ($hdr, $srch) = @_;
426 my $irt = in_reply_to($hdr);
428 my $v = PublicInbox::Hval->new_msgid($irt, 1);
429 my $html = $v->as_html;
430 my $href = $v->as_href;
431 $rv .= "In-Reply-To: <";
432 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
435 # do not display References: if search is present,
436 # we show the thread skeleton at the bottom, instead.
439 my $refs = $hdr->header_raw('References');
441 # avoid redundant URLs wasting bandwidth
443 $seen{$irt} = 1 if defined $irt;
445 my @raw_refs = ($refs =~ /<([^>]+)>/g);
446 foreach my $ref (@raw_refs) {
449 push @refs, linkify_ref_nosrch($ref);
453 $rv .= 'References: '. join(' ', @refs) . "\n";
459 sub mailto_arg_link {
461 my %cc; # everyone else
462 my $to; # this is the From address
464 foreach my $h (qw(From To Cc)) {
465 my $v = $hdr->header($h);
466 defined($v) && ($v ne '') or next;
467 my @addrs = Email::Address->parse($v);
468 foreach my $recip (@addrs) {
469 my $address = $recip->address;
470 my $dst = lc($address);
471 $cc{$dst} ||= $address;
475 Email::Address->purge_cache;
478 my $subj = $hdr->header('Subject') || '';
479 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
480 my $mid = $hdr->header_raw('Message-ID');
481 push @arg, "--in-reply-to='" . ascii_html($mid) . "'";
482 my $irt = uri_escape_utf8($mid);
484 push @arg, '--to=' . ascii_html($to);
485 $to = uri_escape_utf8($to);
486 $subj = uri_escape_utf8($subj);
487 my $cc = join(',', sort values %cc);
488 push @arg, '--cc=' . ascii_html($cc);
489 $cc = uri_escape_utf8($cc);
490 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
497 my ($hdr, $standalone, $full_pfx, $ctx, $mhref) = @_;
499 my $srch = $ctx->{srch} if $ctx;
500 my $upfx = $full_pfx ? '../' : '../../';
501 my $tpfx = $full_pfx ? '' : '../';
502 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
505 if ($srch && $standalone) {
506 $idx .= qq{ / follow: <a\nhref="${tpfx}t.atom">Atom feed</a>\n};
509 thread_skel(\$idx, $ctx, $hdr, $tpfx);
510 my $p = $ctx->{parent_msg};
511 my $next = $ctx->{next_msg};
513 $p = PublicInbox::Hval->new_msgid($p);
515 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
517 $irt = ' ' x length('parent ');
520 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
522 $irt .= ' ' x length('next ');
528 $irt . qq(<a\nhref="${tpfx}R/">reply</a>) . $idx;
531 sub linkify_ref_nosrch {
532 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
533 my $html = $v->as_html;
534 my $href = $v->as_href;
535 "<<a\nhref=\"../$href/\">$html</a>>";
541 if ($id !~ /\A[a-f0-9]{40}\z/) {
542 $id = id_compress(mid_clean($id), 1);
547 sub thread_html_head {
548 my ($hdr, $state) = @_;
549 my $res = delete $state->{res} or die "BUG: no Plack callback in {res}";
550 my $fh = $res->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
553 my $s = ascii_html($hdr->header('Subject'));
554 $fh->write("<html><head><title>$s</title>".
555 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
556 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
557 PublicInbox::Hval::STYLE .
561 sub pre_anchor_entry {
562 my ($seen, $mime) = @_;
563 my $id = anchor_for(mid_mime($mime));
564 $seen->{$id} = "#$id"; # save the anchor for children, later
568 my ($upfx, $mid) = @_;
569 # 'subject dummy' is used internally by Mail::Thread
570 return '[no common parent]' if ($mid eq 'subject dummy');
572 $mid = PublicInbox::Hval->new_msgid($mid);
573 my $href = $mid->as_href;
574 my $html = $mid->as_html;
575 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
578 sub thread_adj_level {
579 my ($state, $level) = @_;
581 my $max = $state->{cur_level};
583 return '' if $max == 0; # flat output
585 # reset existing lists
586 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
587 $state->{fh}->write($x . '</ul>');
588 $state->{cur_level} = 0;
591 if ($level == $max) { # continue existing list
592 $state->{fh}->write('<li>');
593 } elsif ($level < $max) {
594 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
595 $state->{fh}->write($x .= '<li>');
596 $state->{cur_level} = $level;
597 } else { # ($level > $max) # start a new level
598 $state->{cur_level} = $level;
599 $state->{fh}->write(($max ? '<li>' : '') . '<ul><li>');
605 my ($state, $upfx, $mid, $level) = @_;
606 my $end = '<pre>'. ghost_parent($upfx, $mid) . '</pre>';
607 $state->{fh}->write($end .= thread_adj_level($state, $level));
611 my ($state, $mime, $level) = @_;
613 # lazy load the full message from mini_mime:
615 my $path = mid2path(mid_clean(mid_mime($mime)));
616 Email::MIME->new($state->{ctx}->{git}->cat_file('HEAD:'.$path));
619 thread_html_head($mime, $state) if $state->{anchor_idx} == 0;
620 if (my $ghost = delete $state->{ghost}) {
621 # n.b. ghost messages may only be parents, not children
622 foreach my $g (@$ghost) {
623 ghost_flush($state, '../../', @$g);
626 my $end = thread_adj_level($state, $level);
627 index_entry($mime, $level, $state);
628 $state->{fh}->write($end) if $end;
635 INDENT x ($level - 1);
638 sub __ghost_prepare {
639 my ($state, $node, $level) = @_;
640 my $ghost = $state->{ghost} ||= [];
641 push @$ghost, [ $node->messageid, $level ];
645 my ($state, $node, $level) = @_;
647 if (my $mime = $node->message) {
648 unless (__thread_entry($state, $mime, $level)) {
649 __ghost_prepare($state, $node, $level);
652 __ghost_prepare($state, $node, $level);
655 thread_entry($state, $node->child, $level + 1);
656 thread_entry($state, $node->next, $level);
662 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
667 my $ts = eval { str2time($hdr->header('Date')) };
668 defined($ts) ? $ts : 0;
672 my ($msgs, $nosubject, $nosort) = @_;
673 require PublicInbox::Thread;
674 my $th = PublicInbox::Thread->new(@$msgs);
676 # WARNING! both these Mail::Thread knobs were found by inspecting
677 # the Mail::Thread 2.55 source code, and we have some monkey patches
678 # in PublicInbox::Thread to fix memory leaks. Since Mail::Thread
679 # appears unmaintained, I suppose it's safe to depend on these
682 $Mail::Thread::nosubject = $nosubject;
683 # Keep ghosts with only a single direct child:
684 $Mail::Thread::noprune = 1;
686 $th->order(*sort_ts) unless $nosort;
691 my ($res, $ctx) = @_;
692 require PublicInbox::ExtMsg;
694 $res->(PublicInbox::ExtMsg::ext_msg($ctx))
699 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
703 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
706 my ($dst, $state, $upfx, $hdr, $level) = @_;
707 my $dot = $level == 0 ? '' : '` ';
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);
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$dot<b><a\nid=r\nhref=\"#b\">".
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 . '/';
751 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a> $attr\n";
753 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f</a>\n";
758 my ($dst, $state, $upfx, $node, $level) = @_;
760 if (my $mime = $node->message) {
761 my $hdr = $mime->header_obj;
762 my $mid = mid_clean($hdr->header_raw('Message-ID'));
763 _skel_header($dst, $state, $upfx, $hdr, $level);
765 my $mid = $node->messageid;
766 if ($mid eq 'subject dummy') {
767 $$dst .= "\t[no common parent]\n";
769 $$dst .= ' [not found] ';
770 my $dot = $level == 0 ? '' : '` ';
771 $$dst .= indent_for($level) . $dot;
772 $mid = PublicInbox::Hval->new_msgid($mid);
773 my $href = "$upfx../" . $mid->as_href . '/';
774 my $html = $mid->as_html;
775 $$dst .= qq{<<a\nhref="$href">$html</a>>\n};
778 skel_dump($dst, $state, $upfx, $node->child, $level+1);
779 skel_dump($dst, $state, $upfx, $node->next, $level);
784 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
785 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
789 # accumulate recent topics if search is supported
790 # returns 1 if done, undef if not
792 my ($state, $node, $level) = @_;
794 my $child_adjust = 1;
796 if (my $x = $node->message) {
800 $subj = $x->header('Subject');
801 $subj = $state->{srch}->subject_normalized($subj);
803 if (++$state->{subjs}->{$subj} == 1) {
804 push @{$state->{order}}, [ $level, $subj ];
807 my $mid = mid_clean($x->header_raw('Message-ID'));
809 my $ts = $x->header('X-PI-TS');
810 my $exist = $state->{latest}->{$subj};
811 if (!$exist || $exist->[1] < $ts) {
812 $state->{latest}->{$subj} = [ $mid, $ts ];
815 # ghost message, do not bump level
819 add_topic($state, $node->child, $level + $child_adjust);
820 add_topic($state, $node->next, $level);
825 my $order = $state->{order};
826 my $subjs = $state->{subjs};
827 my $latest = $state->{latest};
828 my $fh = $state->{fh};
829 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
835 while (defined(my $info = shift @$order)) {
836 my ($level, $subj) = @$info;
837 my $n = delete $subjs->{$subj};
838 my ($mid, $ts) = @{delete $latest->{$subj}};
839 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
840 $subj = PublicInbox::Hval->new($subj)->as_html;
841 $pfx = indent_for($level);
842 my $nl = $level == $prev ? "\n" : '';
847 $cur ||= [ $ts, '' ];
848 my $dot = $level == 0 ? '' : '` ';
849 $cur->[0] = $ts if $ts > $cur->[0];
850 $cur->[1] .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>";
852 $cur->[1] .= "</b></a>\n";
855 my $attr = " $ts UTC";
857 # $n isn't the total number of posts on the topic,
858 # just the number of posts in the current results window
859 $n = $n == 1 ? '' : " ($n+ messages)";
861 if ($level == 0 || $attr ne $prev_attr) {
862 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
863 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
864 $pfx .= INDENT if $level > 0;
865 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
869 push @recent, $cur if $cur;
870 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
871 $fh->write(join('', @recent) . '</pre>');
874 sub emit_index_topics {
876 my $off = $state->{ctx}->{cgi}->param('o');
877 $off = 0 unless defined $off;
878 $state->{order} = [];
879 $state->{subjs} = {};
880 $state->{latest} = {};
882 my %opts = ( offset => int $off, limit => $max * 4 );
883 while (scalar @{$state->{order}} < $max) {
884 my $sres = $state->{srch}->query('', \%opts);
885 my $nr = scalar @{$sres->{msgs}} or last;
887 for (thread_results(load_results($sres), 1)->rootset) {
888 add_topic($state, $_, 0);
890 $opts{offset} += $nr;