summaryrefslogtreecommitdiff
path: root/inboxapp-service/app/inboxapp
diff options
context:
space:
mode:
authorOla Bini <ola.bini@gmail.com>2014-07-31 19:38:52 -0300
committerOla Bini <ola.bini@gmail.com>2014-07-31 19:38:52 -0300
commitb3d05d37063530e354d7b23b462e7418d7faf6e0 (patch)
tree41cacaba61fbe4f4d42f479b65298a79efe34216 /inboxapp-service/app/inboxapp
parent831efaa8e2bd3544b045308a8a83e388c804c385 (diff)
Add inboxapp temporary service
Diffstat (limited to 'inboxapp-service/app/inboxapp')
-rw-r--r--inboxapp-service/app/inboxapp/__init__.py3
-rw-r--r--inboxapp-service/app/inboxapp/client.py100
-rw-r--r--inboxapp-service/app/inboxapp/mailconverter.py84
3 files changed, 187 insertions, 0 deletions
diff --git a/inboxapp-service/app/inboxapp/__init__.py b/inboxapp-service/app/inboxapp/__init__.py
new file mode 100644
index 00000000..b836e508
--- /dev/null
+++ b/inboxapp-service/app/inboxapp/__init__.py
@@ -0,0 +1,3 @@
+from client import Client
+from mailconverter import MailConverter
+
diff --git a/inboxapp-service/app/inboxapp/client.py b/inboxapp-service/app/inboxapp/client.py
new file mode 100644
index 00000000..cf96a498
--- /dev/null
+++ b/inboxapp-service/app/inboxapp/client.py
@@ -0,0 +1,100 @@
+import json
+import urllib2
+import requests
+
+
+class Client:
+
+ INBOX_APP_ROOT = 'http://localhost:5555/n'
+
+ def _get_user(self, account):
+ accounts = json.load(urllib2.urlopen(self.INBOX_APP_ROOT))
+ return [a for a in accounts if a['email_address'] == account][0]
+
+ def __init__(self, account):
+ self.user = self._get_user(account)
+ self.namespace = self.user['namespace']
+
+ def _get(self, append_url):
+ url = "%s/%s/%s" % (self.INBOX_APP_ROOT, self.namespace, append_url)
+ return requests.get(url).json()
+
+ def _post(self, append_url, body):
+ url = "%s/%s/%s" % (self.INBOX_APP_ROOT, self.namespace, append_url)
+ return requests.post(url, json.dumps(body)).json()
+
+ def _put(self, append_url, body):
+ url = "%s/%s/%s" % (self.INBOX_APP_ROOT, self.namespace, append_url)
+ return requests.put(url, json.dumps(body)).json()
+
+ def mails(self, query):
+ url = "messages"
+ if('tags' in query and len(query['tags']) > 0):
+ url = url + "?tag=%s" % ",".join(query['tags'])
+ return self._get(url)
+
+ def drafts(self):
+ return self._get("drafts")
+
+ def mail(self, mail_id):
+ return self._get("messages/%s" % mail_id)
+
+ def thread(self, thread_id):
+ return self._get("threads/%s" % thread_id)
+
+ def mark_as_read(self, mail_id):
+ mail_to_mark = self.mail(mail_id)
+ self._put("messages/%s" % mail_id, {"unread": False})
+ self.remove_tag_from_thread(mail_to_mark["thread"], "unread")
+
+ def tags_for_thread(self, thread):
+ url = "threads/%s" % thread
+ tags = self._get(url)['tags']
+ return [tag['name'] for tag in tags]
+
+ def add_tag_to_thread(self, thread_id, tag):
+ url = "threads/%s" % thread_id
+ response = self._put(url, {'add_tags': [tag]})
+ return response
+
+ def remove_tag_from_thread(self, thread_id, tag):
+ url = "threads/%s" % thread_id
+ response = self._put(url, {'remove_tags': [tag]})
+ return response
+
+ def delete_mail(self, mail_id):
+ thread_id = self.mail(mail_id)['thread']
+ tags = self.tags_for_thread(thread_id)
+ if('trash' in tags):
+ self.add_tag_to_thread(thread_id, 'delete')
+ else:
+ self.add_tag_to_thread(thread_id, 'trash')
+ return None
+
+ def save_draft(self, draft):
+ if 'id' in draft and draft['id']:
+ url = 'drafts/%s' % draft["id"]
+ else:
+ url = "drafts"
+ result = self._post(url, draft)
+ self.mark_as_read(result['id'])
+ return result['id']
+
+ def send_draft(self, draft):
+ new_draft_id = self.save_draft(draft)
+ response = self._post("send", {"draft_id": new_draft_id})
+ return response
+
+ def draft_reply_for(self, mail_id):
+ thread = self.thread(self.mail(mail_id)["thread"])
+ if thread['drafts']:
+ response = self.mail(thread['drafts'][0])
+ else:
+ response = None
+ return response
+
+ def all_tags(self):
+ return self._get("tags")
+
+ def all_contacts(self, query):
+ return self._get("contacts?filter=%s&order_by=rank" % query['general'])
diff --git a/inboxapp-service/app/inboxapp/mailconverter.py b/inboxapp-service/app/inboxapp/mailconverter.py
new file mode 100644
index 00000000..7841a538
--- /dev/null
+++ b/inboxapp-service/app/inboxapp/mailconverter.py
@@ -0,0 +1,84 @@
+from inboxapp import Client
+from datetime import datetime
+import calendar
+
+
+class MailConverter:
+
+ def __init__(self, client):
+ self.client = client
+
+ def _from_epoch(self, epoch):
+ return datetime.fromtimestamp(epoch).isoformat()
+
+ def _to_epoch(self, iso8601):
+ return calendar.timegm(
+ datetime.strptime(iso8601, "%Y-%m-%dT%H:%M:%S.%f").timetuple()
+ )
+
+ def _to_contacts(self, pixelated_contacts):
+ return [{"name": "", "email": x} for x in pixelated_contacts]
+
+ def _from_contacts(self, inbox_contacts):
+ return [contact['email'] for contact in inbox_contacts]
+
+ def from_mail(self, inbox_mail):
+ tags = sorted(self.client.tags_for_thread(inbox_mail['thread']))
+ status = [] if "unread" in tags else ["read"]
+ return {
+ 'header': {
+ 'from': inbox_mail['from'][0]['email'],
+ 'to': self._from_contacts(inbox_mail['to']),
+ 'cc': self._from_contacts(inbox_mail['cc']),
+ 'bcc': self._from_contacts(inbox_mail['bcc']),
+ 'date': self._from_epoch(inbox_mail['date']),
+ 'subject': inbox_mail['subject']
+ },
+ 'ident': inbox_mail['id'],
+ 'tags': tags,
+ 'status': status,
+ 'security_casing': {},
+ 'body': inbox_mail['body'],
+ }
+
+ def to_mail(self, pixelated_mail, account):
+ mail = {
+ "to": self._to_contacts(pixelated_mail['header']['to']),
+ "cc": self._to_contacts(pixelated_mail['header']['cc']),
+ "bcc": self._to_contacts(pixelated_mail['header']['bcc']),
+ "from": account,
+ "body": pixelated_mail["body"],
+ "subject": pixelated_mail["header"]["subject"],
+ "date": self._to_epoch(datetime.now().isoformat()),
+ "id": pixelated_mail["ident"],
+ "object": "message",
+ }
+ if "draft_reply_for" in pixelated_mail:
+ referred_mail = self.client.mail(pixelated_mail["draft_reply_for"])
+ mail["reply_to_thread"] = referred_mail["thread"]
+ return mail
+
+ def from_tag(self, inbox_tag):
+ default_tags = ["inbox", "sent", "trash", "drafts"]
+ return {
+ 'name': inbox_tag['name'],
+ 'ident': inbox_tag['id'],
+ 'default': inbox_tag['name'] in default_tags,
+ 'counts': {
+ 'total': 0,
+ 'read': 0,
+ 'starred': 0,
+ 'reply': 0
+ }
+ }
+
+ def from_contact(self, inbox_contact):
+ return {
+ 'ident': inbox_contact['id'],
+ 'name': inbox_contact['name'],
+ 'addresses': [inbox_contact['email']],
+ 'mails_received': 0,
+ 'mails_sent': 0,
+ 'last_received': None,
+ 'last_sent': None
+ }