From 9f047d311c9fd40644505f67dd6604d310cd9c03 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sat, 15 Oct 2022 20:38:33 +0200 Subject: [PATCH] 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. --- sat/plugins/plugin_xep_0470.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/sat/plugins/plugin_xep_0470.py b/sat/plugins/plugin_xep_0470.py index e11f960f..dc9e3279 100644 --- a/sat/plugins/plugin_xep_0470.py +++ b/sat/plugins/plugin_xep_0470.py @@ -164,7 +164,9 @@ class PubsubAttachments: 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 @@ class PubsubAttachments: 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"