summaryrefslogtreecommitdiff
path: root/lib/MaildirIndexer/Index/ByRef.rakumod
blob: 3a5ebdbf552a3f5f1fb896d406ae3a24d194e678 (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
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 --> Str{
    MaildirIndexer::LogTimelineSchema::Index::Find.log: :class('ByRef'), {
        for |$email.refs() -> $ref {
            with %!mailboxes-for-id{$ref} { return .keys.sort.[0}
        }
        return Nil;
    }
}