summaryrefslogtreecommitdiff
path: root/inboxapp-service/app/inboxapp/mailconverter.py
blob: 7841a538697e33683bc97d2b0edcdfb9a013dd68 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from inboxapp import Client
from datetime import datetime
import calendar


class MailConverter:

    def __init__(self, client):
        self.client = client

    def _from_epoch(self, epoch):
        return datetime.fromtimestamp(epoch).isoformat()

    def _to_epoch(self, iso8601):
        return calendar.timegm(
            datetime.strptime(iso8601, "%Y-%m-%dT%H:%M:%S.%f").timetuple()
        )

    def _to_contacts(self, pixelated_contacts):
        return [{"name": "", "email": x} for x in pixelated_contacts]

    def _from_contacts(self, inbox_contacts):
        return [contact['email'] for contact in inbox_contacts]

    def from_mail(self, inbox_mail):
        tags = sorted(self.client.tags_for_thread(inbox_mail['thread']))
        status = [] if "unread" in tags else ["read"]
        return {
            'header': {
                'from': inbox_mail['from'][0]['email'],
                'to': self._from_contacts(inbox_mail['to']),
                'cc': self._from_contacts(inbox_mail['cc']),
                'bcc': self._from_contacts(inbox_mail['bcc']),
                'date': self._from_epoch(inbox_mail['date']),
                'subject': inbox_mail['subject']
            },
            'ident': inbox_mail['id'],
            'tags': tags,
            'status': status,
            'security_casing': {},
            'body': inbox_mail['body'],
        }

    def to_mail(self, pixelated_mail, account):
        mail = {
            "to": self._to_contacts(pixelated_mail['header']['to']),
            "cc": self._to_contacts(pixelated_mail['header']['cc']),
            "bcc": self._to_contacts(pixelated_mail['header']['bcc']),
            "from": account,
            "body": pixelated_mail["body"],
            "subject": pixelated_mail["header"]["subject"],
            "date": self._to_epoch(datetime.now().isoformat()),
            "id": pixelated_mail["ident"],
            "object": "message",
        }
        if "draft_reply_for" in pixelated_mail:
            referred_mail = self.client.mail(pixelated_mail["draft_reply_for"])
            mail["reply_to_thread"] = referred_mail["thread"]
        return mail

    def from_tag(self, inbox_tag):
        default_tags = ["inbox", "sent", "trash", "drafts"]
        return {
            'name': inbox_tag['name'],
            'ident': inbox_tag['id'],
            'default': inbox_tag['name'] in default_tags,
            'counts': {
                'total': 0,
                'read': 0,
                'starred': 0,
                'reply': 0
            }
        }

    def from_contact(self, inbox_contact):
        return {
            'ident': inbox_contact['id'],
            'name': inbox_contact['name'],
            'addresses': [inbox_contact['email']],
            'mails_received': 0,
            'mails_sent': 0,
            'last_received': None,
            'last_sent': None
        }