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 | 87c15e3ec5c1ca607890bf72b0d90bb0f114cbb1 (patch) | |
tree | c9e67a7f717367a1e5711f2d50d43d09fb7f2124 /src | |
parent | d7177ea01c968e5ee221aae90eb6e1f815c09972 (diff) |
Add callbacks to inbox insertions
Diffstat (limited to 'src')
-rw-r--r-- | src/leap/mail/incoming/service.py | 38 | ||||
-rw-r--r-- | src/leap/mail/incoming/tests/test_incoming_mail.py | 24 |
2 files changed, 47 insertions, 15 deletions
diff --git a/src/leap/mail/incoming/service.py b/src/leap/mail/incoming/service.py index 2902141..fadfd9f 100644 --- a/src/leap/mail/incoming/service.py +++ b/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/src/leap/mail/incoming/tests/test_incoming_mail.py b/src/leap/mail/incoming/tests/test_incoming_mail.py index 0b1e696..a932a95 100644 --- a/src/leap/mail/incoming/tests/test_incoming_mail.py +++ b/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: |