changeset 3817:998c5318230f

plugin identity: make the plugin compatible with component + description: the plugin can now be used with components. the new `description` field is now available. rel 368
author Goffi <goffi@goffi.org>
date Wed, 29 Jun 2022 11:49:03 +0200
parents 213e83a4ed10
children 2863345c9bbb
files sat/plugins/plugin_misc_identity.py
diffstat 1 files changed, 58 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_identity.py	Wed Jun 29 11:47:48 2022 +0200
+++ b/sat/plugins/plugin_misc_identity.py	Wed Jun 29 11:49:03 2022 +0200
@@ -54,6 +54,7 @@
     C.PI_NAME: "Identity Plugin",
     C.PI_IMPORT_NAME: IMPORT_NAME,
     C.PI_TYPE: C.PLUG_TYPE_MISC,
+    C.PI_MODES: C.PLUG_MODE_BOTH,
     C.PI_PROTOCOLS: [],
     C.PI_DEPENDENCIES: [],
     C.PI_RECOMMENDATIONS: ["XEP-0045"],
@@ -95,6 +96,12 @@
                 "update_is_new_data": self.nicknamesUpdateIsNewData,
                 "store": True,
             },
+            "description": {
+                "type": str,
+                "get_all": True,
+                "get_post_treatment": self.descriptionGetPostTreatment,
+                "store": True,
+            }
         }
         host.trigger.add("roster_update", self._rosterUpdateTrigger)
         host.memory.setSignalOnUpdate("avatar")
@@ -263,6 +270,14 @@
                 f"{value} has wrong type: it is {type(value)} while {value_type} was "
                 f"expected")
 
+    def getFieldType(self, metadata_name: str) -> str:
+        """Return the type the requested field
+
+        @param metadata_name: name of the field to check
+        @raise KeyError: the request field doesn't exist
+        """
+        return self.metadata[metadata_name]["type"]
+
     async def get(
             self,
             client: SatXMPPEntity,
@@ -330,7 +345,10 @@
                 if data:
                     self.checkType(metadata_name, data)
                     if get_all:
-                        all_data.extend(data)
+                        if isinstance(data, list):
+                            all_data.extend(data)
+                        else:
+                            all_data.append(data)
                     else:
                         break
         else:
@@ -396,13 +414,13 @@
             await utils.asDeferred(post_treatment, client, entity, data)
 
     async def update(
-            self,
-            client: SatXMPPEntity,
-            origin: str,
-            metadata_name: str,
-            data: Any,
-            entity: Optional[jid.JID]
-        ):
+        self,
+        client: SatXMPPEntity,
+        origin: str,
+        metadata_name: str,
+        data: Any,
+        entity: Optional[jid.JID]
+    ):
         """Update a metadata in cache
 
         This method may be called by plugins when an identity metadata is available.
@@ -626,17 +644,20 @@
               if there is no user part.
         For MUC, room nick is always put first
         """
-        # we first check roster
         nicknames = []
+
+        # for MUC we add resource
         if entity.resource:
             # getIdentityJid let the resource only if the entity is a MUC room
             # occupant jid
             nicknames.append(entity.resource)
 
-        roster_item = client.roster.getItem(entity.userhostJID())
-        if roster_item is not None and roster_item.name:
-            # user set name has priority over entity set name
-            nicknames.append(roster_item.name)
+        # we first check roster (if we are not in a component)
+        if not client.is_component:
+            roster_item = client.roster.getItem(entity.userhostJID())
+            if roster_item is not None and roster_item.name:
+                # user set name has priority over entity set name
+                nicknames.append(roster_item.name)
 
         nicknames.extend(plugin_nicknames)
 
@@ -652,6 +673,15 @@
     def nicknamesUpdateIsNewData(self, client, entity, cached_data, new_nicknames):
         return not set(new_nicknames).issubset(cached_data)
 
+    async def descriptionGetPostTreatment(
+        self,
+        client: SatXMPPEntity,
+        entity: jid.JID,
+        plugin_description: List[str]
+    ) -> str:
+        """Join all descriptions in a unique string"""
+        return '\n'.join(plugin_description)
+
     def _getIdentity(self, entity_s, metadata_filter, use_cache, profile):
         entity = jid.JID(entity_s)
         client = self.host.getClient(profile)
@@ -661,15 +691,20 @@
         return d
 
     async def getIdentity(
-        self, client, entity=None, metadata_filter=None, use_cache=True):
+        self,
+        client: SatXMPPEntity,
+        entity: Optional[jid.JID] = None,
+        metadata_filter: Optional[List[str]] = None,
+        use_cache: bool = True
+    ) -> Dict[str, Any]:
         """Retrieve identity of an entity
 
-        @param entity(jid.JID, None): entity to check
-        @param metadata_filter(list[str], None): if not None or empty, only return
+        @param entity: entity to check
+        @param metadata_filter: if not None or empty, only return
             metadata in this filter
-        @param use_cache(bool): if False, cache won't be checked
+        @param use_cache: if False, cache won't be checked
             should be True most of time, to avoid useless network requests
-        @return (dict): identity data
+        @return: identity data
         """
         id_data = {}
 
@@ -741,7 +776,10 @@
         @param with_guests: if True, get affiliations of people invited by email
 
         """
-        entities = client.roster.getJids() + [client.jid.userhostJID()]
+        if client.is_component:
+            entities = [client.jid.userhostJID()]
+        else:
+            entities = client.roster.getJids() + [client.jid.userhostJID()]
 
         return await self.getIdentities(
             client,
@@ -757,7 +795,7 @@
     async def setIdentity(self, client, id_data):
         """Update profile's identity
 
-        @param id_data(dict): data to update, key can be on of self.metadata keys
+        @param id_data(dict): data to update, key can be one of self.metadata keys
         """
         if not id_data.keys() <= self.metadata.keys():
             raise ValueError(