changeset 3279:8de63fe6b5c9

plugin XEP-0054: don't use cache anymore in `getAvatar`: caching is now handled by plugin identity.
author Goffi <goffi@goffi.org>
date Tue, 19 May 2020 18:34:06 +0200
parents 27d4b71e264a
children 96b9b65b4368
files sat/plugins/plugin_xep_0054.py
diffstat 1 files changed, 17 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0054.py	Tue May 19 18:34:05 2020 +0200
+++ b/sat/plugins/plugin_xep_0054.py	Tue May 19 18:34:06 2020 +0200
@@ -21,6 +21,7 @@
 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.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 @@
             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
+    async def getAvatar(
+            self,
+            client: SatXMPPEntity,
+            entity_jid: jid.JID
+        ) -> Optional[dict]:
+        """Get avatar data
 
-        if avatar is not in local cache, it will be requested to the server
-        @param entity(jid.JID): entity to get avatar from
-
+        @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)