From 027c230d42d1c0763778e7af055cee55cf015ce6 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Tue, 4 Feb 2020 20:18:53 -0500 Subject: [PATCH] Always run indexing for sent mailbox (#321) * Always run all indexing code paths * Only index emails of users in system --- opwen_email_server/actions.py | 9 ++++----- opwen_email_server/integration/celery.py | 5 +++-- opwen_email_server/integration/webapp.py | 12 ++---------- tests/opwen_email_server/test_actions.py | 5 +++-- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/opwen_email_server/actions.py b/opwen_email_server/actions.py index e44541c9..2feec0f3 100644 --- a/opwen_email_server/actions.py +++ b/opwen_email_server/actions.py @@ -121,8 +121,9 @@ def _action(self, resource_id): # type: ignore for email_address in self._get_pivot(email): domain = get_domain(email_address) - index = f"{domain}/{email_address}/{self._folder}/{email['sent_at']}/{resource_id}" - self._mailbox_storage.store_text(index, 'indexed') + if domain.endswith(mailbox.MAILBOX_DOMAIN): + index = f"{domain}/{email_address}/{self._folder}/{email['sent_at']}/{resource_id}" + self._mailbox_storage.store_text(index, 'indexed') self.log_event(events.MAILBOX_EMAIL_INDEXED, {'folder': self._folder}) # noqa: E501 # yapf: disable return 'OK', 200 @@ -140,9 +141,7 @@ class IndexReceivedEmailForMailbox(_IndexEmailForMailbox): def _get_pivot(self, email: dict) -> Iterable[str]: for email_address in get_recipients(email): - domain = get_domain(email_address) - if domain.endswith(mailbox.MAILBOX_DOMAIN): - yield email_address + yield email_address class IndexSentEmailForMailbox(_IndexEmailForMailbox): diff --git a/opwen_email_server/integration/celery.py b/opwen_email_server/integration/celery.py index 9d81ff97..875df993 100644 --- a/opwen_email_server/integration/celery.py +++ b/opwen_email_server/integration/celery.py @@ -73,9 +73,10 @@ def inbound_store(resource_id: str) -> None: action(resource_id) -def _send_email(resource_id: str) -> None: +def send_and_index_email(resource_id: str) -> None: send.delay(resource_id) index_sent_email_for_mailbox.delay(resource_id) + index_received_email_for_mailbox.delay(resource_id) @celery.task(ignore_result=True) @@ -84,7 +85,7 @@ def written_store(resource_id: str) -> None: client_storage=get_client_storage(), email_storage=get_email_storage(), user_storage=get_user_storage(), - next_task=_send_email, + next_task=send_and_index_email, ) action(resource_id) diff --git a/opwen_email_server/integration/webapp.py b/opwen_email_server/integration/webapp.py index 3be2b1d2..d666a82b 100644 --- a/opwen_email_server/integration/webapp.py +++ b/opwen_email_server/integration/webapp.py @@ -20,9 +20,7 @@ from opwen_email_server.integration.azure import get_mailbox_storage from opwen_email_server.integration.azure import get_pending_storage from opwen_email_server.integration.azure import get_user_storage -from opwen_email_server.integration.celery import index_received_email_for_mailbox -from opwen_email_server.integration.celery import index_sent_email_for_mailbox -from opwen_email_server.integration.celery import send +from opwen_email_server.integration.celery import send_and_index_email from opwen_email_server.services.storage import AzureObjectStorage from opwen_email_server.services.storage import AzureTextStorage from opwen_email_server.utils.collections import chunks @@ -244,12 +242,6 @@ def download(self) -> Iterable: return [] -def _send_email(resource_id: str) -> None: - send.delay(resource_id) - index_sent_email_for_mailbox.delay(resource_id) - index_received_email_for_mailbox.delay(resource_id) - - class AzureIoc: @cached_property def email_store(self): @@ -257,7 +249,7 @@ def email_store(self): email_storage=get_email_storage(), mailbox_storage=get_mailbox_storage(), pending_storage=get_pending_storage(), - send_email=_send_email, + send_email=send_and_index_email, ) @cached_property diff --git a/tests/opwen_email_server/test_actions.py b/tests/opwen_email_server/test_actions.py index ad686790..7c697cde 100644 --- a/tests/opwen_email_server/test_actions.py +++ b/tests/opwen_email_server/test_actions.py @@ -176,7 +176,7 @@ def test_200(self): email_id = '123' email = { 'to': ['1@bar.lokole.ca', 'foo@gmail.com'], 'cc': ['2@baz.lokole.ca'], 'sent_at': '2019-10-26 22:47', - 'from': 'foo@foo' + 'from': 'foo@foo.lokole.ca' } self.email_storage.fetch_object.return_value = email @@ -185,7 +185,8 @@ def test_200(self): self.assertEqual(status, 200) self.email_storage.fetch_object.assert_called_once_with(email_id) - self.mailbox_storage.store_text.assert_called_once_with('foo/foo@foo/sent/2019-10-26 22:47/123', 'indexed') + self.mailbox_storage.store_text.assert_called_once_with( + 'foo.lokole.ca/foo@foo.lokole.ca/sent/2019-10-26 22:47/123', 'indexed') def _execute_action(self, *args, **kwargs): action = actions.IndexSentEmailForMailbox(