changeset 4094:c3b68fdc2de7

component AP gateway: fix handling of XMPP comments authors: the gateway was supposing that comments where emitted from PEP of author. While this is the case for most blog posts, it's not for comments. Instead the component is now using `author_jid` which is retrieved by XEP-0277 plugin, and reject the item if the auhor is not verified (i.e. if `publisher` attribute is not set by XMPP service).
author Goffi <goffi@goffi.org>
date Mon, 12 Jun 2023 14:50:43 +0200
parents 13b1079c27ec
children 684ba556a617
files libervia/backend/plugins/plugin_comp_ap_gateway/__init__.py
diffstat 1 files changed, 24 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_comp_ap_gateway/__init__.py	Mon Jun 12 14:45:13 2023 +0200
+++ b/libervia/backend/plugins/plugin_comp_ap_gateway/__init__.py	Mon Jun 12 14:50:43 2023 +0200
@@ -125,6 +125,10 @@
 RE_ALLOWED_UNQUOTED = re.compile(r"^[a-zA-Z0-9_-]+$")
 
 
+class MissingLocalPartError(Exception):
+    """JID local part is missing"""
+
+
 class APGateway:
     IMPORT_NAME = IMPORT_NAME
     # show data send or received through HTTP, used for debugging
@@ -668,7 +672,7 @@
             user = self.period_encode(user)
 
         if not user:
-            raise exceptions.InternalError("there should be a user part")
+            raise MissingLocalPartError("there should be a local part")
 
         if node:
             account_elts.extend((node, "---"))
@@ -2094,9 +2098,25 @@
                 raise exceptions.InternalError(
                     "node or service is missing in mb_data"
                 )
-            target_ap_account = await self.get_ap_account_from_jid_and_node(
-                service, node
-            )
+            try:
+                target_ap_account = await self.get_ap_account_from_jid_and_node(
+                    service, node
+                )
+            except MissingLocalPartError:
+                # we have no local part, this is probably a comment
+                try:
+                    service = jid.JID(mb_data["author_jid"])
+                    if not mb_data["author_jid_verified"]:
+                        raise KeyError
+                except KeyError:
+                    raise exceptions.DataError(
+                        "Can't identify post author. Be sure to use a pubsub service "
+                        "setting the 'publisher' attribute."
+                    )
+                else:
+                    target_ap_account = await self.get_ap_account_from_jid_and_node(
+                        service, node
+                    )
             if self.is_virtual_jid(service):
                 # service is a proxy JID for AP account
                 actor_data = await self.get_ap_actor_data_from_account(target_ap_account)