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