]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
view: include ghost messages in thread views
[public-inbox.git] / lib / PublicInbox / View.pm
index 0331b62c68b0800a3e1af6357d0469e801ceaa41..c572198a1b753813e98b4130b994763a626bac6d 100644 (file)
@@ -19,6 +19,7 @@ use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
 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;
 
@@ -101,7 +102,7 @@ sub index_entry {
        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";
@@ -132,7 +133,7 @@ sub index_entry {
 
        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/";
                }
@@ -164,12 +165,13 @@ sub emit_thread_html {
        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,
        };
@@ -177,6 +179,7 @@ sub emit_thread_html {
        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);
@@ -187,7 +190,7 @@ sub emit_thread_html {
 
        # 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\">";
@@ -450,22 +453,25 @@ sub thread_inline {
 
        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 {
@@ -474,7 +480,7 @@ 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: &lt;";
@@ -491,7 +497,7 @@ sub _parent_headers_nosrch {
                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) {
@@ -534,12 +540,11 @@ sub html_footer {
        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 ');
                }
@@ -555,8 +560,8 @@ sub html_footer {
        "$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;
        "&lt;<a\nhref=\"../$href/\">$html</a>&gt;";
@@ -580,26 +585,60 @@ sub thread_html_head {
        $$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: &lt;' .
+                                       qq{<a\nhref="../../$href/">}.
+                                       "$html</a>&gt;]</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);
 }
@@ -627,12 +666,10 @@ sub thread_results {
 }
 
 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 {
@@ -643,7 +680,7 @@ 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');
@@ -691,9 +728,20 @@ sub _inline_header {
 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: &lt;' .
+                               qq{<a\nhref="$upfx../$href/">}.
+                               "$html</a>&gt;]\n";
        }
        inline_dump($dst, $state, $upfx, $node->child, $level+1);
        inline_dump($dst, $state, $upfx, $node->next, $level);