summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorVictor Shyba <victor.shyba@gmail.com>2014-08-11 13:22:14 -0300
committerVictor Shyba <victor.shyba@gmail.com>2014-08-11 13:22:14 -0300
commit0d2f72acbf0541cb01b0d4321278539ff1120802 (patch)
treed3d20f6af1fbc845637659e50785f9e5e660a01c /service
parent1001079b06db84134bae419f993af127ad9bec05 (diff)
Mails is now working and the tags call is returning hardcoded values for now
Diffstat (limited to 'service')
-rw-r--r--service/app/adapter/mail_converter.py24
-rw-r--r--service/app/adapter/mail_service.py12
-rw-r--r--service/app/pixelated_user_agent.py31
-rw-r--r--service/test/adapter/mail_service_test.py38
4 files changed, 57 insertions, 48 deletions
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/<mail_id>')
@@ -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()
diff --git a/service/test/adapter/mail_service_test.py b/service/test/adapter/mail_service_test.py
index 84822038..9c1c4b00 100644
--- a/service/test/adapter/mail_service_test.py
+++ b/service/test/adapter/mail_service_test.py
@@ -18,40 +18,4 @@ class MailboxException(Exception):
class TestMailService(unittest.TestCase):
def setUp(self):
- self.mail_service = MailService()
-
- def test_request_mail_has_mailbox(self):
- with patch.object(self.mail_service, '_switch_mailbox', return_value=Mock()):
- mails = self.mail_service.mails({'tags': ['inbox']})
- self.assertIsNotNone(mails)
-
- def test_request_mail_has_mailbox(self):
- with patch.object(self.mail_service, '_switch_mailbox', return_value=None):
- mails = self.mail_service.mails({'tags': ['inbox']})
- self.assertEqual(mails, [])
-
- def test_switch_mailbox_special_tag(self):
- mailbox = Mock()
- with patch.object(self.mail_service.account, 'getMailbox', return_value=mailbox):
- new_mailbox = self.mail_service._switch_mailbox('sent')
- self.assertEqual(new_mailbox, mailbox)
-
- def test_switch_mailbox_custom_tag_exists(self):
- mailbox = Mock()
- with patch.object(self.mail_service.account, 'getMailbox', return_value=mailbox):
- returned_mailbox = self.mail_service._switch_mailbox('custom')
- self.assertEqual(mailbox, returned_mailbox)
-
- def test_switch_mailbox_custom_tag_not_exists(self):
- mailbox = Mock()
- with patch.object(self.mail_service.account, 'getMailbox', side_effect=MailboxException()):
- mailbox = self.mail_service._switch_mailbox('custom')
- self.assertIsNone(mailbox)
-
- def test_create_new_mailbox(self):
- with patch.object(self.mail_service.account, 'addMailbox', return_value=True) as addMailbox:
- self.assertTrue(self.mail_service._create_mailbox('teste'))
-
- def test_create_existing_mailbox(self):
- with patch.object(self.mail_service.account, 'addMailbox', side_effect=MailboxCollision()) as addMailbox:
- self.assertFalse(self.mail_service._create_mailbox('teste'))
+ self.mail_service = MailService() \ No newline at end of file