From 0d2f72acbf0541cb01b0d4321278539ff1120802 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 11 Aug 2014 13:22:14 -0300 Subject: Mails is now working and the tags call is returning hardcoded values for now --- service/app/adapter/mail_converter.py | 24 +++++++++++++++++++++++- service/app/adapter/mail_service.py | 12 +++++------- service/app/pixelated_user_agent.py | 31 ++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 11 deletions(-) (limited to 'service/app') diff --git a/service/app/adapter/mail_converter.py b/service/app/adapter/mail_converter.py index a77fcdb7..cf793a7a 100644 --- a/service/app/adapter/mail_converter.py +++ b/service/app/adapter/mail_converter.py @@ -1,11 +1,33 @@ +import dateutil.parser as dateparser + class MailConverter: def __init__(self, mail_service): pass + def date_to_iso(self, date): + return dateparser.parse(date).isoformat() + def from_mail(self, imap_mail): - return inbox_mail + headers = imap_mail.hdoc.content['headers'] + body = imap_mail.bdoc.content + + return { + 'header': { + 'from': [headers['From']], + 'to': [headers['To']], + 'cc': headers.get('CC', []), + 'bcc': headers.get('BCC', []), + 'date': self.date_to_iso(headers['Date']), + 'subject': headers['Subject'] + }, + 'ident': imap_mail.getUID(), + 'tags': imap_mail.getFlags(), + 'status': [], + 'security_casing': {}, + 'body': body['raw'] + } def to_mail(self, pixelated_mail, account): raise NotImplementedError() diff --git a/service/app/adapter/mail_service.py b/service/app/adapter/mail_service.py index 7b33c290..a08b73f9 100644 --- a/service/app/adapter/mail_service.py +++ b/service/app/adapter/mail_service.py @@ -49,11 +49,7 @@ class MailService: self.mailbox = self.account.getMailbox(self.mailbox_name) def mails(self, query): - if query.get('tags', False) and len(query['tags']): - mailbox = self._switch_mailbox(query['tags'][0]) - return [m for m in mailbox.messages] if mailbox else [] - else: - return [] + return self.mailbox.messages or [] def _switch_mailbox(self, name): mailbox = None @@ -79,7 +75,9 @@ class MailService: return [] def mail(self, mail_id): - raise NotImplementedError() + for message in self.mailbox.messages: + if message.getUID() == int(mail_id): + return message def thread(self, thread_id): raise NotImplementedError() @@ -116,4 +114,4 @@ class MailService: if __name__ == '__main__': print('Running Standalone') - client = Client() + client = Client() \ No newline at end of file diff --git a/service/app/pixelated_user_agent.py b/service/app/pixelated_user_agent.py index 42c16262..eed47554 100644 --- a/service/app/pixelated_user_agent.py +++ b/service/app/pixelated_user_agent.py @@ -60,7 +60,7 @@ def 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: mail['header']['date'], reverse=True) + # mails = sorted(mails, key=lambda mail: mail['header']['date'], reverse=True) response = { "stats": { @@ -83,8 +83,8 @@ def delete_mails(mail_id): @app.route('/tags') def tags(): - tags = map(lambda x: converter.from_tag(x), mail_service.all_tags()) - return respond_json(tags) + #tags = map(lambda x: converter.from_tag(x), mail_service.all_tags()) + return respond_json(['inbox']) @app.route('/mail/') @@ -127,9 +127,34 @@ def index(): 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() -- cgit v1.2.3