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