diff sat/core/xmpp.py @ 3221:b1c90eedbc12

core (xmpp): fixed workflow with post_xml_treatments in sendMessage: post_xml_treatments was incorrectly added to workflow using chainDeferred (which only add the callback, so post_xml_treatments own callbacks were not waited). As a result, plugins callback could not be waited as expected before a message was sent. This patch fixes it by not using chainDeferred anymore.
author Goffi <goffi@goffi.org>
date Sun, 22 Mar 2020 13:59:19 +0100
parents fee8e33e2b3f
children 0404b306641d
line wrap: on
line diff
--- a/sat/core/xmpp.py	Sun Mar 22 12:52:43 2020 +0100
+++ b/sat/core/xmpp.py	Sun Mar 22 13:59:19 2020 +0100
@@ -496,7 +496,7 @@
         @param type_(unicode): IQ type ('set' or 'get')
         @param timeout(None, int): timeout in seconds
         @return((D)domish.Element: result stanza
-            errback is called if and error stanza is returned
+            errback is called if an error stanza is returned
         """
         iq_elt = xmlstream.IQ(self.xmlstream, type_)
         iq_elt.timeout = timeout
@@ -511,7 +511,7 @@
         iq_error_elt = error.StanzaError(condition).toResponse(iq_elt)
         self.xmlstream.send(iq_error_elt)
 
-    def generateMessageXML(self, data):
+    def generateMessageXML(self, data, post_xml_treatments=None):
         """Generate <message/> stanza from message data
 
         @param data(dict): message data
@@ -524,6 +524,8 @@
                 - type
                 - subject
                 - extra
+        @param post_xml_treatments(Deferred): a Deferred which will be called with data
+            one XML is generated
         @return (dict) message data
         """
         data["xml"] = message_elt = domish.Element((None, "message"))
@@ -554,6 +556,9 @@
                 thread_elt["parent"] = data["extra"]["thread_parent"]
             except KeyError:
                 pass
+
+        if post_xml_treatments is not None:
+            post_xml_treatments.callback(data)
         return data
 
     def addPostXmlCallbacks(self, post_xml_treatments):
@@ -650,9 +655,9 @@
         log.debug(_("Sending message (type {type}, to {to})")
                     .format(type=data["type"], to=to_jid.full()))
 
-        pre_xml_treatments.addCallback(lambda __: self.generateMessageXML(data))
-        pre_xml_treatments.chainDeferred(post_xml_treatments)
-        post_xml_treatments.addCallback(self.sendMessageData)
+        pre_xml_treatments.addCallback(lambda __: self.generateMessageXML(data, post_xml_treatments))
+        pre_xml_treatments.addCallback(lambda __: post_xml_treatments)
+        post_xml_treatments.addCallback(lambda __: self.sendMessageData(data))
         if send_only:
             log.debug(_("Triggers, storage and echo have been inhibited by the "
                         "'send_only' parameter"))