diff sat/plugins/plugin_xep_0470.py @ 3956:3cb9ade2ab84

plugin pubsub signing: pubsub items signature implementation: - this is based on a protoXEP, not yet an official XEP: https://github.com/xsf/xeps/pull/1228 - XEP-0470: `set` attachment handler can now be async rel 381
author Goffi <goffi@goffi.org>
date Fri, 28 Oct 2022 18:47:17 +0200
parents 2b2856ae5eeb
children 722c25818778
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0470.py	Fri Oct 28 18:47:17 2022 +0200
+++ b/sat/plugins/plugin_xep_0470.py	Fri Oct 28 18:47:17 2022 +0200
@@ -30,7 +30,7 @@
 from sat.core.core_types import SatXMPPEntity
 from sat.core import exceptions
 from sat.tools.common import uri, data_format, date_utils
-from sat.tools.utils import xmpp_date
+from sat.tools.utils import asDeferred, xmpp_date
 
 
 log = getLogger(__name__)
@@ -63,10 +63,10 @@
         self._p = host.plugins["XEP-0060"]
         self.handlers: Dict[Tuple[str, str], dict[str, Any]] = {}
         host.trigger.add("XEP-0277_send", self.onMBSend)
-        self.registerAttachmentHandler(
+        self.register_attachment_handler(
             "noticed", NS_PUBSUB_ATTACHMENTS, self.noticedGet, self.noticedSet
         )
-        self.registerAttachmentHandler(
+        self.register_attachment_handler(
             "reactions", NS_PUBSUB_ATTACHMENTS, self.reactionsGet, self.reactionsSet
         )
         host.bridge.addMethod(
@@ -89,7 +89,7 @@
     def getHandler(self, client):
         return PubsubAttachments_Handler()
 
-    def registerAttachmentHandler(
+    def register_attachment_handler(
         self,
         name: str,
         namespace: str,
@@ -113,6 +113,7 @@
             it will be called with (client, data, former_elt of None if there was no
             former element). When suitable, ``operation`` should be used to check if we
             request an ``update`` or a ``replace``.
+            The callback can be either a blocking method, a Deferred or a coroutine
         """
         key = (name, namespace)
         if key in self.handlers:
@@ -299,7 +300,7 @@
         @return: A tuple with:
             - the list of attachments data, one item per found sender. The attachments
               data are dict containing attachment, no ``extra`` field is used here
-              (contrarily to attachments data used with ``setAttachments``).
+              (contrarily to attachments data used with ``set_attachements``).
             - metadata returned by the call to ``getItems``
         """
         if extra is None:
@@ -320,9 +321,9 @@
     ) -> None:
         client = self.host.getClient(profile_key)
         attachments = data_format.deserialise(attachments_s)  or {}
-        return defer.ensureDeferred(self.setAttachments(client, attachments))
+        return defer.ensureDeferred(self.set_attachements(client, attachments))
 
-    def applySetHandler(
+    async def apply_set_handler(
         self,
         client: SatXMPPEntity,
         attachments_data: dict,
@@ -375,7 +376,9 @@
                 former_elt = next(attachments_elt.elements(namespace, name))
             except StopIteration:
                 former_elt = None
-            new_elt = handler["set"](client, attachments_data, former_elt)
+            new_elt = await asDeferred(
+                handler["set"], client, attachments_data, former_elt
+            )
             if new_elt != former_elt:
                 if former_elt is not None:
                     attachments_elt.children.remove(former_elt)
@@ -383,7 +386,7 @@
                     attachments_elt.addChild(new_elt)
         return item_elt
 
-    async def setAttachments(
+    async def set_attachements(
         self,
         client: SatXMPPEntity,
         attachments_data: Dict[str, Any]
@@ -425,7 +428,7 @@
             else:
                 item_elt = items[0]
 
-        item_elt = self.applySetHandler(
+        item_elt = await self.apply_set_handler(
             client,
             attachments_data,
             item_elt=item_elt,