diff options
author | Patrick Maia and Victor Shyba <pixelated-team+pmaia+vshyba@thoughtworks.com> | 2014-09-04 17:57:14 +0000 |
---|---|---|
committer | Patrick Maia <pmaia@thoughtworks.com> | 2014-09-04 17:57:14 +0000 |
commit | 96d1aa660bb927282ff7439cab411fb5cae6d5c0 (patch) | |
tree | 4baf5750a5b947baa53799f9d416759688ff1457 | |
parent | 29b128fa0a20f613d61ec292e7881e4a334c5c7d (diff) |
#6 - reimplements email tag addition/removal using headers
-rw-r--r-- | service/pixelated/adapter/mail_service.py | 21 | ||||
-rw-r--r-- | service/pixelated/adapter/pixelated_mail.py | 13 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mail_test.py | 32 | ||||
-rw-r--r-- | service/test/adapter/test_helper.py | 8 |
4 files changed, 17 insertions, 57 deletions
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py index 162fa9c5..bc87d37b 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 twisted.internet import defer -from pixelated.adapter.pixelated_mailbox import PixelatedMailbox from pixelated.adapter.tag import Tag @@ -45,31 +43,12 @@ class MailService: mail = self.mail(mail_id) tags = set(Tag(str_tag) for str_tag in new_tags) current_tags, removed_tags = mail.update_tags(tags) - self._update_mail_flags(current_tags, removed_tags, mail_id) self._update_mailbox_tags(tags) return current_tags def _update_mailbox_tags(self, tags): self.mailbox.update_tags(tags) - def _update_mail_flags(self, current_tags, removed_tags, mail_id): - new_flags = ['tag_' + tag.name for tag in current_tags if tag.name not in PixelatedMailbox.SPECIAL_TAGS] - self._append_mail_flags(mail_id, new_flags) - - removed_flags = ['tag_' + tag.name for tag in removed_tags if tag.name not in PixelatedMailbox.SPECIAL_TAGS] - self._remove_mail_flags(mail_id, removed_flags) - - def _append_mail_flags(self, mail_id, flags): - self._set_mail_flags(mail_id, flags, 1) - - def _remove_mail_flags(self, mail_id, flags): - self._set_mail_flags(mail_id, flags, -1) - - def _set_mail_flags(self, mail_id, flags, operation): - observer = defer.Deferred() - leap_mailbox = self.mailboxes.leap_inbox_mailbox() - self.mailbox.messages.set_flags(leap_mailbox, [mail_id], tuple(flags), operation, observer) - 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 7d34ac4f..6d460626 100644 --- a/service/pixelated/adapter/pixelated_mail.py +++ b/service/pixelated/adapter/pixelated_mail.py @@ -62,13 +62,18 @@ class PixelatedMail: return temporary_headers def _extract_tags(self): - return Tag.from_flags(self.leap_mail.getFlags()) + return set(Tag(tag_name) for tag_name in self.headers.get('x-tags', [])) def update_tags(self, tags): - old_tags = self.tags self.tags = tags - removed_tags = old_tags.difference(self.tags) - return self.tags, removed_tags + self._persist_mail_tags(tags) + return self.tags + + 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 + self.leap_mail._soledad.put_doc(hdoc) def has_tag(self, tag): return Tag(tag) in self.tags diff --git a/service/test/adapter/pixelated_mail_test.py b/service/test/adapter/pixelated_mail_test.py index 5619a1ff..e3ebf727 100644 --- a/service/test/adapter/pixelated_mail_test.py +++ b/service/test/adapter/pixelated_mail_test.py @@ -54,31 +54,6 @@ class TestPixelatedMail(unittest.TestCase): self.assertEqual(str(mail.date), leap_mail_date_in_iso_format) - def test_leap_recent_flag_is_translated_to_inbox_tag(self): - pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=['\\Recent'])) - self.assertIn(Tag('inbox'), pixelated_mail.tags) - - def test_leap_deleted_flag_is_translated_to_trash_tag(self): - pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=['\\Deleted'])) - self.assertIn(Tag('trash'), pixelated_mail.tags) - - def test_leap_draft_flag_is_translated_to_draft_tag(self): - pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=['\\Draft'])) - self.assertIn(Tag('drafts'), pixelated_mail.tags) - - def test_leap_flags_that_are_custom_tags_are_handled(self): - pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(extra_flags=['tag_work'])) - self.assertIn(Tag('work'), pixelated_mail.tags) - - def test_custom_tags_containing_our_prefix_are_handled(self): - pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(extra_flags=['tag_tag_work_tag_'])) - self.assertIn(Tag('tag_work_tag_'), pixelated_mail.tags) - - def test_non_tags_flags_are_ignored(self): - pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=['\\Recent'], - extra_flags=['this_is_not_a_tag', 'tag_custom_tag'])) - self.assertEquals(set([Tag('custom_tag'), Tag('inbox')]), pixelated_mail.tags) - def test_from_dict(self): mail = PixelatedMail.from_dict(self.mail_dict) @@ -90,11 +65,10 @@ 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_and_a_set_for_removed(self): - pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=[], extra_flags=['tag_custom_1', 'tag_custom_2'])) - current_tags, removed_tags = pixelated_mail.update_tags(set([Tag('custom_1'), Tag('custom_3')])) + def test_update_tags_return_a_set_for_current_tags(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) - self.assertEquals(set([Tag('custom_2')]), removed_tags) def test_to_mime_multipart(self): mail = PixelatedMail.from_dict(self.mail_dict) diff --git a/service/test/adapter/test_helper.py b/service/test/adapter/test_helper.py index 07914aea..756013ab 100644 --- a/service/test/adapter/test_helper.py +++ b/service/test/adapter/test_helper.py @@ -24,6 +24,8 @@ LEAP_FLAGS = ['\\Seen', '\\Recent', 'List'] +DEFAULT_HEADERS = {'date': str(datetime.now())} + def mail_dict(): return { @@ -39,8 +41,8 @@ def mail_dict(): } -def leap_mail(uid=0, leap_flags=LEAP_FLAGS, extra_flags=[], headers={'date': str(datetime.now())}): - flags = leap_flags + extra_flags +def leap_mail(uid=0, flags=LEAP_FLAGS, headers=DEFAULT_HEADERS, extra_headers={}): + headers = dict(headers.items() + extra_headers.items()) return Mock(getUID=Mock(return_value=uid), getFlags=Mock(return_value=flags), bdoc=Mock(content={'raw': 'test'}), @@ -51,4 +53,4 @@ 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), - messages=[leap_mail(uid=6, leap_flags=[], extra_flags=[])]) + messages=[leap_mail(uid=6)]) |