| 1 | package Email::Store::Thread; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | our $VERSION = "1.2"; | ||
| 5 | |||
| 6 | # Watch the pea. It's under the first shell | ||
| 7 | use base qw(Email::Store::DBI); # For the DATA stuff | ||
| 8 | sub on_store_order { 10 } | ||
| 9 | sub on_store { | ||
| 10 | my ($self, $mail) = @_; | ||
| 11 | my $threader = Email::Store::Thread::Threader->new($mail); | ||
| 12 | $threader->thread; | ||
| 13 | my $container = $mail->container; | ||
| 14 | |||
| 15 | # If I'm in the root set, then everyone under me has to know the new | ||
| 16 | # root. | ||
| 17 | if (grep { $container == $_ } $threader->rootset) { | ||
| 18 | $container->recurse_down(sub { shift->root($container) }); | ||
| 19 | } else { | ||
| 20 | # Otherwise, work upwards until I find a root. | ||
| 21 | $container->find_root_upwards; | ||
| 22 | } | ||
| 23 | Email::Store::Thread::Container->flush; | ||
| 24 | } | ||
| 25 | |||
| 26 | package Email::Store::Thread::Threader; | ||
| 27 | # Keep watching the pea | ||
| 28 | use base 'Mail::Thread'; | ||
| 29 | sub _get_hdr { my ($class, $msg, $hdr) = @_; $msg->simple->header($hdr); } | ||
| 30 | sub _container_class { "Email::Store::Thread::Container" } | ||
| 31 | |||
| 32 | package Email::Store::Thread::Container; | ||
| 33 | use Email::Store::Mail; | ||
| 34 | # Is it under this one? | ||
| 35 | use base qw(Mail::Thread::Container Email::Store::DBI); | ||
| 36 | __PACKAGE__->table("container"); | ||
| 37 | __PACKAGE__->columns(All => qw[id messageid message parent child next root]); | ||
| 38 | __PACKAGE__->has_a(message => "Email::Store::Mail"); | ||
| 39 | __PACKAGE__->has_a(parent => "Email::Store::Thread::Container"); | ||
| 40 | __PACKAGE__->has_a(child => "Email::Store::Thread::Container"); | ||
| 41 | __PACKAGE__->has_a(next => "Email::Store::Thread::Container"); | ||
| 42 | __PACKAGE__->has_a(root => "Email::Store::Thread::Container"); | ||
| 43 | |||
| 44 | sub find_root_upwards { | ||
| 45 | my $self = shift; | ||
| 46 | if (my $par = $self->parent) { | ||
| 47 | $par->find_root_upwards unless $par->root; | ||
| 48 | $self->root($par->root); | ||
| 49 | } else { | ||
| 50 | $self->root($self); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | my %container_cache = (); | ||
| 56 | sub new { | ||
| 57 | my ($class, $id) = @_; | ||
| 58 | my $container = $container_cache{$id} | ||
| 59 | ||= $class->find_or_create({ messageid => $id }); | ||
| 60 | return $container; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub flush { | ||
| 64 | (delete $container_cache{$_})->update for keys %container_cache; | ||
| 65 | } | ||
| 66 | |||
| 67 | # Thread::Container wants regular accessors | ||
| 68 | { | ||
| 69 | no strict 'refs'; | ||
| 70 | no warnings 'redefine'; | ||
| 71 | for my $method (qw/parent child next/) { | ||
| 72 | *$method = sub { | ||
| 73 | my $self = shift; | ||
| 74 | # ensure we're in the container cache too | ||
| 75 | $container_cache{ $self->messageid } = $self; | ||
| 76 | my $methname = "_${method}_accessor"; | ||
| 77 | $self->$methname(@_) if @_; | ||
| 78 | $self->$methname(); | ||
| 79 | }; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | sub subject { $_[0]->message->message ? shift->message->simple->header("Subject") : "" } | ||
| 84 | |||
| 85 | package Email::Store::Mail; | ||
| 86 | sub container { | ||
| 87 | Email::Store::Thread::Container->new(shift->message_id) | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | package Email::Store::Thread; | ||
| 92 | # Are you sure? | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||
| 96 | =head1 NAME | ||
| 97 | |||
| 98 | Email::Store::Thread - Store threading information for a mail | ||
| 99 | |||
| 100 | =head1 ABSTRACT | ||
| 101 | |||
| 102 | Remember to create the database table: | ||
| 103 | |||
| 104 | % make install | ||
| 105 | % perl -MEmail::Store="..." -e 'Email::Store->setup' | ||
| 106 | |||
| 107 | And now: | ||
| 108 | |||
| 109 | my $container = $mail->container; | ||
| 110 | if ($container->parent) { | ||
| 111 | print "Parent of this message is ".$container->parent->message; | ||
| 112 | print "Root of this method is ".$container->root->message; | ||
| 113 | } | ||
| 114 | |||
| 115 | =head2 DESCRIPTION | ||
| 116 | |||
| 117 | This adds to a mail the concept of a B<thread container>. A thread | ||
| 118 | container is a node in a tree which represents the thread of an email | ||
| 119 | conversation. It plugs into the indexing process and works out where in | ||
| 120 | the tree the mail belongs; you can then ask a mail for its C<container>, | ||
| 121 | a container for its C<message>, and for its C<parent>, C<child> and | ||
| 122 | C<sibling> containers, which are used to navigate the thread tree. | ||
| 123 | There's also a C<root> container which represents the top message | ||
| 124 | in the tree. | ||
| 125 | |||
| 126 | This is distributed separately from the main C<Email::Store> | ||
| 127 | distribution as it tends to slow down indexing somewhat. | ||
| 128 | |||
| 129 | =head1 SEE ALSO | ||
| 130 | |||
| 131 | L<Email::Store>, L<Mail::Thread> | ||
| 132 | |||
| 133 | =head1 AUTHOR | ||
| 134 | |||
| 135 | =head1 AUTHOR | ||
| 136 | |||
| 137 | The original author is Simon Cozens, E<lt>simon@cpan.orgE<gt> | ||
| 138 | Currently maintained by Simon Wistow E<lt>simon@thegestalt.orgE<gt> | ||
| 139 | |||
| 140 | =head1 SUPPORT | ||
| 141 | |||
| 142 | This module is part of the Perl Email Project - http://pep.kwiki.org/ | ||
| 143 | |||
| 144 | There is a mailing list at pep@perl.org (subscribe at pep-subscribe@perl.org) | ||
| 145 | and an archive available at http://nntp.perl.org/group/pep.php | ||
| 146 | |||
| 147 | =head1 COPYRIGHT AND LICENSE | ||
| 148 | |||
| 149 | Copyright 2004 by Simon Cozens | ||
| 150 | |||
| 151 | This library is free software; you can redistribute it and/or modify | ||
| 152 | it under the same terms as Perl itself. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | __DATA__ |