]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchThread.pm
507f25baab0e04760b3e55c57f116d6a958246a6
[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 unavailable 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/References.
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 use PublicInbox::MID qw($MID_EXTRACT);
24
25 sub thread {
26         my ($msgs, $ordersub, $ctx) = @_;
27         my (%id_table, @imposters);
28         keys(%id_table) = scalar @$msgs; # pre-size
29
30         # A. put all current non-imposter $msgs (non-ghosts) into %id_table
31         # (imposters are messages with reused Message-IDs)
32         # Sadly, we sort here anyways since the fill-in-the-blanks References:
33         # can be shakier if somebody used In-Reply-To with multiple, disparate
34         # messages.  So, take the client Date: into account since we can't
35         # always determine ordering when somebody uses multiple In-Reply-To.
36         my @kids = sort { $a->{ds} <=> $b->{ds} } grep {
37                 # this delete saves around 4K across 1K messages
38                 # TODO: move this to a more appropriate place, breaks tests
39                 # if we do it during psgi_cull
40                 delete $_->{num};
41
42                 PublicInbox::SearchThread::Msg::cast($_);
43                 if (exists $id_table{$_->{mid}}) {
44                         $_->{children} = [];
45                         push @imposters, $_; # we'll deal with them later
46                         undef;
47                 } else {
48                         $id_table{$_->{mid}} = $_;
49                         defined($_->{references});
50                 }
51         } @$msgs;
52         for my $smsg (@kids) {
53                 # This loop exists to help fill in gaps left from missing
54                 # messages.  It is not needed in a perfect world where
55                 # everything is perfectly referenced, only the last ref
56                 # matters.
57                 my $prev;
58                 for my $ref ($smsg->{references} =~ m/$MID_EXTRACT/go) {
59                         # Find a Container object for the given Message-ID
60                         my $cont = $id_table{$ref} //=
61                                 PublicInbox::SearchThread::Msg::ghost($ref);
62
63                         # Link the References field's Containers together in
64                         # the order implied by the References header
65                         #
66                         # * If they are already linked don't change the
67                         #   existing links
68                         # * Do not add a link if adding that link would
69                         #   introduce a loop...
70                         if ($prev &&
71                                 !$cont->{parent} &&  # already linked
72                                 !$cont->has_descendent($prev) # would loop
73                            ) {
74                                 $prev->add_child($cont);
75                         }
76                         $prev = $cont;
77                 }
78
79                 # C. Set the parent of this message to be the last element in
80                 # References.
81                 if (defined $prev && !$smsg->has_descendent($prev)) {
82                         $prev->add_child($smsg);
83                 }
84         }
85         my $ibx = $ctx->{ibx};
86         my $rootset = [ grep { # n.b.: delete prevents cyclic refs
87                         !delete($_->{parent}) && $_->visible($ibx)
88                 } values %id_table ];
89         $rootset = $ordersub->($rootset);
90         $_->order_children($ordersub, $ctx) for @$rootset;
91
92         # parent imposter messages with reused Message-IDs
93         unshift(@{$id_table{$_->{mid}}->{children}}, $_) for @imposters;
94         $rootset;
95 }
96
97 package PublicInbox::SearchThread::Msg;
98 use base qw(PublicInbox::Smsg);
99 use strict;
100 use warnings;
101 use Carp qw(croak);
102
103 # declare a ghost smsg (determined by absence of {blob})
104 sub ghost {
105         bless {
106                 mid => $_[0],
107                 children => {}, # becomes an array when sorted by ->order(...)
108         }, __PACKAGE__;
109 }
110
111 # give a existing smsg the methods of this class
112 sub cast {
113         my ($smsg) = @_;
114         $smsg->{children} = {};
115         bless $smsg, __PACKAGE__;
116 }
117
118 sub topmost {
119         my ($self) = @_;
120         my @q = ($self);
121         while (my $cont = shift @q) {
122                 return $cont if $cont->{blob};
123                 push @q, values %{$cont->{children}};
124         }
125         undef;
126 }
127
128 sub add_child {
129         my ($self, $child) = @_;
130         croak "Cowardly refusing to become my own parent: $self"
131           if $self == $child;
132
133         my $cid = $child->{mid};
134
135         # reparenting:
136         if (defined(my $parent = $child->{parent})) {
137                 delete $parent->{children}->{$cid};
138         }
139
140         $self->{children}->{$cid} = $child;
141         $child->{parent} = $self;
142 }
143
144 sub has_descendent {
145         my ($self, $child) = @_;
146         my %seen; # loop prevention
147         while ($child) {
148                 return 1 if $self == $child || $seen{$child}++;
149                 $child = $child->{parent};
150         }
151         0;
152 }
153
154 # Do not show/keep ghosts iff they have no children.  Sometimes
155 # a ghost Message-ID is the result of a long header line
156 # being folded/mangled by a MUA, and not a missing message.
157 sub visible ($$) {
158         my ($self, $ibx) = @_;
159         return 1 if $self->{blob};
160         if (my $by_mid = $ibx->smsg_by_mid($self->{mid})) {
161                 %$self = (%$self, %$by_mid);
162                 1;
163         } else {
164                 (scalar values %{$self->{children}});
165         }
166 }
167
168 sub order_children {
169         my ($cur, $ordersub, $ctx) = @_;
170
171         my %seen = ($cur => 1); # self-referential loop prevention
172         my @q = ($cur);
173         my $ibx = $ctx->{ibx};
174         while (defined($cur = shift @q)) {
175                 my $c = $cur->{children}; # The hashref here...
176
177                 $c = [ grep { !$seen{$_}++ && visible($_, $ibx) } values %$c ];
178                 $c = $ordersub->($c) if scalar @$c > 1;
179                 $cur->{children} = $c; # ...becomes an arrayref
180                 push @q, @$c;
181         }
182 }
183
184 1;