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