]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchThread.pm
view: skip ghosts with no direct children
[public-inbox.git] / lib / PublicInbox / SearchThread.pm
index c6bd999c050038c52bf16589566a09c7499572ef..ee35f0d020616b86595c42d702cdc48082fe7971 100644 (file)
@@ -4,7 +4,7 @@
 # This license differs from the rest of public-inbox
 #
 # Our own jwz-style threading class based on Mail::Thread from CPAN.
-# Mail::Thread is unmaintained and available on some distros.
+# Mail::Thread is unmaintained and unavailable on some distros.
 # We also do not want pruning or subject grouping, since we want
 # to encourage strict threading and hopefully encourage people
 # to use proper In-Reply-To.
@@ -33,7 +33,8 @@ sub thread {
        my $self = shift;
        _add_message($self, $_) foreach @{$self->{messages}};
        my $id_table = delete $self->{id_table};
-       $self->{rootset} = [ grep { !delete $_->{parent} } values %$id_table ];
+       $self->{rootset} = [ grep {
+               !delete($_->{parent}) && $_->visible } values %$id_table ];
 }
 
 sub _get_cont_for_id ($$) {
@@ -98,46 +99,58 @@ sub new {
        }, $_[0];
 }
 
+sub topmost {
+       my ($self) = @_;
+       my @q = ($self);
+       while (my $cont = shift @q) {
+               return $cont if $cont->{smsg};
+               push @q, values %{$cont->{children}};
+       }
+       undef;
+}
+
 sub add_child {
        my ($self, $child) = @_;
        croak "Cowardly refusing to become my own parent: $self"
          if $self == $child;
 
        my $cid = $child->{id};
-       $self->{children}->{$cid} = $child;
 
        # reparenting:
        if (defined(my $parent = $child->{parent})) {
                delete $parent->{children}->{$cid};
        }
 
+       $self->{children}->{$cid} = $child;
        $child->{parent} = $self;
 }
 
 sub has_descendent {
-       my ($cur, $child) = @_;
-       my %seen;
-       my @q = ($cur->{parent} || $cur);
-
-       while (defined($cur = shift @q)) {
-               return 1 if $cur == $child;
-
-               if (!$seen{$cur}++) {
-                       push @q, values %{$cur->{children}};
-               }
+       my ($self, $child) = @_;
+       while ($child) {
+               return 1 if $self == $child;
+               $child = $child->{parent};
        }
        0;
 }
 
+# Do not show/keep ghosts iff they have no children.  Sometimes
+# a ghost Message-ID is the result of a long header line
+# being folded/mangled by a MUA, and not a missing message.
+sub visible ($) {
+       my ($self) = @_;
+       $self->{smsg} || scalar values %{$self->{children}};
+}
+
 sub order_children {
        my ($cur, $ordersub) = @_;
 
-       my %seen = ($cur => 1);
+       my %seen = ($cur => 1); # self-referential loop prevention
        my @q = ($cur);
        while (defined($cur = shift @q)) {
                my $c = $cur->{children}; # The hashref here...
 
-               $c = [ grep { !$seen{$_}++ } values %$c ]; # spot/break loops
+               $c = [ grep { !$seen{$_}++ && visible($_) } values %$c ];
                $c = $ordersub->($c) if scalar @$c > 1;
                $cur->{children} = $c; # ...becomes an arrayref
                push @q, @$c;