comparison sat/plugins/plugin_xep_0054.py @ 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 cf07641b764d
children 9e1ba1e1179f
comparison
equal deleted inserted replaced
3278:27d4b71e264a 3279:8de63fe6b5c9
19 19
20 import io 20 import io
21 from base64 import b64decode, b64encode 21 from base64 import b64decode, b64encode
22 from hashlib import sha1 22 from hashlib import sha1
23 from pathlib import Path 23 from pathlib import Path
24 from typing import Optional
24 from zope.interface import implementer 25 from zope.interface import implementer
25 from twisted.internet import threads, defer 26 from twisted.internet import threads, defer
26 from twisted.words.protocols.jabber import jid, error 27 from twisted.words.protocols.jabber import jid, error
27 from twisted.words.xish import domish 28 from twisted.words.xish import domish
28 from twisted.python.failure import Failure 29 from twisted.python.failure import Failure
29 from wokkel import disco, iwokkel 30 from wokkel import disco, iwokkel
30 from sat.core import exceptions 31 from sat.core import exceptions
31 from sat.core.i18n import _ 32 from sat.core.i18n import _
32 from sat.core.constants import Const as C 33 from sat.core.constants import Const as C
33 from sat.core.log import getLogger 34 from sat.core.log import getLogger
35 from sat.core.xmpp import SatXMPPEntity
34 from sat.memory import persistent 36 from sat.memory import persistent
35 from sat.tools import image 37 from sat.tools import image
36 38
37 log = getLogger(__name__) 39 log = getLogger(__name__)
38 40
286 ).format(entity_jid=entity_jid, e=e)) 288 ).format(entity_jid=entity_jid, e=e))
287 else: 289 else:
288 log.debug(_("VCard found")) 290 log.debug(_("VCard found"))
289 return await self.vCard2Dict(client, vcard_elt, entity_jid) 291 return await self.vCard2Dict(client, vcard_elt, entity_jid)
290 292
291 async def getAvatar(self, client, entity_jid): 293 async def getAvatar(
292 """Get avatar full path or hash 294 self,
293 295 client: SatXMPPEntity,
294 if avatar is not in local cache, it will be requested to the server 296 entity_jid: jid.JID
295 @param entity(jid.JID): entity to get avatar from 297 ) -> Optional[dict]:
296 298 """Get avatar data
299
300 @param entity: entity to get avatar from
301 @return: avatar metadata, or None if no avatar has been found
297 """ 302 """
298 entity_jid = self._i.getIdentityJid(client, entity_jid) 303 entity_jid = self._i.getIdentityJid(client, entity_jid)
299 hashes_cache = client._xep_0054_avatar_hashes 304 hashes_cache = client._xep_0054_avatar_hashes
305 vcard = await self.getCard(client, entity_jid)
306 if vcard is None:
307 return None
300 try: 308 try:
301 avatar_hash = hashes_cache[entity_jid.full()] 309 avatar_hash = hashes_cache[entity_jid.full()]
302 except KeyError: 310 except KeyError:
303 log.debug(f"avatar for {entity_jid} is not in cache, we retrieve it") 311 if 'avatar' in vcard:
304 vcard = await self.getCard(client, entity_jid) 312 raise exceptions.InternalError(
305 if vcard is None: 313 "No avatar hash while avatar is found in vcard")
306 return None 314 return None
307 try:
308 avatar_hash = hashes_cache[entity_jid.full()]
309 except KeyError:
310 if 'avatar' in vcard:
311 raise exceptions.InternalError(
312 "No avatar hash while avatar is found in vcard")
313 return None
314 315
315 if not avatar_hash: 316 if not avatar_hash:
316 return None 317 return None
317 318
318 avatar_cache = self.host.common_cache.getMetadata(avatar_hash) 319 avatar_cache = self.host.common_cache.getMetadata(avatar_hash)
319 if avatar_cache is None:
320 log.debug("avatar is no more in cache, we re-download it")
321 vcard = await self.getCard(client, entity_jid)
322 if vcard is None:
323 return None
324 avatar_cache = self.host.common_cache.getMetadata(avatar_hash)
325
326 return self._i.avatarBuildMetadata( 320 return self._i.avatarBuildMetadata(
327 avatar_cache['path'], avatar_cache['mime_type'], avatar_hash) 321 avatar_cache['path'], avatar_cache['mime_type'], avatar_hash)
328 322
329 def _buildSetAvatar(self, client, vcard_elt, avatar_data): 323 def _buildSetAvatar(self, client, vcard_elt, avatar_data):
330 # XXX: this method is executed in a separate thread 324 # XXX: this method is executed in a separate thread