diff libervia/backend/plugins/plugin_xep_0045.py @ 4245:a7d4007a8fa5

plugin XEP-0272: implement XEP-0272: Multiparty Jingle (Muji) rel 429
author Goffi <goffi@goffi.org>
date Wed, 15 May 2024 17:34:46 +0200
parents 8b673bb307c1
children 0d7bb4df2343
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0045.py	Sat May 11 13:52:43 2024 +0200
+++ b/libervia/backend/plugins/plugin_xep_0045.py	Wed May 15 17:34:46 2024 +0200
@@ -21,6 +21,7 @@
 from typing import Optional
 import uuid
 
+import shortuuid
 from twisted.internet import defer
 from twisted.python import failure
 from twisted.words.protocols.jabber import jid
@@ -80,7 +81,7 @@
         self.room = room
 
 
-class XEP_0045(object):
+class XEP_0045:
     # TODO: handle invitations
     # FIXME: this plugin need a good cleaning, join method is messy
 
@@ -512,25 +513,33 @@
         client = self.host.get_client(profile_key)
         return self.get_unique_name(client, muc_service or None).full()
 
-    def get_unique_name(self, client, muc_service=None):
+    def get_unique_name(
+        self,
+        client: SatXMPPEntity,
+        muc_service: jid.JID|None = None,
+        prefix: str = ""
+    ) -> jid.JID:
         """Return unique name for a room, avoiding collision
 
-        @param muc_service (jid.JID) : leave empty string to use the default service
-        @return: jid.JID (unique room bare JID)
+        @param client: Client instance.
+        @param muc_service: leave empty string to use the default service
+        @param prefix: prefix to use in room name.
+        @return: unique room bare JID
         """
-        # TODO: we should use #RFC-0045 10.1.4 when available here
-        room_name = str(uuid.uuid4())
+        room_name = f"{prefix}{shortuuid.uuid()}"
         if muc_service is None:
             try:
                 muc_service = client.muc_service
             except AttributeError:
-                raise exceptions.NotReady("Main server MUC service has not been checked yet")
+                raise exceptions.NotReady(
+                    "Main server MUC service has not been checked yet"
+                )
             if muc_service is None:
                 log.warning(_("No MUC service found on main server"))
                 raise exceptions.FeatureNotFound
 
         muc_service = muc_service.userhost()
-        return jid.JID("{}@{}".format(room_name, muc_service))
+        return jid.JID(f"{room_name}@{muc_service}")
 
     def get_default_muc(self):
         """Return the default MUC.
@@ -600,6 +609,8 @@
                 self._join_eb(failure.Failure(e), client, room_jid, nick, password)
             )
         else:
+            room.on_joined_callbacks = []
+            room.on_left_callbacks = []
             await defer.ensureDeferred(
                 self._join_cb(room, client, room_jid, nick)
             )
@@ -1258,6 +1269,8 @@
         else:
             if not room.fully_joined.called:
                 return
+            for cb in room.on_joined_callbacks:
+                defer.ensureDeferred(cb(room, user))
             try:
                 self._changing_nicks.remove(user.nick)
             except KeyError:
@@ -1313,6 +1326,8 @@
             if not room.fully_joined.called:
                 return
             log.debug(_("user {nick} left room {room_id}").format(nick=user.nick, room_id=room.occupantJID.userhost()))
+            for cb in room.on_left_callbacks:
+                defer.ensureDeferred(cb(room, user))
             extra = {'info_type': ROOM_USER_LEFT,
                      'user_affiliation': user.affiliation,
                      'user_role': user.role,