changeset 3945:2b2856ae5eeb

plugin XEP-0470: fix autocreate in `create_attachments_node`: Due to config check, `create_attachments_node` was failing when the target node was not already existing. However, this is the case when an item is published and `autocreate` is expected, thus the node is now created if not already existing and new `autocreate` argument is set.
author Goffi <goffi@goffi.org>
date Sat, 15 Oct 2022 20:38:33 +0200
parents 748094d5a74d
children f2a5936f2496
files sat/plugins/plugin_xep_0470.py
diffstat 1 files changed, 23 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0470.py	Sat Oct 15 20:38:33 2022 +0200
+++ b/sat/plugins/plugin_xep_0470.py	Sat Oct 15 20:38:33 2022 +0200
@@ -164,7 +164,9 @@
         data: dict
     ) -> bool:
         """trigger to create attachment node on each publication"""
-        await self.create_attachments_node(client, service, node, item["id"])
+        await self.create_attachments_node(
+            client, service, node, item["id"], autocreate=True
+        )
         return True
 
     async def create_attachments_node(
@@ -172,10 +174,27 @@
         client: SatXMPPEntity,
         service: jid.JID,
         node: str,
-        item_id: str
+        item_id: str,
+        autocreate: bool = False
     ):
-        """Create node for attachements if necessary"""
-        node_config = await self._p.getConfiguration(client, service, node)
+        """Create node for attachements if necessary
+
+        @param service: service of target node
+        @param node: node where target item is published
+        @param item_id: ID of target item
+        @param autocrate: if True, target node is create if it doesn't exist
+        """
+        try:
+            node_config = await self._p.getConfiguration(client, service, node)
+        except error.StanzaError as e:
+            if e.condition == "item-not-found" and autocreate:
+                # we auto-create the missing node
+                await self._p.createNode(
+                    client, service, node
+                )
+                node_config = await self._p.getConfiguration(client, service, node)
+            else:
+                raise e
         try:
             # FIXME: check if this is the best publish_model option
             node_config.fields["pubsub#publish_model"].value = "open"