summaryrefslogtreecommitdiff
path: root/lib/MaildirIndexer/Index/ByRef.rakumod
blob: 30ea58c95809fdad6e1d5762bfe2250dee71c0c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use v6.d;
use MaildirIndexer::Index;
unit class MaildirIndexer::Index::ByRef does MaildirIndexer::Index;
use MaildirIndexer::LogTimelineSchema;
use MaildirIndexer::Email;
 
has Str %!id-for-file;
has SetHash %!mailboxes-for-id;
 
method dump() {
    say "{.key} → {.value}" for %!id-for-file;
    say "{.key} ⇒ {.value.perl}" for %!mailboxes-for-id;
}
 
method add-mail(MaildirIndexer::Email:D $emailStr:D $mailbox --> Nil{
    MaildirIndexer::LogTimelineSchema::Index::Add.log: :class('ByRef'),:$mailbox-> {
        # ignore adding the same file twice, files in maildirs are 
        # immutable 
        return if %!id-for-file{ $email.path }:exists;
 
        my $id = $email.message-id or return;
        %!id-for-file{ $email.path } = $id;
        %!mailboxes-for-id{ $id }{$mailbox}=1;
        return;
    }
}
 
method del-path(IO:D $fileStr:D $mailbox --> Nil{
    MaildirIndexer::LogTimelineSchema::Index::Rm.log: :class('ByRef'),:$mailbox-> {
        my $id = %!id-for-file{ $file.path }:delete or return;
        with %!mailboxes-for-id{ $id } -> $boxes {
            $boxes{$mailbox}:delete;
        }
        return;
    }
}
 
method mailbox-for-email(MaildirIndexer::Email:D $email --> Mailbox{
    my Mailbox $result;
    MaildirIndexer::LogTimelineSchema::Index::Find.log: :class('ByRef'), -> {
        for |$email.refs() -> $ref {
            with %!mailboxes-for-id{$ref} { $result = Mailbox.new(:name(.keys.sort.[0]),:1confidence) }
        }
    }
    return $result;
}