]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchThread.pm
searchthread: delete children early while ordering
[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                 bless $_, 'PublicInbox::SearchThread::Msg';
42                 if (exists $id_table{$_->{mid}}) {
43                         $_->{children} = [];
44                         push @imposters, $_; # we'll deal with them later
45                         undef;
46                 } else {
47                         $_->{children} = {}; # will become arrayref later
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         $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 sub topmost {
112         my ($self) = @_;
113         my @q = ($self);
114         while (my $cont = shift @q) {
115                 return $cont if $cont->{blob};
116                 push @q, values %{$cont->{children}};
117         }
118         undef;
119 }
120
121 sub add_child {
122         my ($self, $child) = @_;
123         croak "Cowardly refusing to become my own parent: $self"
124           if $self == $child;
125
126         my $cid = $child->{mid};
127
128         # reparenting:
129         if (defined(my $parent = $child->{parent})) {
130                 delete $parent->{children}->{$cid};
131         }
132
133         $self->{children}->{$cid} = $child;
134         $child->{parent} = $self;
135 }
136
137 sub has_descendent {
138         my ($self, $child) = @_;
139         my %seen; # loop prevention
140         while ($child) {
141                 return 1 if $self == $child || $seen{$child}++;
142                 $child = $child->{parent};
143         }
144         0;
145 }
146
147 # Do not show/keep ghosts iff they have no children.  Sometimes
148 # a ghost Message-ID is the result of a long header line
149 # being folded/mangled by a MUA, and not a missing message.
150 sub visible ($$) {
151         my ($self, $ibx) = @_;
152         return 1 if $self->{blob};
153         if (my $by_mid = $ibx->smsg_by_mid($self->{mid})) {
154                 %$self = (%$self, %$by_mid);
155                 1;
156         } else {
157                 (scalar values %{$self->{children}});
158         }
159 }
160
161 sub order_children {
162         my ($cur, $ordersub, $ctx) = @_;
163
164         my %seen = ($cur => 1); # self-referential loop prevention
165         my @q = ($cur);
166         my $ibx = $ctx->{ibx};
167         while (defined($cur = shift @q)) {
168                 # the {children} hashref here...
169                 my @c = grep { !$seen{$_}++ && visible($_, $ibx) }
170                         values %{delete $cur->{children}};
171                 $ordersub->(\@c) if scalar(@c) > 1;
172                 $cur->{children} = \@c; # ...becomes an arrayref
173                 push @q, @c;
174         }
175 }
176
177 1;