diff options
author | Ruben Pollan <meskio@sindominio.net> | 2015-02-18 10:24:03 -0600 |
---|---|---|
committer | Ruben Pollan <meskio@sindominio.net> | 2015-02-18 17:30:29 -0600 |
commit | ba240ab67f753ed39f1fdd0467c835a887a7d148 (patch) | |
tree | b20712b01e13173d2a9bc7a0fb44dff21db10bd6 /mail/src/leap | |
parent | cf481efcae39537c804f046f87b39a2c7bf4966a (diff) |
Add callbacks to inbox insertions
Diffstat (limited to 'mail/src/leap')
-rw-r--r-- | mail/src/leap/mail/incoming/service.py | 38 | ||||
-rw-r--r-- | mail/src/leap/mail/incoming/tests/test_incoming_mail.py | 24 |
2 files changed, 47 insertions, 15 deletions
diff --git a/mail/src/leap/mail/incoming/service.py b/mail/src/leap/mail/incoming/service.py index 29021418..fadfd9f6 100644 --- a/mail/src/leap/mail/incoming/service.py +++ b/mail/src/leap/mail/incoming/service.py @@ -135,12 +135,26 @@ class IncomingMail(Service): self._inbox = inbox self._userid = userid + self._listeners = [] self._loop = None self._check_period = check_period # initialize a mail parser only once self._parser = Parser() + def add_listener(self, listener): + """ + Add a listener to inbox insertions. + + This listener function will be called for each message added to the + inbox with its uid as parameter. This function should not be blocking + or it will block the incoming queue. + + :param listener: the listener function + :type listener: callable + """ + self._listeners.append(listener) + # # Public API: fetch, start_loop, stop. # @@ -699,17 +713,21 @@ class IncomingMail(Service): doc, data = msgtuple log.msg('adding message %s to local db' % (doc.doc_id,)) - #if isinstance(data, list): - #if empty(data): - #return False - #data = data[0] - def msgSavedCallback(result): - if not empty(result): - leap_events.signal(IMAP_MSG_SAVED_LOCALLY) - return self._delete_incoming_message(doc) - # TODO add notification as a callback - #leap_events.signal(IMAP_MSG_DELETED_INCOMING) + if empty(result): + return + + for listener in self._listeners: + listener(result) + + def signal_deleted(doc_id): + leap_events.signal(IMAP_MSG_DELETED_INCOMING) + return doc_id + + leap_events.signal(IMAP_MSG_SAVED_LOCALLY) + d = self._delete_incoming_message(doc) + d.addCallback(signal_deleted) + return d d = self._inbox.addMessage(data, (self.RECENT_FLAG,)) d.addCallbacks(msgSavedCallback, self._errback) diff --git a/mail/src/leap/mail/incoming/tests/test_incoming_mail.py b/mail/src/leap/mail/incoming/tests/test_incoming_mail.py index 0b1e6964..a932a959 100644 --- a/mail/src/leap/mail/incoming/tests/test_incoming_mail.py +++ b/mail/src/leap/mail/incoming/tests/test_incoming_mail.py @@ -95,7 +95,8 @@ subject: independence of cyberspace ADDRESS) # The messages don't exist on soledad will fail on deletion - self.fetcher._delete_incoming_message = Mock(return_value=None) + self.fetcher._delete_incoming_message = Mock( + return_value=defer.succeed(None)) d = super(IncomingMailTestCase, self).setUp() d.addCallback(getInbox) @@ -165,12 +166,13 @@ subject: independence of cyberspace message.attach(key) self.fetcher._keymanager.put_raw_key = Mock( return_value=defer.succeed(None)) + self.fetcher._keymanager.fetch_key = Mock() def put_raw_key_called(_): self.fetcher._keymanager.put_raw_key.assert_called_once_with( KEY, OpenPGPKey, address=ADDRESS_2) - d = self._mock_fetch(message.as_string()) + d = self._do_fetch(message.as_string()) d.addCallback(put_raw_key_called) return d @@ -205,12 +207,24 @@ subject: independence of cyberspace d.addCallback(create_encrypted_message) d.addCallback( lambda message: - self._mock_fetch(message.as_string())) + self._do_fetch(message.as_string())) + return d + + def testListener(self): + self.called = False + def listener(uid): + self.called = True + + def listener_called(_): + self.assertTrue(self.called) + + self.fetcher.add_listener(listener) + d = self._do_fetch(self.EMAIL) + d.addCallback(listener_called) return d - def _mock_fetch(self, message): - self.fetcher._keymanager.fetch_key = Mock() + def _do_fetch(self, message): d = self._create_incoming_email(message) d.addCallback( lambda email: |