comparison 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
comparison
equal deleted inserted replaced
3912:ce5d03772689 3913:944f51f9c2b4
614 614
615 @param post_xml_treatments(D): the same Deferred as in sendMessage trigger 615 @param post_xml_treatments(D): the same Deferred as in sendMessage trigger
616 """ 616 """
617 raise NotImplementedError 617 raise NotImplementedError
618 618
619 async def send(self, obj): 619 async def a_send(self, obj):
620 # original send method accept string 620 # original send method accept string
621 # but we restrict to domish.Element to make trigger treatments easier 621 # but we restrict to domish.Element to make trigger treatments easier
622 assert isinstance(obj, domish.Element) 622 assert isinstance(obj, domish.Element)
623 # XXX: this trigger is the last one before sending stanza on wire 623 # XXX: this trigger is the last one before sending stanza on wire
624 # it is intended for things like end 2 end encryption. 624 # it is intended for things like end 2 end encryption.
628 # a lower priority 628 # a lower priority
629 if not (await self.host_app.trigger.asyncPoint("send", self, obj)): 629 if not (await self.host_app.trigger.asyncPoint("send", self, obj)):
630 return 630 return
631 super().send(obj) 631 super().send(obj)
632 632
633 def send(self, obj):
634 defer.ensureDeferred(self.a_send(obj))
635
633 async def sendMessageData(self, mess_data): 636 async def sendMessageData(self, mess_data):
634 """Convenient method to send message data to stream 637 """Convenient method to send message data to stream
635 638
636 This method will send mess_data[u'xml'] to stream, but a trigger is there 639 This method will send mess_data[u'xml'] to stream, but a trigger is there
637 The trigger can't be cancelled, it's a good place for e2e encryption which 640 The trigger can't be cancelled, it's a good place for e2e encryption which
645 # This is intented for e2e encryption which doesn't do full stanza 648 # This is intented for e2e encryption which doesn't do full stanza
646 # encryption (e.g. OTR) 649 # encryption (e.g. OTR)
647 # This trigger point can't cancel the method 650 # This trigger point can't cancel the method
648 await self.host_app.trigger.asyncPoint("sendMessageData", self, mess_data, 651 await self.host_app.trigger.asyncPoint("sendMessageData", self, mess_data,
649 triggers_no_cancel=True) 652 triggers_no_cancel=True)
650 await self.send(mess_data["xml"]) 653 await self.a_send(mess_data["xml"])
651 return mess_data 654 return mess_data
652 655
653 def sendMessage( 656 def sendMessage(
654 self, to_jid, message, subject=None, mess_type="auto", extra=None, uid=None, 657 self, to_jid, message, subject=None, mess_type="auto", extra=None, uid=None,
655 no_trigger=False): 658 no_trigger=False):
736 .format(type=data["type"], to=to_jid.full())) 739 .format(type=data["type"], to=to_jid.full()))
737 740
738 pre_xml_treatments.addCallback(lambda __: self.generateMessageXML(data, post_xml_treatments)) 741 pre_xml_treatments.addCallback(lambda __: self.generateMessageXML(data, post_xml_treatments))
739 pre_xml_treatments.addCallback(lambda __: post_xml_treatments) 742 pre_xml_treatments.addCallback(lambda __: post_xml_treatments)
740 pre_xml_treatments.addErrback(self._cancelErrorTrap) 743 pre_xml_treatments.addErrback(self._cancelErrorTrap)
741 post_xml_treatments.addCallback(lambda __: self.sendMessageData(data)) 744 post_xml_treatments.addCallback(
745 lambda __: defer.ensureDeferred(self.sendMessageData(data))
746 )
742 if send_only: 747 if send_only:
743 log.debug(_("Triggers, storage and echo have been inhibited by the " 748 log.debug(_("Triggers, storage and echo have been inhibited by the "
744 "'send_only' parameter")) 749 "'send_only' parameter"))
745 else: 750 else:
746 self.addPostXmlCallbacks(post_xml_treatments) 751 self.addPostXmlCallbacks(post_xml_treatments)