plugin XEP-0054: don't use cache anymore in getAvatar:

caching is now handled by plugin identity.
This commit is contained in:
Goffi 2020-05-19 18:34:06 +02:00
commit 4fdf49f4cb

View file

@ -21,6 +21,7 @@ import io
from base64 import b64decode, b64encode
from hashlib import sha1
from pathlib import Path
from typing import Optional
from zope.interface import implementer
from twisted.internet import threads, defer
from twisted.words.protocols.jabber import jid, error
@ -31,6 +32,7 @@ from sat.core import exceptions
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
from sat.core.xmpp import SatXMPPEntity
from sat.memory import persistent
from sat.tools import image
@ -288,41 +290,33 @@ class XEP_0054(object):
log.debug(_("VCard found"))
return await self.vCard2Dict(client, vcard_elt, entity_jid)
async def getAvatar(self, client, entity_jid):
"""Get avatar full path or hash
if avatar is not in local cache, it will be requested to the server
@param entity(jid.JID): entity to get avatar from
async def getAvatar(
self,
client: SatXMPPEntity,
entity_jid: jid.JID
) -> Optional[dict]:
"""Get avatar data
@param entity: entity to get avatar from
@return: avatar metadata, or None if no avatar has been found
"""
entity_jid = self._i.getIdentityJid(client, entity_jid)
hashes_cache = client._xep_0054_avatar_hashes
vcard = await self.getCard(client, entity_jid)
if vcard is None:
return None
try:
avatar_hash = hashes_cache[entity_jid.full()]
except KeyError:
log.debug(f"avatar for {entity_jid} is not in cache, we retrieve it")
vcard = await self.getCard(client, entity_jid)
if vcard is None:
return None
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 'avatar' in vcard:
raise exceptions.InternalError(
"No avatar hash while avatar is found in vcard")
return None
if not avatar_hash:
return None
avatar_cache = self.host.common_cache.getMetadata(avatar_hash)
if avatar_cache is None:
log.debug("avatar is no more in cache, we re-download it")
vcard = await self.getCard(client, entity_jid)
if vcard is None:
return None
avatar_cache = self.host.common_cache.getMetadata(avatar_hash)
return self._i.avatarBuildMetadata(
avatar_cache['path'], avatar_cache['mime_type'], avatar_hash)