use constant MAX_TRUNC_LEN => 72;
use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
use constant T_ANCHOR => '#u';
+use constant INDENT => ' ';
*ascii_html = *PublicInbox::Hval::ascii_html;
my $ts = _msg_date($mime);
my $rv = "<table\nsummary=l$level><tr>";
if ($level) {
- $rv .= '<td><pre>' . (' ' x $level) . '</pre></td>';
+ $rv .= '<td><pre>' . (INDENT x $level) . '</pre></td>';
}
$rv .= "<td\nid=s$midx>" . PRE_WRAP;
$rv .= "<b\nid=\"$id\">$subj</b>\n";
if (defined $irt) {
unless (defined $parent_anchor) {
- my $v = PublicInbox::Hval->new_msgid($irt);
+ my $v = PublicInbox::Hval->new_msgid($irt, 1);
$v = $v->as_href;
$parent_anchor = "${path}$v/";
}
my $res = $srch->get_thread($mid);
my $msgs = load_results($res);
my $nr = scalar @$msgs;
- return missing_thread($cb) if $nr == 0;
+ return missing_thread($cb, $ctx) if $nr == 0;
my $flat = $ctx->{flat};
my $orig_cb = $cb;
+ my $seen = {};
my $state = {
ctx => $ctx,
- seen => {},
+ seen => $seen,
root_anchor => anchor_for($mid),
anchor_idx => 0,
};
require PublicInbox::GitCatFile;
my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
if ($flat) {
+ pre_anchor_entry($seen, $_) for (@$msgs);
__thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
} else {
my $th = thread_results($msgs);
# there could be a race due to a message being deleted in git
# but still being in the Xapian index:
- return missing_thread($cb) if ($orig_cb eq $cb);
+ return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
my $final_anchor = $state->{anchor_idx};
my $next = "<a\nid=\"s$final_anchor\">";
if ($nr <= 1) {
$$dst .= "\n[no followups, yet]\n";
- return;
+ return (undef, in_reply_to($cur));
}
my $upfx = $full_pfx ? '' : '../';
$$dst .= "\n\n~$nr messages in thread: ".
"(<a\nhref=\"${upfx}t/#u\">expand</a>)\n";
my $subj = $srch->subject_path($cur->header('Subject'));
+ my $parent = in_reply_to($cur);
my $state = {
seen => { $subj => 1 },
srch => $srch,
cur => $mid,
+ parent_cmp => $parent ? mid_compress($parent) : '',
+ parent => $parent,
};
for (thread_results(load_results($res))->rootset) {
inline_dump($dst, $state, $upfx, $_, 0);
}
- $state->{next_msg};
+ ($state->{next_msg}, $state->{parent});
}
sub _parent_headers_nosrch {
my $irt = in_reply_to($header_obj);
if (defined $irt) {
- my $v = PublicInbox::Hval->new_msgid($irt);
+ my $v = PublicInbox::Hval->new_msgid($irt, 1);
my $html = $v->as_html;
my $href = $v->as_href;
$rv .= "In-Reply-To: <";
foreach my $ref (@raw_refs) {
next if $seen{$ref};
$seen{$ref} = 1;
- push @refs, linkify_ref($ref);
+ push @refs, linkify_ref_nosrch($ref);
}
if (@refs) {
my $upfx = $full_pfx ? '../' : '../../';
my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
if ($idx && $srch) {
- my $next = thread_inline(\$idx, $ctx, $mime, $full_pfx);
- $irt = in_reply_to($mime->header_obj);
- if (defined $irt) {
- $irt = PublicInbox::Hval->new_msgid($irt);
- $irt = $irt->as_href;
- $irt = "<a\nhref=\"$upfx$irt/\">parent</a> ";
+ my ($next, $p) = thread_inline(\$idx, $ctx, $mime, $full_pfx);
+ if (defined $p) {
+ $p = PublicInbox::Hval->new_oneline($p);
+ $p = $p->as_href;
+ $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
} else {
$irt = ' ' x length('parent ');
}
"$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
}
-sub linkify_ref {
- my $v = PublicInbox::Hval->new_msgid($_[0]);
+sub linkify_ref_nosrch {
+ my $v = PublicInbox::Hval->new_msgid($_[0], 1);
my $html = $v->as_html;
my $href = $v->as_href;
"<<a\nhref=\"../$href/\">$html</a>>";
$$cb->write("<html><head><title>$s</title></head><body>");
}
+sub pre_anchor_entry {
+ my ($seen, $mime) = @_;
+ my $id = anchor_for($mime->header('Message-ID'));
+ $seen->{$id} = "#$id"; # save the anchor for children, later
+}
+
sub __thread_entry {
my ($cb, $git, $state, $mime, $level) = @_;
# lazy load the full message from mini_mime:
- my $path = mid2path(mid_clean($mime->header('Message-ID')));
- $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
- if ($mime) {
- if ($state->{anchor_idx} == 0) {
- thread_html_head($cb, $mime);
+ $mime = eval {
+ my $path = mid2path(mid_clean($mime->header('Message-ID')));
+ Email::MIME->new($git->cat_file('HEAD:'.$path));
+ } or return;
+
+ if ($state->{anchor_idx} == 0) {
+ thread_html_head($cb, $mime, $state);
+ }
+
+ if (my $ghost = delete $state->{ghost}) {
+ # n.b. ghost messages may only be parents, not children
+ foreach my $g (@$ghost) {
+ my $mid = PublicInbox::Hval->new_msgid($g->[0]);
+ my $pfx = INDENT x $g->[1];
+ my $href = $mid->as_href;
+ my $html = $mid->as_html;
+ $$cb->write("<table><tr><td>$pfx</td><td>" .
+ PRE_WRAP .
+ '[parent not found: <' .
+ qq{<a\nhref="../../$href/">}.
+ "$html</a>>]</pre></td></table>");
}
- index_entry($$cb, $mime, $level, $state);
}
+ index_entry($$cb, $mime, $level, $state);
+ 1;
+}
+
+sub __ghost_entry {
+ my ($state, $node, $level) = @_;
+ my $ghost = $state->{ghost} ||= [];
+ push @$ghost, [ $node->messageid, $level ];
}
sub thread_entry {
my ($cb, $git, $state, $node, $level) = @_;
return unless $node;
if (my $mime = $node->message) {
- __thread_entry($cb, $git, $state, $mime, $level);
+ unless (__thread_entry($cb, $git, $state, $mime, $level)) {
+ __ghost_entry($state, $node, $level);
+ }
+ } else {
+ __ghost_entry($state, $node, $level);
}
+
thread_entry($cb, $git, $state, $node->child, $level + 1);
thread_entry($cb, $git, $state, $node->next, $level);
}
}
sub missing_thread {
- my ($cb) = @_;
- my $title = 'Thread does not exist';
- $cb->([404, ['Content-Type' => 'text/html']])->write(<<EOF);
-<html><head><title>$title</title></head><body><pre>$title
-<a href="../../">Return to index</a></pre></body></html>
-EOF
+ my ($cb, $ctx) = @_;
+ require PublicInbox::ExtMsg;
+
+ $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
}
sub _msg_date {
sub _inline_header {
my ($dst, $state, $upfx, $mime, $level) = @_;
- my $pfx = ' ' x $level;
+ my $pfx = INDENT x $level;
my $cur = $state->{cur};
my $mid = $mime->header('Message-ID');
sub inline_dump {
my ($dst, $state, $upfx, $node, $level) = @_;
return unless $node;
- return if $state->{stopped};
if (my $mime = $node->message) {
+ my $mid = mid_clean($mime->header('Message-ID'));
+ if ($mid eq $state->{parent_cmp}) {
+ $state->{parent} = $mid;
+ }
_inline_header($dst, $state, $upfx, $mime, $level);
+ } else {
+ my $pfx = INDENT x $level;
+ my $v = PublicInbox::Hval->new_msgid($node->messageid, 1);
+ my $html = $v->as_html;
+ my $href = $v->as_href;
+ $$dst .= $pfx . '` [parent not found: <' .
+ qq{<a\nhref="$upfx../$href/">}.
+ "$html</a>>]\n";
}
inline_dump($dst, $state, $upfx, $node->child, $level+1);
inline_dump($dst, $state, $upfx, $node->next, $level);