summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuda Dornelles <ddornell@thoughtworks.com>2014-09-25 17:34:38 -0300
committerDuda Dornelles <ddornell@thoughtworks.com>2014-09-25 17:34:38 -0300
commit4eaa393b78f956923f4932b60f2c819acba491de (patch)
tree5870ee5d61c880daf398bc7e5c24e098c1403350
parent8be21f61e8f2c3f68de91497ce284cbda20043e6 (diff)
Fixing headers for showing mails and indexing new docs
-rw-r--r--service/pixelated/adapter/mail_service.py2
-rw-r--r--service/pixelated/adapter/pixelated_mail.py33
-rw-r--r--service/pixelated/adapter/soledad_querier.py24
-rw-r--r--service/pixelated/user_agent.py2
4 files changed, 38 insertions, 23 deletions
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py
index 1fe9514e..6606ac41 100644
--- a/service/pixelated/adapter/mail_service.py
+++ b/service/pixelated/adapter/mail_service.py
@@ -32,7 +32,7 @@ class MailService:
def mails(self, query):
_mails = self.mailboxes.mails_by_tag(query['tags']) if query['tags'] else self.querier.all_mails()
- return sorted(_mails or [], key=lambda mail: mail.headers['date'], reverse=True)
+ return sorted(_mails or [], key=lambda mail: mail.headers['Date'], reverse=True)
def update_tags(self, mail_id, new_tags):
mail = self.mail(mail_id)
diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py
index ae6af52f..c1b7f30c 100644
--- a/service/pixelated/adapter/pixelated_mail.py
+++ b/service/pixelated/adapter/pixelated_mail.py
@@ -14,8 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
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
@@ -51,8 +51,8 @@ class InputMail:
@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.headers = {key.capitalize: value for key, value in mail_dict['header'].items()}
+ input_mail.headers['Date'] = pixelated.support.date.iso_now()
input_mail.body = mail_dict['body']
input_mail.tags = set(mail_dict.get('tags', []))
input_mail.status = set(mail_dict.get('status', []))
@@ -98,6 +98,8 @@ class InputMail:
fd[fields.SIZE_KEY] = len(self._raw())
fd[fields.MULTIPART_KEY] = True
fd[fields.RECENT_KEY] = True
+ fd[fields.TYPE_KEY] = fields.TYPE_FLAGS_VAL
+ fd[fields.FLAGS_KEY] = []
self._fd = fd
return fd
@@ -110,11 +112,11 @@ class InputMail:
hd = {}
hd[fields.HEADERS_KEY] = self.headers
- hd[fields.DATE_KEY] = self.headers['date']
+ hd[fields.DATE_KEY] = self.headers['Date']
hd[fields.CONTENT_HASH_KEY] = self._get_chash()
hd[fields.MSGID_KEY] = ''
hd[fields.MULTIPART_KEY] = True
- hd[fields.SUBJECT_KEY] = self.headers.get('subject')
+ hd[fields.SUBJECT_KEY] = self.headers.get('Subject')
hd[fields.TYPE_KEY] = fields.TYPE_HEADERS_VAL
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())
@@ -129,13 +131,13 @@ class InputMail:
mime_multipart = MIMEMultipart()
for header in ['To', 'Cc', 'Bcc']:
- if self.headers[header.lower()]:
- mime_multipart[header] = ", ".join(self.headers[header.lower()])
+ if self.headers[header]:
+ mime_multipart[header] = ", ".join(self.headers[header])
if self.headers['subject']:
- mime_multipart['Subject'] = self.headers['subject']
+ mime_multipart['Subject'] = self.headers['Subject']
- mime_multipart['Date'] = self.headers['date']
+ mime_multipart['Date'] = self.headers['Date']
mime_multipart.attach(MIMEText(self.body, 'plain'))
return mime_multipart
@@ -149,7 +151,6 @@ class PixelatedMail:
def __init__(self, tag_service=TagService.get_instance()):
self.tag_service = tag_service
- self.mailbox_name = None
@staticmethod
def from_soledad(fdoc, hdoc, bdoc, soledad_querier):
@@ -166,9 +167,8 @@ class PixelatedMail:
@property
def headers(self):
- _headers = ['From', 'Date', 'Subject', 'Cc', 'Bcc']
- _headers = {header.lower(): self.hdoc.content['headers'].get(header) for header in _headers}
- map(lambda header: self._split_recipients(header, _headers), ['to', 'bcc', 'cc'])
+ _headers = ['From', 'Date', 'To', 'Subject', 'Cc', 'Bcc']
+ _headers = {header: self.hdoc.content['headers'].get(header) for header in _headers}
return _headers
@property
@@ -215,11 +215,6 @@ class PixelatedMail:
def get_bcc(self):
return self.headers['bcc']
- def _split_recipients(self, header_type, temporary_headers):
- if(temporary_headers.get(header_type) is not None):
- recipients = temporary_headers[header_type].split(',')
- temporary_headers[header_type] = map(lambda x: x.lstrip(), recipients)
-
def mark_as_deleted(self):
# self.remove_all_tags()
# self.leap_mail.setFlags((Status.PixelatedStatus.DELETED,), 1)
@@ -259,7 +254,7 @@ class PixelatedMail:
def as_dict(self):
statuses = [status.name for status in self.status]
return {
- 'header': self.headers,
+ 'header': {k.lower(): v for k, v in self.headers.items()},
'ident': self.ident,
'tags': list(self.tags),
'status': statuses,
diff --git a/service/pixelated/adapter/soledad_querier.py b/service/pixelated/adapter/soledad_querier.py
index fdf03b35..c7a9200c 100644
--- a/service/pixelated/adapter/soledad_querier.py
+++ b/service/pixelated/adapter/soledad_querier.py
@@ -18,10 +18,16 @@ class SoledadQuerier:
def all_mails(self):
fdocs_chash = [(fdoc, fdoc.content['chash']) for fdoc in self.soledad.get_from_index('by-type', 'flags')]
+ import pdb;pdb.set_trace()
+ if len(fdocs_chash) == 0:
+ return []
return self._build_mails_from_fdocs(fdocs_chash)
def all_mails_by_mailbox(self, mailbox_name):
fdocs_chash = [(fdoc, fdoc.content['chash']) for fdoc in self.soledad.get_from_index('by-type-and-mbox', 'flags', mailbox_name)]
+ import pdb;pdb.set_trace()
+ if len(fdocs_chash) == 0:
+ return []
return self._build_mails_from_fdocs(fdocs_chash)
def _build_mails_from_fdocs(self, fdocs_chash):
@@ -37,11 +43,25 @@ class SoledadQuerier:
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)
+ new_docs = [self.soledad.create_doc(doc) for doc in mail._get_for_save(next_uid=uid)]
+ self._update_index(new_docs)
+
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)
+ if len(mails) == 0:
+ return 1
return mails[0].uid + 1
+ def _update_index(self, docs):
+ db = self.soledad._db
+
+ indexed_fields = db._get_indexed_fields()
+ if indexed_fields:
+ # It is expected that len(indexed_fields) is shorter than
+ # len(raw_doc)
+ getters = [(field, db._parse_index_definition(field))
+ for field in indexed_fields]
+ for doc in docs:
+ db._update_indexes(doc.doc_id, doc.content, getters, db._db_handle)
diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py
index 6d53cdaa..aa4b8a86 100644
--- a/service/pixelated/user_agent.py
+++ b/service/pixelated/user_agent.py
@@ -63,7 +63,7 @@ def send_mail():
if 'saveDraft' in DISABLED_FEATURES:
mail_service.send(_mail)
else:
- if _mail.ident:
+ if request.json['ident']:
mail_service.send_draft(_mail)
else:
_mail = mail_service.create_draft(_mail)