diff src/server/blog.py @ 484:ae86b32b959c

server side (blog): manage our own avatars cache because existing entity data are not updated
author souliane <souliane@mailoo.org>
date Tue, 17 Jun 2014 17:13:56 +0200
parents 0bbbef1d53a8
children e588335b6aa8
line wrap: on
line diff
--- a/src/server/blog.py	Tue Jun 17 16:21:42 2014 +0200
+++ b/src/server/blog.py	Tue Jun 17 17:13:56 2014 +0200
@@ -55,6 +55,7 @@
         Resource.__init__(self)
         self.host.bridge.register('entityDataUpdated', self.entityDataUpdatedCb)
         self.host.bridge.register('actionResult', self.actionResultCb)  # FIXME: actionResult is to be removed
+        self.avatars_cache = {}
         self.waiting_deferreds = {}
 
     def entityDataUpdatedCb(self, entity_jid_s, key, value, dummy):
@@ -68,12 +69,14 @@
         """
         if key != 'avatar':
             return
+        log.debug(_("Received a new avatar for entity %s") % entity_jid_s)
+        avatar = C.AVATARS_DIR + value
+        self.avatars_cache[entity_jid_s] = avatar
         try:
-            avatar = (C.AVATARS_DIR + value)
             self.waiting_deferreds[entity_jid_s][1].callback(avatar)
             del self.waiting_deferreds[entity_jid_s]
         except KeyError:
-            log.error(_("Avatar retrieved but key not found in the waiting list for entity %s" % entity_jid_s))
+            pass
 
     def actionResultCb(self, answer_type, action_id, data, dummy):
         """Fires the callback for self.getAvatar to return
@@ -89,9 +92,10 @@
             return
         try:
             entity_jid_s = [key for (key, value) in self.waiting_deferreds.items() if value[0] == action_id][0]
-        except IndexError:
-            log.error(_("Key not found in the waiting list for request ID %s" % action_id))
+        except IndexError:  # impossible to guess the entity
             return
+        log.debug(_("Using default avatar for entity %s") % entity_jid_s)
+        self.avatars_cache[entity_jid_s] = C.DEFAULT_AVATAR
         self.waiting_deferreds[entity_jid_s][1].callback(C.DEFAULT_AVATAR)
         del self.waiting_deferreds[entity_jid_s]
 
@@ -102,9 +106,8 @@
         @return: deferred avatar path, relative to the server's root
         """
         jid_s = profile + '@' + self.host.bridge.getNewAccountDomain()
-        data = self.host.bridge.getEntityData(jid_s, ['avatar'], C.SERVICE_PROFILE)
-        if 'avatar' in data:
-            return defer.succeed(C.AVATARS_DIR + data['avatar'])
+        if jid_s in self.avatars_cache:
+            return defer.succeed(self.avatars_cache[jid_s])
         # FIXME: request_id is no more need when actionResult is removed
         request_id = self.host.bridge.getCard(jid_s, C.SERVICE_PROFILE)
         self.waiting_deferreds[jid_s] = (request_id, defer.Deferred())