comparison src/server/blog.py @ 950:67a59552f3e3

server (blog): fixed avatars handling, there is now a well-known URL to SERVICE_PROFILE cache
author Goffi <goffi@goffi.org>
date Sat, 03 Jun 2017 22:27:04 +0200
parents e9bb7257d051
children 7892f1a1e2cf
comparison
equal deleted inserted replaced
949:36e9747520fd 950:67a59552f3e3
43 43
44 NS_ATOM = 'http://www.w3.org/2005/Atom' 44 NS_ATOM = 'http://www.w3.org/2005/Atom'
45 PARAMS_TO_GET = (C.STATIC_BLOG_PARAM_TITLE, C.STATIC_BLOG_PARAM_BANNER, C.STATIC_BLOG_PARAM_KEYWORDS, C.STATIC_BLOG_PARAM_DESCRIPTION) 45 PARAMS_TO_GET = (C.STATIC_BLOG_PARAM_TITLE, C.STATIC_BLOG_PARAM_BANNER, C.STATIC_BLOG_PARAM_KEYWORDS, C.STATIC_BLOG_PARAM_DESCRIPTION)
46 re_strip_empty_div = re.compile(r"<div ?/>|<div> *?</div>") 46 re_strip_empty_div = re.compile(r"<div ?/>|<div> *?</div>")
47 47
48 # TODO: chech disco features and use max_items when RSM is not available 48 # TODO: check disco features and use max_items when RSM is not available
49 # FIXME: Deferred are not used
50 # FIXME: change navigation links handling, this is is fragile 49 # FIXME: change navigation links handling, this is is fragile
51 # TODO: refactorise this 50 # XXX: this page will disappear, LiberviaPage will be used instead
51 # TODO: delete this page and create a compatibility page for links
52 52
53 53
54 def getDefaultQueryData(request): 54 def getDefaultQueryData(request):
55 """Return query data which must be present in all links 55 """Return query data which must be present in all links
56 56
120 120
121 def __init__(self, host): 121 def __init__(self, host):
122 self.host = host 122 self.host = host
123 Resource.__init__(self) 123 Resource.__init__(self)
124 TemplateProcessor.__init__(self, host) 124 TemplateProcessor.__init__(self, host)
125 self.host.bridge.register_signal('entityDataUpdated', self.entityDataUpdatedHandler)
126 self.avatars_cache = {} 125 self.avatars_cache = {}
127 self.waiting_deferreds = {} 126
128 127 def _avatarPathToUrl(self, avatar, request, bare_jid_s):
129 def entityDataUpdatedHandler(self, entity_s, key, value, dummy): 128 filename = os.path.basename(avatar)
130 """Retrieve the avatar we've been waiting for and fires the callback. 129 avatar_url = os.path.join(self.host.service_cache_url, filename)
131 130 self.avatars_cache[bare_jid_s] = avatar_url
132 @param entity_s (str): JID of the contact 131 return avatar_url
133 @param key (str): entity data key 132
134 @param value (str): entity data value 133
135 @param dummy (str): that would be C.SERVICE_PROFILE 134 def getAvatarURL(self, pub_jid, request):
136 """
137 if key != "avatar":
138 return
139 log.debug(_(u"Received a new avatar for entity %s") % entity_s)
140
141 url = os.path.join(C.AVATARS_DIR, value) if value else ""
142 self.avatars_cache[entity_s] = url
143 try:
144 self.waiting_deferreds.pop(entity_s).callback(url)
145 except KeyError:
146 pass
147
148 def getAvatarURL(self, pub_jid):
149 """Return avatar of a jid if in cache, else ask for it. 135 """Return avatar of a jid if in cache, else ask for it.
150 136
151 @param pub_jid (JID): publisher JID 137 @param pub_jid (JID): publisher JID
152 @return: deferred avatar URL (unicode) 138 @return: deferred avatar URL (unicode)
153 """ 139 """
154 bare_jid_s = pub_jid.userhost() 140 bare_jid_s = pub_jid.userhost()
155 try: 141 try:
156 url = self.avatars_cache[bare_jid_s] 142 url = self.avatars_cache[bare_jid_s]
157 except KeyError: 143 except KeyError:
158 self.avatars_cache[bare_jid_s] = '' # avoid to request the vcard several times 144 self.avatars_cache[bare_jid_s] = '' # avoid to request the vcard several times
159 # self.host.bridge.getCard(bare_jid_s, C.SERVICE_PROFILE) 145 d = self.host.bridgeCall('avatarGet', bare_jid_s, False, False, C.SERVICE_PROFILE)
160 self.host.bridge.avatarGet(bare_jid_s, True, True, C.SERVICE_PROFILE) 146 d.addCallback(self._avatarPathToUrl, request, bare_jid_s)
161 d = defer.Deferred()
162 self.waiting_deferreds[bare_jid_s] = d
163 return d 147 return d
164 return defer.succeed(url if url else C.DEFAULT_AVATAR_URL) 148 return defer.succeed(url if url else C.DEFAULT_AVATAR_URL)
165 149
166 def render_GET(self, request): 150 def render_GET(self, request):
167 if not request.postpath or len(request.postpath) > 2: 151 if not request.postpath or len(request.postpath) > 2:
555 @param profile (unicode): %(doc_profile)s 539 @param profile (unicode): %(doc_profile)s
556 """ 540 """
557 d_list = [] 541 d_list = []
558 options = {} 542 options = {}
559 543
560 d = self.getAvatarURL(pub_jid) 544 d = self.getAvatarURL(pub_jid, request)
561 d.addCallback(self._updateDict, options, 'avatar') 545 d.addCallback(self._updateDict, options, 'avatar')
562 d.addErrback(self.renderError, request, pub_jid) 546 d.addErrback(self.renderError, request, pub_jid)
563 d_list.append(d) 547 d_list.append(d)
564 548
565 for param_name in PARAMS_TO_GET: 549 for param_name in PARAMS_TO_GET: