| 1 | package Sietima::MailStore::FS; | ||||||||||
| 2 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Sietima::Policy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Types::Path::Tiny qw(Dir); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Types::Standard qw(Object ArrayRef Str Slurpy); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Type::Params -sigs; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Sietima::Types qw(EmailMIME TagName); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Digest::SHA qw(sha1_hex); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | our $VERSION = '1.1.5'; # VERSION | |||||||||
| 12 | # ABSTRACT: filesystem-backed email store | ||||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | 1 | with 'Sietima::MailStore'; | |||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | 1 | has root => ( | |||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | required => 1, | ||||||||||
| 21 | isa => Dir, | ||||||||||
| 22 | coerce => 1, | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 1 | has [qw(_tagdir _msgdir)] => ( is => 'lazy' ); | |||||||||
| 26 | 2 | ▶ | sub _build__tagdir($self) { $self->root->child('tags') } | ||||||||
|
Subroutine
_build__tagdir: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 2 | ▶ | sub _build__msgdir($self) { $self->root->child('msgs') } | ||||||||
|
Subroutine
_build__msgdir: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 2 | ▶ | sub BUILD($self,@) { | ||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 30 | 2 | $self->$_->mkpath for qw(_tagdir _msgdir); | |||||||||
| 31 | 2 | return; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | 1 | signature_for store => ( | |||||||||
| 36 | method => Object, | ||||||||||
| 37 | positional => [ | ||||||||||
| 38 | EmailMIME, | ||||||||||
| 39 | Slurpy[ArrayRef[TagName]], | ||||||||||
| 40 | ], | ||||||||||
| 41 | ); | ||||||||||
| 42 | 3 | ▶ | sub store($self,$mail,$tags) { | ||||||||
|
Subroutine
store: called CC=2 SCAR=6.9
| |||||||||||
| 43 | |||||||||||
| 44 | 3 | my $str = $mail->as_string; | |||||||||
| 45 | 3 | my $id = sha1_hex($str); | |||||||||
| 46 | |||||||||||
| 47 | 3 | $self->_msgdir->child($id)->spew_raw($str); | |||||||||
| 48 | |||||||||||
| 49 | 3 | $self->_tagdir->child($_)->append("$id\n") for $tags->@*; | |||||||||
| 50 | |||||||||||
| 51 | 3 | return $id; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | 1 | signature_for retrieve_by_id => ( | |||||||||
| 56 | method => Object, | ||||||||||
| 57 | positional => [ Str ], | ||||||||||
| 58 | ); | ||||||||||
| 59 | 12 | ▶ | sub retrieve_by_id($self,$id) { | ||||||||
|
Subroutine
retrieve_by_id: called CC=2 SCAR=7.1
| |||||||||||
| 60 | 12 | my $msg_path = $self->_msgdir->child($id); | |||||||||
| 61 | 12 | ▶ | return unless -e $msg_path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 12 | return Email::MIME->new($msg_path->slurp_raw); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | |||||||||||
| 66 | 9 | ▶ | sub _tagged_by($self,$tag) { | ||||||||
|
Subroutine
_tagged_by: called CC=2 SCAR=7.1
| |||||||||||
| 67 | 9 | my $tag_file = $self->_tagdir->child($tag); | |||||||||
| 68 | 9 | ▶ | return unless -e $tag_file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 9 | return $tag_file->lines({chomp=>1}); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 1 | signature_for retrieve_ids_by_tags => ( | |||||||||
| 73 | method => Object, | ||||||||||
| 74 | positional => [ | ||||||||||
| 75 | Slurpy[ArrayRef[TagName]], | ||||||||||
| 76 | ], | ||||||||||
| 77 | ); | ||||||||||
| 78 | 10 | ▶ | sub retrieve_ids_by_tags($self,$tags) { | ||||||||
|
Subroutine
retrieve_ids_by_tags: called CC=7 SCAR=19.5
| |||||||||||
| 79 | # this maps: id -> how many of the given @tags it has | ||||||||||
| 80 | 10 | my %msgs; | |||||||||
| 81 | 10 | ▶ | if ($tags->@*) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 7 | for my $tag ($tags->@*) { | |||||||||
| 83 | 9 | $_++ for @msgs{$self->_tagged_by($tag)}; | |||||||||
| 84 | } | ||||||||||
| 85 | } | ||||||||||
| 86 | else { | ||||||||||
| 87 | 3 | $msgs{$_->basename}=0 for $self->_msgdir->children; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 10 | my @ret; | |||||||||
| 91 | 10 | for my $id (keys %msgs) { | |||||||||
| 92 | # if this message id does not have all the required tags, we | ||||||||||
| 93 | # won't return it | ||||||||||
| 94 | 21 | ▶ | next unless $msgs{$id} == $tags->@*; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 17 | push @ret, $id; | |||||||||
| 96 | } | ||||||||||
| 97 | 10 | return \@ret; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | 1 | signature_for retrieve_by_tags => ( | |||||||||
| 102 | method => Object, | ||||||||||
| 103 | positional => [ | ||||||||||
| 104 | Slurpy[ArrayRef[TagName]], | ||||||||||
| 105 | ], | ||||||||||
| 106 | ); | ||||||||||
| 107 | 6 | ▶ | sub retrieve_by_tags($self,$tags) { | ||||||||
|
Subroutine
retrieve_by_tags: called CC=2 SCAR=6.9
| |||||||||||
| 108 | 6 | my @ret; | |||||||||
| 109 | 6 | for my $id ($self->retrieve_ids_by_tags($tags->@*)->@*) { | |||||||||
| 110 | 9 | push @ret, { | |||||||||
| 111 | id => $id, | ||||||||||
| 112 | mail => $self->retrieve_by_id($id), | ||||||||||
| 113 | }; | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 6 | return \@ret; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | |||||||||||
| 120 | 1 | signature_for remove => ( | |||||||||
| 121 | method => Object, | ||||||||||
| 122 | positional => [ Str ], | ||||||||||
| 123 | ); | ||||||||||
| 124 | 1 | ▶ | sub remove($self,$id) { | ||||||||
|
Subroutine
remove: called CC=2 SCAR=6.9
| |||||||||||
| 125 | 1 | for my $tag_file ($self->_tagdir->children) { | |||||||||
| 126 | 2 | ▶ | $tag_file->edit_lines( sub { $_='' if /\A\Q$id\E\n?\z/ } ); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 127 | } | ||||||||||
| 128 | 1 | $self->_msgdir->child($id)->remove; | |||||||||
| 129 | |||||||||||
| 130 | 1 | return; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | |||||||||||
| 134 | 1 | ▶ | sub clear($self) { | ||||||||
|
Subroutine
clear: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 135 | 1 | do { $self->$_->remove_tree;$self->$_->mkpath } for qw(_tagdir _msgdir); | |||||||||
| 136 | 1 | return; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 1 | 1; | |||||||||
| 140 | |||||||||||
| 141 | __END__ | ||||||||||