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)
3 package PublicInbox::View;
6 use URI::Escape qw/uri_escape_utf8/;
7 use Date::Parse qw/str2time/;
8 use Encode qw/find_encoding/;
9 use Encode::MIME::Header;
10 use Email::MIME::ContentType qw/parse_content_type/;
11 use PublicInbox::Hval;
12 use PublicInbox::MID qw/mid_clean id_compress mid2path/;
13 use Digest::SHA qw/sha1_hex/;
15 my $MBOX_TITLE = 'title="download thread as gzipped mbox"';
18 # TODO: make these constants tunable
19 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
20 use constant MAX_TRUNC_LEN => 72;
21 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
22 use constant T_ANCHOR => '#u';
23 use constant INDENT => ' ';
25 *ascii_html = *PublicInbox::Hval::ascii_html;
27 my $enc_utf8 = find_encoding('UTF-8');
31 my ($ctx, $mime, $full_pfx, $footer) = @_;
32 if (defined $footer) {
33 $footer = "\n" . $footer;
37 headers_to_html_header($mime, $full_pfx, $ctx) .
38 multipart_text_as_html($mime, $full_pfx) .
39 '</pre><hr />' . PRE_WRAP .
40 html_footer($mime, 1, $full_pfx, $ctx) .
42 '</pre></body></html>';
46 my ($class, $mime, $full_pfx) = @_;
48 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
52 my ($header_obj) = @_;
53 my $irt = $header_obj->header('In-Reply-To');
55 return mid_clean($irt) if (defined $irt);
57 my $refs = $header_obj->header('References');
58 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
64 # this is already inside a <pre>
66 my ($fh, $mime, $level, $state) = @_;
67 my $midx = $state->{anchor_idx}++;
68 my $ctx = $state->{ctx};
69 my $srch = $ctx->{srch};
70 my ($prev, $next) = ($midx - 1, $midx + 1);
72 my $enc = enc_for($mime->header("Content-Type"));
73 my $subj = $mime->header('Subject');
74 my $header_obj = $mime->header_obj;
76 my $mid_raw = mid_clean($header_obj->header('Message-ID'));
77 my $id = anchor_for($mid_raw);
78 my $seen = $state->{seen};
79 $seen->{$id} = "#$id"; # save the anchor for children, later
81 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
82 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
83 my @from = Email::Address->parse($from);
84 $from = $from[0]->name;
86 $from = PublicInbox::Hval->new_oneline($from)->as_html;
87 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
88 my $more = 'permalink';
89 my $root_anchor = $state->{root_anchor} || '';
90 my $path = $root_anchor ? '../../' : '';
91 my $href = $mid->as_href;
92 my $irt = in_reply_to($header_obj);
93 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
96 my $t = $ctx->{flat} ? 'T' : 't';
97 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
99 if ($root_anchor eq $id) {
100 $subj = "<u\nid=\"u\">$subj</u>";
103 my $ts = _msg_date($mime);
104 my $rv = "<table\nsummary=l$level><tr>";
106 $rv .= '<td><pre>' . (INDENT x $level) . '</pre></td>';
108 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
109 $rv .= "<b\nid=\"$id\">$subj</b>\n";
110 $rv .= "- by $from @ $ts UTC - ";
111 $rv .= "<a\nhref=\"#s$next\">next</a>";
113 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
115 $fh->write($rv .= "\n\n");
117 my ($fhref, $more_ref);
118 my $mhref = "${path}$href/";
120 # show full message if it's our root message
121 if ($root_anchor ne $id || ($level != 0 && !$ctx->{flat})) {
122 $fhref = "${path}$href/f/";
125 # scan through all parts, looking for displayable text
126 $mime->walk_parts(sub {
127 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
131 my $txt = "${path}$href/raw";
132 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
133 $rv .= html_footer($mime, 0, undef, $ctx);
136 unless (defined $parent_anchor) {
137 my $v = PublicInbox::Hval->new_msgid($irt, 1);
139 $parent_anchor = "${path}$v/";
141 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
143 if (my $pct = $state->{pct}) {
144 $rv .= " [$pct->{$mid_raw}%]";
147 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
150 $rv .= " [<b>threaded</b>|" .
151 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
155 $fh->write($rv .= '</pre></td></tr></table>');
159 my ($ctx, $foot, $srch) = @_;
160 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
163 # only private functions below.
165 sub emit_thread_html {
166 my ($cb, $ctx, $foot, $srch) = @_;
167 my $mid = $ctx->{mid};
168 my $res = $srch->get_thread($mid);
169 my $msgs = load_results($res);
170 my $nr = scalar @$msgs;
171 return missing_thread($cb, $ctx) if $nr == 0;
172 my $flat = $ctx->{flat};
178 root_anchor => anchor_for($mid),
182 require PublicInbox::GitCatFile;
183 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
185 pre_anchor_entry($seen, $_) for (@$msgs);
186 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
188 my $th = thread_results($msgs);
189 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
192 Email::Address->purge_cache;
194 # there could be a race due to a message being deleted in git
195 # but still being in the Xapian index:
196 return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
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\n$MBOX_TITLE\nhref=\"../t.mbox.gz\">mbox.gz</a>";
204 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
205 $cb->write("<hr />" . PRE_WRAP . $next . "\n\n". $foot .
206 "</pre></body></html>");
211 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
212 my $s = add_text_body($enc, $part, $part_nr, $fhref);
215 # drop the remainder of git patches, they're usually better
216 # to review when the full message is viewed
217 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
220 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
223 # kill any leading or trailing whitespace lines
228 # kill per-line trailing whitespace
229 $s =~ s/[ \t]+$//sgm;
230 $s .= "\n" unless $s =~ /\n\z/s;
236 my ($ct, $default) = @_;
237 $default ||= $enc_utf8;
238 defined $ct or return $default;
239 my $ct_parsed = parse_content_type($ct);
241 if (my $charset = $ct_parsed->{attributes}->{charset}) {
242 my $enc = find_encoding($charset);
249 sub multipart_text_as_html {
250 my ($mime, $full_pfx, $srch) = @_;
253 my $enc = enc_for($mime->header("Content-Type"));
255 # scan through all parts, looking for displayable text
256 $mime->walk_parts(sub {
258 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
264 sub add_filename_line {
268 $fn = $enc->decode($fn);
270 $pad x= ($len/2) if ($len > 0);
271 "$pad " . ascii_html($fn) . " $pad\n";
274 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
276 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
279 my ($link_map, $s) = @_;
282 # salt this, as this could be exploited to show
283 # links in the HTML which don't show up in the raw mail.
284 my $key = sha1_hex($url . $SALT);
285 $link_map->{$key} = $url;
292 my ($link_map, $s) = @_;
294 # Added "PI-LINK-" prefix to avoid false-positives on git commits
295 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
297 my $url = $link_map->{$key};
299 $url = ascii_html($url);
300 "<a\nhref=\"$url\">$url</a>";
302 # false positive or somebody tried to mess with us
310 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
313 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
316 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
318 $rv = ascii_html($rv);
319 return linkify_2(\%l, $rv);
322 # show a short snippet of quoted text and link to full version:
323 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
324 my $cur = join(' ', @$quot);
325 @$quot = split(/\s+/, $cur);
328 my $tmp = shift(@$quot);
329 my $len = length($tmp) + length($cur);
330 if ($len > MAX_TRUNC_LEN) {
335 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
337 $cur =~ s/ \z/ .../s;
338 $cur = ascii_html($cur);
340 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
342 # show everything in the full version with anchor from
343 # short version (see above)
345 my $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
347 $rv = ascii_html($rv);
348 return linkify_2(\%l, $rv) unless $do_anchor;
350 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
355 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
356 return '' if $part->subparts;
358 my $ct = $part->content_type;
359 # account for filter bugs...
360 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
364 my $enc = enc_for($ct, $enc_msg);
369 $s = $enc->decode($s);
370 my @lines = split(/^/m, $s);
374 my $fn = $part->filename;
375 defined($fn) or $fn = "part #" . ($$part_nr + 1);
376 $s .= add_filename_line($enc, $fn);
380 while (defined(my $cur = shift @lines)) {
382 # show the previously buffered quote inline
384 $s .= flush_quote(\@quot, \$n, $$part_nr,
385 $full_pfx, 0, $do_anchor);
390 $cur = linkify_1(\%l, $cur);
391 $cur = ascii_html($cur);
392 $s .= linkify_2(\%l, $cur);
398 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
401 $s .= "\n" unless $s =~ /\n\z/s;
406 sub headers_to_html_header {
407 my ($mime, $full_pfx, $ctx) = @_;
408 my $srch = $ctx->{srch} if $ctx;
411 my $header_obj = $mime->header_obj;
412 my $mid = $header_obj->header('Message-ID');
413 $mid = PublicInbox::Hval->new_msgid($mid);
414 foreach my $h (qw(From To Cc Subject Date)) {
415 my $v = $header_obj->header($h);
416 defined($v) && ($v ne '') or next;
417 $v = PublicInbox::Hval->new_oneline($v);
420 my @from = Email::Address->parse($v->raw);
421 $title[1] = ascii_html($from[0]->name);
422 } elsif ($h eq 'Subject') {
423 $title[0] = $v->as_html;
425 my $p = $full_pfx ? '' : '../';
426 $rv .= "$h: <a\nid=\"t\"\nhref=\"${p}t/#u\">";
427 $rv .= $v->as_html . "</a>\n";
431 $rv .= "$h: " . $v->as_html . "\n";
434 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
435 my $upfx = $full_pfx ? '' : '../';
436 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
439 if ($header_obj->header('In-Reply-To') ||
440 $header_obj->header('References')) {
441 $rv .= "<a\nhref=\"${upfx}t/#u\">" .
442 "References: [expand]</a>\n";
445 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
446 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
448 $rv .= _parent_headers_nosrch($header_obj);
453 ("<html><head><title>". join(' - ', @title) .
454 "</title>$atom</head><body>" . PRE_WRAP . $rv);
458 my ($dst, $ctx, $cur, $full_pfx) = @_;
459 my $srch = $ctx->{srch};
460 my $mid = mid_clean($cur->header('Message-ID'));
461 my $res = $srch->get_thread($mid);
462 my $nr = $res->{total};
465 $$dst .= "\n[no followups, yet]\n";
466 return (undef, in_reply_to($cur));
468 my $upfx = $full_pfx ? '' : '../';
470 $$dst .= "\n\n~$nr messages in thread: ".
471 "(<a\nhref=\"${upfx}t/#u\">expand</a>)\n";
472 my $subj = $srch->subject_path($cur->header('Subject'));
473 my $parent = in_reply_to($cur);
475 seen => { $subj => 1 },
478 parent_cmp => defined $parent ? $parent : '',
483 for (thread_results(load_results($res))->rootset) {
484 inline_dump($dst, $state, $upfx, $_, 0);
486 ($state->{next_msg}, $state->{parent});
489 sub _parent_headers_nosrch {
490 my ($header_obj) = @_;
493 my $irt = in_reply_to($header_obj);
495 my $v = PublicInbox::Hval->new_msgid($irt, 1);
496 my $html = $v->as_html;
497 my $href = $v->as_href;
498 $rv .= "In-Reply-To: <";
499 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
502 my $refs = $header_obj->header('References');
504 # avoid redundant URLs wasting bandwidth
506 $seen{$irt} = 1 if defined $irt;
508 my @raw_refs = ($refs =~ /<([^>]+)>/g);
509 foreach my $ref (@raw_refs) {
512 push @refs, linkify_ref_nosrch($ref);
516 $rv .= 'References: '. join(' ', @refs) . "\n";
523 my ($mime, $standalone, $full_pfx, $ctx) = @_;
524 my %cc; # everyone else
525 my $to; # this is the From address
527 foreach my $h (qw(From To Cc)) {
528 my $v = $mime->header($h);
529 defined($v) && ($v ne '') or next;
530 my @addrs = Email::Address->parse($v);
531 foreach my $recip (@addrs) {
532 my $address = $recip->address;
533 my $dst = lc($address);
534 $cc{$dst} ||= $address;
538 Email::Address->purge_cache if $standalone;
540 my $subj = $mime->header('Subject') || '';
541 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
542 my $mid = $mime->header('Message-ID');
543 my $irt = uri_escape_utf8($mid);
545 $to = uri_escape_utf8($to);
546 $subj = uri_escape_utf8($subj);
548 my $cc = uri_escape_utf8(join(',', sort values %cc));
549 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
552 my $srch = $ctx->{srch} if $ctx;
553 my $upfx = $full_pfx ? '../' : '../../';
554 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
556 if ($srch && $standalone) {
557 $idx .= qq{ / follow: <a\nhref="t.atom">Atom feed</a>};
560 my ($next, $p) = thread_inline(\$idx, $ctx, $mime, $full_pfx);
562 $p = PublicInbox::Hval->new_oneline($p);
564 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
566 $irt = ' ' x length('parent ');
569 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
577 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
580 sub linkify_ref_nosrch {
581 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
582 my $html = $v->as_html;
583 my $href = $v->as_href;
584 "<<a\nhref=\"../$href/\">$html</a>>";
590 if ($id !~ /\A[a-f0-9]{40}\z/) {
591 $id = id_compress(mid_clean($id), 1);
596 sub thread_html_head {
597 my ($cb, $header, $state) = @_;
598 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
600 my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
602 $$cb->write("<html><head><title>$s</title>".
603 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
604 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
608 sub pre_anchor_entry {
609 my ($seen, $mime) = @_;
610 my $id = anchor_for($mime->header('Message-ID'));
611 $seen->{$id} = "#$id"; # save the anchor for children, later
615 my ($upfx, $mid) = @_;
616 # 'subject dummy' is used internally by Mail::Thread
617 return '[no common parent]' if ($mid eq 'subject dummy');
619 $mid = PublicInbox::Hval->new_msgid($mid);
620 my $href = $mid->as_href;
621 my $html = $mid->as_html;
622 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
626 my ($upfx, $mid, $level) = @_;
627 "<table\nsummary=ghost><tr><td>" .
628 (INDENT x $level) . "</td><td>" .
629 PRE_WRAP . ghost_parent($upfx, $mid) .
630 '</pre></td></table>';
634 my ($cb, $git, $state, $mime, $level) = @_;
636 # lazy load the full message from mini_mime:
638 my $path = mid2path(mid_clean($mime->header('Message-ID')));
639 Email::MIME->new($git->cat_file('HEAD:'.$path));
642 if ($state->{anchor_idx} == 0) {
643 thread_html_head($cb, $mime, $state);
646 if (my $ghost = delete $state->{ghost}) {
647 # n.b. ghost messages may only be parents, not children
648 foreach my $g (@$ghost) {
649 $$cb->write(ghost_table('../../', @$g));
652 index_entry($$cb, $mime, $level, $state);
657 my ($state, $node, $level) = @_;
658 my $ghost = $state->{ghost} ||= [];
659 push @$ghost, [ $node->messageid, $level ];
663 my ($cb, $git, $state, $node, $level) = @_;
665 if (my $mime = $node->message) {
666 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
667 __ghost_entry($state, $node, $level);
670 __ghost_entry($state, $node, $level);
673 thread_entry($cb, $git, $state, $node->child, $level + 1);
674 thread_entry($cb, $git, $state, $node->next, $level);
680 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
685 my $ts = eval { str2time($mime->header('Date')) };
686 defined($ts) ? $ts : 0;
690 my ($msgs, $nosubject) = @_;
691 require PublicInbox::Thread;
692 my $th = PublicInbox::Thread->new(@$msgs);
694 $Mail::Thread::nosubject = $nosubject;
696 $th->order(*sort_ts);
702 require PublicInbox::ExtMsg;
704 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
709 my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
710 POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
714 my ($dst, $state, $upfx, $mime, $level) = @_;
715 my $pfx = INDENT x ($level - 1);
716 my $dot = $level == 0 ? '' : '` ';
718 my $cur = $state->{cur};
719 my $mid = mid_clean($mime->header('Message-ID'));
720 my $f = $mime->header('X-PI-From');
721 my $d = _msg_date($mime);
722 $f = PublicInbox::Hval->new($f)->as_html;
723 $d = PublicInbox::Hval->new($d)->as_html;
724 my $attr = "$f @ $d";
725 $state->{first_level} ||= $level;
726 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
727 $state->{prev_attr} = $attr;
728 $attr = ' - ' . $attr;
729 $attr .= ' UTC' if $level >= $state->{first_level};
733 $state->{prev_level} = $level;
737 delete $state->{cur};
738 $$dst .= "$pfx$dot<b><a\nid=\"r\"\nhref=\"#t\">".
739 "[this message]</a></b>$attr\n";
744 $state->{next_msg} ||= $mid;
747 # Subject is never undef, this mail was loaded from
748 # our Xapian which would've resulted in '' if it were
749 # really missing (and Filter rejects empty subjects)
750 my $s = $mime->header('Subject');
751 my $h = $state->{srch}->subject_path($s);
752 if ($state->{seen}->{$h}) {
755 $state->{seen}->{$h} = 1;
756 $s = PublicInbox::Hval->new($s);
759 my $m = PublicInbox::Hval->new_msgid($mid);
760 $m = $upfx . '../' . $m->as_href . '/';
762 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a>$attr\n";
764 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f @ $d</a>\n";
769 my ($dst, $state, $upfx, $node, $level) = @_;
771 if (my $mime = $node->message) {
772 my $mid = mid_clean($mime->header('Message-ID'));
773 if ($mid eq $state->{parent_cmp}) {
774 $state->{parent} = $mid;
776 _inline_header($dst, $state, $upfx, $mime, $level);
778 my $dot = $level == 0 ? '' : '` ';
779 my $pfx = (INDENT x $level) . $dot;
781 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
783 inline_dump($dst, $state, $upfx, $node->child, $level+1);
784 inline_dump($dst, $state, $upfx, $node->next, $level);
789 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
790 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
796 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
797 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
801 # accumulate recent topics if search is supported
802 # returns 1 if done, undef if not
804 my ($state, $node, $level) = @_;
806 my $child_adjust = 1;
808 if (my $x = $node->message) {
812 $subj = $x->header('Subject');
813 $subj = $state->{srch}->subject_normalized($subj);
816 # kill "[PATCH v2]" etc. for summarization
817 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
818 $topic = substr($topic, 0, 30);
820 if (++$state->{subjs}->{$topic} == 1) {
821 push @{$state->{order}}, [ $level, $subj, $topic ];
824 my $mid = mid_clean($x->header('Message-ID'));
826 my $u = $x->header('X-PI-From');
827 my $ts = $x->header('X-PI-TS');
828 my $exist = $state->{latest}->{$topic};
829 if (!$exist || $exist->[2] < $ts) {
830 $state->{latest}->{$topic} = [ $mid, $u, $ts ];
833 # ghost message, do not bump level
837 add_topic($state, $node->child, $level + $child_adjust);
838 add_topic($state, $node->next, $level);
843 my $order = $state->{order};
844 my $subjs = $state->{subjs};
845 my $latest = $state->{latest};
846 return "\n[No recent topics]</pre>" unless (scalar @$order);
851 while (defined(my $info = shift @$order)) {
852 my ($level, $subj, $topic) = @$info;
853 my $n = delete $subjs->{$topic};
854 my ($mid, $u, $ts) = @{delete $latest->{$topic}};
855 $mid = PublicInbox::Hval->new($mid)->as_href;
856 $subj = PublicInbox::Hval->new($subj)->as_html;
857 $u = PublicInbox::Hval->new($u)->as_html;
858 $pfx = INDENT x ($level - 1);
859 my $nl = $level == $prev ? "\n" : '';
860 my $dot = $level == 0 ? '' : '` ';
861 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
864 $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
866 $attr = "created by $u @ $ts UTC";
869 # $n isn't the total number of posts on the topic,
870 # just the number of posts in the current results
871 # window, so leave it unlabeled
872 $attr = "updated by $u @ $ts UTC";
875 if ($level == 0 || $attr ne $prev_attr) {
876 my $mbox = qq(<a\n$MBOX_TITLE\n) .
877 qq(href="$mid/t.mbox.gz">mbox.gz</a>);
878 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
879 $pfx .= INDENT if $level > 0;
880 $dst .= "$pfx- ". $attr . $n;
881 $dst .= " - $mbox / $atom\n";
888 sub emit_index_topics {
889 my ($state, $fh) = @_;
890 my $off = $state->{ctx}->{cgi}->param('o');
891 $off = 0 unless defined $off;
892 $state->{order} = [];
893 $state->{subjs} = {};
894 $state->{latest} = {};
896 my %opts = ( offset => int $off, limit => $max * 4 );
897 while (scalar @{$state->{order}} < $max) {
898 my $res = $state->{srch}->query('', \%opts);
899 my $nr = scalar @{$res->{msgs}} or last;
901 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
902 add_topic($state, $_, 0);
904 $opts{offset} += $nr;
907 $fh->write(dump_topics($state));