+ 'm' . mid_compressed(mid_clean($msgid));
+}
+
+sub simple_dump {
+ my ($dst, $root, $node, $level) = @_;
+ my $pfx = ' ' x $level;
+ $$dst .= $pfx;
+ if (my $x = $node->message) {
+ my $mid = $x->header('Message-ID');
+ if ($root->[0] ne $mid) {
+ my $s = $x->header('Subject');
+ my $h = hash_subj($s);
+ if ($root->[1]->{$h}) {
+ $s = '';
+ } else {
+ $root->[1]->{$h} = 1;
+ $s = PublicInbox::Hval->new($s);
+ $s = $s->as_html;
+ }
+ my $m = PublicInbox::Hval->new_msgid($mid);
+ my $f = PublicInbox::Hval->new($x->header('X-PI-From'));
+ my $d = PublicInbox::Hval->new($x->header('X-PI-Date'));
+ $m = $m->as_href . '.html';
+ $f = $f->as_html;
+ $d = $d->as_html . ' UTC';
+ if (length($s) == 0) {
+ $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
+ } else {
+ $$dst .= "` <a\nhref=\"$m\">$s</a>\n" .
+ "$pfx by $f @ $d\n";
+ }
+ }
+ }
+ simple_dump($dst, $root, $node->child, $level + 1) if $node->child;
+ simple_dump($dst, $root, $node->next, $level) if $node->next;
+}
+
+sub hash_subj {
+ my ($subj) = @_;
+ $subj =~ s/\A\s+//;
+ $subj =~ s/\s+\z//;
+ $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German)
+ $subj =~ s/\s+/ /;
+ Digest::SHA::sha1($subj);
+}
+
+sub thread_replies {
+ my ($dst, $root, $res) = @_;
+ my @msgs = map { $_->mini_mime } @{$res->{msgs}};
+ foreach (@{$res->{msgs}}) {
+ print STDERR "smsg->path: <", $_->path, ">\n";
+ }
+ require PublicInbox::Thread;
+ $root->header_set('X-PI-TS', '0');
+ my $th = PublicInbox::Thread->new($root, @msgs);
+ $th->thread;
+ $th->order(*PublicInbox::Thread::sort_ts);
+ $root = [ $root->header('Message-ID'),
+ { hash_subj($root->header('Subject')) => 1 } ];
+ simple_dump($dst, $root, $_, 0) for $th->rootset;
+}
+
+sub thread_html_head {
+ my ($mime) = @_;
+ my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
+ $s = $s->as_html;
+ "<html><head><title>$s</title></head><body>" . PRE_WRAP
+
+}
+
+sub thread_entry {
+ my ($dst, $state, $node, $level) = @_;
+ # $state = [ $search_res, $seen, undef, 0 (msg_nr) ];
+ # $seen is overloaded with 3 types of fields:
+ # 1) "root_anchor" => anchor_for(Message-ID),
+ # 2) seen subject hashes: sha1(subject) => 1
+ # 3) anchors hashes: "#$sha1_hex" (same as $seen in index_entry)
+ if (my $mime = $node->message) {
+ if (length($$dst) == 0) {
+ $$dst .= thread_html_head($mime);
+ }
+ $$dst .= index_entry(undef, $mime, $level, $state);
+ }
+ thread_entry($dst, $state, $node->child, $level + 1) if $node->child;
+ thread_entry($dst, $state, $node->next, $level) if $node->next;
+}
+
+sub load_results {
+ my ($ctx, $res) = @_;
+
+ require PublicInbox::GitCatFile;
+ my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
+ my @msgs;
+ while (my $smsg = shift @{$res->{msgs}}) {
+ my $m = $smsg->mid;
+ my $path = mid2path($m);
+
+ # FIXME: duplicated code from Feed.pm
+ my $mime = eval {
+ my $str = $git->cat_file("HEAD:$path");
+ Email::MIME->new($str);
+ };
+ unless ($@) {
+ my $t = eval { str2time($mime->header('Date')) };
+ defined($t) or $t = 0;
+ $mime->header_set('X-PI-TS', $t);
+ push @msgs, $mime;
+ }
+ }
+ \@msgs;