summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorAlexandre Pretto Nunes <anunes@thoughtworks.com>2015-01-28 14:42:47 -0200
committerPixpoa pairing <pixpoapairing@pixelated-project.org>2015-01-29 14:55:22 -0200
commit4ac714acfd3192a397028f1260c5929e80ef15b5 (patch)
tree119377886ac719318bdbe4a6b4e7533dd3fbd936 /service
parent1af7c273015246c0cf4130278b2c17fca9de563a (diff)
#249 make the sending process a deferred
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/services/mail_sender.py4
-rw-r--r--service/pixelated/adapter/services/mail_service.py12
-rw-r--r--service/pixelated/resources/mails_resource.py18
3 files changed, 25 insertions, 9 deletions
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..26b4e162 100644
--- a/service/pixelated/adapter/services/mail_service.py
+++ b/service/pixelated/adapter/services/mail_service.py
@@ -45,10 +45,14 @@ class MailService:
return not(not(self.querier.get_header_by_chash(mail_id)))
def send(self, last_draft_ident, mail):
- self.mail_sender.sendmail(mail)
- if last_draft_ident:
- self.mailboxes.drafts().remove(last_draft_ident)
- return self.mailboxes.sent().add(mail)
+ result = self.mail_sender.sendmail(mail)
+
+ def success(_):
+ if last_draft_ident:
+ self.mailboxes.drafts().remove(last_draft_ident)
+ return self.mailboxes.sent().add(mail)
+ result.addCallback(success)
+ return result
def mark_as_read(self, mail_id):
return self.mail(mail_id).mark_as_read()
diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py
index a6eb0fe0..40b29cfc 100644
--- a/service/pixelated/resources/mails_resource.py
+++ b/service/pixelated/resources/mails_resource.py
@@ -1,6 +1,7 @@
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
@@ -102,10 +103,19 @@ class MailsResource(Resource):
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)
+ sendDeferred = self._mail_service.send(draft_id, _mail)
- return respond_json(_mail.as_dict(), request)
+ def onSuccess(mail):
+ self._search_engine.index_mail(mail)
+ respond_json_deferred(mail.as_dict(), request)
+
+ def onError(error):
+ return respond_json_deferred({'message': _format_exception(error)}, request, status_code=422)
+
+ sendDeferred.addCallback(onSuccess)
+ sendDeferred.addErrback(onError)
+
+ return server.NOT_DONE_YET
except Exception as error:
return respond_json({'message': _format_exception(error)}, request, status_code=422)