summaryrefslogtreecommitdiff
path: root/service/pixelated/controllers/mails_controller.py
blob: 26b3583141fe1c77b142b38537d80c7197d7ea27 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.

import json
from pixelated.adapter.pixelated_mail import InputMail
from pixelated.controllers import respond_json
from flask import request
import dateutil.parser as dateparser


class MailsController:

    def __init__(self, mail_service, draft_service, search_engine):
        self._mail_service = mail_service
        self._draft_service = draft_service
        self._search_engine = search_engine

    def mails(self, _request=request):
        mail_ids = self._search_engine.search(_request.args.get('q'))
        mails = self._mail_service.mails(mail_ids)
        mails = sorted(mails, key=lambda mail: dateparser.parse(mail.get_date()), reverse=True)

        response = {
            "stats": {
                "total": len(mails),
                "read": 0,
                "starred": 0,
                "replied": 0
            },
            "mails": [mail.as_dict() for mail in mails]
        }

        return respond_json(response)

    def mail(self, mail_id):
        mail = self._mail_service.mail(mail_id)
        return respond_json(mail.as_dict())

    def mark_mail_as_read(self, mail_id):
        mail = self._mail_service.mark_as_read(mail_id)
        self._search_engine.index_mail(mail)
        return ""

    def mark_mail_as_unread(self, mail_id):
        mail = self._mail_service.mark_as_unread(mail_id)
        self._search_engine.index_mail(mail)
        return ""

    def mark_many_mail_unread(self):
        idents = json.loads(request.form['idents'])
        for ident in idents:
            self._mail_service.mark_as_unread(ident)
        return ""

    def delete_mail(self, mail_id):
        trashed_mail = self._mail_service.delete_mail(mail_id)
        self._search_engine.index_mail(trashed_mail)
        return respond_json(None)

    def delete_mails(self):
        idents = json.loads(request.form['idents'])
        for ident in idents:
            self._mail_service.delete_mail(ident)
        return respond_json(None)

    def send_mail(self, _request=request):
        try:
            _mail = InputMail.from_dict(_request.json)
            draft_id = _request.json.get('ident')
            if draft_id:
                self._search_engine.remove_from_index(draft_id)
            _mail = self._mail_service.send(draft_id, _mail)
            self._search_engine.index_mail(_mail)

            return respond_json(_mail.as_dict())
        except Exception as error:
            return respond_json({'message': '\n'.join(list(error.args))}, status_code=500)

    def mail_tags(self, mail_id):
        new_tags = map(lambda tag: tag.lower(), request.get_json()['newtags'])
        try:
            tags = self._mail_service.update_tags(mail_id, new_tags)
            self._search_engine.index_mail(self._mail_service.mail(mail_id))
        except ValueError as ve:
            return respond_json(ve.message, 403)
        return respond_json(list(tags))

    def update_draft(self):
        _mail = InputMail.from_dict(request.json)
        draft_id = request.json.get('ident')
        if draft_id:
            ident = self._draft_service.update_draft(draft_id, _mail).ident
            self._search_engine.remove_from_index(draft_id)
        else:
            ident = self._draft_service.create_draft(_mail).ident

        self._search_engine.index_mail(self._mail_service.mail(ident))
        return respond_json({'ident': ident})