comparison sat/plugins/plugin_comp_ap_gateway/__init__.py @ 3855:54305ebf5b94

component AP gateway: "repeat" to "Announce" conversion: when an XMPP blog post is marked as repeated, it is converted to an AP Announce activity rel 370
author Goffi <goffi@goffi.org>
date Thu, 14 Jul 2022 12:55:30 +0200
parents a56d5ad466b3
children bc7f9d0a404f
comparison
equal deleted inserted replaced
3854:8a2c46122a11 3855:54305ebf5b94
1552 1552
1553 return self.buildAPURL( 1553 return self.buildAPURL(
1554 TYPE_ITEM, parent_ap_account, parent_item 1554 TYPE_ITEM, parent_ap_account, parent_item
1555 ) 1555 )
1556 1556
1557 async def repeatedMB2APItem(
1558 self,
1559 mb_data: dict
1560 ) -> dict:
1561 """Convert repeated blog item to suitable AP Announce activity
1562
1563 @param mb_data: microblog metadata of an item repeating an other blog post
1564 @return: Announce activity linking to the repeated item
1565 """
1566 repeated = mb_data["extra"]["repeated"]
1567 repeater = jid.JID(repeated["by"])
1568 repeater_account = await self.getAPAccountFromJidAndNode(
1569 repeater,
1570 None
1571 )
1572 repeater_id = self.buildAPURL(TYPE_ACTOR, repeater_account)
1573 repeated_uri = repeated["uri"]
1574
1575 if not repeated_uri.startswith("xmpp:"):
1576 log.warning(
1577 "Only xmpp: URL are handled for repeated item at the moment, ignoring "
1578 f"item {mb_data}"
1579 )
1580 raise NotImplementedError
1581 parsed_url = uri.parseXMPPUri(repeated_uri)
1582 if parsed_url["type"] != "pubsub":
1583 log.warning(
1584 "Only pubsub URL are handled for repeated item at the moment, ignoring "
1585 f"item {mb_data}"
1586 )
1587 raise NotImplementedError
1588 rep_service = jid.JID(parsed_url["path"])
1589 rep_item = parsed_url["item"]
1590 activity_id = self.buildAPURL("item", repeater.userhost(), mb_data["id"])
1591
1592 if rep_service.host == self.client.jid.userhost():
1593 # it's an AP actor linked through this gateway
1594 # in this case we can simply use the item ID
1595 if not rep_item.startswith("https:"):
1596 log.warning(
1597 f"Was expecting an HTTPS url as item ID and got {rep_item!r}\n"
1598 f"{mb_data}"
1599 )
1600 announced_uri = rep_item
1601 repeated_account = self._e.unescape(rep_service.user)
1602 else:
1603 # the repeated item is an XMPP publication, we build the corresponding ID
1604 rep_node = parsed_url["node"]
1605 repeated_account = await self.getAPAccountFromJidAndNode(
1606 rep_service, rep_node
1607 )
1608 announced_uri = self.buildAPURL("item", repeated_account, rep_item)
1609
1610 announce = self.createActivity(
1611 "Announce", repeater_id, announced_uri, activity_id=activity_id
1612 )
1613 announce["to"] = [NS_AP_PUBLIC]
1614 announce["cc"] = [
1615 self.buildAPURL(TYPE_FOLLOWERS, repeater_account),
1616 await self.getAPActorIdFromAccount(repeated_account)
1617 ]
1618 return announce
1619
1557 async def mbdata2APitem( 1620 async def mbdata2APitem(
1558 self, 1621 self,
1559 client: SatXMPPEntity, 1622 client: SatXMPPEntity,
1560 mb_data: dict, 1623 mb_data: dict,
1561 public=True 1624 public=True
1569 if True, the AP Item will be marked as public, and AP followers of target AP 1632 if True, the AP Item will be marked as public, and AP followers of target AP
1570 account (which retrieve from ``service``) will be put in ``cc``. 1633 account (which retrieve from ``service``) will be put in ``cc``.
1571 ``inReplyTo`` will also be set if suitable 1634 ``inReplyTo`` will also be set if suitable
1572 if False, no destinee will be set (i.e., no ``to`` or ``cc`` or public flag). 1635 if False, no destinee will be set (i.e., no ``to`` or ``cc`` or public flag).
1573 This is usually used for direct messages. 1636 This is usually used for direct messages.
1574 @return: AP item 1637 @return: Activity item
1575 """ 1638 """
1639 extra = mb_data.get("extra", {})
1640 if "repeated" in extra:
1641 return await self.repeatedMB2APItem(mb_data)
1576 if not mb_data.get("id"): 1642 if not mb_data.get("id"):
1577 mb_data["id"] = shortuuid.uuid() 1643 mb_data["id"] = shortuuid.uuid()
1578 if not mb_data.get("author_jid"): 1644 if not mb_data.get("author_jid"):
1579 mb_data["author_jid"] = client.jid.userhost() 1645 mb_data["author_jid"] = client.jid.userhost()
1580 ap_account = await self.getAPAccountFromJidAndNode( 1646 ap_account = await self.getAPAccountFromJidAndNode(