plugin identity: fixed infinite loop on nicknames update

This commit is contained in:
Goffi 2020-05-18 23:52:34 +02:00
commit 7438f4218e

View file

@ -48,10 +48,11 @@ try:
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 @@ class XEP_0054(object):
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 @@ class XEP_0054(object):
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 @@ class XEP_0054(object):
nickname = vcard_dict["nickname"] = str(elem)
await self._i.update(
client,
IMPORT_NAME,
"nicknames",
[nickname],
entity_jid
@ -216,6 +205,7 @@ class XEP_0054(object):
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 @@ class XEP_0054(object):
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 @@ class XEP_0054(object):
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 @@ class XEP_0054(object):
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 @@ class XEP_0054_handler(XMPPHandler):
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 @@ class XEP_0054_handler(XMPPHandler):
)
await self.plugin_parent._i.update(
client,
"avatar",
IMPORT_NAME, "avatar",
{
'path': avatar_cache['path'],
'media_type': avatar_cache['mime_type'],