diff libervia/backend/plugins/plugin_xep_0298.py @ 4294:a0ed5c976bf8

component conferences, plugin XEP-0167, XEP-0298: add stream user metadata: A/V conference now adds user metadata about the stream it is forwarding through XEP-0298. This is parsed and added to metadata during confirmation on client side. rel 448
author Goffi <goffi@goffi.org>
date Tue, 06 Aug 2024 23:43:11 +0200
parents dd0891d0b22b
children
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0298.py	Mon Jul 29 03:49:26 2024 +0200
+++ b/libervia/backend/plugins/plugin_xep_0298.py	Tue Aug 06 23:43:11 2024 +0200
@@ -20,6 +20,7 @@
 from urllib.parse import quote, unquote
 from twisted.words.protocols.jabber import jid
 from twisted.words.xish import domish
+from libervia.backend.core.core_types import SatXMPPEntity
 from libervia.backend.core.i18n import _
 from libervia.backend.core.constants import Const as C
 from libervia.backend.core.log import getLogger
@@ -27,6 +28,8 @@
 from zope.interface import implementer
 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
 
+from libervia.backend.plugins.plugin_xep_0166 import XEP_0166
+
 log = getLogger(__name__)
 
 
@@ -36,7 +39,7 @@
     C.PI_TYPE: "XEP",
     C.PI_MODES: C.PLUG_MODE_BOTH,
     C.PI_PROTOCOLS: [],
-    C.PI_DEPENDENCIES: [],
+    C.PI_DEPENDENCIES: ["XEP-0167"],
     C.PI_MAIN: "XEP_0298",
     C.PI_HANDLER: "yes",
     C.PI_DESCRIPTION: _(
@@ -55,10 +58,58 @@
     def __init__(self, host):
         log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
         self.host = host
+        self._j: XEP_0166 = host.plugins["XEP-0166"]
+        host.trigger.add(
+            "XEP-0167_jingle_session_init", self._jingle_session_init_trigger
+        )
+        host.trigger.add("XEP-0167_jingle_handler", self._jingle_handler_trigger)
 
     def get_handler(self, client):
         return CoinHandler(self)
 
+    def _jingle_session_init_trigger(
+        self,
+        client: SatXMPPEntity,
+        session: dict,
+        content_name: str,
+        media: str,
+        media_data: dict,
+        desc_elt: domish.Element,
+    ) -> None:
+        """Check for the presence of "user" metadata, and add relevant elements."""
+        if client.profile == "conferences":
+            if content_name != next(iter(session["contents"].keys())):
+                # We only apply metadata for the first content, as it is global.
+                return
+            try:
+                user = session["metadata"]["user"]
+            except KeyError:
+                return
+            jingle_elt = session["jingle_elt"]
+            conference_info_elt = self.add_conference_info(jingle_elt, True)
+            self.add_user(conference_info_elt, user)
+
+    async def _jingle_handler_trigger(
+        self,
+        client: SatXMPPEntity,
+        action: str,
+        session: dict,
+        content_name: str,
+        desc_elt: domish.Element,
+    ) -> None:
+        # this is a session metadata, so we only generate it on the first content
+        if action == self._j.A_PREPARE_CONFIRMATION and content_name == next(
+            iter(session["contents"])
+        ):
+            jingle_elt = session["jingle_elt"]
+            infos = self.parse(jingle_elt)
+            try:
+                user = infos["info"]["users"][0]
+            except (KeyError, IndexError):
+                pass
+            else:
+                session.setdefault("metadata", {})["peer_user"] = user
+
     def add_conference_info(
         self, jingle_elt: domish.Element, is_focus: bool = False, **kwargs
     ) -> domish.Element:
@@ -104,7 +155,7 @@
         """
         try:
             conference_info_elt = next(
-                jingle_elt.elements((NS_CONFERENCE_INFO, "conference-info"))
+                jingle_elt.elements(NS_CONFERENCE_INFO, "conference-info")
             )
         except StopIteration:
             return {}