]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchThread.pm
thread: fix parent/child relationships
[public-inbox.git] / lib / PublicInbox / SearchThread.pm
1 # This library is free software; you can redistribute it and/or modify
2 # it under the same terms as Perl itself.
3 #
4 # This license differs from the rest of public-inbox
5 #
6 # Our own jwz-style threading class based on Mail::Thread from CPAN.
7 # Mail::Thread is unmaintained and available on some distros.
8 # We also do not want pruning or subject grouping, since we want
9 # to encourage strict threading and hopefully encourage people
10 # to use proper In-Reply-To.
11 #
12 # This includes fixes from several open bugs for Mail::Thread
13 #
14 # Avoid circular references
15 # - https://rt.cpan.org/Public/Bug/Display.html?id=22817
16 #
17 # And avoid recursion in recurse_down:
18 # - https://rt.cpan.org/Ticket/Display.html?id=116727
19 # - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833479
20 package PublicInbox::SearchThread;
21 use strict;
22 use warnings;
23
24 sub new {
25         return bless {
26                 messages => $_[1],
27                 id_table => {},
28                 rootset  => []
29         }, $_[0];
30 }
31
32 sub thread {
33         my $self = shift;
34         _add_message($self, $_) foreach @{$self->{messages}};
35         my $id_table = delete $self->{id_table};
36         $self->{rootset} = [ grep { !delete $_->{parent} } values %$id_table ];
37 }
38
39 sub _get_cont_for_id ($$) {
40         my ($self, $mid) = @_;
41         $self->{id_table}{$mid} ||= PublicInbox::SearchThread::Msg->new($mid);
42 }
43
44 sub _add_message ($$) {
45         my ($self, $smsg) = @_;
46
47         # A. if id_table...
48         my $this = _get_cont_for_id($self, $smsg->{mid});
49         $this->{smsg} = $smsg;
50
51         # B. For each element in the message's References field:
52         defined(my $refs = $smsg->{references}) or return;
53
54         my $prev;
55         foreach my $ref ($refs =~ m/<([^>]+)>/g) {
56                 # Find a Container object for the given Message-ID
57                 my $cont = _get_cont_for_id($self, $ref);
58
59                 # Link the References field's Containers together in
60                 # the order implied by the References header
61                 #
62                 # * If they are already linked don't change the
63                 #   existing links
64                 # * Do not add a link if adding that link would
65                 #   introduce a loop...
66                 if ($prev &&
67                         !$cont->{parent} &&  # already linked
68                         !$cont->has_descendent($prev) # would loop
69                    ) {
70                         $prev->add_child($cont);
71                 }
72                 $prev = $cont;
73         }
74
75         # C. Set the parent of this message to be the last element in
76         # References...
77         if ($prev && !$this->has_descendent($prev)) { # would loop
78                 $prev->add_child($this)
79         }
80 }
81
82 sub order {
83         my ($self, $ordersub) = @_;
84         my $rootset = $ordersub->($self->{rootset});
85         $self->{rootset} = $rootset;
86         $_->order_children($ordersub) for @$rootset;
87 }
88
89 package PublicInbox::SearchThread::Msg;
90 use strict;
91 use warnings;
92 use Carp qw(croak);
93
94 sub new {
95         bless {
96                 id => $_[1],
97                 children => {}, # becomes an array when sorted by ->order(...)
98         }, $_[0];
99 }
100
101 sub add_child {
102         my ($self, $child) = @_;
103         croak "Cowardly refusing to become my own parent: $self"
104           if $self == $child;
105
106         my $cid = $child->{id};
107
108         # reparenting:
109         if (defined(my $parent = $child->{parent})) {
110                 delete $parent->{children}->{$cid};
111         }
112
113         $self->{children}->{$cid} = $child;
114         $child->{parent} = $self;
115 }
116
117 sub has_descendent {
118         my ($self, $child) = @_;
119         while ($child) {
120                 return 1 if $self == $child;
121                 $child = $child->{parent};
122         }
123         0;
124 }
125
126 sub order_children {
127         my ($cur, $ordersub) = @_;
128
129         my %seen = ($cur => 1);
130         my @q = ($cur);
131         while (defined($cur = shift @q)) {
132                 my $c = $cur->{children}; # The hashref here...
133
134                 $c = [ grep { !$seen{$_}++ } values %$c ]; # spot/break loops
135                 $c = $ordersub->($c) if scalar @$c > 1;
136                 $cur->{children} = $c; # ...becomes an arrayref
137                 push @q, @$c;
138         }
139 }
140
141 1;