summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuda Dornelles <ddornell@thoughtworks.com>2014-09-25 15:10:03 -0300
committerDuda Dornelles <ddornell@thoughtworks.com>2014-09-25 15:10:03 -0300
commit8be21f61e8f2c3f68de91497ce284cbda20043e6 (patch)
tree6679c16cbfb05eb91dde190fb3885f3a06c40ba2
parent99e3cdd9bf7c8f32254b4adbaa0abec97cbabf5e (diff)
Creating mail right on soledad
-rw-r--r--service/pixelated/adapter/pixelated_mail.py47
-rw-r--r--service/pixelated/adapter/pixelated_mailbox.py7
-rw-r--r--service/pixelated/adapter/pixelated_mailboxes.py5
-rw-r--r--service/pixelated/adapter/soledad_querier.py27
-rw-r--r--service/pixelated/user_agent.py3
5 files changed, 57 insertions, 32 deletions
diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py
index 421ab736..ae6af52f 100644
--- a/service/pixelated/adapter/pixelated_mail.py
+++ b/service/pixelated/adapter/pixelated_mail.py
@@ -16,6 +16,7 @@
import json
from leap.mail.imap.fields import fields
from leap.mail.walk import get_parts
+import leap.mail.walk as walk
import dateutil.parser as dateparser
from pixelated.adapter.status import Status
from pixelated.adapter.tag_service import TagService
@@ -23,6 +24,7 @@ import pixelated.support.date
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from pycryptopp.hash import sha256
+from pixelated.support.functional import flatten
class InputMail:
@@ -34,13 +36,24 @@ class InputMail:
self._bd = None
self._mime = None
+ def as_dict(self):
+ statuses = [status.name for status in self.status]
+ return {
+ 'header': self.headers,
+ 'ident': self.ident,
+ 'tags': list(self.tags),
+ 'status': statuses,
+ 'security_casing': {},
+ 'body': self.body
+ }
+
+
@staticmethod
def from_dict(mail_dict):
input_mail = InputMail()
input_mail.headers = mail_dict['header']
input_mail.headers['date'] = pixelated.support.date.iso_now()
input_mail.body = mail_dict['body']
- input_mail.ident = mail_dict.get('ident', None)
input_mail.tags = set(mail_dict.get('tags', []))
input_mail.status = set(mail_dict.get('status', []))
return input_mail
@@ -51,11 +64,15 @@ class InputMail:
return self._mime
mime = MIMEMultipart()
for key, value in self.headers.items():
- mime[key] = value
+ mime[str(key)] = str(value)
mime.attach(MIMEText(self.body, 'plain'))
self._mime = mime
return mime
+ @property
+ def ident(self):
+ return self._get_chash()
+
def _raw(self):
if self._raw_message:
return self._raw_message
@@ -66,7 +83,9 @@ class InputMail:
return sha256.SHA256(self._raw()).hexdigest()
def _get_for_save(self, next_uid):
- return (self._fdoc(next_uid), self._hdoc(), self._bdoc())
+ docs = [self._fdoc(next_uid), self._hdoc()]
+ docs.extend([m for m in self._cdocs()])
+ return docs
def _fdoc(self, next_uid):
if self._fd:
@@ -82,6 +101,9 @@ class InputMail:
self._fd = fd
return fd
+ def _get_body_phash(self):
+ return walk.get_body_phash_multi(walk.get_payloads(self._mime_multipart))
+
def _hdoc(self):
if self._hd:
return self._hd
@@ -94,21 +116,14 @@ class InputMail:
hd[fields.MULTIPART_KEY] = True
hd[fields.SUBJECT_KEY] = self.headers.get('subject')
hd[fields.TYPE_KEY] = fields.TYPE_HEADERS_VAL
- hd[fields.BODY_KEY] = sha256.SHA256.hexdigest(self._mime_multipart.get_payload())
-
- parts_array = get_parts(self._mime_multipart)
- parts = {}
- for index, part in enumerate(parts_array):
- part[fields.PAYLOAD_HASH_KEY] = hd[fields.BODY_KEY]
- parts[index] = part
- hd[fields.PARTS_MAP_KEY] = parts
+ hd[fields.BODY_KEY] = self._get_body_phash()
+ hd[fields.PARTS_MAP_KEY] = walk.walk_msg_tree(walk.get_parts(self._mime_multipart), body_phash=self._get_body_phash())
self._hd = hd
return hd
- def _bdoc(self):
-
- pass
+ def _cdocs(self):
+ return walk.get_raw_docs(self._mime_multipart, self._mime_multipart.walk())
def to_mime_multipart(self):
mime_multipart = MIMEMultipart()
@@ -181,6 +196,10 @@ class PixelatedMail:
def is_recent(self):
return Status('recent') in self.status
+ @property
+ def uid(self):
+ return self.fdoc.content['uid']
+
def save(self):
return self.querier.save_mail(self)
diff --git a/service/pixelated/adapter/pixelated_mailbox.py b/service/pixelated/adapter/pixelated_mailbox.py
index 00f98aaf..91e2cc4d 100644
--- a/service/pixelated/adapter/pixelated_mailbox.py
+++ b/service/pixelated/adapter/pixelated_mailbox.py
@@ -61,12 +61,7 @@ class PixelatedMailbox:
return message
def add(self, mail, use_smtp_format=False):
- return self._do_add_async(mail, use_smtp_format)
-
- @wait_for(timeout=3.0)
- def _do_add_async(self, mail, use_smtp_format):
- raw = mail.to_smtp_format() if use_smtp_format else mail.raw_message()
- return self.leap_mailbox.messages.add_msg(raw)
+ self.querier.create_mail(mail, self.mailbox_name)
def remove(self, mail):
mail.mark_as_deleted()
diff --git a/service/pixelated/adapter/pixelated_mailboxes.py b/service/pixelated/adapter/pixelated_mailboxes.py
index ad88d438..43fa3620 100644
--- a/service/pixelated/adapter/pixelated_mailboxes.py
+++ b/service/pixelated/adapter/pixelated_mailboxes.py
@@ -31,10 +31,7 @@ class PixelatedMailBoxes():
return mails
def add_draft(self, mail):
- drafts = self.drafts()
- draft_id = drafts.add(mail, use_smtp_format=True)
- mail.mailbox_name = drafts.mailbox_name
- mail.uid = draft_id
+ self.drafts().add(mail, use_smtp_format=True)
return mail
def update_draft(self, mail):
diff --git a/service/pixelated/adapter/soledad_querier.py b/service/pixelated/adapter/soledad_querier.py
index b54f8e65..fdf03b35 100644
--- a/service/pixelated/adapter/soledad_querier.py
+++ b/service/pixelated/adapter/soledad_querier.py
@@ -1,13 +1,19 @@
from pixelated.adapter.pixelated_mail import PixelatedMail
+
class SoledadQuerier:
instance = None
+ def __init__(self, soledad):
+ self.soledad = soledad
+
@classmethod
- def get_instance(cls):
+ def get_instance(cls, soledad=None):
if not cls.instance:
- cls.instance = SoledadQuerier()
+ if not soledad:
+ raise Exception("Need a soledad for the first time you call this")
+ cls.instance = SoledadQuerier(soledad)
return cls.instance
def all_mails(self):
@@ -25,8 +31,17 @@ class SoledadQuerier:
return [PixelatedMail.from_soledad(*raw_mail, soledad_querier=self) for raw_mail in fdocs_hdocs_bdocs]
def save_mail(self, mail):
- #self.soledad.put_doc(mail.fdoc)
- #self.soledad.put_doc(mail.hdoc) # XXX update only what has to be updated
- #self.soledad.put_doc(mail.bdoc)
- pass
+ # XXX update only what has to be updated
+ self.soledad.put_doc(mail.fdoc)
+ self.soledad.put_doc(mail.hdoc)
+
+ def create_mail(self, mail, mailbox_name):
+ uid = self._next_uid_for_mailbox(mailbox_name)
+ for doc in mail._get_for_save(next_uid=uid):
+ self.soledad.create_doc(doc)
+
+ def _next_uid_for_mailbox(self, mailbox_name):
+ mails = self.all_mails_by_mailbox(mailbox_name)
+ mails.sort(key=lambda x: x.uid, reverse=True)
+ return mails[0].uid + 1
diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py
index 5bd76e76..6d53cdaa 100644
--- a/service/pixelated/user_agent.py
+++ b/service/pixelated/user_agent.py
@@ -167,8 +167,7 @@ def register_new_user(username):
def start_user_agent(debug_enabled):
leap_session = LeapSession.open(app.config['LEAP_USERNAME'], app.config['LEAP_PASSWORD'],
app.config['LEAP_SERVER_NAME'])
- querier = SoledadQuerier.get_instance()
- querier.soledad = leap_session.account._soledad
+ SoledadQuerier.get_instance(soledad=leap_session.account._soledad)
PixelatedMail.from_email_address = leap_session.account_email()
pixelated_mailboxes = PixelatedMailBoxes(leap_session.account)
pixelated_mail_sender = PixelatedMailSender(leap_session.account_email())