summaryrefslogtreecommitdiff
path: root/poezio/core/handlers.py
diff options
context:
space:
mode:
authorEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2018-03-30 22:29:30 +0200
committerEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2018-03-31 00:47:24 +0200
commita2ad4af79a1435648c96b99308edeb04fd112da1 (patch)
treef3bf5b7aa4cf99d89caaea323667cbe3ad74f172 /poezio/core/handlers.py
parentb03a92e7eab6e0b7eb84a6a526913a449a36d603 (diff)
downloadpoezio-a2ad4af79a1435648c96b99308edeb04fd112da1.tar.gz
poezio-a2ad4af79a1435648c96b99308edeb04fd112da1.tar.bz2
poezio-a2ad4af79a1435648c96b99308edeb04fd112da1.tar.xz
poezio-a2ad4af79a1435648c96b99308edeb04fd112da1.zip
Use slixmpp’s new cache module for avatars too.
Diffstat (limited to 'poezio/core/handlers.py')
-rw-r--r--poezio/core/handlers.py42
1 files changed, 12 insertions, 30 deletions
diff --git a/poezio/core/handlers.py b/poezio/core/handlers.py
index 8c799b58..c73fad1f 100644
--- a/poezio/core/handlers.py
+++ b/poezio/core/handlers.py
@@ -395,19 +395,15 @@ class HandlerCore:
except Exception:
log.debug('Failed getting metadata from 0084:', exc_info=True)
return
- cache_dir = path.join(CACHE_DIR, 'avatars', jid)
for info in metadata:
avatar_hash = info['id']
# First check whether we have it in cache.
- cached_path = path.join(cache_dir, avatar_hash)
- try:
- with open(cached_path, 'rb') as avatar_file:
- contact.avatar = avatar_file.read()
- log.debug('Using cached avatar')
+ cached_avatar = self.core.avatar_cache.retrieve_by_jid(jid, avatar_hash)
+ if cached_avatar:
+ contact.avatar = cached_avatar
+ log.debug('Using cached avatar for %s', jid)
return
- except OSError:
- pass
# If we didn’t have any, query the data instead.
if not info['url']:
@@ -429,16 +425,11 @@ class HandlerCore:
log.debug('Received %s avatar: %s', jid, info['type'])
# Now we save the data on the file system to not have to request it again.
- try:
- makedirs(cache_dir, exist_ok=True)
- with open(cached_path, 'wb') as avatar_file:
- avatar_file.write(contact.avatar)
- except OSError:
+ if not self.core.avatar_cache.store_by_jid(jid, avatar_hash, contact.avatar):
log.debug(
- 'Failed writing %s avatar to cache:',
+ 'Failed writing %s’s avatar to cache:',
jid,
exc_info=True)
- pass
return
@asyncio.coroutine
@@ -451,15 +442,11 @@ class HandlerCore:
log.debug('Received vCard avatar update from %s: %s', jid, avatar_hash)
# First check whether we have it in cache.
- cache_dir = path.join(CACHE_DIR, 'avatars', jid)
- cached_path = path.join(cache_dir, avatar_hash)
- try:
- with open(cached_path, 'rb') as avatar_file:
- contact.avatar = avatar_file.read()
- log.debug('Using cached avatar')
+ cached_avatar = self.core.avatar_cache.retrieve_by_jid(jid, avatar_hash)
+ if cached_avatar:
+ contact.avatar = cached_avatar
+ log.debug('Using cached avatar for %s', jid)
return
- except OSError:
- pass
# If we didn’t have any, query the vCard instead.
try:
@@ -476,13 +463,8 @@ class HandlerCore:
log.debug('Received %s avatar: %s', jid, avatar['TYPE'])
# Now we save the data on the file system to not have to request it again.
- try:
- makedirs(cache_dir, exist_ok=True)
- with open(cached_path, 'wb') as avatar_file:
- avatar_file.write(contact.avatar)
- except OSError:
- log.debug('Failed writing %s avatar to cache:', jid, exc_info=True)
- pass
+ if not self.core.avatar_cache.store_by_jid(jid, avatar_hash, contact.avatar):
+ log.debug('Failed writing %s’s avatar to cache:', jid, exc_info=True)
def on_nick_received(self, message):
"""