Skip Menu |

This queue is for tickets about the Mail-Thread CPAN distribution.

Report information
The Basics
Id: 22691
Status: resolved
Priority: 0/
Queue: Mail-Thread

People
Owner: Nobody in particular
Requestors: rjbs [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 2.5
Fixed in: (no value)



Subject: some refactoring would make threaders open-ended
This simple patch would make it easier to produce code that could add new messages to existing Mail::Thread objects, which would be useful if they were, say, serialized and used a lighterweight class, which can be done with subclassing. -- rjbs
On Sun Oct 29 17:33:19 2006, RJBS wrote: Show quoted text
> This simple patch would make it easier to produce code that could add > new messages to existing Mail::Thread objects, which would be useful if > they were, say, serialized and used a lighterweight class, which can be > done with subclassing.
...and here's the patch. -- rjbs
diff -Nur Mail-Thread-2.5/Thread.pm Mail-Thread-rjbs/Thread.pm --- Mail-Thread-2.5/Thread.pm 2004-05-27 08:51:05.000000000 -0400 +++ Mail-Thread-rjbs/Thread.pm 2006-10-29 17:27:47.000000000 -0500 @@ -141,8 +141,7 @@ my $self = shift; $self->_setup(); $self->{rootset} = [ grep { !$_->parent } values %{$self->{id_table}} ]; - delete $self->{id_table}; - delete $self->{seen}; + unless ($noprune) { my $fakeroot = $self->_container_class->new( 'fakeroot' ); $fakeroot->set_children( $self->rootset ); @@ -150,12 +149,17 @@ my @kids = @{$self->{rootset}} = $fakeroot->children; $fakeroot->remove_child($_) for $fakeroot->children; } - delete $self->{seen}; + $self->_group_set_bysubject() unless $nosubject; $self->_finish(); } -sub _finish { } +sub _finish { + my $self = shift; + delete $self->{id_table}; + delete $self->{seen}; + delete $self->{seen}; +} sub _get_cont_for_id { my $self = shift; @@ -179,67 +183,72 @@ # 1. For each message for my $message (@{$self->{messages}}) { - debug "\n\nLooking at ".$self->_msgid($message)."\n"; - # A. if id_table... - my $this_container = $self->_get_cont_for_id($self->_msgid($message)); - $this_container->message($message); - debug " [".$this_container->subject."]\n----\n"; - - # B. For each element in the message's References field: - my @refs = $self->_references($message); - debug " Now looking at its references: @refs\n"; - - my $prev; - for my $ref (@refs) { - debug " Looking at reference $ref\n"; - # Find a Container object for the given Message-ID - my $container = $self->_get_cont_for_id($ref); - - # Link the References field's Containers together in the - # order implied by the References header - # * If they are already linked don't change the existing links - # * Do not add a link if adding that link would introduce - # a loop... - - if ($prev && - !$container->parent && # already linked - !$container->has_descendent($prev) # would loop - ) { - $prev->add_child($container); - } - $prev = $container; - } + $self->_add_message($message); + } + + debug "\nThe final table:\n"; + if ($Mail::Thread::debug) { + _dump( values %{$self->{id_table}} ); + } +} + +sub _add_message { + my ($self, $message) = @_; + + debug "\n\nLooking at ".$self->_msgid($message)."\n"; + # A. if id_table... + my $this_container = $self->_get_cont_for_id($self->_msgid($message)); + $this_container->message($message); + debug " [".$this_container->subject."]\n----\n"; + + # B. For each element in the message's References field: + my @refs = $self->_references($message); + debug " Now looking at its references: @refs\n"; + + my $prev; + for my $ref (@refs) { + debug " Looking at reference $ref\n"; + # Find a Container object for the given Message-ID + my $container = $self->_get_cont_for_id($ref); + + # Link the References field's Containers together in the + # order implied by the References header + # * If they are already linked don't change the existing links + # * Do not add a link if adding that link would introduce + # a loop... - # C. Set the parent of this message to be the last element in - # References... if ($prev && - !$this_container->has_descendent($prev) # would loop + !$container->parent && # already linked + !$container->has_descendent($prev) # would loop ) { - $prev->add_child($this_container) - } - - debug "Done with this message!\n----\n"; - if ($debug) { - _dump( values %{$self->{id_table}} ); + $prev->add_child($container); } + $prev = $container; + } - if (0) { - # Note that at all times the various 'parent' and 'child' - # fields must be kept inter-consistent - for my $c (values %{ $self->{id_table} }) { - if ($c->parent && !grep { $c == $_ } $c->parent->children) { - die "$c dysfunctional!\n"; - } - } - } + # C. Set the parent of this message to be the last element in + # References... + if ($prev && + !$this_container->has_descendent($prev) # would loop + ) { + $prev->add_child($this_container) } - debug "\nThe final table:\n"; - if ($Mail::Thread::debug) { + debug "Done with this message!\n----\n"; + if ($debug) { _dump( values %{$self->{id_table}} ); } -} + if (0) { + # Note that at all times the various 'parent' and 'child' + # fields must be kept inter-consistent + for my $c (values %{ $self->{id_table} }) { + if ($c->parent && !grep { $c == $_ } $c->parent->children) { + die "$c dysfunctional!\n"; + } + } + } +} sub _prune_empties { my $self = shift; diff -Nur Mail-Thread-2.5/t/2-1.t Mail-Thread-rjbs/t/2-1.t --- Mail-Thread-2.5/t/2-1.t 1969-12-31 19:00:00.000000000 -0500 +++ Mail-Thread-rjbs/t/2-1.t 2006-10-29 17:29:16.000000000 -0500 @@ -0,0 +1,32 @@ +#!perl -w +BEGIN { require 't/common.pl' } + +use Test::More 'no_plan'; +use_ok("Mail::Thread"); + +{ + package Mail::Thread::Open; + @Mail::Thread::Open::ISA = qw(Mail::Thread); + sub _finish { } +} + +my @messages = slurp_messages('t/testbox-2'); +my $tail = pop @messages; +my $threader = Mail::Thread::Open->new(@messages); + +my @stuff; +$threader->thread; +$threader->_add_message($tail); + +use Data::Dump::Streamer; + +is($threader->rootset, 1, "We have one main threads"); + +dump_into($threader => \@stuff); +# Dump(\@stuff); + +deeply(\@stuff, [ + [ 0, "sort numbers", '20030101210258.63148.qmail@web20805.mail.yahoo.com' ], + [ 1, "Re: sort numbers", 'auvpjq$ede$1@post.home.lunix' ], + [ 1, "Re: sort numbers", 'r3i71vcul4g95orb58173qj6b8dus6pnch@4ax.com' ] + ]);
Subject: Re: [rt.cpan.org #22691] some refactoring would make threaders open-ended
Date: Mon, 30 Oct 2006 08:43:59 +0000
To: bug-Mail-Thread [...] rt.cpan.org
From: Tony Bowden <tony [...] kasei.com>
Ricardo Signes via RT wrote: Show quoted text
> This simple patch would make it easier to produce code that could add > new messages to existing Mail::Thread objects, which would be useful if > they were, say, serialized and used a lighterweight class, which can be > done with subclassing.
I'm happy enough with the idea, but I don't really like introducing a new dependency - especially just for a test. Any chance of reworking the test to lose Data::Dumper::Streamer? Thanks, Tony
Subject: Re: [rt.cpan.org #22691] some refactoring would make threaders open-ended
Date: Mon, 30 Oct 2006 08:45:33 +0000
To: bug-Mail-Thread [...] rt.cpan.org
From: Tony Bowden <tony [...] tmtm.com>
Tony Bowden wrote: Show quoted text
> I'm happy enough with the idea, but I don't really like introducing a > new dependency - especially just for a test. Any chance of reworking > the test to lose Data::Dumper::Streamer?
OK - ignore that. I've noticed that the 'use' line was just a leftover - nothing from the module is actually needed. Tony
Subject: Re: [rt.cpan.org #22691] some refactoring would make threaders open-ended
Date: Mon, 30 Oct 2006 08:56:04 +0000
To: bug-Mail-Thread [...] rt.cpan.org
From: Tony Bowden <tony [...] tmtm.com>
Ricardo Signes via RT wrote: Show quoted text
> This simple patch would make it easier to produce code that could add > new messages to existing Mail::Thread objects, which would be useful if > they were, say, serialized and used a lighterweight class, which can be > done with subclassing.
The uploaded file Mail-Thread-2.55.tar.gz has entered CPAN as file: $CPAN/authors/id/T/TM/TMTM/Mail-Thread-2.55.tar.gz size: 40012 bytes md5: 3b30d3087e30462cb6391797ee885f0e Tony