summaryrefslogtreecommitdiff
path: root/service/app/pixelated_user_agent.py
blob: f5bc9a3af878fb369de751d76e2d7ab0553aea79 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import json
import datetime
import dateutil.parser as dateparser

from flask import Flask, request, Response
import app.search_query as search_query
from app.adapter.mail_service import MailService
from app.adapter.mail_converter import MailConverter
from app.tags import Tag
from app.tags import Tags


app = Flask(__name__, static_url_path='', static_folder='../../web-ui/app')

mail_service = MailService()
converter = MailConverter(mail_service)
account = None


def from_iso8061_to_date(iso8061):
    return datetime.datetime.strptime(iso8061, "%Y-%m-%dT%H:%M:%S%z")


def respond_json(entity):
    response = json.dumps(entity)
    return Response(response=response, mimetype="application/json")


@app.route('/disabled_features')
def disabled_features():
    return respond_json([
        'saveDraft',
        'createNewTag',
        'replySection',
        'signatureStatus',
        'encryptionStatus',
        'contacts'
    ])


@app.route('/mails', methods=['POST'])
def save_draft_or_send():
    ident = None
    if 'sent' in request.json['tags']:
        ident = mail_service.send_draft(converter.to_mail(request.json, account))
    else:
        ident = mail_service.save_draft(converter.to_mail(request.json, account))
    return respond_json({'ident': ident})


@app.route('/mails', methods=['PUT'])
def update_draft():
    ident = mail_service.save_draft(converter.to_mail(request.json, account))
    return respond_json({'ident': ident})


@app.route('/mails')
def mails():
    query = search_query.compile(request.args.get("q")) if request.args.get("q") else {'tags': {}}
    mails = mail_service.drafts() if "drafts" in query['tags'] else mail_service.mails(query)
    mails = [converter.from_mail(mail) for mail in mails]

    if "inbox" in query['tags']:
        mails = [mail for mail in mails if (lambda mail: "trash" not in mail['tags'])(mail)]

    mails = sorted(mails, key=lambda mail: dateparser.parse(mail['header']['date']), reverse=True)

    response = {
        "stats": {
            "total": len(mails),
            "read": 0,
            "starred": 0,
            "replied": 0
        },
        "mails": mails
    }

    return respond_json(response)


@app.route('/mail/<mail_id>', methods=['DELETE'])
def delete_mails(mail_id):
    mail_service.delete_mail(mail_id)
    return respond_json(None)


@app.route('/tags')
def tags():
    tags = Tags()
    return respond_json(tags.as_dict())


@app.route('/mail/<mail_id>')
def mail(mail_id):
    mail = mail_service.mail(mail_id)
    return respond_json(converter.from_mail(mail))


@app.route('/mail/<mail_id>/tags')
def mail_tags(mail_id):
    mail = converter.from_mail(mail_service.mail(mail_id))
    return respond_json(mail['tags'])


@app.route('/mail/<mail_id>/read', methods=['POST'])
def mark_mail_as_read(mail_id):
    mail_service.mark_as_read(mail_id)
    return ""


@app.route('/contacts')
def contacts():
    query = search_query.compile(request.args.get("q"))
    desired_contacts = [converter.from_contact(contact) for contact in mail_service.all_contacts(query)]
    return respond_json({'contacts': desired_contacts})


@app.route('/draft_reply_for/<mail_id>')
def draft_reply_for(mail_id):
    draft = mail_service.draft_reply_for(mail_id)
    if draft:
        return respond_json(converter.from_mail(draft))
    else:
        return respond_json(None)


@app.route('/')
def index():
    return app.send_static_file('index.html')


def setup():
    # start_reactor()
    app.config.from_envvar('PIXELATED_UA_CFG')
    account = app.config['ACCOUNT']
    app.run(host=app.config['HOST'], debug=app.config['DEBUG'], port=app.config['PORT'])

from threading import Thread
from twisted.internet import reactor

import signal
import sys


def signal_handler(signal, frame):
        stop_reactor_on_exit()
        sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)


def start_reactor():
    def start_reactor_run():
        reactor.run(False)

    global REACTOR_THREAD
    REACTOR_THREAD = Thread(target=start_reactor_run)
    REACTOR_THREAD.start()


def stop_reactor_on_exit():
    reactor.callFromThread(reactor.stop)
    global REACTOR_THREAD
    REACTOR_THREAD = None

if __name__ == '__main__':
    setup()