1 # This library is free software; you can redistribute it and/or modify
2 # it under the same terms as Perl itself.
4 # This license differs from the rest of public-inbox
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.
12 # This includes fixes from several open bugs for Mail::Thread
14 # Avoid circular references
15 # - https://rt.cpan.org/Public/Bug/Display.html?id=22817
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;
23 use PublicInbox::MID qw($MID_EXTRACT);
26 my ($msgs, $ordersub, $ctx) = @_;
27 my (%id_table, @imposters);
28 keys(%id_table) = scalar @$msgs; # pre-size
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
41 bless $_, 'PublicInbox::SearchThread::Msg';
42 if (exists $id_table{$_->{mid}}) {
44 push @imposters, $_; # we'll deal with them later
47 $_->{children} = {}; # will become arrayref later
48 $id_table{$_->{mid}} = $_;
49 defined($_->{references});
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
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);
63 # Link the References field's Containers together in
64 # the order implied by the References header
66 # * If they are already linked don't change the
68 # * Do not add a link if adding that link would
71 !$cont->{parent} && # already linked
72 !$cont->has_descendent($prev) # would loop
74 $prev->add_child($cont);
79 # C. Set the parent of this message to be the last element in
81 if (defined $prev && !$smsg->has_descendent($prev)) {
82 $prev->add_child($smsg);
85 my $ibx = $ctx->{ibx};
86 my @rootset = grep { # n.b.: delete prevents cyclic refs
87 !delete($_->{parent}) && $_->visible($ibx)
89 $ordersub->(\@rootset);
90 $_->order_children($ordersub, $ctx) for @rootset;
92 # parent imposter messages with reused Message-IDs
93 unshift(@{$id_table{$_->{mid}}->{children}}, $_) for @imposters;
97 package PublicInbox::SearchThread::Msg;
98 use base qw(PublicInbox::Smsg);
103 # declare a ghost smsg (determined by absence of {blob})
107 children => {}, # becomes an array when sorted by ->order(...)
114 while (my $cont = shift @q) {
115 return $cont if $cont->{blob};
116 push @q, values %{$cont->{children}};
122 my ($self, $child) = @_;
123 croak "Cowardly refusing to become my own parent: $self"
126 my $cid = $child->{mid};
129 if (defined(my $parent = $child->{parent})) {
130 delete $parent->{children}->{$cid};
133 $self->{children}->{$cid} = $child;
134 $child->{parent} = $self;
138 my ($self, $child) = @_;
139 my %seen; # loop prevention
141 return 1 if $self == $child || $seen{$child}++;
142 $child = $child->{parent};
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.
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);
157 (scalar values %{$self->{children}});
162 my ($cur, $ordersub, $ctx) = @_;
164 my %seen = ($cur => 1); # self-referential loop prevention
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