diff sat/core/xmpp.py @ 3913:944f51f9c2b4

core (xmpp): make `send` a blocking method, fix `sendMessageData` calls: original `send` method is blocking, and it is used as such by Wokkel and thus can't be changed to an async method easily. However, an Async method is necessary to have an async trigger at the very end of the send workflow for end-to-end encryption. To workaround that, `send` is an async method which call `a_send`, an async method which actually does the sending. This way legacy method can still call `send` while `a_send` can be await otherwise. Fix calls to `sendMessageData`: the method now being an `async` one, `ensureDeferred` had to be used in some calls.
author Goffi <goffi@goffi.org>
date Sat, 24 Sep 2022 16:31:39 +0200
parents 8289ac1b34f4
children 993cc8e56aef
line wrap: on
line diff
--- a/sat/core/xmpp.py	Sat Sep 24 16:08:26 2022 +0200
+++ b/sat/core/xmpp.py	Sat Sep 24 16:31:39 2022 +0200
@@ -616,7 +616,7 @@
         """
         raise NotImplementedError
 
-    async def send(self, obj):
+    async def a_send(self, obj):
         # original send method accept string
         # but we restrict to domish.Element to make trigger treatments easier
         assert isinstance(obj, domish.Element)
@@ -630,6 +630,9 @@
             return
         super().send(obj)
 
+    def send(self, obj):
+        defer.ensureDeferred(self.a_send(obj))
+
     async def sendMessageData(self, mess_data):
         """Convenient method to send message data to stream
 
@@ -647,7 +650,7 @@
         #      This trigger point can't cancel the method
         await self.host_app.trigger.asyncPoint("sendMessageData", self, mess_data,
             triggers_no_cancel=True)
-        await self.send(mess_data["xml"])
+        await self.a_send(mess_data["xml"])
         return mess_data
 
     def sendMessage(
@@ -738,7 +741,9 @@
         pre_xml_treatments.addCallback(lambda __: self.generateMessageXML(data, post_xml_treatments))
         pre_xml_treatments.addCallback(lambda __: post_xml_treatments)
         pre_xml_treatments.addErrback(self._cancelErrorTrap)
-        post_xml_treatments.addCallback(lambda __: self.sendMessageData(data))
+        post_xml_treatments.addCallback(
+            lambda __: defer.ensureDeferred(self.sendMessageData(data))
+        )
         if send_only:
             log.debug(_("Triggers, storage and echo have been inhibited by the "
                         "'send_only' parameter"))