Mercurial > libervia-web
comparison 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 |
comparison
equal
deleted
inserted
replaced
483:0bbbef1d53a8 | 484:ae86b32b959c |
---|---|
53 def __init__(self, host): | 53 def __init__(self, host): |
54 self.host = host | 54 self.host = host |
55 Resource.__init__(self) | 55 Resource.__init__(self) |
56 self.host.bridge.register('entityDataUpdated', self.entityDataUpdatedCb) | 56 self.host.bridge.register('entityDataUpdated', self.entityDataUpdatedCb) |
57 self.host.bridge.register('actionResult', self.actionResultCb) # FIXME: actionResult is to be removed | 57 self.host.bridge.register('actionResult', self.actionResultCb) # FIXME: actionResult is to be removed |
58 self.avatars_cache = {} | |
58 self.waiting_deferreds = {} | 59 self.waiting_deferreds = {} |
59 | 60 |
60 def entityDataUpdatedCb(self, entity_jid_s, key, value, dummy): | 61 def entityDataUpdatedCb(self, entity_jid_s, key, value, dummy): |
61 """Retrieve the avatar we've been waiting for and fires the callback | 62 """Retrieve the avatar we've been waiting for and fires the callback |
62 for self.getAvatar to return. | 63 for self.getAvatar to return. |
66 @param value (str): entity data value | 67 @param value (str): entity data value |
67 @param dummy (str): that would be C.SERVICE_PROFILE | 68 @param dummy (str): that would be C.SERVICE_PROFILE |
68 """ | 69 """ |
69 if key != 'avatar': | 70 if key != 'avatar': |
70 return | 71 return |
72 log.debug(_("Received a new avatar for entity %s") % entity_jid_s) | |
73 avatar = C.AVATARS_DIR + value | |
74 self.avatars_cache[entity_jid_s] = avatar | |
71 try: | 75 try: |
72 avatar = (C.AVATARS_DIR + value) | |
73 self.waiting_deferreds[entity_jid_s][1].callback(avatar) | 76 self.waiting_deferreds[entity_jid_s][1].callback(avatar) |
74 del self.waiting_deferreds[entity_jid_s] | 77 del self.waiting_deferreds[entity_jid_s] |
75 except KeyError: | 78 except KeyError: |
76 log.error(_("Avatar retrieved but key not found in the waiting list for entity %s" % entity_jid_s)) | 79 pass |
77 | 80 |
78 def actionResultCb(self, answer_type, action_id, data, dummy): | 81 def actionResultCb(self, answer_type, action_id, data, dummy): |
79 """Fires the callback for self.getAvatar to return | 82 """Fires the callback for self.getAvatar to return |
80 | 83 |
81 @param answer_type (str): 'SUPPRESS' or another value that we would ignore | 84 @param answer_type (str): 'SUPPRESS' or another value that we would ignore |
87 # when the requested vCard hasn't been found. Replace with the new system. | 90 # when the requested vCard hasn't been found. Replace with the new system. |
88 if answer_type != 'SUPPRESS': | 91 if answer_type != 'SUPPRESS': |
89 return | 92 return |
90 try: | 93 try: |
91 entity_jid_s = [key for (key, value) in self.waiting_deferreds.items() if value[0] == action_id][0] | 94 entity_jid_s = [key for (key, value) in self.waiting_deferreds.items() if value[0] == action_id][0] |
92 except IndexError: | 95 except IndexError: # impossible to guess the entity |
93 log.error(_("Key not found in the waiting list for request ID %s" % action_id)) | |
94 return | 96 return |
97 log.debug(_("Using default avatar for entity %s") % entity_jid_s) | |
98 self.avatars_cache[entity_jid_s] = C.DEFAULT_AVATAR | |
95 self.waiting_deferreds[entity_jid_s][1].callback(C.DEFAULT_AVATAR) | 99 self.waiting_deferreds[entity_jid_s][1].callback(C.DEFAULT_AVATAR) |
96 del self.waiting_deferreds[entity_jid_s] | 100 del self.waiting_deferreds[entity_jid_s] |
97 | 101 |
98 def getAvatar(self, profile): | 102 def getAvatar(self, profile): |
99 """Get the avatar of the given profile | 103 """Get the avatar of the given profile |
100 | 104 |
101 @param profile (str): | 105 @param profile (str): |
102 @return: deferred avatar path, relative to the server's root | 106 @return: deferred avatar path, relative to the server's root |
103 """ | 107 """ |
104 jid_s = profile + '@' + self.host.bridge.getNewAccountDomain() | 108 jid_s = profile + '@' + self.host.bridge.getNewAccountDomain() |
105 data = self.host.bridge.getEntityData(jid_s, ['avatar'], C.SERVICE_PROFILE) | 109 if jid_s in self.avatars_cache: |
106 if 'avatar' in data: | 110 return defer.succeed(self.avatars_cache[jid_s]) |
107 return defer.succeed(C.AVATARS_DIR + data['avatar']) | |
108 # FIXME: request_id is no more need when actionResult is removed | 111 # FIXME: request_id is no more need when actionResult is removed |
109 request_id = self.host.bridge.getCard(jid_s, C.SERVICE_PROFILE) | 112 request_id = self.host.bridge.getCard(jid_s, C.SERVICE_PROFILE) |
110 self.waiting_deferreds[jid_s] = (request_id, defer.Deferred()) | 113 self.waiting_deferreds[jid_s] = (request_id, defer.Deferred()) |
111 return self.waiting_deferreds[jid_s][1] | 114 return self.waiting_deferreds[jid_s][1] |
112 | 115 |