diff sat/plugins/plugin_xep_0054.py @ 3277:cf07641b764d

plugin identity: fixed infinite loop on nicknames update
author Goffi <goffi@goffi.org>
date Mon, 18 May 2020 23:52:34 +0200
parents 7aa01e262e05
children 8de63fe6b5c9
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0054.py	Mon May 18 23:48:40 2020 +0200
+++ b/sat/plugins/plugin_xep_0054.py	Mon May 18 23:52:34 2020 +0200
@@ -48,10 +48,11 @@
 except ImportError:
     from wokkel.subprotocols import XMPPHandler
 
+IMPORT_NAME = "XEP-0054"
 
 PLUGIN_INFO = {
     C.PI_NAME: "XEP 0054 Plugin",
-    C.PI_IMPORT_NAME: "XEP-0054",
+    C.PI_IMPORT_NAME: IMPORT_NAME,
     C.PI_TYPE: "XEP",
     C.PI_PROTOCOLS: ["XEP-0054", "XEP-0153"],
     C.PI_DEPENDENCIES: ["IDENTITY"],
@@ -81,8 +82,8 @@
         log.info(_("Plugin XEP_0054 initialization"))
         self.host = host
         self._i = host.plugins['IDENTITY']
-        self._i.register('avatar', self.getAvatar, self.setAvatar)
-        self._i.register('nicknames', self.getNicknames, self.setNicknames)
+        self._i.register(IMPORT_NAME, 'avatar', self.getAvatar, self.setAvatar)
+        self._i.register(IMPORT_NAME, 'nicknames', self.getNicknames, self.setNicknames)
         host.trigger.add("presence_available", self.presenceAvailableTrigger)
 
     def getHandler(self, client):
@@ -106,19 +107,6 @@
             NS_VCARD, client.profile)
         await client._xep_0054_avatar_hashes.load()
 
-    def getCache(self, client, entity_jid, name):
-        """return cached value for jid
-
-        @param entity_jid(jid.JID): target contact
-        @param name(unicode): name of the value ('nick' or 'avatar')
-        @return(unicode, None): wanted value or None"""
-        entity_jid = self._i.getIdentityJid(client, entity_jid)
-        try:
-            data = self.host.memory.getEntityData(client, entity_jid, [name])
-        except exceptions.UnknownEntityError:
-            return None
-        return data.get(name)
-
     def savePhoto(self, client, photo_elt, entity_jid):
         """Parse a <PHOTO> photo_elt and save the picture"""
         # XXX: this method is launched in a separate thread
@@ -184,6 +172,7 @@
                 nickname = vcard_dict["nickname"] = str(elem)
                 await self._i.update(
                     client,
+                    IMPORT_NAME,
                     "nicknames",
                     [nickname],
                     entity_jid
@@ -216,6 +205,7 @@
                         avatar_cache = self.host.common_cache.getMetadata(avatar_hash)
                         await self._i.update(
                             client,
+                            IMPORT_NAME,
                             "avatar",
                             {
                                 'path': avatar_cache['path'],
@@ -225,7 +215,8 @@
                             entity_jid
                         )
                     else:
-                        await self._i.update(client, "avatar", None, entity_jid)
+                        await self._i.update(
+                            client, IMPORT_NAME, "avatar", None, entity_jid)
             else:
                 log.debug("FIXME: [{}] VCard_elt tag is not managed yet".format(elem.name))
 
@@ -288,7 +279,7 @@
         try:
             vcard_elt = await self.getVCardElement(client, entity_jid)
         except exceptions.DataError:
-            self._i.update(client, "avatar", None, entity_jid)
+            self._i.update(client, IMPORT_NAME, "avatar", IMPORT_NAME, None, entity_jid)
         except Exception as e:
             log.warning(_(
                 "Can't get vCard for {entity_jid}: {e}"
@@ -313,7 +304,13 @@
             vcard = await self.getCard(client, entity_jid)
             if vcard is None:
                 return None
-            avatar_hash = hashes_cache[entity_jid.full()]
+            try:
+                avatar_hash = hashes_cache[entity_jid.full()]
+            except KeyError:
+                if 'avatar' in vcard:
+                    raise exceptions.InternalError(
+                        "No avatar hash while avatar is found in vcard")
+                return None
 
         if not avatar_hash:
             return None
@@ -507,7 +504,7 @@
 
         if not new_hash:
             await self.plugin_parent._i.update(
-                client, "avatar", None, entity_jid)
+                client, IMPORT_NAME, "avatar", None, entity_jid)
             # the avatar has been removed, no need to go further
             return
 
@@ -518,7 +515,7 @@
             )
             await self.plugin_parent._i.update(
                 client,
-                "avatar",
+                IMPORT_NAME, "avatar",
                 {
                     'path': avatar_cache['path'],
                     'media_type': avatar_cache['mime_type'],