summaryrefslogtreecommitdiff
path: root/service/pixelated/adapter/soledad_querier.py
blob: 16196e2acd2db05b2430706d614fbc80b0a0d045 (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
from pixelated.adapter.pixelated_mail import PixelatedMail


class SoledadQuerier:

    instance = None

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

    @classmethod
    def get_instance(cls, soledad=None):
        if not cls.instance:
            if not soledad:
                raise Exception("Need a soledad for the first time you call this")
            cls.instance = SoledadQuerier(soledad)
        return cls.instance

    def all_mails(self):
        fdocs_chash = [(fdoc, fdoc.content['chash']) for fdoc in self.soledad.get_from_index('by-type', 'flags')]
        if len(fdocs_chash) == 0:
            return []
        return self._build_mails_from_fdocs(fdocs_chash)

    def all_mails_by_mailbox(self, mailbox_name):
        fdocs_chash = [(fdoc, fdoc.content['chash']) for fdoc in self.soledad.get_from_index('by-type-and-mbox', 'flags', mailbox_name)]
        if len(fdocs_chash) == 0:
            return []
        return self._build_mails_from_fdocs(fdocs_chash)

    def _build_mails_from_fdocs(self, fdocs_chash):
        fdocs_hdocs = [(f[0], self.soledad.get_from_index('by-type-and-contenthash', 'head', f[1])[0]) for f in fdocs_chash]
        fdocs_hdocs_phash = [(f[0], f[1], f[1].content.get('body')) for f in fdocs_hdocs]
        fdocs_hdocs_bdocs = [(f[0], f[1], self.soledad.get_from_index('by-type-and-payloadhash', 'cnt', f[2])[0]) for f in fdocs_hdocs_phash]
        return [PixelatedMail.from_soledad(*raw_mail, soledad_querier=self) for raw_mail in fdocs_hdocs_bdocs]

    def save_mail(self, mail):
        # XXX update only what has to be updated
        self.soledad.put_doc(mail.fdoc)
        self.soledad.put_doc(mail.hdoc)

    def create_mail(self, mail, mailbox_name):
        uid = self._next_uid_for_mailbox(mailbox_name)
        new_docs = [self.soledad.create_doc(doc) for doc in mail._get_for_save(next_uid=uid)]
        self._update_index(new_docs)


    def _next_uid_for_mailbox(self, mailbox_name):
        mails = self.all_mails_by_mailbox(mailbox_name)
        mails.sort(key=lambda x: x.uid, reverse=True)
        if len(mails) == 0:
            return 1
        return mails[0].uid + 1

    def _update_index(self, docs):
        db = self.soledad._db

        indexed_fields = db._get_indexed_fields()
        if indexed_fields:
            # It is expected that len(indexed_fields) is shorter than
            # len(raw_doc)
            getters = [(field, db._parse_index_definition(field))
                       for field in indexed_fields]
            for doc in docs:
                db._update_indexes(doc.doc_id, doc.content, getters, db._db_handle)