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