component pubsub: PEP presence access model and auto-subscription
- Implement `presence` access model for PEP nodes. - Add PEP auto-subscription (`+notify` handling) - New `get_pep_last_items` storage method to retrieve in a single query items to send to auto-subscribers. - Online resource tracking. This completes the PEP support. rel 476
This commit is contained in:
parent
81bd3dcf01
commit
42c2f88b30
1 changed files with 610 additions and 79 deletions
|
|
@ -192,6 +192,8 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
self.xep_0356 = cast(XEP_0356, G.host.plugins["XEP-0356"])
|
||||
self.xep_0496 = cast(XEP_0496, G.host.plugins["XEP-0496"])
|
||||
|
||||
G.host.trigger.add("XEP-0356_presence_received", self._on_presence_received)
|
||||
|
||||
@property
|
||||
def client(self) -> SatXMPPComponent:
|
||||
return self.service.client
|
||||
|
|
@ -1669,6 +1671,82 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
if not success:
|
||||
log.error(f"Failed to send notification: {result}")
|
||||
|
||||
async def _on_presence_received(
|
||||
self,
|
||||
client: SatXMPPComponent,
|
||||
from_jid: jid.JID,
|
||||
presence_elt: domish.Element,
|
||||
) -> None:
|
||||
"""Handle presence for PEP auto-subscription.
|
||||
|
||||
When a subscriber comes online with +notify capability, send them the last item
|
||||
from each relevant PEP node where they have presence subscription to the node
|
||||
owner.
|
||||
|
||||
@param client: The pubsub component client.
|
||||
@param from_jid: Full JID of the subscriber sending presence.
|
||||
@param presence_elt: The presence stanza.
|
||||
"""
|
||||
if not client.is_component:
|
||||
return
|
||||
|
||||
requestor_jid = from_jid.userhostJID()
|
||||
|
||||
caps_hash = G.host.memory.get_entity_datum(client, from_jid, C.ENTITY_CAP_HASH)
|
||||
if not caps_hash:
|
||||
return
|
||||
|
||||
try:
|
||||
disco_infos = G.host.memory.disco.hashes[caps_hash]
|
||||
except KeyError:
|
||||
return
|
||||
|
||||
notify_nodes = [f[:-7] for f in disco_infos.features if f.endswith("+notify")]
|
||||
if not notify_nodes:
|
||||
return
|
||||
|
||||
# Build list of publishers (entities the requestor can receive notifications from)
|
||||
|
||||
# Self-subscription
|
||||
publishers = {requestor_jid}
|
||||
|
||||
try:
|
||||
roster = await self.xep_0356.get_roster(client, requestor_jid)
|
||||
except exceptions.PermissionError:
|
||||
roster = {}
|
||||
|
||||
# Add publishers that the requestor has subscribed to.
|
||||
publishers.update(
|
||||
roster_jid
|
||||
for roster_jid, roster_item in roster.items()
|
||||
if roster_item.subscriptionTo
|
||||
)
|
||||
|
||||
try:
|
||||
last_items = await G.storage.get_pep_last_items(
|
||||
client,
|
||||
list(publishers),
|
||||
notify_nodes,
|
||||
[AccessModel.open, AccessModel.presence],
|
||||
)
|
||||
except Exception:
|
||||
log.warning(f"Failed to get PEP last items for {from_jid}")
|
||||
return
|
||||
|
||||
# Send notifications
|
||||
for node, item in last_items:
|
||||
# For self-subscription, no need to check access
|
||||
if node.service != requestor_jid:
|
||||
if not await self.xep_0496.has_access(
|
||||
client, node.service, node, requestor_jid
|
||||
):
|
||||
continue
|
||||
|
||||
item_elt = cast(domish.Element, item.data)
|
||||
await self._send_notification(
|
||||
node, "items", str(node.name), from_jid, [item_elt]
|
||||
)
|
||||
|
||||
|
||||
class PubsubService(rsm.PubSubService):
|
||||
def __init__(self, component: "PubsubComponent"):
|
||||
|
|
|
|||
Loading…
Reference in a new issue