]> Sergey Matveev's repositories - public-inbox.git/commitdiff
thread: last Reference always wins
authorEric Wong <e@80x24.org>
Sat, 10 Dec 2016 01:09:50 +0000 (01:09 +0000)
committerEric Wong <e@80x24.org>
Sat, 10 Dec 2016 03:29:07 +0000 (03:29 +0000)
Since we use SearchMsg from Xapian data, we can be
assured we do not get self-referential {references}
field.

However, we may need to be more careful when checking
has_descendent for loops, as blindly calling add_child
could open us up to that possibility...

lib/PublicInbox/SearchThread.pm
t/thread-cycle.t

index ee35f0d020616b86595c42d702cdc48082fe7971..601a84b0e263428618fafc5d74540a704f4e3bfa 100644 (file)
@@ -52,6 +52,10 @@ sub _add_message ($$) {
        # B. For each element in the message's References field:
        defined(my $refs = $smsg->{references}) or return;
 
+       # This loop exists to help fill in gaps left from missing
+       # messages.  It is not needed in a perfect world where
+       # everything is perfectly referenced, only the last ref
+       # matters.
        my $prev;
        foreach my $ref ($refs =~ m/<([^>]+)>/g) {
                # Find a Container object for the given Message-ID
@@ -74,10 +78,8 @@ sub _add_message ($$) {
        }
 
        # C. Set the parent of this message to be the last element in
-       # References...
-       if ($prev && !$this->has_descendent($prev)) { # would loop
-               $prev->add_child($this)
-       }
+       # References.
+       $prev->add_child($this) if defined $prev;
 }
 
 sub order {
@@ -127,8 +129,9 @@ sub add_child {
 
 sub has_descendent {
        my ($self, $child) = @_;
+       my %seen; # loop prevention XXX may not be necessary
        while ($child) {
-               return 1 if $self == $child;
+               return 1 if $self == $child || $seen{$child}++;
                $child = $child->{parent};
        }
        0;
index 0e1ecfe261507efa5e1697d999e393c3ff050176..b0844490c92085565b567c5d3e49c465d5f86e23 100644 (file)
@@ -70,14 +70,6 @@ SKIP: {
        is($check, $st, 'Mail::Thread output matches');
 }
 
-@msgs = map { bless $_, 'PublicInbox::SearchMsg' } (
-       { mid => 'a@b' },
-       { mid => 'b@c', references => '<a@b> <b@c>' },
-       { mid => 'd@e', references => '<d@e>' },
-);
-
-is(thread_to_s(\@msgs), "a\@b\n b\@c\nd\@e\n", 'ok with self-references');
-
 done_testing();
 
 sub thread_to_s {