diff options
author | Pixpoa pairing <pixpoapairing@pixelated-project.org> | 2015-01-30 14:10:25 -0200 |
---|---|---|
committer | Pixpoa pairing <pixpoapairing@pixelated-project.org> | 2015-01-30 14:10:25 -0200 |
commit | fb7f661648480b16d75a6147605767720be26d00 (patch) | |
tree | 55d3d110846f329ee7eaa904ab92ce9792112c90 /service | |
parent | e8e9cbaaa713a1f00ed8d00cb4744d8f4f340f38 (diff) | |
parent | cad4bbf073663db828fa7ac43b27ae00556e2797 (diff) |
Merge branch 'message_sending_error_handling'
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/adapter/model/mail.py | 11 | ||||
-rw-r--r-- | service/pixelated/adapter/services/mail_sender.py | 4 | ||||
-rw-r--r-- | service/pixelated/adapter/services/mail_service.py | 6 | ||||
-rw-r--r-- | service/pixelated/resources/mails_resource.py | 53 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mail_service.py | 2 |
5 files changed, 49 insertions, 27 deletions
diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index 42953064..dfc0ea1c 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -186,6 +186,17 @@ class InputMail(Mail): return mime_multipart.as_string() @staticmethod + def delivery_error_template(delivery_address): + return InputMail.from_dict({ + 'body': "Mail undelivered for %s" % delivery_address, + 'header': { + 'bcc': [], + 'cc': [], + 'subject': "Mail undelivered for %s" % delivery_address + } + }) + + @staticmethod def from_dict(mail_dict): input_mail = InputMail() input_mail.headers = {key.capitalize(): value for key, value in mail_dict.get('header', {}).items()} diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py index 50c17ba5..1c2d7ce1 100644 --- a/service/pixelated/adapter/services/mail_sender.py +++ b/service/pixelated/adapter/services/mail_sender.py @@ -35,4 +35,6 @@ class MailSender(): file=StringIO(mail.to_smtp_format()), deferred=resultDeferred) - return reactor.connectTCP('localhost', 4650, senderFactory) + reactor.connectTCP('localhost', 4650, senderFactory) + + return resultDeferred diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 3b70890b..7bf76676 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -13,8 +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/>. - - class MailService: __slots__ = ['leap_session', 'account', 'mailbox_name'] @@ -44,8 +42,10 @@ class MailService: def mail_exists(self, mail_id): return not(not(self.querier.get_header_by_chash(mail_id))) - def send(self, last_draft_ident, mail): + def send(self, mail): self.mail_sender.sendmail(mail) + + def move_to_send(self, last_draft_ident, mail): if last_draft_ident: self.mailboxes.drafts().remove(last_draft_ident) return self.mailboxes.sent().add(mail) diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py index a6eb0fe0..4280d7ec 100644 --- a/service/pixelated/resources/mails_resource.py +++ b/service/pixelated/resources/mails_resource.py @@ -1,16 +1,16 @@ import json from pixelated.adapter.model.mail import InputMail -from pixelated.resources import respond_json +from pixelated.resources import respond_json, respond_json_deferred +from twisted.web import server from twisted.web.resource import Resource - - -def _format_exception(e): - exception_info = map(str, list(e.args)) - return '\n'.join(exception_info) +from leap.common.events import ( + register, + unregister, + events_pb2 as proto +) class MailsUnreadResource(Resource): - isLeaf = True def __init__(self, mail_service, search_engine): @@ -28,7 +28,6 @@ class MailsUnreadResource(Resource): class MailsReadResource(Resource): - isLeaf = True def __init__(self, mail_service, search_engine): @@ -46,7 +45,6 @@ class MailsReadResource(Resource): class MailsDeleteResource(Resource): - isLeaf = True def __init__(self, mail_service, search_engine): @@ -72,6 +70,15 @@ class MailsDeleteResource(Resource): class MailsResource(Resource): + def _register_smtp_error_handler(self): + + def on_error(event): + delivery_error_mail = InputMail.delivery_error_template(delivery_address=event.content) + delivery_error_mail = self._mail_service.mailboxes.inbox().add(delivery_error_mail) + self._search_engine.index_mail(delivery_error_mail) + + register(signal=proto.SMTP_SEND_MESSAGE_ERROR, callback=on_error) + def __init__(self, search_engine, mail_service, draft_service): Resource.__init__(self) self.putChild('delete', MailsDeleteResource(mail_service, search_engine)) @@ -81,9 +88,11 @@ class MailsResource(Resource): self._draft_service = draft_service self._mail_service = mail_service self._search_engine = search_engine + self._register_smtp_error_handler() def render_GET(self, request): - mail_ids, total = self._search_engine.search(request.args.get('q')[0], request.args.get('w')[0], request.args.get('p')[0]) + mail_ids, total = self._search_engine.search(request.args.get('q')[0], request.args.get('w')[0], + request.args.get('p')[0]) mails = self._mail_service.mails(mail_ids) response = { @@ -96,18 +105,18 @@ class MailsResource(Resource): return respond_json(response, request) def render_POST(self, request): - try: - content_dict = json.loads(request.content.read()) - _mail = InputMail.from_dict(content_dict) - draft_id = content_dict.get('ident') - if draft_id: - self._search_engine.remove_from_index(draft_id) - _mail = self._mail_service.send(draft_id, _mail) - self._search_engine.index_mail(_mail) - - return respond_json(_mail.as_dict(), request) - except Exception as error: - return respond_json({'message': _format_exception(error)}, request, status_code=422) + content_dict = json.loads(request.content.read()) + _mail = InputMail.from_dict(content_dict) + draft_id = content_dict.get('ident') + + self._mail_service.send(_mail) + sent_mail = self._mail_service.move_to_send(draft_id, _mail) + self._search_engine.index_mail(sent_mail) + + if draft_id: + self._search_engine.remove_from_index(draft_id) + + return respond_json(sent_mail.as_dict(), request) def render_PUT(self, request): content_dict = json.loads(request.content.read()) diff --git a/service/test/unit/adapter/test_mail_service.py b/service/test/unit/adapter/test_mail_service.py index fca6e79b..32b45dc4 100644 --- a/service/test/unit/adapter/test_mail_service.py +++ b/service/test/unit/adapter/test_mail_service.py @@ -34,7 +34,7 @@ class TestMailService(unittest.TestCase): def test_send_mail(self): mail = "mail" - self.mail_service.send(1, mail) + self.mail_service.send(mail) verify(self.mail_sender).sendmail(mail) |