summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorPatrick Maia and Victor Shyba <pixelated-team+pmaia+vshyba@thoughtworks.com>2014-09-05 22:45:55 +0000
committerPatrick Maia <pmaia@thoughtworks.com>2014-09-05 22:46:17 +0000
commit3c79a54ab332e15f31a4a57a4a9baabf4b62e26a (patch)
treece6b592049227da18b0500f4695b0feb490a944d /service
parentd2cf8b51904420917a5f86986ce7c02e89935998 (diff)
#51 - persists new tags globally (in a local file) and shows on tag list
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/mail_service.py12
-rw-r--r--service/pixelated/adapter/pixelated_mail.py16
-rw-r--r--service/pixelated/adapter/pixelated_mailbox.py37
-rw-r--r--service/pixelated/adapter/tag.py61
-rw-r--r--service/pixelated/adapter/tag_index.py44
-rw-r--r--service/test/adapter/pixelated_mail_test.py8
-rw-r--r--service/test/adapter/pixelated_mailbox_test.py42
-rw-r--r--service/test/adapter/test_helper.py6
-rw-r--r--service/test/adapter/test_tag.py76
9 files changed, 185 insertions, 117 deletions
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py
index e3444b9f..2c67ae2d 100644
--- a/service/pixelated/adapter/mail_service.py
+++ b/service/pixelated/adapter/mail_service.py
@@ -14,8 +14,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
-from pixelated.adapter.tag import Tag
-
class MailService:
__slots__ = ['leap_session', 'account', 'mailbox_name']
@@ -41,13 +39,9 @@ class MailService:
def update_tags(self, mail_id, new_tags):
mail = self.mail(mail_id)
- tags = set(Tag(str_tag) for str_tag in new_tags)
- current_tags = mail.update_tags(tags)
- self._update_mailbox_tags(tags)
- return current_tags
-
- def _update_mailbox_tags(self, tags):
- self.mailbox.update_tags(tags)
+ added, removed = mail.update_tags(new_tags)
+ self.mailbox.notify_tags_updated(added, removed, self.ident)
+ return new_tags
def mail(self, mail_id):
return self.mailbox.mail(mail_id)
diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py
index cd34fe46..31e8ccc7 100644
--- a/service/pixelated/adapter/pixelated_mail.py
+++ b/service/pixelated/adapter/pixelated_mail.py
@@ -13,7 +13,6 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
-from pixelated.adapter.tag import Tag
from pixelated.adapter.status import Status
import dateutil.parser as dateparser
from email.MIMEMultipart import MIMEMultipart
@@ -68,34 +67,35 @@ class PixelatedMail:
return temporary_headers
def _extract_tags(self):
- return set(Tag(tag_name) for tag_name in self.headers.get('x-tags', []))
+ return set(self.headers.get('x-tags', []))
def update_tags(self, tags):
+ old_tags = self.tags
self.tags = tags
+ removed = old_tags.difference(tags)
+ added = tags.difference(old_tags)
self._persist_mail_tags(tags)
- return self.tags
+ return added, removed
def mark_as_read(self):
self.status.add("read")
def _persist_mail_tags(self, current_tags):
- tags_headers = [tag.name for tag in current_tags]
hdoc = self.leap_mail.hdoc
- hdoc.content['headers']['X-Tags'] = tags_headers
+ hdoc.content['headers']['X-Tags'] = current_tags
self.leap_mail._soledad.put_doc(hdoc)
def has_tag(self, tag):
- return Tag(tag) in self.tags
+ return tag in self.tags
def as_dict(self):
- tags = [tag.name for tag in self.tags]
statuses = [status.name for status in self.status]
_headers = self.headers.copy()
_headers['date'] = self.date
return {
'header': _headers,
'ident': self.ident,
- 'tags': tags,
+ 'tags': self.tags,
'status': statuses,
'security_casing': self.security_casing,
'body': self.body
diff --git a/service/pixelated/adapter/pixelated_mailbox.py b/service/pixelated/adapter/pixelated_mailbox.py
index 06e0cccc..3424ddd7 100644
--- a/service/pixelated/adapter/pixelated_mailbox.py
+++ b/service/pixelated/adapter/pixelated_mailbox.py
@@ -17,14 +17,19 @@
from pixelated.adapter.pixelated_mail import PixelatedMail
from pixelated.adapter.tag import Tag
+from pixelated.adapter.tag_index import TagIndex
class PixelatedMailbox:
- SPECIAL_TAGS = ['inbox', 'sent', 'drafts', 'trash']
+ SPECIAL_TAGS = set([Tag('inbox', True), Tag('sent', True), Tag('drafts', True), Tag('trash', True)])
- def __init__(self, leap_mailbox):
+ def __init__(self, leap_mailbox, index_file_path):
self.leap_mailbox = leap_mailbox
+ self.tag_index = TagIndex(index_file_path)
+ for tag in self.SPECIAL_TAGS:
+ if tag not in self.tag_index.values():
+ self.tag_index.set(tag)
@property
def messages(self):
@@ -47,22 +52,18 @@ class PixelatedMailbox:
return PixelatedMail.from_leap_mail(message)
def all_tags(self):
- return Tag.from_flags(self._getFlags())
-
- def _getFlags(self):
- # XXX Temporary workaround while getFlags from leap is disabled
- mbox = self.leap_mailbox._get_mbox_doc()
- if not mbox:
- return self.leap_mailbox.getFlags()
- return mbox.content.get(self.leap_mailbox.FLAGS_KEY, [])
-
- def update_tags(self, tags):
- new_flags = set(tag.to_flag() for tag in tags)
- current_flags = set(self._getFlags())
-
- flags = tuple(current_flags.union(new_flags))
- self.leap_mailbox.setFlags(flags)
+ return self.tag_index.values().union(self.SPECIAL_TAGS)
+
+ def notify_tags_updated(self, added_tags, removed_tags, mail_ident):
+ for removed_tag in removed_tags:
+ tag = self.tag_index.get(removed_tag)
+ tag.decrement(mail_ident)
+ self.tag_index.set(tag)
+ for added_tag in added_tags:
+ tag = self.tag_index.get(added_tag) or Tag(added_tag)
+ tag.increment(mail_ident)
+ self.tag_index.set(tag)
@classmethod
def create(cls, account, mailbox_name='INBOX'):
- return PixelatedMailbox(account.getMailbox(mailbox_name))
+ return PixelatedMailbox(account.getMailbox(mailbox_name), '~/.pixelated_index')
diff --git a/service/pixelated/adapter/tag.py b/service/pixelated/adapter/tag.py
index 386ccafc..45a972ab 100644
--- a/service/pixelated/adapter/tag.py
+++ b/service/pixelated/adapter/tag.py
@@ -14,41 +14,32 @@
# 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
-class Tag:
- LEAP_FLAGS_TAGS = {
- '\\Deleted': 'trash',
- '\\Draft': 'drafts',
- '\\Recent': 'inbox'
- }
+class Tag:
@classmethod
- def from_flags(cls, flags):
- return set(filter(None, (cls.from_flag(flag) for flag in flags)))
+ def from_dict(cls, tag_dict):
+ tag = Tag(tag_dict['name'], tag_dict['default'])
+ tag.mails = tag_dict['mails']
+ return tag
@classmethod
- def from_flag(cls, flag):
- if flag in cls.LEAP_FLAGS_TAGS.keys():
- return Tag(cls.LEAP_FLAGS_TAGS[flag])
- if flag.startswith('tag_'):
- return Tag(cls._remove_prefix(flag))
- return None
+ def from_json_string(cls, json_string):
+ tag_dict = json.loads(json_string)
+ tag_dict['mails'] = set(tag_dict['mails'])
+ return Tag.from_dict(tag_dict)
- def to_flag(self):
- for flag, tag in self.LEAP_FLAGS_TAGS.items():
- if tag == str(self.name):
- return flag
- return 'tag_' + str(self.name)
-
- @classmethod
- def _remove_prefix(cls, flag_name):
- return flag_name.replace('tag_', '', 1)
+ @property
+ def total(self):
+ return len(self.mails)
def __init__(self, name, default=False):
self.name = name
- self.default = default
self.ident = name.__hash__()
+ self.default = default
+ self.mails = set()
def __eq__(self, other):
return self.name == other.name
@@ -56,18 +47,28 @@ class Tag:
def __hash__(self):
return self.name.__hash__()
+ def increment(self, mail_ident):
+ self.mails.add(mail_ident)
+
+ def decrement(self, mail_ident):
+ self.mails.discard(mail_ident)
+
def as_dict(self):
return {
'name': self.name,
'default': self.default,
'ident': self.ident,
- 'counts': {
- 'total': 0,
- 'read': 0,
- 'starred': 0,
- 'replied': 0
- }
+ 'counts': {'total': self.total,
+ 'read': 0,
+ 'starred': 0,
+ 'replied': 0},
+ 'mails': self.mails
}
+ def as_json_string(self):
+ tag_dict = self.as_dict()
+ tag_dict['mails'] = list(self.mails)
+ return json.dumps(tag_dict)
+
def __repr__(self):
return self.name
diff --git a/service/pixelated/adapter/tag_index.py b/service/pixelated/adapter/tag_index.py
new file mode 100644
index 00000000..6cc2a68d
--- /dev/null
+++ b/service/pixelated/adapter/tag_index.py
@@ -0,0 +1,44 @@
+#
+# Copyright (c) 2014 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# 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 dbm
+import atexit
+from pixelated.adapter.tag import Tag
+
+
+class TagIndex:
+ """
+ Manages an index for mail's tags using a file storage.
+ """
+
+ def __init__(self, filename):
+ self.db = dbm.open(filename, 'c')
+ atexit.register(self.close_db)
+
+ def set(self, tag):
+ self.db[tag.name] = tag.as_json_string()
+
+ def get(self, tag_name):
+ if tag_name in self.db:
+ return Tag.from_json_string(self.db.get(tag_name))
+ else:
+ return None
+
+ def values(self):
+ return set(self.get(key) for key in self.db.keys())
+
+ def close_db(self):
+ self.db.close()
diff --git a/service/test/adapter/pixelated_mail_test.py b/service/test/adapter/pixelated_mail_test.py
index 7de7c96a..557c3f6a 100644
--- a/service/test/adapter/pixelated_mail_test.py
+++ b/service/test/adapter/pixelated_mail_test.py
@@ -16,7 +16,6 @@
import unittest
from pixelated.adapter.pixelated_mail import PixelatedMail
-from pixelated.adapter.tag import Tag
import test_helper
@@ -65,10 +64,11 @@ class TestPixelatedMail(unittest.TestCase):
self.assertEqual(mail.tags, ['sent'])
self.assertEqual(mail.body, 'Este \xe9 o corpo')
- def test_update_tags_return_a_set_for_current_tags(self):
+ def test_update_tags_return_a_set_for_added_tags_and_a_set_for_removed_ones(self):
pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(extra_headers={'X-tags': ['custom_1', 'custom_2']}))
- current_tags = pixelated_mail.update_tags(set([Tag('custom_1'), Tag('custom_3')]))
- self.assertEquals(set([Tag('custom_3'), Tag('custom_1')]), current_tags)
+ added, removed = pixelated_mail.update_tags(set(['custom_1', 'custom_3']))
+ self.assertEquals(set(['custom_3']), added)
+ self.assertEquals(set(['custom_2']), removed)
def test_to_mime_multipart(self):
mail = PixelatedMail.from_dict(self.mail_dict)
diff --git a/service/test/adapter/pixelated_mailbox_test.py b/service/test/adapter/pixelated_mailbox_test.py
index 1047eed4..2944275f 100644
--- a/service/test/adapter/pixelated_mailbox_test.py
+++ b/service/test/adapter/pixelated_mailbox_test.py
@@ -14,28 +14,42 @@
# 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 unittest
+import os
import test_helper
from pixelated.adapter.tag import Tag
+from pixelated.adapter.tag_index import TagIndex
from pixelated.adapter.pixelated_mailbox import PixelatedMailbox
class TestPixelatedMailbox(unittest.TestCase):
- def test_retrieve_all_tags_from_mailbox(self):
- leap_flags = ['\\Deleted', '\\Draft', '\\Recent', 'tag_custom', 'should_ignore_all_from_here', 'List']
- mailbox = PixelatedMailbox(test_helper.leap_mailbox(leap_flags=leap_flags))
-
- self.assertEquals(set([Tag('trash'), Tag('inbox'), Tag('drafts'), Tag('custom')]), mailbox.all_tags())
+ def setUp(self):
+ self.db_file_path = '/tmp/test_tags'
- def test_new_tags_are_added_to_mailbox(self):
- leap_flags = ['\\Deleted', 'tag_custom_one', 'tag_custom_two']
- leap_mailbox_mock = test_helper.leap_mailbox(leap_flags=leap_flags)
- mailbox = PixelatedMailbox(leap_mailbox_mock)
- tags = [Tag('custom_one'), Tag('custom_three')]
- mailbox.update_tags(tags)
+ def tearDown(self):
+ os.remove(self.db_file_path + '.db')
- expected = set(('\\Deleted', 'tag_custom_one', 'tag_custom_two', 'tag_custom_three'))
- actual_args = set(leap_mailbox_mock.setFlags.call_args[0][0])
+ def test_special_tags_always_exists(self):
+ mailbox = PixelatedMailbox(test_helper.leap_mailbox(), self.db_file_path)
+ self.assertEquals(mailbox.SPECIAL_TAGS, mailbox.all_tags())
- self.assertEquals(expected, actual_args)
+ def test_retrieve_all_tags_from_mailbox(self):
+ tag_index = TagIndex(self.db_file_path)
+ tag_index.set(Tag('one_tag'))
+ tag_index.set(Tag('two_tag'))
+ tag_index.close_db()
+ mailbox = PixelatedMailbox(test_helper.leap_mailbox(), self.db_file_path)
+ expected_tags = set([Tag('one_tag'), Tag('two_tag')]).union(mailbox.SPECIAL_TAGS)
+ self.assertEquals(expected_tags, mailbox.all_tags())
+
+ def test_notify_tags_updated_method_properly_changes_tags_state(self):
+ tag_index = TagIndex(self.db_file_path)
+ tag = Tag('one_tag')
+ tag.increment(12)
+ tag_index.set(tag)
+ tag_index.close_db()
+ mailbox = PixelatedMailbox(test_helper.leap_mailbox(), self.db_file_path)
+ self.assertEquals(0, mailbox.tag_index.get('inbox').total)
+ mailbox.notify_tags_updated(set(['inbox']), set(['one_tag']), 12)
+ self.assertEquals(1, mailbox.tag_index.get('inbox').total)
diff --git a/service/test/adapter/test_helper.py b/service/test/adapter/test_helper.py
index 756013ab..70673481 100644
--- a/service/test/adapter/test_helper.py
+++ b/service/test/adapter/test_helper.py
@@ -49,8 +49,6 @@ def leap_mail(uid=0, flags=LEAP_FLAGS, headers=DEFAULT_HEADERS, extra_headers={}
hdoc=Mock(content={'headers': headers}))
-def leap_mailbox(leap_flags=LEAP_FLAGS, extra_flags=[]):
- flags = leap_flags + extra_flags
- return Mock(getFlags=Mock(return_value=flags),
- _get_mbox_doc=Mock(return_value=None),
+def leap_mailbox():
+ return Mock(_get_mbox_doc=Mock(return_value=None),
messages=[leap_mail(uid=6)])
diff --git a/service/test/adapter/test_tag.py b/service/test/adapter/test_tag.py
index b9b502d4..1f0478d3 100644
--- a/service/test/adapter/test_tag.py
+++ b/service/test/adapter/test_tag.py
@@ -20,42 +20,58 @@ from pixelated.adapter.tag import Tag
class TestTag(unittest.TestCase):
- def test_leap_recent_flag_is_translated_to_inbox_tag(self):
- tag = Tag.from_flag('\\Recent')
- self.assertEquals(Tag('inbox'), tag)
+ def test_from_dict_sets_all_tag_attributes(self):
+ tag_dict = {'name': 'a_tag',
+ 'default': False,
+ 'counts': {'total': 3,
+ 'read': 1,
+ 'starred': 1,
+ 'replied': 1},
+ 'mails': set([1, 2, 3])}
- def test_leap_deleted_flag_is_translated_to_trash_tag(self):
- tag = Tag.from_flag('\\Deleted')
- self.assertEquals(Tag('trash'), tag)
+ tag = Tag.from_dict(tag_dict)
- def test_leap_draft_flag_is_translated_to_draft_tag(self):
- tag = Tag.from_flag('\\Draft')
- self.assertEquals(Tag('drafts'), tag)
+ self.assertEquals(tag_dict['name'], tag.name)
+ self.assertEquals(tag_dict['default'], tag.default)
+ self.assertEquals(tag_dict['counts']['total'], tag.total)
+ self.assertEquals(tag_dict['mails'], tag.mails)
- def test_leap_flags_that_are_custom_tags_are_handled(self):
- tag = Tag.from_flag('tag_work')
- self.assertEquals(Tag('work'), tag)
+ def test_as_dict_puts_all_tag_attributes_in_the_returning_dict(self):
+ tag = Tag('some_tag', default=True)
+ tag.counts = {'total': 0, 'read': 0, 'starred': 0, 'replied': 0}
+ tag.mails = set([1, 2, 3])
- def test_custom_tags_containing_our_prefix_are_handled(self):
- tag = Tag.from_flag('tag_tag_work_tag_')
- self.assertEquals(Tag('tag_work_tag_'), tag)
+ tag_dict = tag.as_dict()
- def test_bulk_conversion(self):
- tags = Tag.from_flags(['\\Answered', '\\Seen', '\\Recent', 'tag_a_custom', 'List'])
- self.assertEquals(set([Tag('inbox'), Tag('a_custom')]), tags)
+ self.assertEquals(tag.name, tag_dict['name'])
+ self.assertEquals(tag.default, tag_dict['default'])
+ self.assertEquals(tag.total, tag_dict['counts']['total'])
+ self.assertEquals(tag.mails, tag_dict['mails'])
- def test_inbox_tag_is_translated_to_leap_recent_flag(self):
- flag = Tag('inbox').to_flag()
- self.assertEquals('\\Recent', flag)
+ def test_increments_total_count_and_adds_mails_id_to_mails(self):
+ tag = Tag('another')
+ tag.increment(12)
- def test_trash_tag_is_translated_to_leap_deleted_flag(self):
- flag = Tag('trash').to_flag()
- self.assertEquals('\\Deleted', flag)
+ self.assertIn(12, tag.mails)
+ self.assertEquals(1, tag.total)
- def test_drafts_tag_is_translated_to_leap_draft_flag(self):
- flag = Tag('drafts').to_flag()
- self.assertEquals('\\Draft', flag)
+ def test_decrement_does_nothing_if_mail_has_not_the_tag(self):
+ tag = Tag('tag')
+ tag.decrement(2000)
- def test_custom_tag_has_prefix_when_translated_to_flag(self):
- flag = Tag('work').to_flag()
- self.assertEquals('tag_work', flag)
+ self.assertEquals(0, tag.total)
+
+ def test_increment_does_nothing_if_mail_already_has_the_tag(self):
+ tag = Tag('tag')
+ tag.mails = set([12])
+ tag.increment(12)
+
+ self.assertEquals(1, tag.total)
+
+ def test_decrements_total_count_and_removes_mails_id_from_mails(self):
+ tag = Tag('one_more')
+ tag.mails = set([12])
+ tag.decrement(12)
+
+ self.assertNotIn(12, tag.mails)
+ self.assertEquals(0, tag.total)