summaryrefslogtreecommitdiff
path: root/service/app/adapter/pixelated_mail.py
blob: 412414edcd56562d1ccb5ddae920410855ae8f3c (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from app.tags import Tag


class PixelatedMail:

    LEAP_FLAGS = ['\\Seen',
                  '\\Answered',
                  '\\Flagged',
                  '\\Deleted',
                  '\\Draft',
                  '\\Recent',
                  'List']

    LEAP_FLAGS_STATUSES = {
        '\\Seen': 'read',
        '\\Answered': 'replied'
    }

    LEAP_FLAGS_TAGS = {
        '\\Deleted': 'trash',
        '\\Draft': 'drafts',
        '\\Recent': 'inbox'
    }

    def __init__(self, leap_mail):
        self.leap_mail = leap_mail
        self.body = leap_mail.bdoc.content['raw']
        self.headers = self.extract_headers(leap_mail)
        self.ident = leap_mail.getUID()
        self.status = self.extract_status(leap_mail)
        self.security_casing = {}
        self.tags = self.extract_tags(leap_mail)

    def extract_status(self, leap_mail):
        flags = leap_mail.getFlags()
        return [converted for flag, converted in self.LEAP_FLAGS_STATUSES.items() if flag in flags]

    def extract_headers(self, leap_mail):
        temporary_headers = {}
        for header, value in leap_mail.hdoc.content['headers'].items():
            temporary_headers[header.lower()] = value
        return temporary_headers

    def extract_tags(self, leap_mail):
        flags = leap_mail.getFlags()
        converted_tags = [Tag(converted) for flag, converted in self.LEAP_FLAGS_TAGS.items() if flag in flags]
        tags = converted_tags + [Tag(flag) for flag in leap_mail.getFlags() if flag not in self.LEAP_FLAGS]
        return tags

    def has_tag(self, tag):
        return Tag(tag) in self.tags

    def as_dict(self):
        tags = [tag.name for tag in self.tags]
        return {
            'header': self.headers,
            'ident': self.ident,
            'tags': tags,
            'status': self.status,
            'security_casing': self.security_casing,
            'body': self.body
        }