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;
264 if (defined $fn && $fn =~ /\A[\w\.-]+[a-zA-Z0-9]\z/) {
266 } elsif ($ct eq 'text/plain') {
271 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
272 my $ts = "Type: $ct, Size: $size bytes";
273 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
274 join('', @ret, '</a>');
278 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
279 my ($part, $depth, @idx) = @$p;
280 my $ct = $part->content_type;
281 my $fn = $part->filename;
283 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
284 return attach_link($upfx, $ct, $p, $fn);
287 my $s = eval { $part->body_str };
289 # badly-encoded message? tell the world about it!
290 return attach_link($upfx, $ct, $p, $fn) if $@;
292 my @lines = split(/^/m, $s);
294 if (defined($fn) || $depth > 0) {
295 $s .= attach_link($upfx, $ct, $p, $fn);
299 my $l = PublicInbox::Linkify->new;
300 while (defined(my $cur = shift @lines)) {
302 # show the previously buffered quote inline
303 flush_quote(\$s, $l, \@quot) if @quot;
306 $cur = $l->linkify_1($cur);
307 $cur = ascii_html($cur);
308 $s .= $l->linkify_2($cur);
314 flush_quote(\$s, $l, \@quot) if @quot;
315 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
316 $s =~ s/\A\n+//s; # kill leading blank lines
317 $s =~ s/\s+\z//s; # kill all trailing spaces (final "\n" added if ne '')
321 sub headers_to_html_header {
322 my ($hdr, $ctx) = @_;
323 my $srch = $ctx->{srch} if $ctx;
329 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
330 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
334 my $mid = $hdr->header_raw('Message-ID');
335 $mid = PublicInbox::Hval->new_msgid($mid);
336 foreach my $h (qw(From To Cc Subject Date)) {
337 my $v = $hdr->header($h);
338 defined($v) && ($v ne '') or next;
339 $v = PublicInbox::Hval->new($v);
342 my @from = Email::Address->parse($v->raw);
343 $title[1] = ascii_html($from[0]->name);
344 } elsif ($h eq 'Subject') {
345 $title[0] = $v->as_html;
347 $rv .= qq($h: <a\nhref="#r"\nid=t>);
348 $rv .= $v->as_html . "</a>\n";
352 $rv .= "$h: " . $v->as_html . "\n";
355 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
356 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
357 $rv .= _parent_headers($hdr, $srch);
360 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
361 PublicInbox::Hval::STYLE .
362 "</head><body><pre\nid=b>" . # anchor for body start
367 my ($dst, $ctx, $hdr, $tpfx) = @_;
368 my $srch = $ctx->{srch};
369 my $mid = mid_clean($hdr->header_raw('Message-ID'));
370 my $sres = $srch->get_thread($mid);
371 my $nr = $sres->{total};
372 my $expand = qq(<a\nhref="${tpfx}t/#u">expand</a> ) .
373 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
374 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
376 my $parent = in_reply_to($hdr);
378 if (defined $parent) {
379 $$dst .= "($expand)\n ";
380 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
382 $$dst .= "[no followups, yet] ($expand)\n";
384 $ctx->{next_msg} = undef;
385 $ctx->{parent_msg} = $parent;
389 $$dst .= "$nr+ messages in thread ($expand";
390 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
392 my $subj = $srch->subject_path($hdr->header('Subject'));
394 seen => { $subj => 1 },
400 for (thread_results(load_results($sres))->rootset) {
401 skel_dump($dst, $state, $tpfx, $_, 0);
403 $ctx->{next_msg} = $state->{next_msg};
404 $ctx->{parent_msg} = $parent;
407 sub _parent_headers {
408 my ($hdr, $srch) = @_;
411 my $irt = in_reply_to($hdr);
413 my $v = PublicInbox::Hval->new_msgid($irt, 1);
414 my $html = $v->as_html;
415 my $href = $v->as_href;
416 $rv .= "In-Reply-To: <";
417 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
420 # do not display References: if search is present,
421 # we show the thread skeleton at the bottom, instead.
424 my $refs = $hdr->header_raw('References');
426 # avoid redundant URLs wasting bandwidth
428 $seen{$irt} = 1 if defined $irt;
430 my @raw_refs = ($refs =~ /<([^>]+)>/g);
431 foreach my $ref (@raw_refs) {
434 push @refs, linkify_ref_nosrch($ref);
438 $rv .= 'References: '. join(' ', @refs) . "\n";
444 sub mailto_arg_link {
446 my %cc; # everyone else
447 my $to; # this is the From address
449 foreach my $h (qw(From To Cc)) {
450 my $v = $hdr->header($h);
451 defined($v) && ($v ne '') or next;
452 my @addrs = Email::Address->parse($v);
453 foreach my $recip (@addrs) {
454 my $address = $recip->address;
455 my $dst = lc($address);
456 $cc{$dst} ||= $address;
460 Email::Address->purge_cache;
463 my $subj = $hdr->header('Subject') || '';
464 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
465 my $mid = $hdr->header_raw('Message-ID');
466 push @arg, "--in-reply-to='" . ascii_html($mid) . "'";
467 my $irt = uri_escape_utf8($mid);
469 push @arg, '--to=' . ascii_html($to);
470 $to = uri_escape_utf8($to);
471 $subj = uri_escape_utf8($subj);
472 my $cc = join(',', sort values %cc);
473 push @arg, '--cc=' . ascii_html($cc);
474 $cc = uri_escape_utf8($cc);
475 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
482 my ($hdr, $standalone, $ctx, $rhref) = @_;
484 my $srch = $ctx->{srch} if $ctx;
487 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
491 thread_skel(\$idx, $ctx, $hdr, $tpfx);
492 my $p = $ctx->{parent_msg};
493 my $next = $ctx->{next_msg};
495 $p = PublicInbox::Hval->new_msgid($p);
497 $irt = "<a\nhref=\"$upfx$p/\"rel=prev>parent</a> ";
499 $irt = ' ' x length('parent ');
502 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
503 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
505 $irt .= ' ' x length('next ');
511 $irt . qq(<a\nhref="$rhref">reply</a>) . $idx;
514 sub linkify_ref_nosrch {
515 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
516 my $html = $v->as_html;
517 my $href = $v->as_href;
518 "<<a\nhref=\"../$href/\">$html</a>>";
524 if ($id !~ /\A[a-f0-9]{40}\z/) {
525 $id = id_compress(mid_clean($id), 1);
530 sub thread_html_head {
531 my ($hdr, $state) = @_;
532 my $res = delete $state->{res} or die "BUG: no Plack callback in {res}";
533 my $fh = $res->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
536 my $s = ascii_html($hdr->header('Subject'));
537 $fh->write("<html><head><title>$s</title>".
538 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
539 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
540 PublicInbox::Hval::STYLE .
544 sub pre_anchor_entry {
545 my ($seen, $mime) = @_;
546 my $id = anchor_for(mid_mime($mime));
547 $seen->{$id} = "#$id"; # save the anchor for children, later
551 my ($upfx, $mid) = @_;
552 # 'subject dummy' is used internally by Mail::Thread
553 return '[no common parent]' if ($mid eq 'subject dummy');
555 $mid = PublicInbox::Hval->new_msgid($mid);
556 my $href = $mid->as_href;
557 my $html = $mid->as_html;
558 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
561 sub thread_adj_level {
562 my ($state, $level) = @_;
564 my $max = $state->{cur_level};
566 return '' if $max == 0; # flat output
568 # reset existing lists
569 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
570 $state->{fh}->write($x . '</ul>');
571 $state->{cur_level} = 0;
574 if ($level == $max) { # continue existing list
575 $state->{fh}->write('<li>');
576 } elsif ($level < $max) {
577 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
578 $state->{fh}->write($x .= '<li>');
579 $state->{cur_level} = $level;
580 } else { # ($level > $max) # start a new level
581 $state->{cur_level} = $level;
582 $state->{fh}->write(($max ? '<li>' : '') . '<ul><li>');
588 my ($state, $upfx, $mid, $level) = @_;
589 my $end = '<pre>'. ghost_parent($upfx, $mid) . '</pre>';
590 $state->{fh}->write($end .= thread_adj_level($state, $level));
594 my ($state, $mime, $level) = @_;
596 # lazy load the full message from mini_mime:
598 my $path = mid2path(mid_clean(mid_mime($mime)));
599 Email::MIME->new($state->{ctx}->{git}->cat_file('HEAD:'.$path));
602 thread_html_head($mime, $state) if $state->{anchor_idx} == 0;
603 if (my $ghost = delete $state->{ghost}) {
604 # n.b. ghost messages may only be parents, not children
605 foreach my $g (@$ghost) {
606 ghost_flush($state, '../../', @$g);
609 my $end = thread_adj_level($state, $level);
610 index_entry($mime, $level, $state);
611 $state->{fh}->write($end) if $end;
618 INDENT x ($level - 1);
621 sub __ghost_prepare {
622 my ($state, $node, $level) = @_;
623 my $ghost = $state->{ghost} ||= [];
624 push @$ghost, [ $node->messageid, $level ];
628 my ($state, $node, $level) = @_;
630 if (my $mime = $node->message) {
631 unless (__thread_entry($state, $mime, $level)) {
632 __ghost_prepare($state, $node, $level);
635 __ghost_prepare($state, $node, $level);
638 thread_entry($state, $node->child, $level + 1);
639 thread_entry($state, $node->next, $level);
645 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
650 my $ts = eval { str2time($hdr->header('Date')) };
651 defined($ts) ? $ts : 0;
656 require PublicInbox::Thread;
657 my $th = PublicInbox::Thread->new(@$msgs);
659 $th->order(*sort_ts);
664 my ($res, $ctx) = @_;
665 require PublicInbox::ExtMsg;
667 $res->(PublicInbox::ExtMsg::ext_msg($ctx))
672 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
676 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
679 my ($dst, $state, $upfx, $hdr, $level) = @_;
681 my $cur = $state->{cur};
682 my $mid = mid_clean($hdr->header_raw('Message-ID'));
683 my $f = ascii_html($hdr->header('X-PI-From'));
684 my $d = _msg_date($hdr);
685 my $pfx = "$d " . indent_for($level) . th_pfx($level);
687 $state->{first_level} ||= $level;
689 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
690 $state->{prev_attr} = $attr;
694 $state->{prev_level} = $level;
698 delete $state->{cur};
699 $$dst .= "$pfx<b><a\nid=r\nhref=\"#t\">".
700 "$attr [this message]</a></b>\n";
705 $state->{next_msg} ||= $mid;
708 # Subject is never undef, this mail was loaded from
709 # our Xapian which would've resulted in '' if it were
710 # really missing (and Filter rejects empty subjects)
711 my $s = $hdr->header('Subject');
712 my $h = $state->{srch}->subject_path($s);
713 if ($state->{seen}->{$h}) {
716 $state->{seen}->{$h} = 1;
717 $s = PublicInbox::Hval->new($s);
720 my $m = PublicInbox::Hval->new_msgid($mid);
721 $m = $upfx . '../' . $m->as_href . '/';
722 $$dst .= "$pfx<a\nhref=\"$m\">";
723 $$dst .= defined($s) ? "$s</a> $f\n" : "$f</a>\n";
727 my ($dst, $state, $upfx, $node, $level) = @_;
729 if (my $mime = $node->message) {
730 my $hdr = $mime->header_obj;
731 my $mid = mid_clean($hdr->header_raw('Message-ID'));
732 _skel_header($dst, $state, $upfx, $hdr, $level);
734 my $mid = $node->messageid;
735 if ($mid eq 'subject dummy') {
736 $$dst .= "\t[no common parent]\n";
738 $$dst .= ' [not found] ';
739 $$dst .= indent_for($level) . th_pfx($level);
740 $mid = PublicInbox::Hval->new_msgid($mid);
741 my $href = "$upfx../" . $mid->as_href . '/';
742 my $html = $mid->as_html;
743 $$dst .= qq{<<a\nhref="$href">$html</a>>\n};
746 skel_dump($dst, $state, $upfx, $node->child, $level+1);
747 skel_dump($dst, $state, $upfx, $node->next, $level);
752 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
753 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
757 # accumulate recent topics if search is supported
758 # returns 1 if done, undef if not
760 my ($state, $node, $level) = @_;
762 my $child_adjust = 1;
764 if (my $x = $node->message) {
768 $subj = $x->header('Subject');
769 $subj = $state->{srch}->subject_normalized($subj);
771 if (++$state->{subjs}->{$subj} == 1) {
772 push @{$state->{order}}, [ $level, $subj ];
775 my $mid = mid_clean($x->header_raw('Message-ID'));
777 my $ts = $x->header('X-PI-TS');
778 my $exist = $state->{latest}->{$subj};
779 if (!$exist || $exist->[1] < $ts) {
780 $state->{latest}->{$subj} = [ $mid, $ts ];
783 # ghost message, do not bump level
787 add_topic($state, $node->child, $level + $child_adjust);
788 add_topic($state, $node->next, $level);
793 my $order = $state->{order};
794 my $subjs = $state->{subjs};
795 my $latest = $state->{latest};
796 my $fh = $state->{fh};
797 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
803 while (defined(my $info = shift @$order)) {
804 my ($level, $subj) = @$info;
805 my $n = delete $subjs->{$subj};
806 my ($mid, $ts) = @{delete $latest->{$subj}};
807 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
808 $subj = PublicInbox::Hval->new($subj)->as_html;
809 $pfx = indent_for($level);
810 my $nl = $level == $prev ? "\n" : '';
815 $cur ||= [ $ts, '' ];
816 $cur->[0] = $ts if $ts > $cur->[0];
817 $cur->[1] .= $nl . $pfx . th_pfx($level) .
818 "<a\nhref=\"$mid/t/#u\"><b>" .
819 $subj . "</b></a>\n";
822 my $attr = " $ts UTC";
824 # $n isn't the total number of posts on the topic,
825 # just the number of posts in the current results window
826 $n = $n == 1 ? '' : " ($n+ messages)";
828 if ($level == 0 || $attr ne $prev_attr) {
829 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
830 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
831 $pfx .= INDENT if $level > 0;
832 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
836 push @recent, $cur if $cur;
837 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
838 $fh->write(join('', @recent) . '</pre>');
841 sub emit_index_topics {
843 my $off = $state->{ctx}->{cgi}->param('o');
844 $off = 0 unless defined $off;
845 $state->{order} = [];
846 $state->{subjs} = {};
847 $state->{latest} = {};
849 my %opts = ( offset => int $off, limit => $max * 4 );
850 while (scalar @{$state->{order}} < $max) {
851 my $sres = $state->{srch}->query('', \%opts);
852 my $nr = scalar @{$sres->{msgs}} or last;
854 for (thread_results(load_results($sres))->rootset) {
855 add_topic($state, $_, 0);
857 $opts{offset} += $nr;