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 mid_compress mid2path/;
13 use Digest::SHA qw/sha1_hex/;
17 # TODO: make these constants tunable
18 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
19 use constant MAX_TRUNC_LEN => 72;
20 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
21 use constant T_ANCHOR => '#u';
22 use constant INDENT => ' ';
24 *ascii_html = *PublicInbox::Hval::ascii_html;
26 my $enc_utf8 = find_encoding('UTF-8');
30 my ($ctx, $mime, $full_pfx, $footer) = @_;
31 if (defined $footer) {
32 $footer = "\n" . $footer;
36 headers_to_html_header($mime, $full_pfx, $ctx) .
37 multipart_text_as_html($mime, $full_pfx) .
38 '</pre><hr />' . PRE_WRAP .
39 html_footer($mime, 1, $full_pfx, $ctx) .
41 '</pre></body></html>';
45 my ($class, $mime, $full_pfx) = @_;
47 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
51 my ($header_obj) = @_;
52 my $irt = $header_obj->header('In-Reply-To');
54 return mid_clean($irt) if (defined $irt);
56 my $refs = $header_obj->header('References');
57 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
63 # this is already inside a <pre>
65 my ($fh, $mime, $level, $state) = @_;
66 my $midx = $state->{anchor_idx}++;
67 my $ctx = $state->{ctx};
68 my $srch = $ctx->{srch};
69 my ($prev, $next) = ($midx - 1, $midx + 1);
71 my $enc = enc_for($mime->header("Content-Type"));
72 my $subj = $mime->header('Subject');
73 my $header_obj = $mime->header_obj;
75 my $mid_raw = mid_clean($header_obj->header('Message-ID'));
76 my $id = anchor_for($mid_raw);
77 my $seen = $state->{seen};
78 $seen->{$id} = "#$id"; # save the anchor for children, later
80 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
81 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
82 my @from = Email::Address->parse($from);
83 $from = $from[0]->name;
85 $from = PublicInbox::Hval->new_oneline($from)->as_html;
86 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
87 my $more = 'permalink';
88 my $root_anchor = $state->{root_anchor} || '';
89 my $path = $root_anchor ? '../../' : '';
90 my $href = $mid->as_href;
91 my $irt = in_reply_to($header_obj);
92 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
95 my $t = $ctx->{flat} ? 'T' : 't';
96 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
98 if ($root_anchor eq $id) {
99 $subj = "<u\nid=\"u\">$subj</u>";
102 my $ts = _msg_date($mime);
103 my $rv = "<table\nsummary=l$level><tr>";
105 $rv .= '<td><pre>' . (INDENT x $level) . '</pre></td>';
107 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
108 $rv .= "<b\nid=\"$id\">$subj</b>\n";
109 $rv .= "- by $from @ $ts UTC - ";
110 $rv .= "<a\nhref=\"#s$next\">next</a>";
112 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
114 $fh->write($rv .= "\n\n");
116 my ($fhref, $more_ref);
117 my $mhref = "${path}$href/";
119 # show full message if it's our root message
120 if ($root_anchor ne $id || ($level != 0 && !$ctx->{flat})) {
121 $fhref = "${path}$href/f/";
124 # scan through all parts, looking for displayable text
125 $mime->walk_parts(sub {
126 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
130 my $txt = "${path}$href/raw";
131 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
132 $rv .= html_footer($mime, 0, undef, $ctx);
135 unless (defined $parent_anchor) {
136 my $v = PublicInbox::Hval->new_msgid($irt, 1);
138 $parent_anchor = "${path}$v/";
140 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
142 if (my $pct = $state->{pct}) {
143 $rv .= " [$pct->{$mid_raw}%]";
146 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
149 $rv .= " [<b>threaded</b>|" .
150 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
154 $fh->write($rv .= '</pre></td></tr></table>');
158 my ($ctx, $foot, $srch) = @_;
159 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
162 # only private functions below.
164 sub emit_thread_html {
165 my ($cb, $ctx, $foot, $srch) = @_;
166 my $mid = $ctx->{mid};
167 my $res = $srch->get_thread($mid);
168 my $msgs = load_results($res);
169 my $nr = scalar @$msgs;
170 return missing_thread($cb, $ctx) if $nr == 0;
171 my $flat = $ctx->{flat};
177 root_anchor => anchor_for($mid),
181 require PublicInbox::GitCatFile;
182 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
184 pre_anchor_entry($seen, $_) for (@$msgs);
185 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
187 my $th = thread_results($msgs);
188 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
191 Email::Address->purge_cache;
193 # there could be a race due to a message being deleted in git
194 # but still being in the Xapian index:
195 return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
197 my $final_anchor = $state->{anchor_idx};
198 my $next = "<a\nid=\"s$final_anchor\">";
199 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
200 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
201 $next .= "\ndownload thread: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
202 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
203 $cb->write("<hr />" . PRE_WRAP . $next . "\n\n". $foot .
204 "</pre></body></html>");
209 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
210 my $s = add_text_body($enc, $part, $part_nr, $fhref);
213 # drop the remainder of git patches, they're usually better
214 # to review when the full message is viewed
215 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
218 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
221 # kill any leading or trailing whitespace lines
226 # kill per-line trailing whitespace
227 $s =~ s/[ \t]+$//sgm;
228 $s .= "\n" unless $s =~ /\n\z/s;
234 my ($ct, $default) = @_;
235 $default ||= $enc_utf8;
236 defined $ct or return $default;
237 my $ct_parsed = parse_content_type($ct);
239 if (my $charset = $ct_parsed->{attributes}->{charset}) {
240 my $enc = find_encoding($charset);
247 sub multipart_text_as_html {
248 my ($mime, $full_pfx, $srch) = @_;
251 my $enc = enc_for($mime->header("Content-Type"));
253 # scan through all parts, looking for displayable text
254 $mime->walk_parts(sub {
256 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
262 sub add_filename_line {
266 $fn = $enc->decode($fn);
268 $pad x= ($len/2) if ($len > 0);
269 "$pad " . ascii_html($fn) . " $pad\n";
272 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
274 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
277 my ($link_map, $s) = @_;
280 # salt this, as this could be exploited to show
281 # links in the HTML which don't show up in the raw mail.
282 my $key = sha1_hex($url . $SALT);
283 $link_map->{$key} = $url;
290 my ($link_map, $s) = @_;
292 # Added "PI-LINK-" prefix to avoid false-positives on git commits
293 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
295 my $url = $link_map->{$key};
297 $url = ascii_html($url);
298 "<a\nhref=\"$url\">$url</a>";
300 # false positive or somebody tried to mess with us
308 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
311 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
314 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
316 $rv = ascii_html($rv);
317 return linkify_2(\%l, $rv);
320 # show a short snippet of quoted text and link to full version:
321 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
322 my $cur = join(' ', @$quot);
323 @$quot = split(/\s+/, $cur);
326 my $tmp = shift(@$quot);
327 my $len = length($tmp) + length($cur);
328 if ($len > MAX_TRUNC_LEN) {
333 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
335 $cur =~ s/ \z/ .../s;
336 $cur = ascii_html($cur);
338 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
340 # show everything in the full version with anchor from
341 # short version (see above)
343 my $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
345 $rv = ascii_html($rv);
346 return linkify_2(\%l, $rv) unless $do_anchor;
348 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
353 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
354 return '' if $part->subparts;
356 my $ct = $part->content_type;
357 # account for filter bugs...
358 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
362 my $enc = enc_for($ct, $enc_msg);
367 $s = $enc->decode($s);
368 my @lines = split(/^/m, $s);
372 my $fn = $part->filename;
373 defined($fn) or $fn = "part #" . ($$part_nr + 1);
374 $s .= add_filename_line($enc, $fn);
378 while (defined(my $cur = shift @lines)) {
380 # show the previously buffered quote inline
382 $s .= flush_quote(\@quot, \$n, $$part_nr,
383 $full_pfx, 0, $do_anchor);
388 $cur = linkify_1(\%l, $cur);
389 $cur = ascii_html($cur);
390 $s .= linkify_2(\%l, $cur);
396 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
399 $s .= "\n" unless $s =~ /\n\z/s;
404 sub headers_to_html_header {
405 my ($mime, $full_pfx, $ctx) = @_;
406 my $srch = $ctx->{srch} if $ctx;
409 my $header_obj = $mime->header_obj;
410 my $mid = $header_obj->header('Message-ID');
411 $mid = PublicInbox::Hval->new_msgid($mid);
412 foreach my $h (qw(From To Cc Subject Date)) {
413 my $v = $header_obj->header($h);
414 defined($v) && ($v ne '') or next;
415 $v = PublicInbox::Hval->new_oneline($v);
418 my @from = Email::Address->parse($v->raw);
419 $title[1] = ascii_html($from[0]->name);
420 } elsif ($h eq 'Subject') {
421 $title[0] = $v->as_html;
423 my $p = $full_pfx ? '' : '../';
424 $rv .= "$h: <a\nid=\"t\"\nhref=\"${p}t/#u\">";
425 $rv .= $v->as_html . "</a>\n";
429 $rv .= "$h: " . $v->as_html . "\n";
432 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
433 my $upfx = $full_pfx ? '' : '../';
434 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
437 if ($header_obj->header('In-Reply-To') ||
438 $header_obj->header('References')) {
439 $rv .= "<a\nhref=\"${upfx}t/#u\">" .
440 "References: [expand]</a>\n";
443 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
444 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
446 $rv .= _parent_headers_nosrch($header_obj);
451 ("<html><head><title>". join(' - ', @title) .
452 "</title>$atom</head><body>" . PRE_WRAP . $rv);
456 my ($dst, $ctx, $cur, $full_pfx) = @_;
457 my $srch = $ctx->{srch};
458 my $mid = mid_clean($cur->header('Message-ID'));
459 my $res = $srch->get_thread($mid);
460 my $nr = $res->{total};
463 $$dst .= "\n[no followups, yet]\n";
464 return (undef, in_reply_to($cur));
466 my $upfx = $full_pfx ? '' : '../';
468 $$dst .= "\n\n~$nr messages in thread: ".
469 "(<a\nhref=\"${upfx}t/#u\">expand</a>)\n";
470 my $subj = $srch->subject_path($cur->header('Subject'));
471 my $parent = in_reply_to($cur);
473 seen => { $subj => 1 },
476 parent_cmp => defined $parent ? $parent : '',
481 for (thread_results(load_results($res))->rootset) {
482 inline_dump($dst, $state, $upfx, $_, 0);
484 ($state->{next_msg}, $state->{parent});
487 sub _parent_headers_nosrch {
488 my ($header_obj) = @_;
491 my $irt = in_reply_to($header_obj);
493 my $v = PublicInbox::Hval->new_msgid($irt, 1);
494 my $html = $v->as_html;
495 my $href = $v->as_href;
496 $rv .= "In-Reply-To: <";
497 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
500 my $refs = $header_obj->header('References');
502 # avoid redundant URLs wasting bandwidth
504 $seen{$irt} = 1 if defined $irt;
506 my @raw_refs = ($refs =~ /<([^>]+)>/g);
507 foreach my $ref (@raw_refs) {
510 push @refs, linkify_ref_nosrch($ref);
514 $rv .= 'References: '. join(' ', @refs) . "\n";
521 my ($mime, $standalone, $full_pfx, $ctx) = @_;
522 my %cc; # everyone else
523 my $to; # this is the From address
525 foreach my $h (qw(From To Cc)) {
526 my $v = $mime->header($h);
527 defined($v) && ($v ne '') or next;
528 my @addrs = Email::Address->parse($v);
529 foreach my $recip (@addrs) {
530 my $address = $recip->address;
531 my $dst = lc($address);
532 $cc{$dst} ||= $address;
536 Email::Address->purge_cache if $standalone;
538 my $subj = $mime->header('Subject') || '';
539 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
540 my $mid = $mime->header('Message-ID');
541 my $irt = uri_escape_utf8($mid);
543 $to = uri_escape_utf8($to);
544 $subj = uri_escape_utf8($subj);
546 my $cc = uri_escape_utf8(join(',', sort values %cc));
547 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
550 my $srch = $ctx->{srch} if $ctx;
551 my $upfx = $full_pfx ? '../' : '../../';
552 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
554 if ($srch && $standalone) {
555 $idx .= qq{ / follow: <a\nhref="t.atom">Atom feed</a>};
558 my ($next, $p) = thread_inline(\$idx, $ctx, $mime, $full_pfx);
560 $p = PublicInbox::Hval->new_oneline($p);
562 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
564 $irt = ' ' x length('parent ');
567 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
575 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
578 sub linkify_ref_nosrch {
579 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
580 my $html = $v->as_html;
581 my $href = $v->as_href;
582 "<<a\nhref=\"../$href/\">$html</a>>";
588 if ($id !~ /\A[a-f0-9]{40}\z/) {
589 $id = mid_compress(mid_clean($id), 1);
594 sub thread_html_head {
595 my ($cb, $header, $state) = @_;
596 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
598 my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
600 $$cb->write("<html><head><title>$s</title>".
601 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
602 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
606 sub pre_anchor_entry {
607 my ($seen, $mime) = @_;
608 my $id = anchor_for($mime->header('Message-ID'));
609 $seen->{$id} = "#$id"; # save the anchor for children, later
613 my ($upfx, $mid) = @_;
614 # 'subject dummy' is used internally by Mail::Thread
615 return '[no common parent]' if ($mid eq 'subject dummy');
617 $mid = PublicInbox::Hval->new_msgid($mid);
618 my $href = $mid->as_href;
619 my $html = $mid->as_html;
620 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
624 my ($upfx, $mid, $level) = @_;
625 "<table\nsummary=ghost><tr><td>" .
626 (INDENT x $level) . "</td><td>" .
627 PRE_WRAP . ghost_parent($upfx, $mid) .
628 '</pre></td></table>';
632 my ($cb, $git, $state, $mime, $level) = @_;
634 # lazy load the full message from mini_mime:
636 my $path = mid2path(mid_clean($mime->header('Message-ID')));
637 Email::MIME->new($git->cat_file('HEAD:'.$path));
640 if ($state->{anchor_idx} == 0) {
641 thread_html_head($cb, $mime, $state);
644 if (my $ghost = delete $state->{ghost}) {
645 # n.b. ghost messages may only be parents, not children
646 foreach my $g (@$ghost) {
647 $$cb->write(ghost_table('../../', @$g));
650 index_entry($$cb, $mime, $level, $state);
655 my ($state, $node, $level) = @_;
656 my $ghost = $state->{ghost} ||= [];
657 push @$ghost, [ $node->messageid, $level ];
661 my ($cb, $git, $state, $node, $level) = @_;
663 if (my $mime = $node->message) {
664 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
665 __ghost_entry($state, $node, $level);
668 __ghost_entry($state, $node, $level);
671 thread_entry($cb, $git, $state, $node->child, $level + 1);
672 thread_entry($cb, $git, $state, $node->next, $level);
678 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
683 my $ts = eval { str2time($mime->header('Date')) };
684 defined($ts) ? $ts : 0;
688 my ($msgs, $nosubject) = @_;
689 require PublicInbox::Thread;
690 my $th = PublicInbox::Thread->new(@$msgs);
692 $Mail::Thread::nosubject = $nosubject;
694 $th->order(*sort_ts);
700 require PublicInbox::ExtMsg;
702 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
707 my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
708 POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
712 my ($dst, $state, $upfx, $mime, $level) = @_;
713 my $pfx = INDENT x ($level - 1);
714 my $dot = $level == 0 ? '' : '` ';
716 my $cur = $state->{cur};
717 my $mid = mid_clean($mime->header('Message-ID'));
718 my $f = $mime->header('X-PI-From');
719 my $d = _msg_date($mime);
720 $f = PublicInbox::Hval->new($f)->as_html;
721 $d = PublicInbox::Hval->new($d)->as_html;
722 my $attr = "$f @ $d";
723 $state->{first_level} ||= $level;
724 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
725 $state->{prev_attr} = $attr;
726 $attr = ' - ' . $attr;
727 $attr .= ' UTC' if $level >= $state->{first_level};
731 $state->{prev_level} = $level;
735 delete $state->{cur};
736 $$dst .= "$pfx$dot<b><a\nid=\"r\"\nhref=\"#t\">".
737 "[this message]</a></b>$attr\n";
742 $state->{next_msg} ||= $mid;
745 # Subject is never undef, this mail was loaded from
746 # our Xapian which would've resulted in '' if it were
747 # really missing (and Filter rejects empty subjects)
748 my $s = $mime->header('Subject');
749 my $h = $state->{srch}->subject_path($s);
750 if ($state->{seen}->{$h}) {
753 $state->{seen}->{$h} = 1;
754 $s = PublicInbox::Hval->new($s);
757 my $m = PublicInbox::Hval->new_msgid($mid);
758 $m = $upfx . '../' . $m->as_href . '/';
760 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a>$attr\n";
762 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f @ $d</a>\n";
767 my ($dst, $state, $upfx, $node, $level) = @_;
769 if (my $mime = $node->message) {
770 my $mid = mid_clean($mime->header('Message-ID'));
771 if ($mid eq $state->{parent_cmp}) {
772 $state->{parent} = $mid;
774 _inline_header($dst, $state, $upfx, $mime, $level);
776 my $dot = $level == 0 ? '' : '` ';
777 my $pfx = (INDENT x $level) . $dot;
779 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
781 inline_dump($dst, $state, $upfx, $node->child, $level+1);
782 inline_dump($dst, $state, $upfx, $node->next, $level);
787 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
788 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
794 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
795 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
799 # accumulate recent topics if search is supported
800 # returns 1 if done, undef if not
802 my ($state, $node, $level) = @_;
804 my $child_adjust = 1;
806 if (my $x = $node->message) {
810 $subj = $x->header('Subject');
811 $subj = $state->{srch}->subject_normalized($subj);
814 # kill "[PATCH v2]" etc. for summarization
815 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
816 $topic = substr($topic, 0, 30);
818 if (++$state->{subjs}->{$topic} == 1) {
819 push @{$state->{order}}, [ $level, $subj, $topic ];
822 my $mid = mid_clean($x->header('Message-ID'));
824 my $u = $x->header('X-PI-From');
825 my $ts = $x->header('X-PI-TS');
826 my $exist = $state->{latest}->{$topic};
827 if (!$exist || $exist->[2] < $ts) {
828 $state->{latest}->{$topic} = [ $mid, $u, $ts ];
831 # ghost message, do not bump level
835 add_topic($state, $node->child, $level + $child_adjust);
836 add_topic($state, $node->next, $level);
841 my $order = $state->{order};
842 my $subjs = $state->{subjs};
843 my $latest = $state->{latest};
844 return "\n[No recent topics]</pre>" unless (scalar @$order);
849 while (defined(my $info = shift @$order)) {
850 my ($level, $subj, $topic) = @$info;
851 my $n = delete $subjs->{$topic};
852 my ($mid, $u, $ts) = @{delete $latest->{$topic}};
853 $mid = PublicInbox::Hval->new($mid)->as_href;
854 $subj = PublicInbox::Hval->new($subj)->as_html;
855 $u = PublicInbox::Hval->new($u)->as_html;
856 $pfx = INDENT x ($level - 1);
857 my $nl = $level == $prev ? "\n" : '';
858 my $dot = $level == 0 ? '' : '` ';
859 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
862 $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
864 $attr = "created by $u @ $ts UTC";
867 # $n isn't the total number of posts on the topic,
868 # just the number of posts in the current results
869 # window, so leave it unlabeled
870 $attr = "updated by $u @ $ts UTC";
873 if ($level == 0 || $attr ne $prev_attr) {
874 $pfx .= INDENT if $level > 0;
875 $dst .= "$pfx- ". $attr . $n;
882 sub emit_index_topics {
883 my ($state, $fh) = @_;
884 my $off = $state->{ctx}->{cgi}->param('o');
885 $off = 0 unless defined $off;
886 $state->{order} = [];
887 $state->{subjs} = {};
888 $state->{latest} = {};
890 my %opts = ( offset => int $off, limit => $max * 4 );
891 while (scalar @{$state->{order}} < $max) {
892 my $res = $state->{srch}->query('', \%opts);
893 my $nr = scalar @{$res->{msgs}} or last;
895 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
896 add_topic($state, $_, 0);
898 $opts{offset} += $nr;
901 $fh->write(dump_topics($state));