comparison sat/plugins/plugin_comp_ap_gateway/__init__.py @ 3792:865167c34b82

comp AP gateway: convert pubsub item retractation to AP `Delete` activity: when an item `retract` is received from a node from which an AP actor is subscribed, a corresponding `Delete` activity is created and sent to AP followers. Trying to retract an item from a virtual node of the gateway pubsub service now return a `forbidden` stanza error (those nodes are handled on AP side and thus are not owned by XMPP entities). rel 367
author Goffi <goffi@goffi.org>
date Fri, 17 Jun 2022 14:15:23 +0200
parents efc34a89e70b
children b5c9021020df
comparison
equal deleted inserted replaced
3791:c61233f51b86 3792:865167c34b82
66 LRU_MAX_SIZE, 66 LRU_MAX_SIZE,
67 MEDIA_TYPE_AP, 67 MEDIA_TYPE_AP,
68 TYPE_ACTOR, 68 TYPE_ACTOR,
69 TYPE_ITEM, 69 TYPE_ITEM,
70 TYPE_FOLLOWERS, 70 TYPE_FOLLOWERS,
71 TYPE_TOMBSTONE,
71 NS_AP_PUBLIC, 72 NS_AP_PUBLIC,
72 PUBLIC_TUPLE 73 PUBLIC_TUPLE
73 ) 74 )
74 from .http_server import HTTPServer 75 from .http_server import HTTPServer
75 from .pubsub_service import APPubsubService 76 from .pubsub_service import APPubsubService
468 469
469 if self.client is None: 470 if self.client is None:
470 raise exceptions.InternalError("Client is not set yet") 471 raise exceptions.InternalError("Client is not set yet")
471 472
472 if jid_.host == self.client.jid.userhost(): 473 if jid_.host == self.client.jid.userhost():
473 # this is an proxy JId to an AP Actor 474 # this is an proxy JID to an AP Actor
474 return self._e.unescape(jid_.user) 475 return self._e.unescape(jid_.user)
475 476
476 if node and not jid_.user and not self.mustEncode(node): 477 if node and not jid_.user and not self.mustEncode(node):
477 is_pubsub = await self.isPubsub(jid_) 478 is_pubsub = await self.isPubsub(jid_)
478 # when we have a pubsub service, the user part can be used to set the node 479 # when we have a pubsub service, the user part can be used to set the node
858 they will be automatically subscribed 859 they will be automatically subscribed
859 """ 860 """
860 actor_id = await self.getAPActorIdFromAccount(ap_account) 861 actor_id = await self.getAPActorIdFromAccount(ap_account)
861 inbox = await self.getAPInboxFromId(actor_id) 862 inbox = await self.getAPInboxFromId(actor_id)
862 for item in items: 863 for item in items:
863 mb_data = await self._m.item2mbdata(client, item, service, node) 864 if item.name == "item":
864 if subscribe_comments_nodes: 865 mb_data = await self._m.item2mbdata(client, item, service, node)
865 # we subscribe automatically to comment nodes if any 866 if subscribe_comments_nodes:
866 for comment_data in mb_data.get("comments", []): 867 # we subscribe automatically to comment nodes if any
867 comment_service = jid.JID(comment_data["service"]) 868 for comment_data in mb_data.get("comments", []):
868 comment_node = comment_data["node"] 869 comment_service = jid.JID(comment_data["service"])
869 await self._p.subscribe(client, comment_service, comment_node) 870 comment_node = comment_data["node"]
870 ap_item = await self.mbdata2APitem(client, mb_data) 871 await self._p.subscribe(client, comment_service, comment_node)
871 url_actor = ap_item["object"]["attributedTo"] 872 ap_item = await self.mbdata2APitem(client, mb_data)
873 url_actor = ap_item["object"]["attributedTo"]
874 elif item.name == "retract":
875 author_account = await self.getAPAccountFromJidAndNode(client.jid, node)
876 author_actor_id = self.buildAPURL(TYPE_ACTOR, author_account)
877 url_item = self.buildAPURL(TYPE_ITEM, author_account, item["id"])
878 ap_item = self.createActivity(
879 "Delete",
880 author_actor_id,
881 {
882 "id": url_item,
883 "type": TYPE_TOMBSTONE
884 }
885 )
886 ap_item["to"] = [NS_AP_PUBLIC]
887 url_actor = author_actor_id
888 else:
889 raise exceptions.InternalError(f"unexpected element: {item.toXml()}")
872 resp = await self.signAndPost(inbox, url_actor, ap_item) 890 resp = await self.signAndPost(inbox, url_actor, ap_item)
873 if resp.code >= 300: 891 if resp.code >= 300:
874 text = await resp.text() 892 text = await resp.text()
875 log.warning( 893 log.warning(
876 f"unexpected return code while sending AP item: {resp.code}\n{text}\n" 894 f"unexpected return code while sending AP item: {resp.code}\n{text}\n"