diff sat/plugins/plugin_comp_ap_gateway/__init__.py @ 3793:b5c9021020df

component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`: when a `Delete` actity is received, the item is removed from cache, and the corresponding item `retract` is sent to local XMPP subscribers. rel 367
author Goffi <goffi@goffi.org>
date Fri, 17 Jun 2022 14:15:23 +0200
parents 865167c34b82
children d5f343939239
line wrap: on
line diff
--- a/sat/plugins/plugin_comp_ap_gateway/__init__.py	Fri Jun 17 14:15:23 2022 +0200
+++ b/sat/plugins/plugin_comp_ap_gateway/__init__.py	Fri Jun 17 14:15:23 2022 +0200
@@ -1792,3 +1792,43 @@
                 [(subscription.subscriber, None, [item_elt])]
             )
 
+    async def newAPDeleteItem(
+        self,
+        client: SatXMPPEntity,
+        destinee: Optional[jid.JID],
+        node: str,
+        item: dict,
+    ) -> None:
+        """Analyse, cache and send notification for received AP item
+
+        @param destinee: jid of the destinee,
+        @param node: XMPP pubsub node
+        @param item: AP object payload
+        """
+        item_id = item.get("id")
+        if not item_id:
+            raise exceptions.DataError('"id" attribute is missing in item')
+        if self.isLocalURL(item_id):
+            raise ValueError("Local IDs should not be used")
+
+        cached_node = await self.host.memory.storage.getPubsubNode(
+            client, client.jid, node, with_subscriptions=True
+        )
+        if cached_node is None:
+            log.warning(
+                f"Received an item retract for node {node!r} at {client.jid} which is "
+                "not cached"
+            )
+        else:
+            await self.host.memory.storage.deletePubsubItems(cached_node, [item_id])
+            # notifyRetract is expecting domish.Element instances
+            item_elt = domish.Element((None, "item"))
+            item_elt["id"] = item_id
+            for subscription in cached_node.subscriptions:
+                if subscription.state != SubscriptionState.SUBSCRIBED:
+                    continue
+                self.pubsub_service.notifyRetract(
+                    client.jid,
+                    node,
+                    [(subscription.subscriber, None, [item_elt])]
+                )